Parser.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
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
00086
00087
00088
00089
00090
00091
00092
00093
00094 public Parser(String inputName, InputStream inputStream, Builder builder)
00095 {
00096
00097 this.inputName = inputName;
00098 this.inputStream = inputStream;
00099 this.builder = builder;
00100 this.builder.setParser(this);
00101
00102
00103 this.lineNumberReader = new LineNumberReader(new InputStreamReader(this.inputStream));
00104 }
00105
00106
00107
00108
00110
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 public void parse()
00122 throws
00123 Exception
00124 {
00125
00126 this.builder.begin();
00127
00128
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
00139 this.builder.error(this.getLineNumber());
00140 return;
00141 }
00142 catch( IOException e )
00143 {
00144
00145 this.builder.error(this.getLineNumber());
00146 throw e;
00147 }
00148 catch( Exception e )
00149 {
00150 if(e instanceof OldXMLFormatException) {
00151
00152 throw e;
00153 } else if(e.getCause() instanceof OldXMLFormatException) {
00154
00155 throw new OldXMLFormatException();
00156 } else {
00157 e.printStackTrace();
00158 }
00159
00160
00161 this.builder.error(this.getLineNumber());
00162 return;
00163
00164
00165
00166
00167
00168
00169 }
00170
00171
00172 this.builder.end();
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 public int getLineNumber()
00189 {
00190 return this.lineNumberReader.getLineNumber();
00191 }
00192
00193
00194
00195
00197
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
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);
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