1 21 package oracle.toplink.essentials.platform.xml.jaxp; 23 24 import java.io.OutputStream ; 25 import java.io.Writer ; 26 import java.net.URL ; 27 import javax.xml.transform.OutputKeys ; 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerConfigurationException ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.TransformerFactory ; 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.stream.StreamResult ; 38 import javax.xml.transform.stream.StreamSource ; 39 import oracle.toplink.essentials.platform.xml.XMLPlatformException; 40 import oracle.toplink.essentials.platform.xml.XMLTransformer; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Node ; 43 import org.xml.sax.ContentHandler ; 44 45 public class JAXPTransformer implements XMLTransformer { 46 private boolean fragment; 47 private static final String NO = "no"; 48 private static final String YES = "yes"; 49 private Transformer transformer; 50 51 public JAXPTransformer() { 52 super(); 53 try { 54 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 55 transformer = transformerFactory.newTransformer(); 56 } catch (TransformerConfigurationException e) { 57 throw XMLPlatformException.xmlPlatformTransformException(e); 58 } 59 } 60 61 public String getEncoding() { 62 return transformer.getOutputProperty(OutputKeys.ENCODING); 63 } 64 65 public void setEncoding(String encoding) { 66 transformer.setOutputProperty(OutputKeys.ENCODING, encoding); 67 } 68 69 public boolean isFormattedOutput() { 70 return transformer.getOutputProperty(OutputKeys.INDENT).equals(YES); 71 } 72 73 public void setFormattedOutput(boolean shouldFormat) { 74 if (shouldFormat) { 75 transformer.setOutputProperty(OutputKeys.INDENT, YES); 76 } else { 77 transformer.setOutputProperty(OutputKeys.INDENT, NO); 78 } 79 } 80 81 public String getVersion() { 82 return transformer.getOutputProperty(OutputKeys.VERSION); 83 } 84 85 public void setVersion(String version) { 86 transformer.setOutputProperty(OutputKeys.VERSION, version); 87 } 88 89 public void transform(Node sourceNode, OutputStream resultOutputStream) throws XMLPlatformException { 90 DOMSource source = new DOMSource (sourceNode); 91 StreamResult result = new StreamResult (resultOutputStream); 92 if (isFragment()) { 93 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 94 } 95 transform(source, result); 96 } 97 98 public void transform(Node sourceNode, ContentHandler resultContentHandler) throws XMLPlatformException { 99 DOMSource source = new DOMSource (sourceNode); 100 SAXResult result = new SAXResult (resultContentHandler); 101 102 transform(source, result); 103 } 104 105 public void transform(Node sourceNode, Result result) throws XMLPlatformException { 106 DOMSource source = null; 107 if ((isFragment()) && (result instanceof SAXResult )) { 108 if (sourceNode instanceof Document ) { 109 source = new DOMSource (((Document )sourceNode).getDocumentElement()); 110 } 111 } else { 112 source = new DOMSource (sourceNode); 113 } 114 transform(source, result); 115 } 116 117 public void transform(Node sourceNode, Writer resultWriter) throws XMLPlatformException { 118 DOMSource source = new DOMSource (sourceNode); 119 StreamResult result = new StreamResult (resultWriter); 120 121 if (isFragment()) { 122 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 123 } 124 transform(source, result); 125 } 126 127 public void transform(Source source, Result result) throws XMLPlatformException { 128 try { 129 if ((result instanceof StreamResult ) && (isFragment())) { 130 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 131 } 132 transformer.transform(source, result); 133 } catch (TransformerException e) { 134 throw XMLPlatformException.xmlPlatformTransformException(e); 135 } 136 } 137 138 public void transform(Document sourceDocument, Node resultParentNode, URL stylesheet) throws XMLPlatformException { 139 try { 140 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 141 StreamSource stylesheetSource = new StreamSource (stylesheet.openStream()); 142 Transformer transformer = transformerFactory.newTransformer(stylesheetSource); 143 DOMSource source = new DOMSource (sourceDocument); 144 DOMResult result = new DOMResult (resultParentNode); 145 transformer.transform(source, result); 146 } catch (Exception e) { 147 throw XMLPlatformException.xmlPlatformTransformException(e); 148 } 149 } 150 151 public void setFragment(boolean fragment) { 152 this.fragment = fragment; 153 } 154 155 public boolean isFragment() { 156 return fragment; 157 } 158 } 159 | Popular Tags |