Parser.java

Go to the documentation of this file.
00001 
00002 //                                  //
00003 //  Parser.java                         //
00004 //                                  //
00005 //  experiment.xml.Parser -- fast callback-based XML parser     //
00006 //  Last edited: January 16, 2002 at 11:27 am           //
00007 //                                  //
00008 //  (c) Copyright 2002 Rice University. All rights reserved.    //
00009 //                                  //
00011 
00012 
00013 
00014 
00015 package edu.rice.cs.hpc.data.experiment.xml;
00016 
00017 import java.io.*;
00018 
00019 import org.xml.sax.*;
00020 import org.xml.sax.helpers.*;
00021 
00022 
00023 
00025 //  CLASS PARSER                            //
00027 
00052 public class Parser extends Object
00053 {
00054 
00055 
00057 protected String inputName;
00058 
00060 protected InputStream inputStream;
00061 
00063 protected LineNumberReader lineNumberReader;
00064 
00066 protected Builder builder;
00067 
00069 protected Locator locator;
00070 
00071 
00072 
00073 
00075 final String parserClass = "com.jclark.xml.sax.Driver";
00076 
00077 final String xmlReaderClass = "org.apache.xerces.parsers.SAXParser";
00078 
00079 
00080 
00081 
00082 
00084 //  INITIALIZATION                                                      //
00086 
00087 
00088 
00089 
00090 /*************************************************************************
00091  *  Creates a Parser.
00092  ************************************************************************/
00093     
00094 public Parser(String inputName, InputStream inputStream, Builder builder)
00095 {
00096     // creation arguments
00097     this.inputName = inputName;
00098     this.inputStream = inputStream;
00099     this.builder = builder;
00100     this.builder.setParser(this);
00101 
00102     // input line number, for error messages
00103     this.lineNumberReader = new LineNumberReader(new InputStreamReader(this.inputStream));
00104 }
00105 
00106 
00107 
00108 
00110 //  PARSING                             //
00112 
00113 
00114 
00115 
00116 /*************************************************************************
00117  *  Parses the XML file sending parse actions to the builder.
00118  * @throws Exception 
00119  ************************************************************************/
00120     
00121 public void parse()
00122 throws
00123     Exception
00124 {
00125     // initialize building
00126     this.builder.begin();
00127 
00128     // parse the file
00129     try
00130     {
00131         XMLReader parser = new org.apache.xerces.parsers.SAXParser();
00132         ContentHandler handler = new Handler();
00133         parser.setContentHandler(handler);
00134         parser.parse(new InputSource(this.lineNumberReader));
00135     }
00136     catch( SAXException e )
00137     {
00138         // turn these into parse errors
00139         this.builder.error(this.getLineNumber());
00140         return;
00141     }
00142     catch( IOException e )
00143     {
00144         // let caller handle these, getting the line number from the builder
00145         this.builder.error(this.getLineNumber());
00146         throw e;
00147     }
00148     catch( Exception e )
00149     {
00150         if(e instanceof OldXMLFormatException) {
00151             //System.err.println("Error found: old XML file");
00152             throw e;
00153         } else if(e.getCause() instanceof OldXMLFormatException) {
00154             //System.err.println("Cause of error: old XML file");
00155             throw new OldXMLFormatException();
00156         } else {
00157             e.printStackTrace();
00158         }
00159         // TEMPORARY: do better inside 'builder'
00160         // turn these into parse errors
00161         this.builder.error(this.getLineNumber());
00162         return;
00163 /**************************************************
00164         // treat everything else as fatal
00165         int lineNumber = this.getLineNumber();
00166         String what = this.inputName + ", line " + lineNumber;
00167         Dialogs.fatalException(Strings.XML_PARSE_EXCEPTION, what, e);
00168 ***************************************************/
00169     }
00170 
00171     // finalize building
00172     this.builder.end();
00173 }
00174 
00175 
00176 
00177 
00178 /*************************************************************************
00179  *  Returns the 1-based line number of the most recently read input line.
00180  *
00181  *  <p>
00182  *  We'd like this to be the line at which a syntax error was detected,
00183  *  but the lean parser we're using does not keep track of that. If it
00184  *  did this method should return <code>this.locator.getLineNumber()</code>.
00185  *
00186  ************************************************************************/
00187     
00188 public int getLineNumber()
00189 {
00190     return this.lineNumberReader.getLineNumber();
00191 }
00192 
00193 
00194 
00195 
00197 //  CLASS HANDLER                                                       //
00199 
00211     public class Handler extends DefaultHandler
00212     {
00213 
00215     public void setDocumentLocator(Locator locator)
00216     {
00217         Parser.this.locator = locator;
00218     }
00219 
00220 
00222     public void startElement(String uri, String localName, String qualifiedName, Attributes attrs)
00223     {
00224         // Note: AttributeList.getType() is discarded here
00225 
00226         final int count = attrs.getLength();
00227         String[] attributeNames  = new String[count];
00228         String[] attributeValues = new String[count];
00229         for( int k = 0;  k < count;  k++ )
00230         {
00231             attributeNames [k] = attrs.getLocalName (k); // johnmc - was getName, perhaps getLocalName?
00232             attributeValues[k] = attrs.getValue(k);
00233         }
00234 
00235         Parser.this.builder.beginElement(localName, attributeNames, attributeValues);
00236     }
00237 
00238 
00240     public void characters(char[] chars, int start, int length)
00241     {
00242         String s = new String(chars, start, length).trim();
00243         if( s.length() > 0 )
00244         Parser.this.builder.content(s);
00245     }
00246 
00247 
00249     public void endElement(String uri, String localName, String qualifiedName)
00250     {
00251         Parser.this.builder.endElement(localName);
00252     }
00253 
00254 
00255 
00256 }
00257 
00258 
00259 
00260 
00261 }
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1