/**
  * Procedure required by the CDOInterface. This function is only supposed to be called by the JCFL
  * library
  */
 public void endDocument() {
   logger.debug("Closing document");
   logger.info("End CDO Object");
 }
  /**
   * Read a Reaction from a file in MDL RXN format
   *
   * @return The Reaction that was read from the MDL file.
   */
  private IReaction readReaction(IChemObjectBuilder builder) throws CDKException {
    IReaction reaction = builder.newReaction();
    try {
      input.readLine(); // first line should be $RXN
      input.readLine(); // second line
      input.readLine(); // third line
      input.readLine(); // fourth line
    } catch (IOException exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading header of RXN file", exception);
    }

    int reactantCount = 0;
    int productCount = 0;
    try {
      String countsLine = input.readLine();
      /* this line contains the number of reactants
      and products */
      StringTokenizer tokenizer = new StringTokenizer(countsLine);
      reactantCount = Integer.valueOf(tokenizer.nextToken()).intValue();
      logger.info("Expecting " + reactantCount + " reactants in file");
      productCount = Integer.valueOf(tokenizer.nextToken()).intValue();
      logger.info("Expecting " + productCount + " products in file");
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while counts line of RXN file", exception);
    }

    // now read the reactants
    try {
      for (int i = 1; i <= reactantCount; i++) {
        StringBuffer molFile = new StringBuffer();
        input.readLine(); // announceMDLFileLine
        String molFileLine = "";
        do {
          molFileLine = input.readLine();
          molFile.append(molFileLine);
          molFile.append(System.getProperty("line.separator"));
        } while (!molFileLine.equals("M  END"));

        // read MDL molfile content
        // Changed this to mdlv2000 reader
        MDLV2000Reader reader =
            new MDLV2000Reader(new StringReader(molFile.toString()), super.mode);
        IMolecule reactant = (IMolecule) reader.read(builder.newMolecule());

        // add reactant
        reaction.addReactant(reactant);
      }
    } catch (CDKException exception) {
      // rethrow exception from MDLReader
      throw exception;
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading reactant", exception);
    }

    // now read the products
    try {
      for (int i = 1; i <= productCount; i++) {
        StringBuffer molFile = new StringBuffer();
        input.readLine(); // String announceMDLFileLine =
        String molFileLine = "";
        do {
          molFileLine = input.readLine();
          molFile.append(molFileLine);
          molFile.append(System.getProperty("line.separator"));
        } while (!molFileLine.equals("M  END"));

        // read MDL molfile content
        MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.toString()));
        IMolecule product = (IMolecule) reader.read(builder.newMolecule());

        // add reactant
        reaction.addProduct(product);
      }
    } catch (CDKException exception) {
      // rethrow exception from MDLReader
      throw exception;
    } catch (Exception exception) {
      logger.debug(exception);
      throw new CDKException("Error while reading products", exception);
    }

    // now try to map things, if wanted
    logger.info("Reading atom-atom mapping from file");
    // distribute all atoms over two AtomContainer's
    IAtomContainer reactingSide = builder.newAtomContainer();
    java.util.Iterator molecules = reaction.getReactants().molecules().iterator();
    while (molecules.hasNext()) {
      reactingSide.add((IMolecule) molecules.next());
    }
    IAtomContainer producedSide = builder.newAtomContainer();
    molecules = reaction.getProducts().molecules().iterator();
    while (molecules.hasNext()) {
      producedSide.add((IMolecule) molecules.next());
    }

    // map the atoms
    int mappingCount = 0;
    //        IAtom[] reactantAtoms = reactingSide.getAtoms();
    //        IAtom[] producedAtoms = producedSide.getAtoms();
    for (int i = 0; i < reactingSide.getAtomCount(); i++) {
      for (int j = 0; j < producedSide.getAtomCount(); j++) {
        IAtom eductAtom = reactingSide.getAtom(i);
        IAtom productAtom = producedSide.getAtom(j);
        if (eductAtom.getID() != null && eductAtom.getID().equals(productAtom.getID())) {
          reaction.addMapping(builder.newMapping(eductAtom, productAtom));
          mappingCount++;
          break;
        }
      }
    }
    logger.info("Mapped atom pairs: " + mappingCount);

    return reaction;
  }
 /**
  * Procedure required by the CDOInterface. This function is only supposed to be called by the JCFL
  * library
  */
 public void startDocument() {
   logger.info("New CDO Object");
 }