1 package com.icl.saxon; 2 3 import com.icl.saxon.trace.TraceListener; 4 import com.icl.saxon.output.*; 5 import com.icl.saxon.om.NodeInfo; 6 import com.icl.saxon.om.Namespace; 7 import com.icl.saxon.om.NamePool; 8 import com.icl.saxon.om.Builder; 9 import com.icl.saxon.om.DocumentInfo; 10 11 import java.io.*; 12 13 import java.util.Properties ; 14 import java.util.Enumeration ; 15 16 import org.xml.sax.*; 17 import org.xml.sax.helpers.ParserAdapter ; 18 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.Node ; 21 22 import javax.xml.parsers.SAXParserFactory ; 23 import javax.xml.transform.*; 24 import javax.xml.transform.stream.*; 25 import javax.xml.transform.sax.*; 26 import javax.xml.transform.dom.*; 27 28 29 42 43 public class TransformerFactoryImpl extends SAXTransformerFactory { 44 45 private URIResolver resolver = new StandardURIResolver(this); 46 private ErrorListener listener = new StandardErrorListener(); 47 private int treeModel = Builder.TINY_TREE; 48 private boolean lineNumbering = false; 49 private TraceListener traceListener = null; 50 private int recoveryPolicy = Controller.RECOVER_WITH_WARNINGS; 51 private String messageEmitterClass = "com.icl.saxon.output.MessageEmitter"; 52 private String sourceParserClass; 53 private String styleParserClass; 54 private boolean timing = false; 55 private boolean allowExternalFunctions = true; 56 57 60 public TransformerFactoryImpl() {} 61 62 76 77 public Transformer newTransformer(Source source) 78 throws TransformerConfigurationException { 79 Templates templates = newTemplates(source); 80 Transformer trans = templates.newTransformer(); 81 return trans; 84 } 85 86 97 98 public Transformer newTransformer() 99 throws TransformerConfigurationException { 100 101 return new IdentityTransformer(this); 102 } 103 104 105 121 122 public Templates newTemplates(Source source) 123 throws TransformerConfigurationException { 124 125 PreparedStyleSheet pss = new PreparedStyleSheet(this); 126 SAXSource saxSource = getSAXSource(source, true); 127 pss.prepare(saxSource); 128 return pss; 129 } 130 131 132 138 139 public SAXSource getSAXSource(Source source, boolean isStyleSheet) { 140 if (source instanceof SAXSource) { 141 if (((SAXSource)source).getXMLReader()==null) { 142 SAXSource ss = new SAXSource(); 143 ss.setInputSource(((SAXSource)source).getInputSource()); 144 ss.setSystemId(source.getSystemId()); 145 ss.setXMLReader((isStyleSheet ? getStyleParser() : getSourceParser())); 146 return ss; 147 } else { 148 return (SAXSource)source; 149 } 150 159 } else if (source instanceof DOMSource) { 160 InputSource is = new InputSource("dummy"); 161 Node startNode = ((DOMSource)source).getNode(); 162 Document doc; 163 if (startNode instanceof Document ) { 164 doc = (Document )startNode; 165 } else { 166 doc = startNode.getOwnerDocument(); 167 } 168 DOMDriver driver; 169 if (startNode instanceof NodeInfo) { 170 driver = new TreeDriver(); 171 } else { 172 driver = new DOMDriver(); 173 } 174 driver.setStartNode(doc); 175 is.setSystemId(source.getSystemId()); 176 driver.setSystemId(source.getSystemId()); 177 return new SAXSource(driver, is); 178 } else if (source instanceof StreamSource) { 179 StreamSource ss = (StreamSource)source; 180 181 188 String url = source.getSystemId(); 189 193 InputSource is = new InputSource(url); 194 is.setCharacterStream(ss.getReader()); 195 is.setByteStream(ss.getInputStream()); 196 return new SAXSource( 197 (isStyleSheet ? getStyleParser() : getSourceParser()), 198 is); 199 } else { 200 throw new IllegalArgumentException ("Unknown type of source"); 201 } 202 } 203 204 205 224 225 public Source getAssociatedStylesheet( 226 Source source, String media, String title, String charset) 227 throws TransformerConfigurationException { 228 229 230 PIGrabber grabber = new PIGrabber(); 231 grabber.setCriteria(media, title, charset); 232 grabber.setBaseURI(source.getSystemId()); 233 grabber.setURIResolver(resolver); 234 235 SAXSource saxSource = getSAXSource(source, false); 236 XMLReader parser = saxSource.getXMLReader(); 237 238 parser.setContentHandler(grabber); 239 try { 240 parser.parse(saxSource.getInputSource()); } catch (SAXException err) { 242 if (err.getMessage().equals("#start#")) { 243 } else { 245 System.err.println("Failed while looking for xml-stylesheet PI"); 247 System.err.println(err.getMessage()); 248 if (err.getException()!=null) { 249 err.getException().printStackTrace(); 250 } 251 if (err instanceof SAXParseException) { 252 SAXParseException pe = (SAXParseException)err; 253 System.err.println("At line " + pe.getLineNumber() + " in " + pe.getSystemId()); 254 } 255 throw new TransformerConfigurationException(err); 256 } 257 } catch (java.io.IOException ierr) { 258 System.err.println(ierr.getMessage()); 259 throw new TransformerConfigurationException( 260 "XML parsing failure while looking for <?xml-stylesheet?>"); 261 } 262 try { 263 SAXSource[] sources = grabber.getAssociatedStylesheets(); 264 if (sources==null) { 265 throw new TransformerConfigurationException( 266 "No matching <?xml-stylesheet?> processing instruction found"); 267 } 268 return compositeStylesheet(sources); 269 } catch (TransformerException err) { 270 if (err instanceof TransformerConfigurationException) { 271 throw (TransformerConfigurationException)err; 272 } else { 273 throw new TransformerConfigurationException(err); 274 } 275 } 276 } 277 278 286 287 public Source compositeStylesheet(SAXSource[] sources) 288 throws TransformerConfigurationException { 289 290 292 if (sources.length == 1) { 293 return sources[0]; 294 } else if (sources.length == 0) { 295 throw new TransformerConfigurationException( 296 "No stylesheets were supplied"); 297 } 298 299 301 StringBuffer sb = new StringBuffer (); 302 sb.append("<xsl:stylesheet version='1.0' "); 303 sb.append(" xmlns:xsl='" + Namespace.XSLT + "'>"); 304 for (int i=0; i<sources.length; i++) { 305 sb.append("<xsl:import HREF='" + sources[i].getInputSource().getSystemId() + "'/>"); 306 } 307 sb.append("</xsl:stylesheet>"); 308 InputSource composite = new InputSource(); 309 composite.setCharacterStream(new StringReader(sb.toString())); 310 return new SAXSource(getSourceParser(), composite); 311 } 312 313 320 321 public void setURIResolver(URIResolver resolver) { 322 this.resolver = resolver; 323 } 324 325 331 332 public URIResolver getURIResolver() { 333 return resolver; 334 } 335 336 338 345 346 public boolean getFeature(String name) { 347 if (name.equals(SAXSource.FEATURE)) return true; 348 if (name.equals(SAXResult.FEATURE)) return true; 349 if (name.equals(DOMSource.FEATURE)) return true; 350 if (name.equals(DOMResult.FEATURE)) return true; 351 if (name.equals(StreamSource.FEATURE)) return true; 352 if (name.equals(StreamResult.FEATURE)) return true; 353 if (name.equals(SAXTransformerFactory.FEATURE)) return true; 354 if (name.equals(SAXTransformerFactory.FEATURE_XMLFILTER)) return true; 355 throw new IllegalArgumentException ("Unknown feature " + name); 356 } 357 358 369 370 public void setAttribute(String name, Object value) 371 throws IllegalArgumentException { 372 if (name.equals(FeatureKeys.TREE_MODEL)) { 373 if (!(value instanceof Integer )) { 374 throw new IllegalArgumentException ("Tree model must be an Integer"); 375 } 376 treeModel = ((Integer )value).intValue(); 377 378 } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) { 379 if (!(value instanceof Boolean )) { 380 throw new IllegalArgumentException ("allow-external-functions must be a boolean"); 381 } 382 allowExternalFunctions = ((Boolean )value).booleanValue(); 383 384 } else if (name.equals(FeatureKeys.TIMING)) { 385 if (!(value instanceof Boolean )) { 386 throw new IllegalArgumentException ("Timing must be a boolean"); 387 } 388 timing = ((Boolean )value).booleanValue(); 389 390 } else if (name.equals(FeatureKeys.TRACE_LISTENER)) { 391 if (!(value instanceof TraceListener)) { 392 throw new IllegalArgumentException ("Trace listener is of wrong class"); 393 } 394 traceListener = (TraceListener)value; 395 396 } else if (name.equals(FeatureKeys.LINE_NUMBERING)) { 397 if (!(value instanceof Boolean )) { 398 throw new IllegalArgumentException ("Line Numbering value must be Boolean"); 399 } 400 lineNumbering = ((Boolean )value).booleanValue(); 401 402 } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) { 403 if (!(value instanceof Integer )) { 404 throw new IllegalArgumentException ("Recovery Policy value must be Integer"); 405 } 406 recoveryPolicy = ((Integer )value).intValue(); 407 408 } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) { 409 if (!(value instanceof String )) { 410 throw new IllegalArgumentException ("Message Emitter class must be a String"); 411 } 412 messageEmitterClass = (String )value; 413 414 } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) { 415 if (!(value instanceof String )) { 416 throw new IllegalArgumentException ("Source Parser class must be a String"); 417 } 418 sourceParserClass = (String )value; 419 420 } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) { 421 if (!(value instanceof String )) { 422 throw new IllegalArgumentException ("Style Parser class must be a String"); 423 } 424 styleParserClass = (String )value; 425 426 } else { 427 throw new IllegalArgumentException ("Unknown attribute " + name); 428 } 429 } 430 431 439 public Object getAttribute(String name) 440 throws IllegalArgumentException { 441 if (name.equals(FeatureKeys.TREE_MODEL)) { 442 return new Integer (treeModel); 443 444 } else if (name.equals(FeatureKeys.TIMING)) { 445 return new Boolean (timing); 446 447 } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) { 448 return new Boolean (allowExternalFunctions); 449 450 } else if (name.equals(FeatureKeys.TRACE_LISTENER)) { 451 return traceListener; 452 453 } else if (name.equals(FeatureKeys.LINE_NUMBERING)) { 454 return new Boolean (lineNumbering); 455 456 } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) { 457 return new Integer (recoveryPolicy); 458 459 } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) { 460 return messageEmitterClass; 461 462 } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) { 463 return sourceParserClass; 464 465 } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) { 466 return styleParserClass; 467 468 } else { 469 throw new IllegalArgumentException ("Unknown attribute " + name); 470 } 471 } 472 473 481 482 public void setErrorListener(ErrorListener listener) 483 throws IllegalArgumentException { 484 this.listener = listener; 485 } 486 487 492 public ErrorListener getErrorListener() { 493 return listener; 494 } 495 496 499 500 public XMLReader getSourceParser() throws TransformerFactoryConfigurationError { 501 if (sourceParserClass!=null) { 502 return makeParser(sourceParserClass); 503 } else { 504 try { 505 return SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 506 } catch (Exception err) { 507 throw new TransformerFactoryConfigurationError(err); 508 } 509 } 510 } 511 512 515 516 public XMLReader getStyleParser() throws TransformerFactoryConfigurationError { 517 if (styleParserClass!=null) { 518 return makeParser(styleParserClass); 519 } else { 520 try { 521 return SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 522 } catch (Exception err) { 523 throw new TransformerFactoryConfigurationError(err); 524 } 525 } 526 } 527 528 542 public static XMLReader makeParser (String className) 543 throws TransformerFactoryConfigurationError 544 { 545 Object obj; 546 try { 547 obj = Loader.getInstance(className); 548 } catch (TransformerException err) { 549 throw new TransformerFactoryConfigurationError(err); 550 } 551 if (obj instanceof XMLReader) { 552 return (XMLReader)obj; 553 } 554 if (obj instanceof Parser) { 555 return new ParserAdapter ((Parser)obj); 556 } 557 throw new TransformerFactoryConfigurationError("Class " + className + 558 " is neither a SAX1 Parser nor a SAX2 XMLReader"); 559 } 560 561 565 577 578 public TransformerHandler newTransformerHandler(Source src) 579 throws TransformerConfigurationException { 580 Templates tmpl = newTemplates(src); 581 return newTransformerHandler(tmpl); 582 } 583 584 595 596 public TransformerHandler newTransformerHandler(Templates templates) 597 throws TransformerConfigurationException { 598 if (!(templates instanceof PreparedStyleSheet)) { 599 throw new TransformerConfigurationException("Templates object was not created by Saxon"); 600 } 601 Controller controller = (Controller)templates.newTransformer(); 602 if (controller.usesPreviewMode()) { 603 throw new TransformerConfigurationException("Preview mode is not available with a TransformerHandler"); 604 } 605 TransformerHandlerImpl handler = new TransformerHandlerImpl(controller); 606 return handler; 607 } 608 609 621 622 public TransformerHandler newTransformerHandler() 623 throws TransformerConfigurationException { 624 Controller controller = new IdentityTransformer(this); 625 return new IdentityTransformerHandler(controller); 626 } 627 628 638 639 public TemplatesHandler newTemplatesHandler() 640 throws TransformerConfigurationException { 641 return new TemplatesHandlerImpl(this); 642 } 643 644 655 656 public XMLFilter newXMLFilter(Source src) 657 throws TransformerConfigurationException { 658 Templates tmpl = newTemplates(src); 659 return newXMLFilter(tmpl); 660 } 661 662 672 673 public XMLFilter newXMLFilter(Templates templates) 674 throws TransformerConfigurationException { 675 if (!(templates instanceof PreparedStyleSheet)) { 676 throw new TransformerConfigurationException("Supplied Templates object was not created using Saxon"); 677 } 678 Controller controller = (Controller)templates.newTransformer(); 679 return new Filter(controller); 680 } 681 682 683 } 684 | Popular Tags |