1 16 19 20 package org.apache.xalan.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.URL ; 30 import java.net.URLConnection ; 31 import java.net.UnknownServiceException ; 32 import java.util.Enumeration ; 33 import java.util.Properties ; 34 import java.util.StringTokenizer ; 35 import java.util.Vector ; 36 37 import javax.xml.parsers.DocumentBuilder ; 38 import javax.xml.parsers.DocumentBuilderFactory ; 39 import javax.xml.parsers.ParserConfigurationException ; 40 import javax.xml.transform.ErrorListener ; 41 import javax.xml.transform.OutputKeys ; 42 import javax.xml.transform.Result ; 43 import javax.xml.transform.Source ; 44 import javax.xml.transform.Transformer ; 45 import javax.xml.transform.TransformerException ; 46 import javax.xml.transform.URIResolver ; 47 import javax.xml.transform.dom.DOMResult ; 48 import javax.xml.transform.dom.DOMSource ; 49 import javax.xml.transform.sax.SAXResult ; 50 import javax.xml.transform.sax.SAXSource ; 51 import javax.xml.transform.stream.StreamResult ; 52 import javax.xml.transform.stream.StreamSource ; 53 54 import org.apache.xml.utils.SystemIDResolver; 55 56 import org.apache.xalan.xsltc.DOM; 57 import org.apache.xalan.xsltc.DOMCache; 58 import org.apache.xalan.xsltc.DOMEnhancedForDTM; 59 import org.apache.xalan.xsltc.StripFilter; 60 import org.apache.xalan.xsltc.Translet; 61 import org.apache.xalan.xsltc.TransletException; 62 import org.apache.xml.serializer.OutputPropertiesFactory; 63 import org.apache.xml.serializer.SerializationHandler; 64 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 65 import org.apache.xalan.xsltc.dom.DOMWSFilter; 66 import org.apache.xalan.xsltc.dom.SAXImpl; 67 import org.apache.xalan.xsltc.dom.XSLTCDTMManager; 68 import org.apache.xalan.xsltc.runtime.AbstractTranslet; 69 import org.apache.xalan.xsltc.runtime.Hashtable; 70 import org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory; 71 72 import org.apache.xml.dtm.DTMWSFilter; 73 import org.apache.xml.utils.XMLReaderManager; 74 75 import org.xml.sax.ContentHandler ; 76 import org.xml.sax.InputSource ; 77 import org.xml.sax.SAXException ; 78 import org.xml.sax.XMLReader ; 79 import org.xml.sax.ext.LexicalHandler ; 80 81 86 public final class TransformerImpl extends Transformer 87 implements DOMCache, ErrorListener 88 { 89 private final static String EMPTY_STRING = ""; 90 private final static String NO_STRING = "no"; 91 private final static String YES_STRING = "yes"; 92 private final static String XML_STRING = "xml"; 93 94 private final static String LEXICAL_HANDLER_PROPERTY = 95 "http://xml.org/sax/properties/lexical-handler"; 96 private static final String NAMESPACE_FEATURE = 97 "http://xml.org/sax/features/namespaces"; 98 99 102 private AbstractTranslet _translet = null; 103 104 107 private String _method = null; 108 109 112 private String _encoding = null; 113 114 117 private String _sourceSystemId = null; 118 119 122 private ErrorListener _errorListener = this; 123 124 127 private URIResolver _uriResolver = null; 128 129 132 private Properties _properties, _propertiesClone; 133 134 137 private TransletOutputHandlerFactory _tohFactory = null; 138 139 142 private DOM _dom = null; 143 144 147 private int _indentNumber; 148 149 153 private TransformerFactoryImpl _tfactory = null; 154 155 159 private XSLTCDTMManager _dtmManager = null; 160 161 164 private XMLReaderManager _readerManager = XMLReaderManager.getInstance(); 165 166 169 171 175 private boolean _isIdentity = false; 176 177 182 private Hashtable _parameters = null; 183 184 188 static class MessageHandler 189 extends org.apache.xalan.xsltc.runtime.MessageHandler 190 { 191 private ErrorListener _errorListener; 192 193 public MessageHandler(ErrorListener errorListener) { 194 _errorListener = errorListener; 195 } 196 197 public void displayMessage(String msg) { 198 if(_errorListener == null) { 199 System.err.println(msg); 200 } 201 else { 202 try { 203 _errorListener.warning(new TransformerException (msg)); 204 } 205 catch (TransformerException e) { 206 } 208 } 209 } 210 } 211 212 protected TransformerImpl(Properties outputProperties, int indentNumber, 213 TransformerFactoryImpl tfactory) 214 { 215 this(null, outputProperties, indentNumber, tfactory); 216 _isIdentity = true; 217 } 219 220 protected TransformerImpl(Translet translet, Properties outputProperties, 221 int indentNumber, TransformerFactoryImpl tfactory) 222 { 223 _translet = (AbstractTranslet) translet; 224 _properties = createOutputProperties(outputProperties); 225 _propertiesClone = (Properties ) _properties.clone(); 226 _indentNumber = indentNumber; 227 _tfactory = tfactory; 228 } 230 231 235 protected AbstractTranslet getTranslet() { 236 return _translet; 237 } 238 239 public boolean isIdentity() { 240 return _isIdentity; 241 } 242 243 250 public void transform(Source source, Result result) 251 throws TransformerException 252 { 253 if (!_isIdentity) { 254 if (_translet == null) { 255 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_TRANSLET_ERR); 256 throw new TransformerException (err.toString()); 257 } 258 transferOutputProperties(_translet); 260 } 261 262 final SerializationHandler toHandler = getOutputHandler(result); 263 if (toHandler == null) { 264 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_HANDLER_ERR); 265 throw new TransformerException (err.toString()); 266 } 267 268 if (_uriResolver != null && !_isIdentity) { 269 _translet.setDOMCache(this); 270 } 271 272 if (_isIdentity) { 274 transferOutputProperties(toHandler); 275 } 276 277 transform(source, toHandler, _encoding); 278 279 if (result instanceof DOMResult ) { 280 ((DOMResult )result).setNode(_tohFactory.getNode()); 281 } 282 } 283 284 289 public SerializationHandler getOutputHandler(Result result) 290 throws TransformerException 291 { 292 _method = (String ) _properties.get(OutputKeys.METHOD); 294 295 _encoding = (String ) _properties.getProperty(OutputKeys.ENCODING); 297 298 _tohFactory = TransletOutputHandlerFactory.newInstance(); 299 _tohFactory.setEncoding(_encoding); 300 if (_method != null) { 301 _tohFactory.setOutputMethod(_method); 302 } 303 304 if (_indentNumber >= 0) { 306 _tohFactory.setIndentNumber(_indentNumber); 307 } 308 309 try { 311 if (result instanceof SAXResult ) { 313 final SAXResult target = (SAXResult )result; 314 final ContentHandler handler = target.getHandler(); 315 316 _tohFactory.setHandler(handler); 317 318 323 LexicalHandler lexicalHandler = target.getLexicalHandler(); 324 325 if (lexicalHandler != null ) { 326 _tohFactory.setLexicalHandler(lexicalHandler); 327 } 328 329 _tohFactory.setOutputType(TransletOutputHandlerFactory.SAX); 330 return _tohFactory.getSerializationHandler(); 331 } 332 else if (result instanceof DOMResult ) { 333 _tohFactory.setNode(((DOMResult ) result).getNode()); 334 _tohFactory.setOutputType(TransletOutputHandlerFactory.DOM); 335 return _tohFactory.getSerializationHandler(); 336 } 337 else if (result instanceof StreamResult ) { 338 final StreamResult target = (StreamResult ) result; 340 341 345 _tohFactory.setOutputType(TransletOutputHandlerFactory.STREAM); 346 347 final Writer writer = target.getWriter(); 349 if (writer != null) { 350 _tohFactory.setWriter(writer); 351 return _tohFactory.getSerializationHandler(); 352 } 353 354 final OutputStream ostream = target.getOutputStream(); 356 if (ostream != null) { 357 _tohFactory.setOutputStream(ostream); 358 return _tohFactory.getSerializationHandler(); 359 } 360 361 String systemId = result.getSystemId(); 363 if (systemId == null) { 364 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_RESULT_ERR); 365 throw new TransformerException (err.toString()); 366 } 367 368 URL url = null; 372 if (systemId.startsWith("file:")) { 373 url = new URL (systemId); 374 _tohFactory.setOutputStream( 375 new FileOutputStream (url.getFile())); 376 return _tohFactory.getSerializationHandler(); 377 } 378 else if (systemId.startsWith("http:")) { 379 url = new URL (systemId); 380 final URLConnection connection = url.openConnection(); 381 _tohFactory.setOutputStream(connection.getOutputStream()); 382 return _tohFactory.getSerializationHandler(); 383 } 384 else { 385 url = new File (systemId).toURL(); 387 _tohFactory.setOutputStream( 388 new FileOutputStream (url.getFile())); 389 return _tohFactory.getSerializationHandler(); 390 } 391 } 392 } 393 catch (UnknownServiceException e) { 395 throw new TransformerException (e); 396 } 397 catch (ParserConfigurationException e) { 398 throw new TransformerException (e); 399 } 400 catch (IOException e) { 402 throw new TransformerException (e); 403 } 404 return null; 405 } 406 407 410 protected void setDOM(DOM dom) { 411 _dom = dom; 412 } 413 414 417 private DOM getDOM(Source source) throws TransformerException { 418 try { 419 DOM dom = null; 420 421 if (source != null) { 422 DTMWSFilter wsfilter; 423 if (_translet != null && _translet instanceof StripFilter) { 424 wsfilter = new DOMWSFilter(_translet); 425 } else { 426 wsfilter = null; 427 } 428 429 boolean hasIdCall = (_translet != null) ? _translet.hasIdCall() 430 : false; 431 432 if (_dtmManager == null) { 433 _dtmManager = 434 (XSLTCDTMManager)_tfactory.getDTMManagerClass() 435 .newInstance(); 436 } 437 dom = (DOM)_dtmManager.getDTM(source, false, wsfilter, true, 438 false, false, 0, hasIdCall); 439 } else if (_dom != null) { 440 dom = _dom; 441 _dom = null; } else { 443 return null; 444 } 445 446 if (!_isIdentity) { 447 _translet.prepassDocument(dom); 450 } 451 452 return dom; 453 454 } 455 catch (Exception e) { 456 if (_errorListener != null) { 457 postErrorToListener(e.getMessage()); 458 } 459 throw new TransformerException (e); 460 } 461 } 462 463 467 protected TransformerFactoryImpl getTransformerFactory() { 468 return _tfactory; 469 } 470 471 private void transformIdentity(Source source, SerializationHandler handler) 472 throws Exception 473 { 474 if (source != null) { 476 _sourceSystemId = source.getSystemId(); 477 } 478 479 if (source instanceof StreamSource ) { 480 final StreamSource stream = (StreamSource ) source; 481 final InputStream streamInput = stream.getInputStream(); 482 final Reader streamReader = stream.getReader(); 483 final XMLReader reader = _readerManager.getXMLReader(); 484 485 try { 486 try { 488 reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); 489 } 490 catch (SAXException e) { 491 } 493 reader.setContentHandler(handler); 494 495 InputSource input; 497 if (streamInput != null) { 498 input = new InputSource (streamInput); 499 input.setSystemId(_sourceSystemId); 500 } 501 else if (streamReader != null) { 502 input = new InputSource (streamReader); 503 input.setSystemId(_sourceSystemId); 504 } 505 else if (_sourceSystemId != null) { 506 input = new InputSource (_sourceSystemId); 507 } 508 else { 509 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR); 510 throw new TransformerException (err.toString()); 511 } 512 513 reader.parse(input); 515 } finally { 516 _readerManager.releaseXMLReader(reader); 517 } 518 } else if (source instanceof SAXSource ) { 519 final SAXSource sax = (SAXSource ) source; 520 XMLReader reader = sax.getXMLReader(); 521 final InputSource input = sax.getInputSource(); 522 boolean userReader = true; 523 524 try { 525 if (reader == null) { 527 reader = _readerManager.getXMLReader(); 528 userReader = false; 529 } 530 531 try { 533 reader.setProperty(LEXICAL_HANDLER_PROPERTY, handler); 534 } 535 catch (SAXException e) { 536 } 538 reader.setContentHandler(handler); 539 540 reader.parse(input); 542 } finally { 543 if (!userReader) { 544 _readerManager.releaseXMLReader(reader); 545 } 546 } 547 } else if (source instanceof DOMSource ) { 548 final DOMSource domsrc = (DOMSource ) source; 549 new DOM2TO(domsrc.getNode(), handler).parse(); 550 } else if (source instanceof XSLTCSource) { 551 final DOM dom = ((XSLTCSource) source).getDOM(null, _translet); 552 ((SAXImpl)dom).copy(handler); 553 } else { 554 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_NO_SOURCE_ERR); 555 throw new TransformerException (err.toString()); 556 } 557 } 558 559 562 private void transform(Source source, SerializationHandler handler, 563 String encoding) throws TransformerException 564 { 565 try { 566 574 if ((source instanceof StreamSource && source.getSystemId()==null 575 && ((StreamSource )source).getInputStream()==null && 576 ((StreamSource )source).getReader()==null)|| 577 (source instanceof SAXSource && 578 ((SAXSource )source).getInputSource()==null && 579 ((SAXSource )source).getXMLReader()==null )|| 580 (source instanceof DOMSource && 581 ((DOMSource )source).getNode()==null)){ 582 DocumentBuilderFactory builderF = 583 DocumentBuilderFactory.newInstance(); 584 DocumentBuilder builder = 585 builderF.newDocumentBuilder(); 586 String systemID = source.getSystemId(); 587 source = new DOMSource (builder.newDocument()); 588 589 if (systemID != null) { 591 source.setSystemId(systemID); 592 } 593 } 594 if (_isIdentity) { 595 transformIdentity(source, handler); 596 } else { 597 _translet.transform(getDOM(source), handler); 598 } 599 } catch (TransletException e) { 600 if (_errorListener != null) postErrorToListener(e.getMessage()); 601 throw new TransformerException (e); 602 } catch (RuntimeException e) { 603 if (_errorListener != null) postErrorToListener(e.getMessage()); 604 throw new TransformerException (e); 605 } catch (Exception e) { 606 if (_errorListener != null) postErrorToListener(e.getMessage()); 607 throw new TransformerException (e); 608 } finally { 609 _dtmManager = null; 610 } 611 } 612 613 619 public ErrorListener getErrorListener() { 620 return _errorListener; 621 } 622 623 632 public void setErrorListener(ErrorListener listener) 633 throws IllegalArgumentException { 634 if (listener == null) { 635 ErrorMsg err = new ErrorMsg(ErrorMsg.ERROR_LISTENER_NULL_ERR, 636 "Transformer"); 637 throw new IllegalArgumentException (err.toString()); 638 } 639 _errorListener = listener; 640 641 if (_translet != null) 643 _translet.setMessageHandler(new MessageHandler(_errorListener)); 644 } 645 646 649 private void postErrorToListener(String message) { 650 try { 651 _errorListener.error(new TransformerException (message)); 652 } 653 catch (TransformerException e) { 654 } 656 } 657 658 661 private void postWarningToListener(String message) { 662 try { 663 _errorListener.warning(new TransformerException (message)); 664 } 665 catch (TransformerException e) { 666 } 668 } 669 670 675 private String makeCDATAString(Hashtable cdata) { 676 if (cdata == null) return null; 678 679 StringBuffer result = new StringBuffer (); 680 681 Enumeration elements = cdata.keys(); 683 if (elements.hasMoreElements()) { 684 result.append((String )elements.nextElement()); 685 while (elements.hasMoreElements()) { 686 String element = (String )elements.nextElement(); 687 result.append(' '); 688 result.append(element); 689 } 690 } 691 692 return(result.toString()); 693 } 694 695 706 public Properties getOutputProperties() { 707 return (Properties ) _properties.clone(); 708 } 709 710 719 public String getOutputProperty(String name) 720 throws IllegalArgumentException 721 { 722 if (!validOutputProperty(name)) { 723 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 724 throw new IllegalArgumentException (err.toString()); 725 } 726 return _properties.getProperty(name); 727 } 728 729 738 public void setOutputProperties(Properties properties) 739 throws IllegalArgumentException 740 { 741 if (properties != null) { 742 final Enumeration names = properties.propertyNames(); 743 744 while (names.hasMoreElements()) { 745 final String name = (String ) names.nextElement(); 746 747 if (isDefaultProperty(name, properties)) continue; 749 750 if (validOutputProperty(name)) { 751 _properties.setProperty(name, properties.getProperty(name)); 752 } 753 else { 754 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 755 throw new IllegalArgumentException (err.toString()); 756 } 757 } 758 } 759 else { 760 _properties = _propertiesClone; 761 } 762 } 763 764 774 public void setOutputProperty(String name, String value) 775 throws IllegalArgumentException 776 { 777 if (!validOutputProperty(name)) { 778 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNKNOWN_PROP_ERR, name); 779 throw new IllegalArgumentException (err.toString()); 780 } 781 _properties.setProperty(name, value); 782 } 783 784 788 private void transferOutputProperties(AbstractTranslet translet) 789 { 790 if (_properties == null) return; 792 793 Enumeration names = _properties.propertyNames(); 795 while (names.hasMoreElements()) { 796 String name = (String ) names.nextElement(); 798 String value = (String ) _properties.get(name); 799 800 if (value == null) continue; 802 803 if (name.equals(OutputKeys.ENCODING)) { 805 translet._encoding = value; 806 } 807 else if (name.equals(OutputKeys.METHOD)) { 808 translet._method = value; 809 } 810 else if (name.equals(OutputKeys.DOCTYPE_PUBLIC)) { 811 translet._doctypePublic = value; 812 } 813 else if (name.equals(OutputKeys.DOCTYPE_SYSTEM)) { 814 translet._doctypeSystem = value; 815 } 816 else if (name.equals(OutputKeys.MEDIA_TYPE)) { 817 translet._mediaType = value; 818 } 819 else if (name.equals(OutputKeys.STANDALONE)) { 820 translet._standalone = value; 821 } 822 else if (name.equals(OutputKeys.VERSION)) { 823 translet._version = value; 824 } 825 else if (name.equals(OutputKeys.OMIT_XML_DECLARATION)) { 826 translet._omitHeader = 827 (value != null && value.toLowerCase().equals("yes")); 828 } 829 else if (name.equals(OutputKeys.INDENT)) { 830 translet._indent = 831 (value != null && value.toLowerCase().equals("yes")); 832 } 833 else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { 834 if (value != null) { 835 translet._cdata = null; StringTokenizer e = new StringTokenizer (value); 837 while (e.hasMoreTokens()) { 838 translet.addCdataElement(e.nextToken()); 839 } 840 } 841 } 842 } 843 } 844 845 849 public void transferOutputProperties(SerializationHandler handler) 850 { 851 if (_properties == null) return; 853 854 String doctypePublic = null; 855 String doctypeSystem = null; 856 857 Enumeration names = _properties.propertyNames(); 859 while (names.hasMoreElements()) { 860 String name = (String ) names.nextElement(); 862 String value = (String ) _properties.get(name); 863 864 if (value == null) continue; 866 867 if (name.equals(OutputKeys.DOCTYPE_PUBLIC)) { 869 doctypePublic = value; 870 } 871 else if (name.equals(OutputKeys.DOCTYPE_SYSTEM)) { 872 doctypeSystem = value; 873 } 874 else if (name.equals(OutputKeys.MEDIA_TYPE)) { 875 handler.setMediaType(value); 876 } 877 else if (name.equals(OutputKeys.STANDALONE)) { 878 handler.setStandalone(value); 879 } 880 else if (name.equals(OutputKeys.VERSION)) { 881 handler.setVersion(value); 882 } 883 else if (name.equals(OutputKeys.OMIT_XML_DECLARATION)) { 884 handler.setOmitXMLDeclaration( 885 value != null && value.toLowerCase().equals("yes")); 886 } 887 else if (name.equals(OutputKeys.INDENT)) { 888 handler.setIndent( 889 value != null && value.toLowerCase().equals("yes")); 890 } 891 else if (name.equals(OutputKeys.CDATA_SECTION_ELEMENTS)) { 892 if (value != null) { 893 StringTokenizer e = new StringTokenizer (value); 894 Vector uriAndLocalNames = null; 895 while (e.hasMoreTokens()) { 896 final String token = e.nextToken(); 897 898 int lastcolon = token.lastIndexOf(':'); 901 String uri; 902 String localName; 903 if (lastcolon > 0) { 904 uri = token.substring(0, lastcolon); 905 localName = token.substring(lastcolon+1); 906 } else { 907 uri = null; 910 localName = token; 911 } 912 913 if (uriAndLocalNames == null) { 914 uriAndLocalNames = new Vector (); 915 } 916 uriAndLocalNames.addElement(uri); 918 uriAndLocalNames.addElement(localName); 919 } 920 handler.setCdataSectionElements(uriAndLocalNames); 921 } 922 } 923 } 924 925 if (doctypePublic != null || doctypeSystem != null) { 927 handler.setDoctype(doctypeSystem, doctypePublic); 928 } 929 } 930 931 937 private Properties createOutputProperties(Properties outputProperties) { 938 final Properties defaults = new Properties (); 939 setDefaults(defaults, "xml"); 940 941 final Properties base = new Properties (defaults); 943 if (outputProperties != null) { 944 final Enumeration names = outputProperties.propertyNames(); 945 while (names.hasMoreElements()) { 946 final String name = (String ) names.nextElement(); 947 base.setProperty(name, outputProperties.getProperty(name)); 948 } 949 } 950 else { 951 base.setProperty(OutputKeys.ENCODING, _translet._encoding); 952 if (_translet._method != null) 953 base.setProperty(OutputKeys.METHOD, _translet._method); 954 } 955 956 final String method = base.getProperty(OutputKeys.METHOD); 958 if (method != null) { 959 if (method.equals("html")) { 960 setDefaults(defaults,"html"); 961 } 962 else if (method.equals("text")) { 963 setDefaults(defaults,"text"); 964 } 965 } 966 967 return base; 968 } 969 970 976 private void setDefaults(Properties props, String method) 977 { 978 final Properties method_props = 979 OutputPropertiesFactory.getDefaultMethodProperties(method); 980 { 981 final Enumeration names = method_props.propertyNames(); 982 while (names.hasMoreElements()) 983 { 984 final String name = (String )names.nextElement(); 985 props.setProperty(name, method_props.getProperty(name)); 986 } 987 } 988 } 989 993 private boolean validOutputProperty(String name) { 994 return (name.equals(OutputKeys.ENCODING) || 995 name.equals(OutputKeys.METHOD) || 996 name.equals(OutputKeys.INDENT) || 997 name.equals(OutputKeys.DOCTYPE_PUBLIC) || 998 name.equals(OutputKeys.DOCTYPE_SYSTEM) || 999 name.equals(OutputKeys.CDATA_SECTION_ELEMENTS) || 1000 name.equals(OutputKeys.MEDIA_TYPE) || 1001 name.equals(OutputKeys.OMIT_XML_DECLARATION) || 1002 name.equals(OutputKeys.STANDALONE) || 1003 name.equals(OutputKeys.VERSION) || 1004 name.charAt(0) == '{'); 1005 } 1006 1007 1010 private boolean isDefaultProperty(String name, Properties properties) { 1011 return (properties.get(name) == null); 1012 } 1013 1014 1023 public void setParameter(String name, Object value) { 1024 1025 if (value == null) { 1026 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_INVALID_SET_PARAM_VALUE, name); 1027 throw new IllegalArgumentException (err.toString()); 1028 } 1029 1030 if (_isIdentity) { 1031 if (_parameters == null) { 1032 _parameters = new Hashtable(); 1033 } 1034 _parameters.put(name, value); 1035 } 1036 else { 1037 _translet.addParameter(name, value); 1038 } 1039 } 1040 1041 1046 public void clearParameters() { 1047 if (_isIdentity && _parameters != null) { 1048 _parameters.clear(); 1049 } 1050 else { 1051 _translet.clearParameters(); 1052 } 1053 } 1054 1055 1063 public final Object getParameter(String name) { 1064 if (_isIdentity) { 1065 return (_parameters != null) ? _parameters.get(name) : null; 1066 } 1067 else { 1068 return _translet.getParameter(name); 1069 } 1070 } 1071 1072 1078 public URIResolver getURIResolver() { 1079 return _uriResolver; 1080 } 1081 1082 1088 public void setURIResolver(URIResolver resolver) { 1089 _uriResolver = resolver; 1090 } 1091 1092 1106 public DOM retrieveDocument(String baseURI, String href, Translet translet) { 1107 try { 1108 if (href.length() == 0) { 1110 href = new String (baseURI); 1111 } 1112 1113 1121 Source resolvedSource = _uriResolver.resolve(href, baseURI); 1122 if (resolvedSource == null) { 1123 StreamSource streamSource = new StreamSource ( 1124 SystemIDResolver.getAbsoluteURI(href, baseURI)); 1125 return getDOM(streamSource) ; 1126 } 1127 1128 return getDOM(resolvedSource); 1129 } 1130 catch (TransformerException e) { 1131 if (_errorListener != null) 1132 postErrorToListener("File not found: " + e.getMessage()); 1133 return(null); 1134 } 1135 } 1136 1137 1148 public void error(TransformerException e) 1149 throws TransformerException 1150 { 1151 Throwable wrapped = e.getException(); 1152 if (wrapped != null) { 1153 System.err.println(new ErrorMsg(ErrorMsg.ERROR_PLUS_WRAPPED_MSG, 1154 e.getMessageAndLocation(), 1155 wrapped.getMessage())); 1156 } else { 1157 System.err.println(new ErrorMsg(ErrorMsg.ERROR_MSG, 1158 e.getMessageAndLocation())); 1159 } 1160 throw e; 1161 } 1162 1163 1176 public void fatalError(TransformerException e) 1177 throws TransformerException 1178 { 1179 Throwable wrapped = e.getException(); 1180 if (wrapped != null) { 1181 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_PLUS_WRAPPED_MSG, 1182 e.getMessageAndLocation(), 1183 wrapped.getMessage())); 1184 } else { 1185 System.err.println(new ErrorMsg(ErrorMsg.FATAL_ERR_MSG, 1186 e.getMessageAndLocation())); 1187 } 1188 throw e; 1189 } 1190 1191 1204 public void warning(TransformerException e) 1205 throws TransformerException 1206 { 1207 Throwable wrapped = e.getException(); 1208 if (wrapped != null) { 1209 System.err.println(new ErrorMsg(ErrorMsg.WARNING_PLUS_WRAPPED_MSG, 1210 e.getMessageAndLocation(), 1211 wrapped.getMessage())); 1212 } else { 1213 System.err.println(new ErrorMsg(ErrorMsg.WARNING_MSG, 1214 e.getMessageAndLocation())); 1215 } 1216 } 1217 1218} 1219 | Popular Tags |