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

Quick Search    Search Deep

Source code: org/openscience/miniJmol/CMLHandler.java


1   /*
2    * @(#)CMLHandler.java   0.1 99/05/05
3    *
4    * Copyright (c) 1999 E.L. Willighagen All Rights Reserved.
5    *
6    */
7   
8   package org.openscience.miniJmol;
9   
10  import java.util.*;
11  import org.xml.sax.*;
12  import org.openscience.jmol.FortranFormat;
13  
14  public class CMLHandler extends org.xml.sax.HandlerBase {
15  
16      private final int UNKNOWN = -1;
17  
18      private final int STRING = 1;
19      private final int LINK = 2;
20      private final int FLOAT = 3;
21      private final int INTEGER = 4;
22      private final int STRINGARRAY = 5;
23      private final int FLOATARRAY = 6;
24      private final int INTEGERARRAY = 7;
25      private final int FLOATMATRIX = 8;
26      private final int COORDINATE2 = 9;
27      private final int COORDINATE3 = 10;
28      private final int ANGLE = 11;
29      private final int TORSION = 12;
30      private final int LIST = 13;
31      private final int MOLECULE = 14;
32      private final int ATOM = 15;
33      private final int ATOMARRAY = 16;
34      private final int BOND = 17;
35      private final int BONDARRAY = 18;
36      private final int ELECTRON = 19;
37      private final int REACTION = 20;
38      private final int CRYSTAL = 21;
39      private final int SEQUENCE = 22;
40      private final int FEATURE = 23;
41  
42      private final String SYSTEMID = "CML-1999-05-15";
43  
44      private int CurrentElement;
45      private String BUILTIN = "";
46  
47      private ChemFrame cf;
48      private Vector cfs;
49  
50      private int frameNo;
51  
52      private Vector elsym;
53      private Vector elid;
54      private Vector x3;
55      private Vector y3;
56      private Vector z3;
57  
58      public CMLHandler () {
59          cfs = new Vector();
60          frameNo = 0;
61      };
62  
63      public void startDocument () {}
64      public void endDocument () {}
65  
66      public Vector returnChemFrames () {
67                              return cfs;
68                          }
69  
70                          public void doctypeDecl(String name, String publicId, String systemId) throws Exception {
71                              warn("Name: " + name);
72                              warn("PublicId: " + publicId);
73                              warn("SystemId: " + systemId);
74                          }
75  
76                          public void startElement (String name, AttributeList atts)
77                          throws SAXException {
78                              setCurrentElement(name);
79                              switch (CurrentElement) {
80                              case ATOM :
81                                  for (int i = 0; i < atts.getLength(); i++) {
82                                      if (atts.getName(i).equals("id")) {
83                                          elid.addElement(atts.getValue(i));
84                                      }
85                                  }
86                                  break;
87                              case COORDINATE3 :
88                                  for (int i = 0; i < atts.getLength(); i++) {
89                                      if (atts.getName(i).equals("builtin")) {
90                                          BUILTIN = atts.getValue(i);
91                                      }
92                                  }
93                                  break;
94                              case STRING :
95                                  for (int i = 0; i < atts.getLength(); i++) {
96                                      if (atts.getName(i).equals("builtin"))
97                                          BUILTIN = atts.getValue(i);
98                                  }
99                                  break;
100                             case ATOMARRAY :
101                                 break;
102                             case STRINGARRAY :
103                                 for (int i = 0; i < atts.getLength(); i++) {
104                                     if (atts.getName(i).equals("builtin"))
105                                         BUILTIN = atts.getValue(i);
106                                 }
107                                 break;
108                             case FLOATARRAY :
109                                 for (int i = 0; i < atts.getLength(); i++) {
110                                     if (atts.getName(i).equals("builtin"))
111                                         BUILTIN = atts.getValue(i);
112                                 }
113                                 break;
114                             case MOLECULE :
115                                 cf = new ChemFrame();
116                                 elsym = new Vector();
117                                 elid = new Vector();
118                                 x3 = new Vector();
119                                 y3 = new Vector();
120                                 z3 = new Vector();
121                                 frameNo++;
122                                 for (int i = 0; i < atts.getLength(); i++) {
123                                     if (atts.getName(i).equals("id"))
124                                         cf.setInfo(atts.getValue(i));
125                                 }
126                                 break;
127                             case LIST :
128                                 for (int i = 0; i < atts.getLength(); i++) {
129                                     if (atts.getName(i).equals("convention"))
130                                         warn("Convention: " + atts.getValue(i));
131                                 }
132                                 break;
133                             }
134                         }
135 
136                         public void endElement (String name) {
137                             setCurrentElement(name);
138                             BUILTIN = "";
139                             switch (CurrentElement) {
140                             case MOLECULE :
141                                 int atomcount = elsym.size();
142                                 if ((x3.size() == atomcount) &&
143                                         (y3.size() == atomcount) &&
144                                         (z3.size() == atomcount)) {
145                                     Enumeration atoms = elsym.elements();
146                                     Enumeration x3s = x3.elements();
147                                     Enumeration y3s = y3.elements();
148                                     Enumeration z3s = z3.elements();
149                                     while (atoms.hasMoreElements()) {
150                                         String atype = (String)atoms.nextElement();
151                                         double x = FortranFormat.atof((String)x3s.nextElement());
152                                         double y = FortranFormat.atof((String)y3s.nextElement());
153                                         double z = FortranFormat.atof((String)z3s.nextElement());
154 
155                                         try {
156                                             cf.addAtom(atype, (float) x, (float) y, (float) z);
157                                         } catch (Exception e) {
158                                             e.printStackTrace ();
159                                             notify("CMLhandler error while adding atom: " + e, SYSTEMID, 149,1);
160                                         }
161                                     }
162                                 }
163                                 cfs.addElement(cf);
164                                 break;
165                             }
166                         }
167 
168                         public void characters (char ch[], int start, int length) {
169                             String s = toString(ch, start, length).trim();
170                             switch (CurrentElement) {
171                             case STRING :
172                                 if (BUILTIN.equals("elementType")) {
173                                     elsym.addElement(s);
174                                 }
175                                 break;
176                             case COORDINATE3 :
177                                 if (BUILTIN.equals("xyz3")) {
178                                     try {
179                                         StringTokenizer st = new StringTokenizer(s);
180                                         x3.addElement(st.nextToken());
181                                         y3.addElement(st.nextToken());
182                                         z3.addElement(st.nextToken());
183                                     } catch (Exception e) {
184                                         notify("CMLParsing error: " + e, SYSTEMID, 175,1);
185                                     }
186                                 }
187                                 break;
188                             case STRINGARRAY :
189                                 if (BUILTIN.equals("id")) {
190                                     try {
191                                         StringTokenizer st = new StringTokenizer(s);
192                                         while (st.hasMoreTokens())
193                                             elid.addElement(st.nextToken());
194                                     } catch (Exception e) {
195                                         notify("CMLParsing error: " + e, SYSTEMID, 186,1);
196                                     }
197                                 } else if (BUILTIN.equals("elementType")) {
198                                     try {
199                                         StringTokenizer st = new StringTokenizer(s);
200                                         while (st.hasMoreTokens())
201                                             elsym.addElement(st.nextToken());
202                                     } catch (Exception e) {
203                                         notify("CMLParsing error: " + e, SYSTEMID, 194,1);
204                                     }
205                                 }
206                                 break;
207                             case FLOATARRAY :
208                                 if (BUILTIN.equals("x3")) {
209                                     try {
210                                         StringTokenizer st = new StringTokenizer(s);
211                                         while (st.hasMoreTokens())
212                                             x3.addElement(st.nextToken());
213                                     } catch (Exception e) {
214                                         notify("CMLParsing error: " + e, SYSTEMID, 205,1);
215                                     }
216                                 } else if (BUILTIN.equals("y3")) {
217                                     try {
218                                         StringTokenizer st = new StringTokenizer(s);
219                                         while (st.hasMoreTokens())
220                                             y3.addElement(st.nextToken());
221                                     } catch (Exception e) {
222                                         notify("CMLParsing error: " + e, SYSTEMID, 213,1);
223                                     }
224                                 } else if (BUILTIN.equals("z3")) {
225                                     try {
226                                         StringTokenizer st = new StringTokenizer(s);
227                                         while (st.hasMoreTokens())
228                                             z3.addElement(st.nextToken());
229                                     } catch (Exception e) {
230                                         notify("CMLParsing error: " + e, SYSTEMID, 221,1);
231                                     }
232                                 }
233                                 break;
234                             }
235                         }
236 
237                         private void setCurrentElement(String name) {
238                             if (name.equals("string")) {
239                                 CurrentElement = STRING;
240                             } else if (name.equals("link")) {
241                                 CurrentElement = LINK;
242                             } else if (name.equals("float")) {
243                                 CurrentElement = FLOAT;
244                             } else if (name.equals("integer")) {
245                                 CurrentElement = INTEGER;
246                             } else if (name.equals("stringArray")) {
247                                 CurrentElement = STRINGARRAY;
248                             } else if (name.equals("floatArray")) {
249                                 CurrentElement = FLOATARRAY;
250                             } else if (name.equals("integerArray")) {
251                                 CurrentElement = INTEGERARRAY;
252                             } else if (name.equals("floatMatrix")) {
253                                 CurrentElement = FLOATMATRIX;
254                             } else if (name.equals("coordinate2")) {
255                                 CurrentElement = COORDINATE2;
256                             } else if (name.equals("coordinate3")) {
257                                 CurrentElement = COORDINATE3;
258                             } else if (name.equals("angle")) {
259                                 CurrentElement = ANGLE;
260                             } else if (name.equals("torsion")) {
261                                 CurrentElement = TORSION;
262                             } else if (name.equals("list")) {
263                                 CurrentElement = LIST;
264                             } else if (name.equals("molecule")) {
265                                 CurrentElement = MOLECULE;
266                             } else if (name.equals("atom")) {
267                                 CurrentElement = ATOM;
268                             } else if (name.equals("atomArray")) {
269                                 CurrentElement = ATOMARRAY;
270                             } else if (name.equals("bond")) {
271                                 CurrentElement = BOND;
272                             } else if (name.equals("bondArray")) {
273                                 CurrentElement = BONDARRAY;
274                             } else if (name.equals("electron")) {
275                                 CurrentElement = ELECTRON;
276                             } else if (name.equals("reaction")) {
277                                 CurrentElement = REACTION;
278                             } else if (name.equals("crystal")) {
279                                 CurrentElement = CRYSTAL;
280                             } else if (name.equals("sequence")) {
281                                 CurrentElement = SEQUENCE;
282                             } else if (name.equals("feature")) {
283                                 CurrentElement = FEATURE;
284                             } else {
285                                 CurrentElement = UNKNOWN;
286                             };
287                         }
288 
289                         public void error(String message, String systemId, int line, int column) throws Exception {
290                             notify(message, systemId, line, column);
291                         }
292 
293                         public void notify(String message, String systemId, int line, int column) {
294                             System.out.println("Message: " + message);
295                             System.out.println("SystemId: " + systemId);
296                             System.out.println("Line: " + line);
297                             System.out.println("Column: " + column);
298                         }
299 
300                         public void warn(String s) {
301                             System.out.println(s);
302                         }
303 
304                         private String toString(char ch[], int start, int length) {
305                             StringBuffer x = new StringBuffer();
306                             for (int i =0; i < length; i++)
307                                 x.append(ch[start+i]);
308                             return x.toString();
309                         }
310 
311                         public void warning (SAXParseException e) throws SAXException {
312                             e.printStackTrace ();
313                         }
314                     }