1 16 19 package org.apache.xalan.transformer; 20 21 import java.io.IOException ; 22 23 import javax.xml.transform.Result ; 24 import javax.xml.transform.Transformer ; 25 import javax.xml.transform.sax.TransformerHandler ; 26 27 import org.apache.xalan.res.XSLMessages; 28 import org.apache.xalan.res.XSLTErrorResources; 29 import org.apache.xml.dtm.DTM; 30 import org.apache.xml.dtm.DTMManager; 31 import org.apache.xml.dtm.ref.IncrementalSAXSource_Filter; 32 import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM; 33 import org.apache.xpath.XPathContext; 34 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.ContentHandler ; 37 import org.xml.sax.DTDHandler ; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.ErrorHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.Locator ; 42 import org.xml.sax.SAXException ; 43 import org.xml.sax.SAXParseException ; 44 import org.xml.sax.ext.DeclHandler ; 45 import org.xml.sax.ext.LexicalHandler ; 46 import org.apache.xml.serializer.SerializationHandler; 47 48 49 54 public class TransformerHandlerImpl 55 implements EntityResolver , DTDHandler , ContentHandler , ErrorHandler , 56 LexicalHandler , TransformerHandler , DeclHandler 57 { 58 59 private boolean m_insideParse = false; 60 61 65 72 public TransformerHandlerImpl(TransformerImpl transformer, 73 boolean doFragment, String baseSystemID) 74 { 75 76 super(); 77 78 m_transformer = transformer; 79 m_baseSystemID = baseSystemID; 80 81 XPathContext xctxt = transformer.getXPathContext(); 82 DTM dtm = xctxt.getDTM(null, true, transformer, true, true); 83 84 m_dtm = dtm; 85 dtm.setDocumentBaseURI(baseSystemID); 86 87 m_contentHandler = dtm.getContentHandler(); 88 m_dtdHandler = dtm.getDTDHandler(); 89 m_entityResolver = dtm.getEntityResolver(); 90 m_errorHandler = dtm.getErrorHandler(); 91 m_lexicalHandler = dtm.getLexicalHandler(); 92 } 93 94 97 protected void clearCoRoutine() 98 { 99 clearCoRoutine(null); 100 } 101 102 105 protected void clearCoRoutine(SAXException ex) 106 { 107 if(null != ex) 108 m_transformer.setExceptionThrown(ex); 109 110 if(m_dtm instanceof SAX2DTM) 111 { 112 if(DEBUG) 113 System.err.println("In clearCoRoutine..."); 114 try 115 { 116 SAX2DTM sax2dtm = ((SAX2DTM)m_dtm); 117 if(null != m_contentHandler 118 && m_contentHandler instanceof IncrementalSAXSource_Filter) 119 { 120 IncrementalSAXSource_Filter sp = 121 (IncrementalSAXSource_Filter)m_contentHandler; 122 sp.deliverMoreNodes(false); 124 } 125 126 sax2dtm.clearCoRoutine(true); 127 m_contentHandler = null; 128 m_dtdHandler = null; 129 m_entityResolver = null; 130 m_errorHandler = null; 131 m_lexicalHandler = null; 132 } 133 catch(Throwable throwable) 134 { 135 throwable.printStackTrace(); 136 } 137 138 if(DEBUG) 139 System.err.println("...exiting clearCoRoutine"); 140 } 141 } 142 143 147 155 public void setResult(Result result) throws IllegalArgumentException 156 { 157 158 if (null == result) 159 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_NULL, null)); 161 try 162 { 163 SerializationHandler xoh = 167 m_transformer.createSerializationHandler(result); 168 m_transformer.setSerializationHandler(xoh); 169 } 170 catch (javax.xml.transform.TransformerException te) 171 { 172 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_COULD_NOT_BE_SET, null)); } 174 175 m_result = result; 176 } 177 178 183 public void setSystemId(String systemID) 184 { 185 m_baseSystemID = systemID; 186 m_dtm.setDocumentBaseURI(systemID); 187 } 188 189 194 public String getSystemId() 195 { 196 return m_baseSystemID; 197 } 198 199 205 public Transformer getTransformer() 206 { 207 return m_transformer; 208 } 209 210 214 229 public InputSource resolveEntity(String publicId, String systemId) 230 throws SAXException , IOException 231 { 232 233 if (m_entityResolver != null) 234 { 235 return m_entityResolver.resolveEntity(publicId, systemId); 236 } 237 else 238 { 239 return null; 240 } 241 } 242 243 247 257 public void notationDecl(String name, String publicId, String systemId) 258 throws SAXException 259 { 260 261 if (m_dtdHandler != null) 262 { 263 m_dtdHandler.notationDecl(name, publicId, systemId); 264 } 265 } 266 267 278 public void unparsedEntityDecl( 279 String name, String publicId, String systemId, String notationName) 280 throws SAXException 281 { 282 283 if (m_dtdHandler != null) 284 { 285 m_dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName); 286 } 287 } 288 289 293 299 public void setDocumentLocator(Locator locator) 300 { 301 302 if (DEBUG) 303 System.out.println("TransformerHandlerImpl#setDocumentLocator: " 304 + locator.getSystemId()); 305 306 this.m_locator = locator; 307 308 if(null == m_baseSystemID) 309 { 310 setSystemId(locator.getSystemId()); 311 } 312 313 if (m_contentHandler != null) 314 { 315 m_contentHandler.setDocumentLocator(locator); 316 } 317 } 318 319 326 public void startDocument() throws SAXException 327 { 328 329 if (DEBUG) 330 System.out.println("TransformerHandlerImpl#startDocument"); 331 332 m_insideParse = true; 333 334 336 if (m_contentHandler != null) 337 { 338 if(DTMManager.getIncremental()) 340 { 341 m_transformer.setSourceTreeDocForThread(m_dtm.getDocument()); 342 343 int cpriority = Thread.currentThread().getPriority(); 344 345 m_transformer.runTransformThread( cpriority ); 348 } 349 350 m_contentHandler.startDocument(); 355 } 356 357 360 } 361 362 369 public void endDocument() throws SAXException 370 { 371 372 if (DEBUG) 373 System.out.println("TransformerHandlerImpl#endDocument"); 374 375 m_insideParse = false; 376 377 if (m_contentHandler != null) 378 { 379 m_contentHandler.endDocument(); 380 } 381 382 if(DTMManager.getIncremental()) 383 { 384 m_transformer.waitTransformThread(); 385 } 386 else 387 { 388 m_transformer.setSourceTreeDocForThread(m_dtm.getDocument()); 389 m_transformer.run(); 390 } 391 413 } 414 415 424 public void startPrefixMapping(String prefix, String uri) 425 throws SAXException 426 { 427 428 if (DEBUG) 429 System.out.println("TransformerHandlerImpl#startPrefixMapping: " 430 + prefix + ", " + uri); 431 432 if (m_contentHandler != null) 433 { 434 m_contentHandler.startPrefixMapping(prefix, uri); 435 } 436 } 437 438 446 public void endPrefixMapping(String prefix) throws SAXException 447 { 448 449 if (DEBUG) 450 System.out.println("TransformerHandlerImpl#endPrefixMapping: " 451 + prefix); 452 453 if (m_contentHandler != null) 454 { 455 m_contentHandler.endPrefixMapping(prefix); 456 } 457 } 458 459 471 public void startElement( 472 String uri, String localName, String qName, Attributes atts) 473 throws SAXException 474 { 475 476 if (DEBUG) 477 System.out.println("TransformerHandlerImpl#startElement: " + qName); 478 479 if (m_contentHandler != null) 480 { 481 m_contentHandler.startElement(uri, localName, qName, atts); 482 } 483 } 484 485 496 public void endElement(String uri, String localName, String qName) 497 throws SAXException 498 { 499 500 if (DEBUG) 501 System.out.println("TransformerHandlerImpl#endElement: " + qName); 502 503 if (m_contentHandler != null) 504 { 505 m_contentHandler.endElement(uri, localName, qName); 506 } 507 } 508 509 519 public void characters(char ch[], int start, int length) throws SAXException 520 { 521 522 if (DEBUG) 523 System.out.println("TransformerHandlerImpl#characters: " + start + ", " 524 + length); 525 526 if (m_contentHandler != null) 527 { 528 m_contentHandler.characters(ch, start, length); 529 } 530 } 531 532 542 public void ignorableWhitespace(char ch[], int start, int length) 543 throws SAXException 544 { 545 546 if (DEBUG) 547 System.out.println("TransformerHandlerImpl#ignorableWhitespace: " 548 + start + ", " + length); 549 550 if (m_contentHandler != null) 551 { 552 m_contentHandler.ignorableWhitespace(ch, start, length); 553 } 554 } 555 556 565 public void processingInstruction(String target, String data) 566 throws SAXException 567 { 568 569 if (DEBUG) 570 System.out.println("TransformerHandlerImpl#processingInstruction: " 571 + target + ", " + data); 572 573 if (m_contentHandler != null) 574 { 575 m_contentHandler.processingInstruction(target, data); 576 } 577 } 578 579 587 public void skippedEntity(String name) throws SAXException 588 { 589 590 if (DEBUG) 591 System.out.println("TransformerHandlerImpl#skippedEntity: " + name); 592 593 if (m_contentHandler != null) 594 { 595 m_contentHandler.skippedEntity(name); 596 } 597 } 598 599 603 611 public void warning(SAXParseException e) throws SAXException 612 { 613 javax.xml.transform.ErrorListener errorListener = m_transformer.getErrorListener(); 618 if(errorListener instanceof ErrorHandler ) 619 { 620 ((ErrorHandler )errorListener).warning(e); 621 } 622 else 623 { 624 try 625 { 626 errorListener.warning(new javax.xml.transform.TransformerException (e)); 627 } 628 catch(javax.xml.transform.TransformerException te) 629 { 630 throw e; 631 } 632 } 633 } 634 635 643 public void error(SAXParseException e) throws SAXException 644 { 645 648 javax.xml.transform.ErrorListener errorListener = m_transformer.getErrorListener(); 653 if(errorListener instanceof ErrorHandler ) 654 { 655 ((ErrorHandler )errorListener).error(e); 656 if(null != m_errorHandler) 657 m_errorHandler.error(e); } 659 else 660 { 661 try 662 { 663 errorListener.error(new javax.xml.transform.TransformerException (e)); 664 if(null != m_errorHandler) 665 m_errorHandler.error(e); } 667 catch(javax.xml.transform.TransformerException te) 668 { 669 throw e; 670 } 671 } 672 } 673 674 682 public void fatalError(SAXParseException e) throws SAXException 683 { 684 if(null != m_errorHandler) 685 { 686 try 687 { 688 m_errorHandler.fatalError(e); 689 } 690 catch(SAXParseException se) 691 { 692 } 694 } 696 697 javax.xml.transform.ErrorListener errorListener = m_transformer.getErrorListener(); 702 703 if(errorListener instanceof ErrorHandler ) 704 { 705 ((ErrorHandler )errorListener).fatalError(e); 706 if(null != m_errorHandler) 707 m_errorHandler.fatalError(e); } 709 else 710 { 711 try 712 { 713 errorListener.fatalError(new javax.xml.transform.TransformerException (e)); 714 if(null != m_errorHandler) 715 m_errorHandler.fatalError(e); } 717 catch(javax.xml.transform.TransformerException te) 718 { 719 throw e; 720 } 721 } 722 } 723 724 728 749 public void startDTD(String name, String publicId, String systemId) 750 throws SAXException 751 { 752 753 if (DEBUG) 754 System.out.println("TransformerHandlerImpl#startDTD: " + name + ", " 755 + publicId + ", " + systemId); 756 757 if (null != m_lexicalHandler) 758 { 759 m_lexicalHandler.startDTD(name, publicId, systemId); 760 } 761 } 762 763 769 public void endDTD() throws SAXException 770 { 771 772 if (DEBUG) 773 System.out.println("TransformerHandlerImpl#endDTD"); 774 775 if (null != m_lexicalHandler) 776 { 777 m_lexicalHandler.endDTD(); 778 } 779 } 780 781 803 public void startEntity(String name) throws SAXException 804 { 805 806 if (DEBUG) 807 System.out.println("TransformerHandlerImpl#startEntity: " + name); 808 809 if (null != m_lexicalHandler) 810 { 811 m_lexicalHandler.startEntity(name); 812 } 813 } 814 815 822 public void endEntity(String name) throws SAXException 823 { 824 825 if (DEBUG) 826 System.out.println("TransformerHandlerImpl#endEntity: " + name); 827 828 if (null != m_lexicalHandler) 829 { 830 m_lexicalHandler.endEntity(name); 831 } 832 } 833 834 844 public void startCDATA() throws SAXException 845 { 846 847 if (DEBUG) 848 System.out.println("TransformerHandlerImpl#startCDATA"); 849 850 if (null != m_lexicalHandler) 851 { 852 m_lexicalHandler.startCDATA(); 853 } 854 } 855 856 862 public void endCDATA() throws SAXException 863 { 864 865 if (DEBUG) 866 System.out.println("TransformerHandlerImpl#endCDATA"); 867 868 if (null != m_lexicalHandler) 869 { 870 m_lexicalHandler.endCDATA(); 871 } 872 } 873 874 886 public void comment(char ch[], int start, int length) throws SAXException 887 { 888 889 if (DEBUG) 890 System.out.println("TransformerHandlerImpl#comment: " + start + ", " 891 + length); 892 893 if (null != m_lexicalHandler) 894 { 895 m_lexicalHandler.comment(ch, start, length); 896 } 897 } 898 899 903 916 public void elementDecl(String name, String model) throws SAXException 917 { 918 919 if (DEBUG) 920 System.out.println("TransformerHandlerImpl#elementDecl: " + name + ", " 921 + model); 922 923 if (null != m_declHandler) 924 { 925 m_declHandler.elementDecl(name, model); 926 } 927 } 928 929 948 public void attributeDecl( 949 String eName, String aName, String type, String valueDefault, String value) 950 throws SAXException 951 { 952 953 if (DEBUG) 954 System.out.println("TransformerHandlerImpl#attributeDecl: " + eName 955 + ", " + aName + ", etc..."); 956 957 if (null != m_declHandler) 958 { 959 m_declHandler.attributeDecl(eName, aName, type, valueDefault, value); 960 } 961 } 962 963 976 public void internalEntityDecl(String name, String value) 977 throws SAXException 978 { 979 980 if (DEBUG) 981 System.out.println("TransformerHandlerImpl#internalEntityDecl: " + name 982 + ", " + value); 983 984 if (null != m_declHandler) 985 { 986 m_declHandler.internalEntityDecl(name, value); 987 } 988 } 989 990 1005 public void externalEntityDecl( 1006 String name, String publicId, String systemId) throws SAXException 1007 { 1008 1009 if (DEBUG) 1010 System.out.println("TransformerHandlerImpl#externalEntityDecl: " + name 1011 + ", " + publicId + ", " + systemId); 1012 1013 if (null != m_declHandler) 1014 { 1015 m_declHandler.externalEntityDecl(name, publicId, systemId); 1016 } 1017 } 1018 1019 1023 1024 private static boolean DEBUG = false; 1025 1026 1030 private TransformerImpl m_transformer; 1031 1032 1033 private String m_baseSystemID; 1034 1035 1036 private Result m_result = null; 1037 1038 1039 private Locator m_locator = null; 1040 1041 1042 private EntityResolver m_entityResolver = null; 1043 1044 1045 private DTDHandler m_dtdHandler = null; 1046 1047 1048 private ContentHandler m_contentHandler = null; 1049 1050 1051 private ErrorHandler m_errorHandler = null; 1052 1053 1054 private LexicalHandler m_lexicalHandler = null; 1055 1056 1057 private DeclHandler m_declHandler = null; 1058 1059 1060 DTM m_dtm; 1061} 1062 | Popular Tags |