1 16 19 package org.apache.xalan.transformer; 20 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 import java.util.Properties ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.transform.ErrorListener ; 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.Result ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.TransformerException ; 34 import javax.xml.transform.URIResolver ; 35 import javax.xml.transform.dom.DOMResult ; 36 import javax.xml.transform.dom.DOMSource ; 37 import javax.xml.transform.sax.SAXResult ; 38 import javax.xml.transform.sax.SAXSource ; 39 import javax.xml.transform.sax.TransformerHandler ; 40 import javax.xml.transform.stream.StreamSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 43 import org.apache.xalan.res.XSLMessages; 44 import org.apache.xalan.res.XSLTErrorResources; 45 import org.apache.xalan.templates.OutputProperties; 46 import org.apache.xml.serializer.Serializer; 47 import org.apache.xml.serializer.SerializerFactory; 48 import org.apache.xml.serializer.Method; 49 import org.apache.xml.utils.DOMBuilder; 50 import org.apache.xml.utils.TreeWalker; 51 import org.apache.xml.utils.XMLReaderManager; 52 53 import org.w3c.dom.Document ; 54 import org.w3c.dom.DocumentFragment ; 55 import org.w3c.dom.Node ; 56 57 import org.xml.sax.Attributes ; 58 import org.xml.sax.ContentHandler ; 59 import org.xml.sax.DTDHandler ; 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.Locator ; 62 import org.xml.sax.SAXException ; 63 import org.xml.sax.XMLReader ; 64 import org.xml.sax.ext.DeclHandler ; 65 import org.xml.sax.ext.LexicalHandler ; 66 67 76 public class TransformerIdentityImpl extends Transformer 77 implements TransformerHandler , DeclHandler 78 { 79 80 84 public TransformerIdentityImpl() 85 { 86 m_outputFormat = new OutputProperties(Method.XML); 87 } 88 89 97 public void setResult(Result result) throws IllegalArgumentException 98 { 99 if(null == result) 100 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_RESULT_NULL, null)); m_result = result; 102 } 103 104 109 public void setSystemId(String systemID) 110 { 111 m_systemID = systemID; 112 } 113 114 119 public String getSystemId() 120 { 121 return m_systemID; 122 } 123 124 130 public Transformer getTransformer() 131 { 132 return this; 133 } 134 135 147 private void createResultContentHandler(Result outputTarget) 148 throws TransformerException 149 { 150 151 if (outputTarget instanceof SAXResult ) 152 { 153 SAXResult saxResult = (SAXResult ) outputTarget; 154 155 m_resultContentHandler = saxResult.getHandler(); 156 m_resultLexicalHandler = saxResult.getLexicalHandler(); 157 158 if (m_resultContentHandler instanceof Serializer) 159 { 160 161 m_serializer = (Serializer) m_resultContentHandler; 163 } 164 } 165 else if (outputTarget instanceof DOMResult ) 166 { 167 DOMResult domResult = (DOMResult ) outputTarget; 168 Node outputNode = domResult.getNode(); 169 Document doc; 170 short type; 171 172 if (null != outputNode) 173 { 174 type = outputNode.getNodeType(); 175 doc = (Node.DOCUMENT_NODE == type) 176 ? (Document ) outputNode : outputNode.getOwnerDocument(); 177 } 178 else 179 { 180 try 181 { 182 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 183 184 dbf.setNamespaceAware(true); 185 186 DocumentBuilder db = dbf.newDocumentBuilder(); 187 188 doc = db.newDocument(); 189 } 190 catch (ParserConfigurationException pce) 191 { 192 throw new TransformerException (pce); 193 } 194 195 outputNode = doc; 196 type = outputNode.getNodeType(); 197 198 ((DOMResult ) outputTarget).setNode(outputNode); 199 } 200 201 m_resultContentHandler = 202 (Node.DOCUMENT_FRAGMENT_NODE == type) 203 ? new DOMBuilder(doc, (DocumentFragment ) outputNode) 204 : new DOMBuilder(doc, outputNode); 205 m_resultLexicalHandler = (LexicalHandler ) m_resultContentHandler; 206 } 207 else if (outputTarget instanceof StreamResult ) 208 { 209 StreamResult sresult = (StreamResult ) outputTarget; 210 String method = m_outputFormat.getProperty(OutputKeys.METHOD); 211 212 try 213 { 214 Serializer serializer = 215 SerializerFactory.getSerializer(m_outputFormat.getProperties()); 216 217 m_serializer = serializer; 218 219 if (null != sresult.getWriter()) 220 serializer.setWriter(sresult.getWriter()); 221 else if (null != sresult.getOutputStream()) 222 serializer.setOutputStream(sresult.getOutputStream()); 223 else if (null != sresult.getSystemId()) 224 { 225 String fileURL = sresult.getSystemId(); 226 227 if (fileURL.startsWith("file:///")) 228 { 229 if (fileURL.substring(8).indexOf(":") >0) 230 fileURL = fileURL.substring(8); 231 else 232 fileURL = fileURL.substring(7); 233 } 234 235 m_outputStream = new java.io.FileOutputStream (fileURL); 236 serializer.setOutputStream(m_outputStream); 237 } 238 else 239 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null)); 241 m_resultContentHandler = serializer.asContentHandler(); 242 } 243 catch (IOException ioe) 244 { 245 throw new TransformerException (ioe); 246 } 247 } 248 else 249 { 250 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object []{outputTarget.getClass().getName()})); } 254 255 if (m_resultContentHandler instanceof DTDHandler ) 256 m_resultDTDHandler = (DTDHandler ) m_resultContentHandler; 257 258 if (m_resultContentHandler instanceof DeclHandler ) 259 m_resultDeclHandler = (DeclHandler ) m_resultContentHandler; 260 261 if (m_resultContentHandler instanceof LexicalHandler ) 262 m_resultLexicalHandler = (LexicalHandler ) m_resultContentHandler; 263 } 264 265 274 public void transform(Source source, Result outputTarget) 275 throws TransformerException 276 { 277 278 createResultContentHandler(outputTarget); 279 280 288 if ((source instanceof StreamSource && source.getSystemId()==null && 289 ((StreamSource )source).getInputStream()==null && 290 ((StreamSource )source).getReader()==null)|| 291 (source instanceof SAXSource && 292 ((SAXSource )source).getInputSource()==null && 293 ((SAXSource )source).getXMLReader()==null )|| 294 (source instanceof DOMSource && ((DOMSource )source).getNode()==null)){ 295 try { 296 DocumentBuilderFactory builderF = DocumentBuilderFactory.newInstance(); 297 DocumentBuilder builder = builderF.newDocumentBuilder(); 298 String systemID = source.getSystemId(); 299 source = new DOMSource (builder.newDocument()); 300 301 if (systemID != null) { 303 source.setSystemId(systemID); 304 } 305 } catch (ParserConfigurationException e){ 306 throw new TransformerException (e.getMessage()); 307 } 308 } 309 310 try 311 { 312 if (source instanceof DOMSource ) 313 { 314 DOMSource dsource = (DOMSource ) source; 315 316 m_systemID = dsource.getSystemId(); 317 318 Node dNode = dsource.getNode(); 319 320 if (null != dNode) 321 { 322 try 323 { 324 if(dNode.getNodeType() == Node.ATTRIBUTE_NODE) 325 this.startDocument(); 326 try 327 { 328 if(dNode.getNodeType() == Node.ATTRIBUTE_NODE) 329 { 330 String data = dNode.getNodeValue(); 331 char[] chars = data.toCharArray(); 332 characters(chars, 0, chars.length); 333 } 334 else 335 { 336 TreeWalker walker = new TreeWalker(this, new org.apache.xml.utils.DOM2Helper(), m_systemID); 337 walker.traverse(dNode); 338 } 339 } 340 finally 341 { 342 if(dNode.getNodeType() == Node.ATTRIBUTE_NODE) 343 this.endDocument(); 344 } 345 } 346 catch (SAXException se) 347 { 348 throw new TransformerException (se); 349 } 350 351 return; 352 } 353 else 354 { 355 String messageStr = XSLMessages.createMessage( 356 XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null); 357 358 throw new IllegalArgumentException (messageStr); 359 } 360 } 361 362 InputSource xmlSource = SAXSource.sourceToInputSource(source); 363 364 if (null == xmlSource) 365 { 366 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_SOURCE_TYPE, new Object []{source.getClass().getName()})); } 369 370 if (null != xmlSource.getSystemId()) 371 m_systemID = xmlSource.getSystemId(); 372 373 XMLReader reader = null; 374 boolean managedReader = false; 375 376 try 377 { 378 if (source instanceof SAXSource ) { 379 reader = ((SAXSource ) source).getXMLReader(); 380 } 381 382 if (null == reader) { 383 try { 384 reader = XMLReaderManager.getInstance().getXMLReader(); 385 managedReader = true; 386 } catch (SAXException se) { 387 throw new TransformerException (se); 388 } 389 } else { 390 try { 391 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", 392 true); 393 } catch (org.xml.sax.SAXException se) { 394 } 396 } 397 398 ContentHandler inputHandler = this; 401 402 reader.setContentHandler(inputHandler); 403 404 if (inputHandler instanceof org.xml.sax.DTDHandler ) 405 reader.setDTDHandler((org.xml.sax.DTDHandler ) inputHandler); 406 407 try 408 { 409 if (inputHandler instanceof org.xml.sax.ext.LexicalHandler ) 410 reader.setProperty("http://xml.org/sax/properties/lexical-handler", 411 inputHandler); 412 413 if (inputHandler instanceof org.xml.sax.ext.DeclHandler ) 414 reader.setProperty( 415 "http://xml.org/sax/properties/declaration-handler", 416 inputHandler); 417 } 418 catch (org.xml.sax.SAXException se){} 419 420 try 421 { 422 if (inputHandler instanceof org.xml.sax.ext.LexicalHandler ) 423 reader.setProperty("http://xml.org/sax/handlers/LexicalHandler", 424 inputHandler); 425 426 if (inputHandler instanceof org.xml.sax.ext.DeclHandler ) 427 reader.setProperty("http://xml.org/sax/handlers/DeclHandler", 428 inputHandler); 429 } 430 catch (org.xml.sax.SAXNotRecognizedException snre){} 431 432 reader.parse(xmlSource); 433 } 434 catch (org.apache.xml.utils.WrappedRuntimeException wre) 435 { 436 Throwable throwable = wre.getException(); 437 438 while (throwable 439 instanceof org.apache.xml.utils.WrappedRuntimeException) 440 { 441 throwable = 442 ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException(); 443 } 444 445 throw new TransformerException (wre.getException()); 446 } 447 catch (org.xml.sax.SAXException se) 448 { 449 throw new TransformerException (se); 450 } 451 catch (IOException ioe) 452 { 453 throw new TransformerException (ioe); 454 } finally { 455 if (managedReader) { 456 XMLReaderManager.getInstance().releaseXMLReader(reader); 457 } 458 } 459 } 460 finally 461 { 462 if(null != m_outputStream) 463 { 464 try 465 { 466 m_outputStream.close(); 467 } 468 catch(IOException ioe){} 469 m_outputStream = null; 470 } 471 } 472 } 473 474 493 public void setParameter(String name, Object value) 494 { 495 if (value == null) { 496 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_SET_PARAM_VALUE, new Object []{name})); 497 } 498 499 if (null == m_params) 500 { 501 m_params = new Hashtable (); 502 } 503 504 m_params.put(name, value); 505 } 506 507 519 public Object getParameter(String name) 520 { 521 522 if (null == m_params) 523 return null; 524 525 return m_params.get(name); 526 } 527 528 531 public void clearParameters() 532 { 533 534 if (null == m_params) 535 return; 536 537 m_params.clear(); 538 } 539 540 550 public void setURIResolver(URIResolver resolver) 551 { 552 m_URIResolver = resolver; 553 } 554 555 562 public URIResolver getURIResolver() 563 { 564 return m_URIResolver; 565 } 566 567 596 public void setOutputProperties(Properties oformat) 597 throws IllegalArgumentException 598 { 599 600 if (null != oformat) 601 { 602 603 String method = (String ) oformat.get(OutputKeys.METHOD); 605 606 if (null != method) 607 m_outputFormat = new OutputProperties(method); 608 else 609 m_outputFormat = new OutputProperties(); 610 611 m_outputFormat.copyFrom(oformat); 612 } 613 else { 614 m_outputFormat = null; 617 } 618 } 619 620 651 public Properties getOutputProperties() 652 { 653 return (Properties ) m_outputFormat.getProperties().clone(); 654 } 655 656 682 public void setOutputProperty(String name, String value) 683 throws IllegalArgumentException 684 { 685 686 if (!m_outputFormat.isLegalPropertyKey(name)) 687 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{name})); 690 m_outputFormat.setProperty(name, value); 691 } 692 693 709 public String getOutputProperty(String name) throws IllegalArgumentException 710 { 711 712 String value = null; 713 OutputProperties props = m_outputFormat; 714 715 value = props.getProperty(name); 716 717 if (null == value) 718 { 719 if (!props.isLegalPropertyKey(name)) 720 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_OUTPUT_PROPERTY_NOT_RECOGNIZED, new Object []{name})); } 723 724 return value; 725 } 726 727 733 public void setErrorListener(ErrorListener listener) 734 throws IllegalArgumentException 735 { 736 if (listener == null) 737 throw new IllegalArgumentException (XSLMessages.createMessage(XSLTErrorResources.ER_NULL_ERROR_HANDLER, null)); 738 else 739 m_errorListener = listener; 740 } 741 742 747 public ErrorListener getErrorListener() 748 { 749 return m_errorListener; 750 } 751 752 756 773 public void notationDecl(String name, String publicId, String systemId) 774 throws SAXException 775 { 776 if (null != m_resultDTDHandler) 777 m_resultDTDHandler.notationDecl(name, publicId, systemId); 778 } 779 780 798 public void unparsedEntityDecl( 799 String name, String publicId, String systemId, String notationName) 800 throws SAXException 801 { 802 803 if (null != m_resultDTDHandler) 804 m_resultDTDHandler.unparsedEntityDecl(name, publicId, systemId, 805 notationName); 806 } 807 808 812 823 public void setDocumentLocator(Locator locator) 824 { 825 try 826 { 827 if (null == m_resultContentHandler) 828 createResultContentHandler(m_result); 829 } 830 catch (TransformerException te) 831 { 832 throw new org.apache.xml.utils.WrappedRuntimeException(te); 833 } 834 835 m_resultContentHandler.setDocumentLocator(locator); 836 } 837 838 852 public void startDocument() throws SAXException 853 { 854 855 try 856 { 857 if (null == m_resultContentHandler) 858 createResultContentHandler(m_result); 859 } 860 catch (TransformerException te) 861 { 862 throw new SAXException (te.getMessage(), te); 863 } 864 865 m_flushedStartDoc = false; 867 m_foundFirstElement = false; 868 } 869 870 boolean m_flushedStartDoc = false; 871 872 protected final void flushStartDoc() 873 throws SAXException 874 { 875 if(!m_flushedStartDoc) 876 { 877 if (m_resultContentHandler == null) 878 { 879 try 880 { 881 createResultContentHandler(m_result); 882 } 883 catch(TransformerException te) 884 { 885 throw new SAXException (te); 886 } 887 } 888 m_resultContentHandler.startDocument(); 889 m_flushedStartDoc = true; 890 } 891 } 892 893 907 public void endDocument() throws SAXException 908 { 909 flushStartDoc(); 910 m_resultContentHandler.endDocument(); 911 } 912 913 928 public void startPrefixMapping(String prefix, String uri) 929 throws SAXException 930 { 931 flushStartDoc(); 932 m_resultContentHandler.startPrefixMapping(prefix, uri); 933 } 934 935 949 public void endPrefixMapping(String prefix) throws SAXException 950 { 951 flushStartDoc(); 952 m_resultContentHandler.endPrefixMapping(prefix); 953 } 954 955 978 public void startElement( 979 String uri, String localName, String qName, Attributes attributes) 980 throws SAXException 981 { 982 983 if (!m_foundFirstElement && null != m_serializer) 984 { 985 m_foundFirstElement = true; 986 987 Serializer newSerializer; 988 989 try 990 { 991 newSerializer = SerializerSwitcher.switchSerializerIfHTML(uri, 992 localName, m_outputFormat.getProperties(), m_serializer); 993 } 994 catch (TransformerException te) 995 { 996 throw new SAXException (te); 997 } 998 999 if (newSerializer != m_serializer) 1000 { 1001 try 1002 { 1003 m_resultContentHandler = newSerializer.asContentHandler(); 1004 } 1005 catch (IOException ioe) { 1007 throw new SAXException (ioe); 1008 } 1009 1010 if (m_resultContentHandler instanceof DTDHandler ) 1011 m_resultDTDHandler = (DTDHandler ) m_resultContentHandler; 1012 1013 if (m_resultContentHandler instanceof LexicalHandler ) 1014 m_resultLexicalHandler = (LexicalHandler ) m_resultContentHandler; 1015 1016 m_serializer = newSerializer; 1017 } 1018 } 1019 flushStartDoc(); 1020 m_resultContentHandler.startElement(uri, localName, qName, attributes); 1021 } 1022 1023 1047 public void endElement(String uri, String localName, String qName) 1048 throws SAXException 1049 { 1050 m_resultContentHandler.endElement(uri, localName, qName); 1051 } 1052 1053 1071 public void characters(char ch[], int start, int length) throws SAXException 1072 { 1073 flushStartDoc(); 1074 m_resultContentHandler.characters(ch, start, length); 1075 } 1076 1077 1095 public void ignorableWhitespace(char ch[], int start, int length) 1096 throws SAXException 1097 { 1098 m_resultContentHandler.ignorableWhitespace(ch, start, length); 1099 } 1100 1101 1118 public void processingInstruction(String target, String data) 1119 throws SAXException 1120 { 1121 flushStartDoc(); 1122 m_resultContentHandler.processingInstruction(target, data); 1123 } 1124 1125 1140 public void skippedEntity(String name) throws SAXException 1141 { 1142 flushStartDoc(); 1143 m_resultContentHandler.skippedEntity(name); 1144 } 1145 1146 1167 public void startDTD(String name, String publicId, String systemId) 1168 throws SAXException 1169 { 1170 flushStartDoc(); 1171 if (null != m_resultLexicalHandler) 1172 m_resultLexicalHandler.startDTD(name, publicId, systemId); 1173 } 1174 1175 1181 public void endDTD() throws SAXException 1182 { 1183 if (null != m_resultLexicalHandler) 1184 m_resultLexicalHandler.endDTD(); 1185 } 1186 1187 1209 public void startEntity(String name) throws SAXException 1210 { 1211 if (null != m_resultLexicalHandler) 1212 m_resultLexicalHandler.startEntity(name); 1213 } 1214 1215 1222 public void endEntity(String name) throws SAXException 1223 { 1224 if (null != m_resultLexicalHandler) 1225 m_resultLexicalHandler.endEntity(name); 1226 } 1227 1228 1238 public void startCDATA() throws SAXException 1239 { 1240 if (null != m_resultLexicalHandler) 1241 m_resultLexicalHandler.startCDATA(); 1242 } 1243 1244 1250 public void endCDATA() throws SAXException 1251 { 1252 if (null != m_resultLexicalHandler) 1253 m_resultLexicalHandler.endCDATA(); 1254 } 1255 1256 1268 public void comment(char ch[], int start, int length) throws SAXException 1269 { 1270 flushStartDoc(); 1271 if (null != m_resultLexicalHandler) 1272 m_resultLexicalHandler.comment(ch, start, length); 1273 } 1274 1275 1277 1290 public void elementDecl (String name, String model) 1291 throws SAXException 1292 { 1293 if (null != m_resultDeclHandler) 1294 m_resultDeclHandler.elementDecl(name, model); 1295 } 1296 1297 1298 1317 public void attributeDecl (String eName, 1318 String aName, 1319 String type, 1320 String valueDefault, 1321 String value) 1322 throws SAXException 1323 { 1324 if (null != m_resultDeclHandler) 1325 m_resultDeclHandler.attributeDecl(eName, aName, type, valueDefault, value); 1326 } 1327 1328 1329 1342 public void internalEntityDecl (String name, String value) 1343 throws SAXException 1344 { 1345 if (null != m_resultDeclHandler) 1346 m_resultDeclHandler.internalEntityDecl(name, value); 1347 } 1348 1349 1350 1365 public void externalEntityDecl (String name, String publicId, 1366 String systemId) 1367 throws SAXException 1368 { 1369 if (null != m_resultDeclHandler) 1370 m_resultDeclHandler.externalEntityDecl(name, publicId, systemId); 1371 } 1372 1373 1376 private java.io.FileOutputStream m_outputStream = null; 1377 1378 1379 private ContentHandler m_resultContentHandler; 1380 1381 1382 private LexicalHandler m_resultLexicalHandler; 1383 1384 1385 private DTDHandler m_resultDTDHandler; 1386 1387 1388 private DeclHandler m_resultDeclHandler; 1389 1390 1391 private Serializer m_serializer; 1392 1393 1394 private Result m_result; 1395 1396 1400 private String m_systemID; 1401 1402 1406 private Hashtable m_params; 1407 1408 1409 private ErrorListener m_errorListener = 1410 new org.apache.xml.utils.DefaultErrorHandler(); 1411 1412 1416 URIResolver m_URIResolver; 1417 1418 1419 private OutputProperties m_outputFormat; 1420 1421 1423 boolean m_foundFirstElement; 1424} 1425 | Popular Tags |