Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: joelib/io/types/cml/CMLHandler.java


1   ///////////////////////////////////////////////////////////////////////////////
2   //Filename: $RCSfile: CMLHandler.java,v $
3   //Purpose:  Chemical Markup Language.
4   //Language: Java
5   //Compiler: JDK 1.4
6   //Authors:  steinbeck@ice.mpg.de, gezelter@maul.chem.nd.edu,
7   //      egonw@sci.kun.nl, wegnerj@informatik.uni-tuebingen.de
8   //Version:  $Revision: 1.6 $
9   //      $Date: 2003/08/22 15:56:18 $
10  //      $Author: wegner $
11  //
12  //Copyright (C) 1997-2003  The Chemistry Development Kit (CDK) project
13  //Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
14  //
15  //This program is free software; you can redistribute it and/or
16  //modify it under the terms of the GNU Lesser General Public License
17  //as published by the Free Software Foundation; either version 2.1
18  //of the License, or (at your option) any later version.
19  //All we ask is that proper credit is given for our work, which includes
20  //- but is not limited to - adding the above copyright notice to the beginning
21  //of your source code files, and to any copyright notice that you may distribute
22  //with programs based on this work.
23  //  
24  //This program is distributed in the hope that it will be useful,
25  //but WITHOUT ANY WARRANTY; without even the implied warranty of
26  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  //GNU Lesser General Public License for more details.
28  //  
29  //You should have received a copy of the GNU Lesser General Public License
30  //along with this program; if not, write to the Free Software
31  //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  ///////////////////////////////////////////////////////////////////////////////
33  package joelib.io.types.cml;
34  
35  import java.io.PrintStream;
36  
37  import java.util.Hashtable;
38  import java.util.Vector;
39  
40  import org.apache.log4j.*;
41  
42  import org.xml.sax.*;
43  import org.xml.sax.helpers.*;
44  
45  
46  /**
47   * SAX2 implementation for CML XML fragment reading. CML Core is supported
48   * as well is the CRML module.
49   *
50   * <p>Data is stored into the Chemical Document Object which is passed when
51   * instantiating this class.
52   *
53   * @author egonw
54   * @author c.steinbeck@uni-koeln.de
55   * @author gezelter@maul.chem.nd.edu
56   * @author     wegnerj
57   * @license LGPL
58   * @cvsversion    $Revision: 1.6 $, $Date: 2003/08/22 15:56:18 $
59   **/
60  public class CMLHandler extends DefaultHandler
61  {
62      //~ Static fields/initializers /////////////////////////////////////////////
63  
64      // Obtain a suitable logger.
65      private static Category logger = Category.getInstance(
66              "joelib.io.types.cml.CMLHandler");
67  
68      //~ Instance fields ////////////////////////////////////////////////////////
69  
70      private Hashtable userConventions;
71      private ModuleInterface conv;
72  
73      //~ Constructors ///////////////////////////////////////////////////////////
74  
75      /**
76       * Constructor for the CMLHandler.
77       *
78       * @param cdo The Chemical Document Object in which data is stored
79       **/
80      public CMLHandler(CDOInterface cdo)
81      {
82          conv = new CMLCoreModule(cdo);
83          userConventions = new Hashtable();
84      }
85  
86      //~ Methods ////////////////////////////////////////////////////////////////
87  
88      /**
89       * Implementation of the characters() procedure overwriting the DefaultHandler interface.
90       *
91       * @param ch        characters to handle
92       */
93      public void characters(char[] ch, int start, int length)
94      {
95          if (logger.isDebugEnabled())
96          {
97              logger.debug(new String(ch, start, length));
98          }
99  
100         conv.characterData(ch, start, length);
101     }
102 
103     public void doctypeDecl(String name, String publicId, String systemId)
104         throws Exception
105     {
106     }
107 
108     /**
109      * Calling this procedure signals the end of the XML document.
110      */
111     public void endDocument()
112     {
113         conv.endDocument();
114     }
115 
116     public void endElement(String uri, String local, String raw)
117     {
118         if (logger.isDebugEnabled())
119         {
120             logger.debug("</" + raw + ">");
121         }
122 
123         conv.endElement(uri, local, raw);
124     }
125 
126     public void registerConvention(String convention, ModuleInterface conv)
127     {
128         userConventions.put(convention, conv);
129     }
130 
131     public CDOInterface returnCDO()
132     {
133         return conv.returnCDO();
134     }
135 
136     public void startDocument()
137     {
138         conv.startDocument();
139     }
140 
141     public void startElement(String uri, String local, String raw,
142         Attributes atts)
143     {
144         StringBuffer sb = new StringBuffer();
145         sb.append("<" + raw);
146 
147         if (uri.length() > 0)
148         {
149             sb.append(" xmlns=\"" + uri + "\"");
150         }
151 
152         sb.append(">");
153 
154         if (logger.isDebugEnabled())
155         {
156             logger.debug(sb.toString());
157         }
158 
159         // Detect CML modules, like CRML and CCML
160         if (local.startsWith("reaction"))
161         {
162             // e.g. reactionList, reaction -> CRML module
163             logger.info("Detected CRML module");
164             conv = new CMLReactionModule(conv);
165         }
166         else
167         {
168             // assume CML Core
169             // Detect conventions
170             for (int i = 0; i < atts.getLength(); i++)
171             {
172                 if (atts.getQName(i).equals("convention"))
173                 {
174                     //logger.info(new StringBuffer("New Convention: ").append(atts.getValue(i)).toString());
175                     if (atts.getValue(i).equals("CML"))
176                     {
177                         logger.debug("Doing nothing");
178 
179                         /*} else if (atts.getValue(i).equals("PDB")) {
180                             //conv = new PDBConvention(conv);
181                         } else if (atts.getValue(i).equals("PMP")) {
182                             //conv = new PMPConvention(conv);
183                         } else if (atts.getValue(i).equals("MDLMol")) {
184                             //logger.debug("MDLMolConvention instantiated...");
185                             //conv = new MDLMolConvention(conv);
186                         } else if (atts.getValue(i).equals("JMOL-ANIMATION")) {
187                             //conv = new JMOLANIMATIONConvention(conv);*/
188                     }
189                     else
190                     {
191                         //unknown convention. userConvention?
192                         if (userConventions.containsKey(atts.getValue(i)))
193                         {
194                             ConventionInterface newconv = (ConventionInterface) userConventions.get(atts.getValue(
195                                         i));
196                             newconv.inherit(conv);
197                             conv = newconv;
198                         }
199                     }
200                 }
201             }
202         }
203 
204         conv.startElement(uri, local, raw, atts);
205     }
206 }
207 ///////////////////////////////////////////////////////////////////////////////
208 //  END OF FILE.
209 ///////////////////////////////////////////////////////////////////////////////