1 package com.icl.saxon.om; 2 import com.icl.saxon.Controller; 3 import com.icl.saxon.expr.*; 4 import com.icl.saxon.ContentEmitter; 5 import com.icl.saxon.PreviewManager; 6 import com.icl.saxon.ExtendedInputSource; 7 import com.icl.saxon.output.Emitter; 8 9 import org.xml.sax.*; 10 11 import javax.xml.parsers.SAXParserFactory ; 12 import javax.xml.transform.TransformerException ; 13 import javax.xml.transform.SourceLocator ; 14 import javax.xml.transform.ErrorListener ; 15 import javax.xml.transform.sax.SAXSource ; 16 17 import java.util.*; 18 import java.io.*; 19 import java.net.URL ; 20 21 27 28 public abstract class Builder extends Emitter 29 implements ErrorHandler, Locator , SourceLocator 30 31 { 32 public final static int STANDARD_TREE = 0; 33 public final static int TINY_TREE = 1; 34 35 protected int estimatedLength; protected Writer errorOutput = new PrintWriter(System.err); 39 protected Stripper stripper; protected PreviewManager previewManager = null; 42 protected boolean discardComments; 44 protected DocumentInfo currentDocument; 45 protected ErrorHandler errorHandler = this; protected ErrorListener errorListener = null; 48 protected boolean failed = false; 49 protected boolean started = false; 50 protected boolean timing = false; 51 52 protected boolean inDTD = false; 53 protected boolean lineNumbering = false; 54 protected int lineNumber = -1; 55 protected int columnNumber = -1; 56 57 private long startTime; 58 59 protected Controller controller; 60 61 public void setController(Controller c) { 62 controller = c; 63 } 64 65 68 69 public Builder() {} 70 71 75 81 82 public void setRootNode(DocumentInfo doc) { 83 currentDocument = doc; 84 } 85 86 87 90 91 public void setTiming(boolean on) { 92 timing = on; 93 } 94 95 98 99 public boolean isTiming() { 100 return timing; 101 } 102 103 106 107 public void setLineNumbering(boolean onOrOff) { 108 lineNumbering = onOrOff; 109 } 110 111 114 115 public void setStripper(Stripper s) { 116 stripper = s; 117 } 118 119 122 123 public Stripper getStripper() { 124 return stripper; 125 } 126 127 130 131 public void setPreviewManager(PreviewManager pm) { 132 previewManager = pm; 133 } 134 135 136 141 142 public void setDiscardCommentsAndPIs(boolean discard) { 143 discardComments = discard; 144 } 145 146 152 153 public void setErrorHandler(ErrorHandler eh) { 154 this.errorHandler = eh; 155 } 156 157 162 163 public void setErrorListener(ErrorListener eh) { 164 this.errorListener = eh; 165 } 166 167 177 178 public void setErrorOutput(Writer writer) { 179 errorOutput = writer; 180 } 181 182 192 193 public DocumentInfo build(SAXSource source) throws TransformerException 194 { 195 InputSource in = source.getInputSource(); 196 XMLReader parser = source.getXMLReader(); 197 198 if (timing) { 200 System.err.println("Building tree for " + in.getSystemId() + " using " + getClass()); 201 startTime = (new Date()).getTime(); 202 } 203 204 failed = true; started = false; 206 if (source.getSystemId() != null) { 207 setSystemId(source.getSystemId()); 208 } else { 209 setSystemId(in.getSystemId()); 210 } 211 212 if (in instanceof ExtendedInputSource) { 213 estimatedLength = ((ExtendedInputSource)in).getEstimatedLength(); 214 if (estimatedLength < 1) estimatedLength = 4096; 215 if (estimatedLength > 1000000) estimatedLength = 1000000; 216 } else { 217 estimatedLength = 4096; 218 } 219 220 222 if (parser==null) { 223 try { 224 parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 225 } catch (Exception err) { 227 throw new TransformerException (err); 228 } 229 } 230 231 ContentEmitter ce = new ContentEmitter(); 232 ce.setNamePool(namePool); 233 parser.setContentHandler(ce); 234 parser.setDTDHandler(ce); 235 parser.setErrorHandler(errorHandler); 236 237 if (!discardComments) { 238 try { 239 parser.setProperty("http://xml.org/sax/properties/lexical-handler", ce); 240 } catch (SAXNotSupportedException err) { } catch (SAXNotRecognizedException err) { 242 } 243 } 244 245 if (stripper!=null) { 246 ce.setEmitter(stripper); 247 stripper.setUnderlyingEmitter(this); 248 } else { 249 ce.setEmitter(this); 250 } 251 252 try { 253 parser.setFeature("http://xml.org/sax/features/namespaces", true); 254 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 255 } catch (SAXNotSupportedException err) { throw new TransformerException ( 257 "The SAX2 parser does not recognize a required namespace feature"); 258 } catch (SAXNotRecognizedException err) { 259 throw new TransformerException ( 260 "The SAX2 parser does not support a required namespace feature"); 261 } 262 263 264 try { 265 parser.parse(in); 266 } catch (java.io.IOException err1) { 267 throw new TransformerException ("Failure reading " + in.getSystemId(), err1); 268 } catch (SAXException err2) { 269 Exception wrapped = err2.getException(); 270 if (wrapped != null && wrapped instanceof TransformerException ) { 271 throw (TransformerException )wrapped; 272 } 273 throw new TransformerException (err2); 274 } 275 276 277 if (!started) { 278 throw new TransformerException ("Source document not supplied"); 280 } 281 282 if (failed) { 283 throw new TransformerException ("XML Parsing failed"); 285 } 286 287 if (timing) { 288 long endTime = (new Date()).getTime(); 289 System.err.println("Tree built in " + (endTime-startTime) + " milliseconds"); 290 startTime = endTime; 291 } 292 293 return currentDocument; 294 } 295 296 300 301 public DocumentInfo getCurrentDocument() { 302 return currentDocument; 303 } 304 305 310 313 314 public void warning (SAXParseException e) { 315 if (errorListener != null) { 316 try { 317 errorListener.warning(new TransformerException (e)); 318 } catch (Exception err) {} 319 } 320 } 321 322 325 326 public void error (SAXParseException e) throws SAXException { 327 reportError(e, false); 328 failed = true; 329 } 330 331 334 335 public void fatalError (SAXParseException e) throws SAXException { 336 reportError(e, true); 337 failed = true; 338 throw e; 339 } 340 341 344 345 protected void reportError (SAXParseException e, boolean isFatal) { 346 347 if (errorListener != null) { 348 try { 349 systemId = e.getSystemId(); 350 lineNumber = e.getLineNumber(); 351 columnNumber = e.getColumnNumber(); 352 TransformerException err = 353 new TransformerException ("Error reported by XML parser", this, e); 354 if (isFatal) { 355 errorListener.fatalError(err); 356 } else { 357 errorListener.error(err); 358 } 359 } catch (Exception err) {} 360 } else { 361 362 try { 363 String errcat = (isFatal ? "Fatal error" : "Error"); 364 errorOutput.write(errcat + " reported by XML parser: " + e.getMessage() + "\n"); 365 errorOutput.write(" URL: " + e.getSystemId() + "\n"); 366 errorOutput.write(" Line: " + e.getLineNumber() + "\n"); 367 errorOutput.write(" Column: " + e.getColumnNumber() + "\n"); 368 errorOutput.flush(); 369 } catch (Exception e2) { 370 System.err.println(e); 371 System.err.println(e2); 372 e2.printStackTrace(); 373 }; 374 } 375 } 376 377 378 379 383 384 public abstract void setUnparsedEntity(String name, String uri); 385 386 387 391 395 399 public String getPublicId() { 400 return null; 401 } 402 403 public int getLineNumber() { 404 return lineNumber; 405 } 406 407 public int getColumnNumber() { 408 return columnNumber; 409 } 410 411 412 } 414 | Popular Tags |