1 16 19 package org.apache.xalan.processor; 20 21 import java.io.BufferedInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 27 import javax.xml.transform.ErrorListener ; 28 import javax.xml.transform.Source ; 29 import javax.xml.transform.Templates ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.URIResolver ; 34 import javax.xml.transform.dom.DOMResult ; 35 import javax.xml.transform.dom.DOMSource ; 36 import javax.xml.transform.sax.SAXResult ; 37 import javax.xml.transform.sax.SAXSource ; 38 import javax.xml.transform.sax.SAXTransformerFactory ; 39 import javax.xml.transform.sax.TemplatesHandler ; 40 import javax.xml.transform.sax.TransformerHandler ; 41 import javax.xml.transform.stream.StreamResult ; 42 import javax.xml.transform.stream.StreamSource ; 43 44 import org.apache.xalan.res.XSLMessages; 45 import org.apache.xalan.res.XSLTErrorResources; 46 import org.apache.xalan.transformer.TrAXFilter; 47 import org.apache.xalan.transformer.TransformerIdentityImpl; 48 import org.apache.xalan.transformer.TransformerImpl; 49 import org.apache.xalan.transformer.XalanProperties; 50 51 import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM; 52 import org.apache.xml.utils.DefaultErrorHandler; 53 import org.apache.xml.utils.SystemIDResolver; 54 import org.apache.xml.utils.TreeWalker; 55 import org.apache.xml.utils.StylesheetPIHandler; 56 import org.apache.xml.utils.StopParseException; 57 58 import org.w3c.dom.Node ; 59 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.XMLFilter ; 62 import org.xml.sax.XMLReader ; 63 import org.xml.sax.helpers.XMLReaderFactory ; 64 65 70 public class TransformerFactoryImpl extends SAXTransformerFactory 71 { 72 73 78 public static final String XSLT_PROPERTIES = 79 "org/apache/xalan/res/XSLTInfo.properties"; 80 81 85 public TransformerFactoryImpl() 86 { 87 } 88 89 90 public static final String FEATURE_INCREMENTAL = 91 "http://xml.apache.org/xalan/features/incremental"; 92 93 94 public static final String FEATURE_OPTIMIZE = 95 "http://xml.apache.org/xalan/features/optimize"; 96 97 98 public static final String FEATURE_SOURCE_LOCATION = 99 XalanProperties.SOURCE_LOCATION; 100 101 public javax.xml.transform.Templates processFromNode(Node node) 102 throws TransformerConfigurationException 103 { 104 105 try 106 { 107 TemplatesHandler builder = newTemplatesHandler(); 108 TreeWalker walker = new TreeWalker(builder, 109 new org.apache.xml.utils.DOM2Helper(), 110 builder.getSystemId()); 111 112 walker.traverse(node); 113 114 return builder.getTemplates(); 115 } 116 catch (org.xml.sax.SAXException se) 117 { 118 if (m_errorListener != null) 119 { 120 try 121 { 122 m_errorListener.fatalError(new TransformerException (se)); 123 } 124 catch (TransformerException ex) 125 { 126 throw new TransformerConfigurationException (ex); 127 } 128 129 return null; 130 } 131 else 132 133 throw new TransformerConfigurationException (XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se); } 139 catch (TransformerConfigurationException tce) 140 { 141 throw tce; 143 } 144 149 catch (Exception e) 150 { 151 if (m_errorListener != null) 152 { 153 try 154 { 155 m_errorListener.fatalError(new TransformerException (e)); 156 } 157 catch (TransformerException ex) 158 { 159 throw new TransformerConfigurationException (ex); 160 } 161 162 return null; 163 } 164 else 165 166 throw new TransformerConfigurationException (XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e); } 172 } 173 174 178 private String m_DOMsystemID = null; 179 180 186 String getDOMsystemID() 187 { 188 return m_DOMsystemID; 189 } 190 191 205 javax.xml.transform.Templates processFromNode(Node node, String systemID) 206 throws TransformerConfigurationException 207 { 208 209 m_DOMsystemID = systemID; 210 211 return processFromNode(node); 212 } 213 214 237 public Source getAssociatedStylesheet( 238 Source source, String media, String title, String charset) 239 throws TransformerConfigurationException 240 { 241 242 String baseID; 243 InputSource isource = null; 244 Node node = null; 245 XMLReader reader = null; 246 247 if (source instanceof DOMSource ) 248 { 249 DOMSource dsource = (DOMSource ) source; 250 251 node = dsource.getNode(); 252 baseID = dsource.getSystemId(); 253 } 254 else 255 { 256 isource = SAXSource.sourceToInputSource(source); 257 baseID = isource.getSystemId(); 258 } 259 260 StylesheetPIHandler handler = new StylesheetPIHandler(baseID, media, 264 title, charset); 265 266 if (m_uriResolver != null) 268 { 269 handler.setURIResolver(m_uriResolver); 270 } 271 272 try 273 { 274 if (null != node) 275 { 276 TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), baseID); 277 278 walker.traverse(node); 279 } 280 else 281 { 282 283 try 285 { 286 javax.xml.parsers.SAXParserFactory factory = 287 javax.xml.parsers.SAXParserFactory.newInstance(); 288 289 factory.setNamespaceAware(true); 290 291 javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser(); 292 293 reader = jaxpParser.getXMLReader(); 294 } 295 catch (javax.xml.parsers.ParserConfigurationException ex) 296 { 297 throw new org.xml.sax.SAXException (ex); 298 } 299 catch (javax.xml.parsers.FactoryConfigurationError ex1) 300 { 301 throw new org.xml.sax.SAXException (ex1.toString()); 302 } 303 catch (NoSuchMethodError ex2){} 304 catch (AbstractMethodError ame){} 305 306 if (null == reader) 307 { 308 reader = XMLReaderFactory.createXMLReader(); 309 } 310 311 reader.setContentHandler(handler); 313 reader.parse(isource); 314 } 315 } 316 catch (StopParseException spe) 317 { 318 319 } 321 catch (org.xml.sax.SAXException se) 322 { 323 throw new TransformerConfigurationException ( 324 "getAssociatedStylesheets failed", se); 325 } 326 catch (IOException ioe) 327 { 328 throw new TransformerConfigurationException ( 329 "getAssociatedStylesheets failed", ioe); 330 } 331 332 return handler.getAssociatedStylesheet(); 333 } 334 335 348 public TemplatesHandler newTemplatesHandler() 349 throws TransformerConfigurationException 350 { 351 return new StylesheetHandler(this); 352 } 353 354 367 public boolean getFeature(String name) 368 { 369 370 if ((DOMResult.FEATURE == name) || (DOMSource.FEATURE == name) 373 || (SAXResult.FEATURE == name) || (SAXSource.FEATURE == name) 374 || (StreamResult.FEATURE == name) 375 || (StreamSource.FEATURE == name) 376 || (SAXTransformerFactory.FEATURE == name) 377 || (SAXTransformerFactory.FEATURE_XMLFILTER == name)) 378 return true; 379 else if ((DOMResult.FEATURE.equals(name)) 380 || (DOMSource.FEATURE.equals(name)) 381 || (SAXResult.FEATURE.equals(name)) 382 || (SAXSource.FEATURE.equals(name)) 383 || (StreamResult.FEATURE.equals(name)) 384 || (StreamSource.FEATURE.equals(name)) 385 || (SAXTransformerFactory.FEATURE.equals(name)) 386 || (SAXTransformerFactory.FEATURE_XMLFILTER.equals(name))) 387 return true; 388 else 389 return false; 390 } 391 392 public static boolean m_optimize = true; 393 394 404 public static boolean m_source_location = false; 405 406 416 public void setAttribute(String name, Object value) 417 throws IllegalArgumentException 418 { 419 if (name.equals(FEATURE_INCREMENTAL)) 420 { 421 if(value instanceof Boolean ) 422 { 423 org.apache.xml.dtm.DTMManager.setIncremental(((Boolean )value).booleanValue()); 425 } 426 else if(value instanceof String ) 427 { 428 org.apache.xml.dtm.DTMManager.setIncremental((new Boolean ((String )value)).booleanValue()); 430 } 431 else 432 { 433 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object []{name, value})); } 436 } 437 else if (name.equals(FEATURE_OPTIMIZE)) 438 { 439 if(value instanceof Boolean ) 440 { 441 m_optimize = ((Boolean )value).booleanValue(); 443 } 444 else if(value instanceof String ) 445 { 446 m_optimize = (new Boolean ((String )value)).booleanValue(); 448 } 449 else 450 { 451 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object []{name, value})); } 454 } 455 456 else if(name.equals(FEATURE_SOURCE_LOCATION)) 462 { 463 if(value instanceof Boolean ) 464 { 465 m_source_location = ((Boolean )value).booleanValue(); 467 SAX2DTM.setUseSourceLocation(m_source_location); 469 } 470 else if(value instanceof String ) 471 { 472 m_source_location = (new Boolean ((String )value)).booleanValue(); 474 SAX2DTM.setUseSourceLocation(m_source_location); 475 } 476 else 477 { 478 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_BAD_VALUE, new Object []{name, value})); } 481 } 482 483 else 484 { 485 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUPPORTED, new Object []{name})); } 487 } 488 489 499 public Object getAttribute(String name) throws IllegalArgumentException 500 { 501 if (name.equals(FEATURE_INCREMENTAL)) 502 { 503 return new Boolean (org.apache.xml.dtm.DTMManager.getIncremental()); 504 } 505 else if (name.equals(FEATURE_OPTIMIZE)) 506 { 507 return new Boolean (m_optimize); 508 } 509 else if (name.equals(FEATURE_SOURCE_LOCATION)) 510 { 511 return new Boolean (m_source_location); 512 } 513 else 514 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_ATTRIB_VALUE_NOT_RECOGNIZED, new Object []{name})); } 516 517 527 public XMLFilter newXMLFilter(Source src) 528 throws TransformerConfigurationException 529 { 530 531 Templates templates = newTemplates(src); 532 if( templates==null ) return null; 533 534 return newXMLFilter(templates); 535 } 536 537 549 public XMLFilter newXMLFilter(Templates templates) 550 throws TransformerConfigurationException 551 { 552 try { 553 return new TrAXFilter(templates); 554 } catch( TransformerConfigurationException ex ) { 555 if( m_errorListener != null) { 556 try { 557 m_errorListener.fatalError( ex ); 558 return null; 559 } catch( TransformerException ex1 ) { 560 new TransformerConfigurationException (ex1); 561 } 562 } 563 throw ex; 564 } 565 } 566 567 578 public TransformerHandler newTransformerHandler(Source src) 579 throws TransformerConfigurationException 580 { 581 582 Templates templates = newTemplates(src); 583 if( templates==null ) return null; 584 585 return newTransformerHandler(templates); 586 } 587 588 597 public TransformerHandler newTransformerHandler(Templates templates) 598 throws TransformerConfigurationException 599 { 600 try { 601 TransformerImpl transformer = 602 (TransformerImpl) templates.newTransformer(); 603 transformer.setURIResolver(m_uriResolver); 604 TransformerHandler th = 605 (TransformerHandler ) transformer.getInputContentHandler(true); 606 607 return th; 608 } catch( TransformerConfigurationException ex ) { 609 if( m_errorListener != null ) { 610 try { 611 m_errorListener.fatalError( ex ); 612 return null; 613 } catch (TransformerException ex1 ) { 614 ex=new TransformerConfigurationException (ex1); 615 } 616 } 617 throw ex; 618 } 619 620 } 621 622 633 643 public TransformerHandler newTransformerHandler() 644 throws TransformerConfigurationException 645 { 646 return new TransformerIdentityImpl(); 647 } 648 649 662 public Transformer newTransformer(Source source) 663 throws TransformerConfigurationException 664 { 665 try { 666 Templates tmpl=newTemplates( source ); 667 673 if( tmpl==null ) return null; 674 Transformer transformer = tmpl.newTransformer(); 675 transformer.setURIResolver(m_uriResolver); 676 return transformer; 677 } catch( TransformerConfigurationException ex ) { 678 if( m_errorListener != null ) { 679 try { 680 m_errorListener.fatalError( ex ); 681 return null; 682 } catch( TransformerException ex1 ) { 683 ex=new TransformerConfigurationException ( ex1 ); 684 } 685 } 686 throw ex; 687 } 688 } 689 690 703 public Transformer newTransformer() throws TransformerConfigurationException 704 { 705 return new TransformerIdentityImpl(); 706 } 707 708 722 public Templates newTemplates(Source source) 723 throws TransformerConfigurationException 724 { 725 726 String baseID = source.getSystemId(); 727 728 if (null != baseID) { 729 baseID = SystemIDResolver.getAbsoluteURI(baseID); 730 } 731 732 733 if (source instanceof DOMSource ) 734 { 735 DOMSource dsource = (DOMSource ) source; 736 Node node = dsource.getNode(); 737 738 if (null != node) 739 return processFromNode(node, baseID); 740 else 741 { 742 String messageStr = XSLMessages.createMessage( 743 XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null); 744 745 throw new IllegalArgumentException (messageStr); 746 } 747 } 748 749 TemplatesHandler builder = newTemplatesHandler(); 750 builder.setSystemId(baseID); 751 752 try 753 { 754 InputSource isource = SAXSource.sourceToInputSource(source); 755 isource.setSystemId(baseID); 756 XMLReader reader = null; 757 758 if (source instanceof SAXSource ) 759 reader = ((SAXSource ) source).getXMLReader(); 760 761 if (null == reader) 762 { 763 764 try 766 { 767 javax.xml.parsers.SAXParserFactory factory = 768 javax.xml.parsers.SAXParserFactory.newInstance(); 769 770 factory.setNamespaceAware(true); 771 772 javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser(); 773 774 reader = jaxpParser.getXMLReader(); 775 } 776 catch (javax.xml.parsers.ParserConfigurationException ex) 777 { 778 throw new org.xml.sax.SAXException (ex); 779 } 780 catch (javax.xml.parsers.FactoryConfigurationError ex1) 781 { 782 throw new org.xml.sax.SAXException (ex1.toString()); 783 } 784 catch (NoSuchMethodError ex2){} 785 catch (AbstractMethodError ame){} 786 } 787 788 if (null == reader) 789 reader = XMLReaderFactory.createXMLReader(); 790 791 reader.setContentHandler(builder); 795 reader.parse(isource); 796 } 797 catch (org.xml.sax.SAXException se) 798 { 799 if (m_errorListener != null) 800 { 801 try 802 { 803 m_errorListener.fatalError(new TransformerException (se)); 804 } 805 catch (TransformerException ex1) 806 { 807 throw new TransformerConfigurationException (ex1); 808 } 809 } 810 else 811 throw new TransformerConfigurationException (se.getMessage(), se); 812 } 813 catch (Exception e) 814 { 815 if (m_errorListener != null) 816 { 817 try 818 { 819 m_errorListener.fatalError(new TransformerException (e)); 820 821 return null; 822 } 823 catch (TransformerException ex1) 824 { 825 throw new TransformerConfigurationException (ex1); 826 } 827 } 828 else 829 throw new TransformerConfigurationException (e.getMessage(), e); 830 } 831 832 return builder.getTemplates(); 833 } 834 835 839 URIResolver m_uriResolver; 840 841 848 public void setURIResolver(URIResolver resolver) 849 { 850 m_uriResolver = resolver; 851 } 852 853 860 public URIResolver getURIResolver() 861 { 862 return m_uriResolver; 863 } 864 865 866 private ErrorListener m_errorListener = new DefaultErrorHandler(); 867 868 873 public ErrorListener getErrorListener() 874 { 875 return m_errorListener; 876 } 877 878 885 public void setErrorListener(ErrorListener listener) 886 throws IllegalArgumentException 887 { 888 889 if (null == listener) 890 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_ERRORLISTENER, null)); 891 893 m_errorListener = listener; 894 } 895 } 896 | Popular Tags |