1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.io.Reader ; 28 import java.io.Writer ; 29 import java.net.URI ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.net.UnknownServiceException ; 33 import java.util.Enumeration ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 import java.util.Vector ; 37 import java.lang.reflect.Constructor ; 38 39 import javax.xml.parsers.DocumentBuilder ; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 import javax.xml.parsers.ParserConfigurationException ; 42 import javax.xml.transform.ErrorListener ; 43 import javax.xml.transform.OutputKeys ; 44 import javax.xml.transform.Result ; 45 import javax.xml.transform.Source ; 46 import javax.xml.transform.Transformer ; 47 import javax.xml.transform.TransformerException ; 48 import javax.xml.transform.URIResolver ; 49 import javax.xml.transform.dom.DOMResult ; 50 import javax.xml.transform.dom.DOMSource ; 51 import javax.xml.transform.sax.SAXResult ; 52 import javax.xml.transform.sax.SAXSource ; 53 import javax.xml.transform.stream.StreamResult ; 54 import javax.xml.transform.stream.StreamSource ; 55 56 import com.sun.org.apache.xml.internal.utils.SystemIDResolver; 57 58 import com.sun.org.apache.xalan.internal.xsltc.DOM; 59 import com.sun.org.apache.xalan.internal.xsltc.DOMCache; 60 import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM; 61 import com.sun.org.apache.xalan.internal.xsltc.StripFilter; 62 import com.sun.org.apache.xalan.internal.xsltc.Translet; 63 import com.sun.org.apache.xalan.internal.xsltc.TransletException; 64 import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory; 65 import com.sun.org.apache.xml.internal.serializer.SerializationHandler; 66 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 67 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter; 68 import com.sun.org.apache.xalan.internal.xsltc.dom.SAXImpl; 69 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager; 70 import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; 71 import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable; 72 import com.sun.org.apache.xalan.internal.xsltc.runtime.output.TransletOutputHandlerFactory; 73 74 import com.sun.org.apache.xml.internal.dtm.DTMWSFilter; 75 import com.sun.org.apache.xml.internal.utils.XMLReaderManager; 76 77 import org.xml.sax.ContentHandler ; 78 import org.xml.sax.InputSource ; 79 import org.xml.sax.SAXException ; 80 import org.xml.sax.XMLReader ; 81 import org.xml.sax.ext.LexicalHandler ; 82 83 88 public final class TransformerImpl extends Transformer 89 implements DOMCache, ErrorListener 90 { 91 private final static String EMPTY_STRING = ""; 92 private final static String NO_STRING = "no"; 93 private final static String YES_STRING = "yes"; 94 private final static String XML_STRING = "xml"; 95 96 private final static String LEXICAL_HANDLER_PROPERTY = 97 "http://xml.org/sax/properties/lexical-handler"; 98 private static final String NAMESPACE_FEATURE = 99 "http://xml.org/sax/features/namespaces"; 100 private static final String NAMESPACE_PREFIXES_FEATURE = 101 "http://xml.org/sax/features/namespace-prefixes"; 102 103 106 private AbstractTranslet _translet = null; 107 108 111 private String _method = null; 112 113 116 private String _encoding = null; 117 118 121 private String _sourceSystemId = null; 122 123 126 private ErrorListener _errorListener = this; 127 128 131 private URIResolver _uriResolver = null; 132 133 136 private Properties _properties, _propertiesClone; 137 138 141 private TransletOutputHandlerFactory _tohFactory = null; 142 143 146 private DOM _dom = null; 147 148 151 private int _indentNumber; 152 153 157 private TransformerFactoryImpl _tfactory = null; 158 159 163 private XSLTCDTMManager _dtmManager = null; 164 165 168 private XMLReaderManager _readerManager = XMLReaderManager.getInstance(); 169 170 173 175 179 private boolean _isIdentity = false; 180 181 184 private boolean _isSecureProcessing = false; 185 186 191 private Hashtable _parameters = null; 192 193 197 static class MessageHandler 198 extends com.sun.org.apache.xalan.internal.xsltc.runtime.MessageHandler 199 { 200 private ErrorListener _errorListener; 201 202 public MessageHandler(ErrorListener errorListener) { 203 _errorListener = errorListener; 204 } 205 206 public void displayMessage(String msg) { 207 if(_errorListener == null) { 208 System.err.println(msg); 209 } 210 else { 211 try { 212 _errorListener.warning(new TransformerException (msg)); 213 } 214 catch (TransformerException e) { 215 } 217 } 218 } 219 } 220 221 protected TransformerImpl(Properties outputProperties, int indentNumber, 222 TransformerFactoryImpl tfactory) 223 { 224 this(null, outputProperties, indentNumber, tfactory); 225 _isIdentity = true; 226 } 228 229 protected TransformerImpl(Translet translet, Properties outputProperties, 230 int indentNumber, TransformerFactoryImpl tfactory) 231 { 232 _translet = (AbstractTranslet) translet; 233 _properties = createOutputProperties(outputProperties); 234 _propertiesClone = (Properties ) _properties.clone(); 235 _indentNumber = indentNumber; 236 _tfactory = tfactory; 237 } 239 240 243 public boolean isSecureProcessing() { 244 return _isSecureProcessing; 245 } 246 247 250 public void setSecureProcessing(boolean flag) { 251 _isSecureProcessing = flag; 252 } 253 254 258 protected AbstractTranslet getTranslet() { 259 return _translet; 260 } 261 262 public boolean isIdentity() { 263 return _isIdentity; 264 } 265 266 273 public void transform(Source source, Result result) 274 throws TransformerException 275 { 276 if (!_isIdentity) { 277 if (_translet == null) { 278 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_TRANSLET_ERR); 279 throw new TransformerException (err.toString()); 280 } 281 transferOutputProperties(_translet); 283 } 284 285 final SerializationHandler toHandler = getOutputHandler(result); 286 if (toHandler == null) { 287 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_HANDLER_ERR); 288 throw new TransformerException (err.toString()); 289 } 290 291 if (_uriResolver != null && !_isIdentity) { 292 _translet.setDOMCache(this); 293 } 294 295 if (_isIdentity) { 297 transferOutputProperties(toHandler); 298 } 299 300 transform(source, toHandler, _encoding); 301 302 if (result instanceof DOMResult ) { 303 ((DOMResult )result).setNode(_tohFactory.getNode()); 304 } 305 } 306 307 312 public SerializationHandler getOutputHandler(Result result) 313 throws TransformerException 314 { 315 _method = (String ) _properties.get(OutputKeys.METHOD); 317 318 _encoding = (String ) _properties.getProperty(OutputKeys.ENCODING); 320 321 _tohFactory = TransletOutputHandlerFactory.newInstance(); 322 _tohFactory.setEncoding(_encoding); 323 if (_method != null) { 324 _tohFactory.setOutputMethod(_method); 325 } 326 327 if (_indentNumber >= 0) { 329 _tohFactory.setIndentNumber(_indentNumber); 330 } 331 332 try { 334 if (result instanceof SAXResult ) { 336 final SAXResult target = (SAXResult )result; 337 final ContentHandler handler = target.getHandler(); 338 339 _tohFactory.setHandler(handler); 340 341 346 LexicalHandler lexicalHandler = target.getLexicalHandler(); 347 348 if (lexicalHandler != null ) { 349 _tohFactory.setLexicalHandler(lexicalHandler); 350 } 351 352 _tohFactory.setOutputType(TransletOutputHandlerFactory.SAX); 353 return _tohFactory.getSerializationHandler(); 354 } 355 else if (result instanceof DOMResult ) { 356 _tohFactory.setNode(((DOMResult ) result).getNode()); 357 _tohFactory.setOutputType(TransletOutputHandlerFactory.DOM); 358 return _tohFactory.getSerializationHandler(); 359 } 360 else if (result instanceof StreamResult ) { 361 final StreamResult target = (StreamResult ) result; 363 364 368 _tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM); 369 370 final Writer writer = target.getWriter(); 372 if (writer != null) { 373 _tohFactory.setWriter(writer); 374 return _tohFactory.getSerializationHandler(); 375 } 376 377 final OutputStream ostream = target.getOutputStream(); 379 if (ostream != null) { 380 _tohFactory.setOutputStream(ostream); 381 return _tohFactory.getSerializationHandler(); 382 } 383 384 String systemId = result.getSystemId(); 386 if (systemId == null) { 387 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_RESULT_ERR); 388 throw new TransformerException (err.toString()); 389 } 390 391 URL url = null; 395 396 if (systemId.startsWith("file:")) { 397 try{ 401 Class clazz = ObjectFactory.findProviderClass("java.net.URI", ObjectFactory.findClassLoader(), true); 402 Constructor construct = clazz.getConstructor(new Class [] {java.lang.String .class} ); 403 URI uri = (URI ) construct.newInstance(new Object []{systemId}) ; 404 systemId = "file:"; 405 406 String host = uri.getHost(); String path = uri.getPath(); if (path == null) { 409 path = ""; 410 } 411 412 if (host != null) { 415 systemId += "//" + host + path; 416 } else { 417 systemId += path; 418 } 419 } 420 catch(ClassNotFoundException e){ 421 } 424 catch (Exception exception) { 425 } 427 428 url = new URL (systemId); 429 430 _tohFactory.setOutputStream( 431 new FileOutputStream (url.getFile())); 432 return _tohFactory.getSerializationHandler(); 433 } 434 else if (systemId.startsWith("http:")) { 435 url = new URL (systemId); 436 final URLConnection connection = url.openConnection(); 437 _tohFactory.setOutputStream(connection.getOutputStream()); 438 return _tohFactory.getSerializationHandler(); 439 } 440 else { 441 url = new File (systemId).toURL(); 443 _tohFactory.setOutputStream( 444 new FileOutputStream (url.getFile())); 445 return _tohFactory.getSerializationHandler(); 446 } 447 } 448 } 449 catch (UnknownServiceException e) { 451 throw new TransformerException (e); 452 } 453 catch (ParserConfigurationException e) { 454 throw new TransformerException (e); 455 } 456 catch (IOException e) { 458 throw new TransformerException (e); 459 } 460 return null; 461 } 462 463 466 protected void setDOM(DOM dom) { 467 _dom = dom; 468 } 469 470 473 private DOM getDOM(Source source) throws TransformerException { 474 try { 475 DOM dom = null; 476 477 if (source != null) { 478 DTMWSFilter wsfilter; 479 if (_translet != null && _translet instanceof StripFilter) { 480 wsfilter = new DOMWSFilter(_translet); 481 } else { 482 wsfilter = null; 483 } 484 485 boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() 486 : false; 487 488 if (_dtmManager == null) { 489 _dtmManager = 490 (XSLTCDTMManager)_tfactory.getDTMManagerClass() 491 .newInstance(); 492 } 493 dom = (DOM)_dtmManager.getDTM(source, false, wsfilter, true, 494 false, false, 0, hasIdCall); 495 } else if (_dom != null) { 496 dom = _dom; 497 _dom = null; } else { 499 return null; 500 } 501 502 if (!_isIdentity) { 503 _translet.prepassDocument(dom); 506 } 507 508 return dom; 509 510 } 511 catch (Exception e) { 512 if (_errorListener != null) { 513 postErrorToListener(e.getMessage()); 514 } 515 throw new TransformerException (e); 516 } 517 } 518 519 523 protected TransformerFactoryImpl getTransformerFactory() { 524 return _tfactory; 525 } 526 527 531 protected TransletOutputHandlerFactory getTransletOutputHandlerFactory() { 532 return _tohFactory; 533 } 534 535 private void transformIdentity(Source source, SerializationHandler handler) 536 throws Exception 537 { 538 if (source != null) { 540 _sourceSystemId = source.getSystemId(); 541 } 542 543 if (source instanceof StreamSource ) { 544 final StreamSource stream = (StreamSource ) source; 545 final InputStream streamInput = stream.getInputStream(); 546 final Reader streamReader = stream.getReader(); 547 final XMLReader reader = _readerManager.getXMLReader(); 548 549 try { 550 try { 552 reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); 553 reader.setFeature(NAMESPACE_PREFIXES_FEATURE, true); 554 } 555 catch (SAXException e) { 556 } 558 reader.setContentHandler(handler); 559 560 InputSource input; 562 if (streamInput != null) { 563 input = new InputSource (streamInput); 564 input.setSystemId(_sourceSystemId); 565 } 566 else if (streamReader != null) { 567 input = new InputSource (streamReader); 568 input.setSystemId(_sourceSystemId); 569 } 570 else if (_sourceSystemId != null) { 571 input = new InputSource (_sourceSystemId); 572 } 573 else { 574 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR); 575 throw new TransformerException (err.toString()); 576 } 577 578 reader.parse(input); 580 } finally { 581 _readerManager.releaseXMLReader(reader); 582 } 583 } else if (source instanceof SAXSource ) { 584 final SAXSource sax = (SAXSource ) source; 585 XMLReader reader = sax.getXMLReader(); 586 final InputSource input = sax.getInputSource(); 587 boolean userReader = true; 588 589 try { 590 if (reader == null) { 592 reader = _readerManager.getXMLReader(); 593 userReader = false; 594 } 595 596 try { 598 reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); 599 reader.setFeature(NAMESPACE_PREFIXES_FEATURE, true); 600 } 601 catch (SAXException e) { 602 } 604 reader.setContentHandler(handler); 605 606 reader.parse(input); 608 } finally { 609 if (!userReader) { 610 _readerManager.releaseXMLReader(reader); 611 } 612 } 613 } else if (source instanceof DOMSource ) { 614 final DOMSource domsrc = (DOMSource ) source; 615 new DOM2TO(domsrc.getNode(), handler).parse(); 616 } else if (source instanceof XSLTCSource) { 617 final DOM dom = ((XSLTCSource) source).getDOM(null, _translet); 618 ((SAXImpl)dom).copy(handler); 619 } else { 620 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR); 621 throw new TransformerException (err.toString()); 622 } 623 } 624 625 628 private void transform(Source source, SerializationHandler handler, 629 String encoding) throws TransformerException 630 { 631 try { 632 640 if ((source instanceof StreamSource && source.getSystemId()==null 641 && ((StreamSource )source).getInputStream()==null && 642 ((StreamSource )source).getReader()==null)|| 643 (source instanceof SAXSource && 644 ((SAXSource )source).getInputSource()==null && 645 ((SAXSource )source).getXMLReader()==null )|| 646 (source instanceof DOMSource && 647 ((DOMSource )source).getNode()==null)){ 648 DocumentBuilderFactory builderF = 649 DocumentBuilderFactory.newInstance(); 650 DocumentBuilder builder = 651 builderF.newDocumentBuilder(); 652 String systemID = source.getSystemId(); 653 source = new DOMSource (builder.newDocument()); 654 655 if (systemID != null) { 657 source.setSystemId(systemID); 658 } 659 } 660 if (_isIdentity) { 661 transformIdentity(source, handler); 662 } else { 663 _translet.transform(getDOM(source), handler); 664 } 665 } catch (TransletException e) { 666 if (_errorListener != null) postErrorToListener(e.getMessage()); 667 throw new TransformerException (e); 668 } catch (RuntimeException e) { 669 if (_errorListener != null) postErrorToListener(e.getMessage()); 670 throw new TransformerException (e); 671 } catch (Exception e) { 672 if (_errorListener != null) postErrorToListener(e.getMessage()); 673 throw new TransformerException (e); 674 } finally { 675 _dtmManager = null; 676 } 677 } 678 679 685 public ErrorListener getErrorListener() { 686 return _errorListener; 687 } 688 689 698 public void setErrorListener(ErrorListener listener) 699 throws IllegalArgumentException { 700 if (listener == null) { 701 ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR, 702 "Transformer"); 703 throw new IllegalArgumentException (err.toString()); 704 } 705 _errorListener = listener; 706 707 if (_translet != null) 709 _translet.setMessageHandler(new MessageHandler(_errorListener)); 710 } 711 712 715 private void postErrorToListener(String message) { 716 try { 717 _errorListener.error(new TransformerException (message)); 718 } 719 catch (TransformerException e) { 720 } 722 } 723 724 727 private void postWarningToListener(String message) { 728 try { 729 _errorListener.warning(new TransformerException (message)); 730 } 731 catch (TransformerException e) { 732 } 734 } 735 736 741 private String makeCDATAString(Hashtable cdata) { 742 if (cdata == null) return null; 744 745 StringBuffer result = new StringBuffer (); 746 747 Enumeration elements = cdata.keys(); 749 if (elements.hasMoreElements()) { 750 result.append((String )elements.nextElement()); 751 while (elements.hasMoreElements()) { 752 String element = (String )elements.nextElement(); 753 result.append(' '); 754 result.append(element); 755 } 756 } 757 758 return(result.toString()); 759 } 760 761 772 public Properties getOutputProperties() { 773 return (Properties ) _properties.clone(); 774 } 775 776 785 public String getOutputProperty(String name) 786 throws IllegalArgumentException 787 { 788 if (!validOutputProperty(name)) { 789 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 790 throw new IllegalArgumentException (err.toString()); 791 } 792 return _properties.getProperty(name); 793 } 794 795 804 public void setOutputProperties(Properties properties) 805 throws IllegalArgumentException 806 { 807 if (properties != null) { 808 final Enumeration names = properties.propertyNames(); 809 810 while (names.hasMoreElements()) { 811 final String name = (String ) names.nextElement(); 812 813 if (isDefaultProperty(name, properties)) continue; 815 816 if (validOutputProperty(name)) { 817 _properties.setProperty(name, properties.getProperty(name)); 818 } 819 else { 820 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 821 throw new IllegalArgumentException (err.toString()); 822 } 823 } 824 } 825 else { 826 _properties = _propertiesClone; 827 } 828 } 829 830 840 public void setOutputProperty(String name, String value) 841 throws IllegalArgumentException 842 { 843 if (!validOutputProperty(name)) { 844 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 845 throw new IllegalArgumentException (err.toString()); 846 } 847 _properties.setProperty(name, value); 848 } 849 850 854 private void transferOutputProperties(AbstractTranslet translet) 855 { 856 if (_properties == null) return; 858 859 Enumeration names = _properties.propertyNames(); 861 while (names.hasMoreElements()) { 862 String name = (String ) names.nextElement(); 864 String value = (String ) _properties.get(name); 865 866 if (value == null) continue; 868 869 if (name.equals(OutputKeys.ENCODING)) { 871 translet._encoding = value; 872 } 873 else if (name.equals(OutputKeys.METHOD)) { 874 translet._method = value; 875 } 876 else if (name.equals(OutputKeys.DOCTYPE_PUBLIC)) { 877 translet._doctypePublic = value; 878 } 879 else if (name.equals(OutputKeys.DOCTYPE_SYSTEM)) { 880 translet._doctypeSystem = value; 881 } 882 else if (name.equals(OutputKeys.MEDIA_TYPE)) { 883 translet._mediaType = value; 884 } 885 else if (name.equals(OutputKeys.STANDALONE)) { 886 translet._standalone = value; 887 } 888 else if (name.equals(OutputKeys.VERSION)) { 889 translet._version = value; 890 } 891 else if (name.equals(OutputKeys.OMIT_XML_DECLARATION)) { 892 translet._omitHeader = 893 (value != null && value.toLowerCase().equals("yes")); 894 } 895 else if (name.equals(OutputKeys.INDENT)) { 896 translet._indent = 897 (value != null && value.toLowerCase().equals("yes")); 898 } 899 else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { 900 if (value != null) { 901 translet._cdata = null; StringTokenizer e = new StringTokenizer (value); 903 while (e.hasMoreTokens()) { 904 translet.addCdataElement(e.nextToken()); 905 } 906 } 907 } 908 } 909 } 910 911 915 public void transferOutputProperties(SerializationHandler handler) 916 { 917 if (_properties == null) return; 919 920 String doctypePublic = null; 921 String doctypeSystem = null; 922 923 Enumeration names = _properties.propertyNames(); 925 while (names.hasMoreElements()) { 926 String name = (String ) names.nextElement(); 928 String value = (String ) _properties.get(name); 929 930 if (value == null) continue; 932 933 if (name.equals(OutputKeys.DOCTYPE_PUBLIC)) { 935 doctypePublic = value; 936 } 937 else if (name.equals(OutputKeys.DOCTYPE_SYSTEM)) { 938 doctypeSystem = value; 939 } 940 else if (name.equals(OutputKeys.MEDIA_TYPE)) { 941 handler.setMediaType(value); 942 } 943 else if (name.equals(OutputKeys.STANDALONE)) { 944 handler.setStandalone(value); 945 } 946 else if (name.equals(OutputKeys.VERSION)) { 947 handler.setVersion(value); 948 } 949 else if (name.equals(OutputKeys.OMIT_XML_DECLARATION)) { 950 handler.setOmitXMLDeclaration( 951 value != null && value.toLowerCase().equals("yes")); 952 } 953 else if (name.equals(OutputKeys.INDENT)) { 954 handler.setIndent( 955 value != null && value.toLowerCase().equals("yes")); 956 } 957 else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { 958 if (value != null) { 959 StringTokenizer e = new StringTokenizer (value); 960 Vector uriAndLocalNames = null; 961 while (e.hasMoreTokens()) { 962 final String token = e.nextToken(); 963 964 int lastcolon = token.lastIndexOf(':'); 967 String uri; 968 String localName; 969 if (lastcolon > 0) { 970 uri = token.substring(0, lastcolon); 971 localName = token.substring(lastcolon+1); 972 } else { 973 uri = null; 976 localName = token; 977 } 978 979 if (uriAndLocalNames == null) { 980 uriAndLocalNames = new Vector (); 981 } 982 uriAndLocalNames.addElement(uri); 984 uriAndLocalNames.addElement(localName); 985 } 986 handler.setCdataSectionElements(uriAndLocalNames); 987 } 988 } 989 } 990 991 if (doctypePublic != null || doctypeSystem != null) { 993 handler.setDoctype(doctypeSystem, doctypePublic); 994 } 995 } 996 997 1003 private Properties createOutputProperties(Properties outputProperties) { 1004 final Properties defaults = new Properties (); 1005 setDefaults(defaults, "xml"); 1006 1007 final Properties base = new Properties (defaults); 1009 if (outputProperties != null) { 1010 final Enumeration names = outputProperties.propertyNames(); 1011 while (names.hasMoreElements()) { 1012 final String name = (String ) names.nextElement(); 1013 base.setProperty(name, outputProperties.getProperty(name)); 1014 } 1015 } 1016 else { 1017 base.setProperty(OutputKeys.ENCODING, _translet._encoding); 1018 if (_translet._method != null) 1019 base.setProperty(OutputKeys.METHOD, _translet._method); 1020 } 1021 1022 final String method = base.getProperty(OutputKeys.METHOD); 1024 if (method != null) { 1025 if (method.equals("html")) { 1026 setDefaults(defaults,"html"); 1027 } 1028 else if (method.equals("text")) { 1029 setDefaults(defaults,"text"); 1030 } 1031 } 1032 1033 return base; 1034 } 1035 1036 1042 private void setDefaults(Properties props, String method) 1043 { 1044 final Properties method_props = 1045 OutputPropertiesFactory.getDefaultMethodProperties(method); 1046 { 1047 final Enumeration names = method_props.propertyNames(); 1048 while (names.hasMoreElements()) 1049 { 1050 final String name = (String )names.nextElement(); 1051 props.setProperty(name, method_props.getProperty(name)); 1052 } 1053 } 1054 } 1055 1059 private boolean validOutputProperty(String name) { 1060 return (name.equals(OutputKeys.ENCODING) || 1061 name.equals(OutputKeys.METHOD) || 1062 name.equals(OutputKeys.INDENT) || 1063 name.equals(OutputKeys.DOCTYPE_PUBLIC) || 1064 name.equals(OutputKeys.DOCTYPE_SYSTEM) || 1065 name.equals(OutputKeys.CDATA_SECTION_ELEMENTS) || 1066 name.equals(OutputKeys.MEDIA_TYPE) || 1067 name.equals(OutputKeys.OMIT_XML_DECLARATION) || 1068 name.equals(OutputKeys.STANDALONE) || 1069 name.equals(OutputKeys.VERSION) || 1070 name.charAt(0) == '{'); 1071 } 1072 1073 1076 private boolean isDefaultProperty(String name, Properties properties) { 1077 return (properties.get(name) == null); 1078 } 1079 1080 1089 public void setParameter(String name, Object value) { 1090 1091 if (value == null) { 1092 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_SET_PARAM_VALUE, name); 1093 throw new IllegalArgumentException (err.toString()); 1094 } 1095 1096 if (_isIdentity) { 1097 if (_parameters == null) { 1098 _parameters = new Hashtable(); 1099 } 1100 _parameters.put(name, value); 1101 } 1102 else { 1103 _translet.addParameter(name, value); 1104 } 1105 } 1106 1107 1112 public void clearParameters() { 1113 if (_isIdentity && _parameters != null) { 1114 _parameters.clear(); 1115 } 1116 else { 1117 _translet.clearParameters(); 1118 } 1119 } 1120 1121 1129 public final Object getParameter(String name) { 1130 if (_isIdentity) { 1131 return (_parameters != null) ? _parameters.get(name) : null; 1132 } 1133 else { 1134 return _translet.getParameter(name); 1135 } 1136 } 1137 1138 1144 public URIResolver getURIResolver() { 1145 return _uriResolver; 1146 } 1147 1148 1154 public void setURIResolver(URIResolver resolver) { 1155 _uriResolver = resolver; 1156 } 1157 1158 1172 public DOM retrieveDocument(String baseURI, String href, Translet translet) { 1173 try { 1174 if (href.length() == 0) { 1176 href = new String (baseURI); 1177 } 1178 1179 1187 Source resolvedSource = _uriResolver.resolve(href, baseURI); 1188 if (resolvedSource == null) { 1189 StreamSource streamSource = new StreamSource ( 1190 SystemIDResolver.getAbsoluteURI(href, baseURI)); 1191 return getDOM(streamSource) ; 1192 } 1193 1194 return getDOM(resolvedSource); 1195 } 1196 catch (TransformerException e) { 1197 if (_errorListener != null) 1198 postErrorToListener("File not found: " + e.getMessage()); 1199 return(null); 1200 } 1201 } 1202 1203 1214 public void error(TransformerException e) 1215 throws TransformerException 1216 { 1217 Throwable wrapped = e.getException(); 1218 if (wrapped != null) { 1219 System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG, 1220 e.getMessageAndLocation(), 1221 wrapped.getMessage())); 1222 } else { 1223 System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG, 1224 e.getMessageAndLocation())); 1225 } 1226 throw e; 1227 } 1228 1229 1242 public void fatalError(TransformerException e) 1243 throws TransformerException 1244 { 1245 Throwable wrapped = e.getException(); 1246 if (wrapped != null) { 1247 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG, 1248 e.getMessageAndLocation(), 1249 wrapped.getMessage())); 1250 } else { 1251 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG, 1252 e.getMessageAndLocation())); 1253 } 1254 throw e; 1255 } 1256 1257 1270 public void warning(TransformerException e) 1271 throws TransformerException 1272 { 1273 Throwable wrapped = e.getException(); 1274 if (wrapped != null) { 1275 System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG, 1276 e.getMessageAndLocation(), 1277 wrapped.getMessage())); 1278 } else { 1279 System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG, 1280 e.getMessageAndLocation())); 1281 } 1282 } 1283 1284 1285 1291 public void reset() { 1292 1293 _method = null; 1294 _encoding = null; 1295 _sourceSystemId = null; 1296 _errorListener = this; 1297 _uriResolver = null; 1298 _dom = null; 1299 _parameters = null; 1300 _indentNumber = 0; 1301 setOutputProperties (null); 1302 1303 } 1304 1305 1306 1307} 1308 | Popular Tags |