/** Returns true if another IMolecule can be read. */
  public boolean hasNext() {
    if (!nextAvailableIsKnown) {
      hasNext = false;

      // now try to parse the next Molecule
      try {
        if ((currentLine = input.readLine()) != null) {
          currentFormat = (IChemFormat) MDLFormat.getInstance();
          StringBuffer buffer = new StringBuffer();
          while (currentLine != null && !currentLine.equals("M  END")) {
            // still in a molecule
            buffer.append(currentLine);
            buffer.append(System.getProperty("line.separator"));

            currentLine = input.readLine();

            // do MDL molfile version checking
            if (currentLine.contains("V2000") || currentLine.contains("v2000")) {
              currentFormat = (IChemFormat) MDLV2000Format.getInstance();
            } else if (currentLine.contains("V3000") || currentLine.contains("v3000")) {
              currentFormat = (IChemFormat) MDLV3000Format.getInstance();
            }
          }
          buffer.append(currentLine);
          buffer.append(System.getProperty("line.separator"));
          logger.debug("MDL file part read: ", buffer);
          ISimpleChemObjectReader reader = factory.createReader(currentFormat);
          reader.setReader(new StringReader(buffer.toString()));
          if (currentFormat instanceof MDLV2000Format) {
            reader.addChemObjectIOListener(this);
            ((MDLV2000Reader) reader).customizeJob();
          }
          nextMolecule = (IMolecule) reader.read(builder.newInstance(IMolecule.class));

          // note that a molecule may have 0 atoms, but still
          // be useful (by having SD tags for example), so just
          // check for null'ness rather than atom count
          hasNext = nextMolecule != null;

          // now read the data part
          currentLine = input.readLine();
          readDataBlockInto(nextMolecule);
        } else {
          hasNext = false;
        }
      } catch (Exception exception) {
        logger.error("Error while reading next molecule: " + exception.getMessage());
        logger.debug(exception);
        hasNext = false;
      }
      if (!hasNext) nextMolecule = null;
      nextAvailableIsKnown = true;
    }
    return hasNext;
  }
Ejemplo n.º 2
0
 /* (non-Javadoc)
  * @see org.openscience.cdk.io.IChemObjectIO#getFormat()
  */
 @TestMethod("testGetFormat")
 public IResourceFormat getFormat() {
   return MDLFormat.getInstance();
 }