1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.Reader ; 24 import java.io.StringWriter ; 25 26 import javax.jbi.messaging.MessagingException; 27 import javax.jbi.messaging.NormalizedMessage; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerConfigurationException ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.sax.SAXSource ; 39 import javax.xml.transform.stream.StreamResult ; 40 import javax.xml.transform.stream.StreamSource ; 41 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.apache.xalan.xsltc.trax.DOM2SAX; 45 import org.apache.xalan.xsltc.trax.SAX2DOM; 46 import org.w3c.dom.Document ; 47 import org.w3c.dom.Element ; 48 import org.w3c.dom.Node ; 49 import org.xml.sax.InputSource ; 50 import org.xml.sax.SAXException ; 51 import org.xml.sax.XMLReader ; 52 import org.xml.sax.helpers.XMLReaderFactory ; 53 54 59 public class SourceTransformer { 60 public static final String CONTENT_DOCUMENT_PROPERTY = "org.apache.servicemix.content.document"; 61 62 private static final transient Log log = LogFactory.getLog(SourceTransformer.class); 63 64 private DocumentBuilderFactory documentBuilderFactory; 65 private TransformerFactory transformerFactory; 66 67 public static final String defaultCharEncodingName = "UTF-8"; 68 69 public SourceTransformer() { 70 } 71 72 public SourceTransformer(DocumentBuilderFactory documentBuilderFactory) { 73 this.documentBuilderFactory = documentBuilderFactory; 74 } 75 76 77 80 public void toResult(Source source, Result result) throws TransformerException { 81 Transformer transformer = createTransfomer(); 82 if (transformer == null) { 83 throw new TransformerException ("Could not create a transformer - JAXP is misconfigured!"); 84 } 85 if (source != null) { 86 transformer.transform(source, result); 87 } 88 else { 89 log.warn("No Source available"); 90 } 91 } 92 93 94 97 public String toString(Source source) throws TransformerException { 98 if (source == null) { 99 return null; 100 } else if (source instanceof StringSource) { 101 return ((StringSource) source).getText(); 102 } else if (source instanceof BytesSource) { 103 return new String (((BytesSource) source).getData()); 104 } else { 105 StringWriter buffer = new StringWriter (); 106 toResult(source, new StreamResult (buffer)); 107 return buffer.toString(); 108 } 109 } 110 111 114 public String toString(Node node) throws TransformerException { 115 return toString(new DOMSource (node)); 116 } 117 118 124 public String contentToString(NormalizedMessage message) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 125 return toString(message.getContent()); 126 } 127 128 132 public DOMSource toDOMSource(Source source) throws ParserConfigurationException , IOException , SAXException , TransformerException { 133 if (source instanceof DOMSource ) { 134 return (DOMSource ) source; 135 } 136 else if (source instanceof SAXSource ) { 137 return toDOMSourceFromSAX((SAXSource ) source); 138 } 139 else if (source instanceof StreamSource ) { 140 return toDOMSourceFromStream((StreamSource ) source); 141 } 142 else { 143 return null; 144 } 145 } 146 147 public Source toDOMSource(NormalizedMessage message) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 148 Node node = toDOMNode(message); 149 return new DOMSource (node); 150 } 151 152 156 public SAXSource toSAXSource(Source source) throws IOException , SAXException { 157 if (source instanceof SAXSource ) { 158 return (SAXSource ) source; 159 } 160 else if (source instanceof DOMSource ) { 161 return toSAXSourceFromDOM((DOMSource ) source); 162 } 163 else if (source instanceof StreamSource ) { 164 return toSAXSourceFromStream((StreamSource ) source); 165 } 166 else { 167 return null; 168 } 169 } 170 171 public StreamSource toStreamSource(Source source) throws TransformerException { 172 if (source instanceof StreamSource ) { 173 return (StreamSource ) source; 174 } else if (source instanceof DOMSource ) { 175 return toStreamSourceFromDOM((DOMSource ) source); 176 } else if (source instanceof SAXSource ) { 177 return toStreamSourceFromSAX((SAXSource ) source); 178 } else { 179 return null; 180 } 181 } 182 183 public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException { 184 InputSource inputSource = source.getInputSource(); 185 if (inputSource != null) { 186 if (inputSource.getByteStream() != null) { 187 return new StreamSource (inputSource.getByteStream()); 188 } 189 if (inputSource.getCharacterStream() != null) { 190 return new StreamSource (inputSource.getCharacterStream()); 191 } 192 } 193 String result = toString(source); 194 return new StreamSource (new ByteArrayInputStream (result.getBytes())); 195 } 196 197 public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException { 198 String result = toString(source); 199 return new StreamSource (new ByteArrayInputStream (result.getBytes())); 200 } 201 202 public SAXSource toSAXSourceFromStream(StreamSource source) { 203 InputSource inputSource = new InputSource (source.getInputStream()); 204 inputSource.setSystemId(source.getSystemId()); 205 inputSource.setPublicId(source.getPublicId()); 206 return new SAXSource (inputSource); 207 } 208 209 public Reader toReaderFromSource(Source src) throws TransformerException { 210 StreamSource stSrc = toStreamSource(src); 211 Reader r = stSrc.getReader(); 212 if (r == null) { 213 r = new InputStreamReader (stSrc.getInputStream()); 214 } 215 return r; 216 } 217 218 public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException , IOException , 219 SAXException { 220 DocumentBuilder builder = createDocumentBuilder(); 221 String systemId = source.getSystemId(); 222 Document document = null; 223 InputStream inputStream = source.getInputStream(); 224 if (inputStream != null) { 225 InputSource inputsource = new InputSource (inputStream); 226 inputsource.setSystemId(systemId); 227 inputsource.setEncoding(defaultCharEncodingName); 228 document = builder.parse(inputsource); 229 } 230 else { 231 Reader reader = source.getReader(); 232 if (reader != null) { 233 document = builder.parse(new InputSource (reader)); 234 } 235 else { 236 throw new IOException ("No input stream or reader available"); 237 } 238 } 239 return new DOMSource (document, systemId); 240 } 241 242 public SAXSource toSAXSourceFromDOM(DOMSource source) { 243 DOM2SAX converter = new DOM2SAX(source.getNode()); 244 String systemId = converter.getSystemId(); 245 return new SAXSource (converter, new InputSource (systemId)); 246 } 247 248 public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException , SAXException , ParserConfigurationException { 249 return new DOMSource (toDOMNodeFromSAX(source)); 250 } 251 252 public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException , IOException , SAXException { 253 SAX2DOM converter = new SAX2DOM(createDocument()); 254 XMLReader xmlReader = source.getXMLReader(); 255 if (xmlReader == null) { 256 xmlReader = createXMLReader(); 257 } 258 xmlReader.setContentHandler(converter); 259 xmlReader.parse(source.getInputSource()); 260 return converter.getDOM(); 261 } 262 263 private XMLReader createXMLReader() throws SAXException { 264 try { 268 return XMLReaderFactory.createXMLReader(); 269 } catch (Exception e) { 270 return XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); 271 } 272 } 273 274 275 281 public Node toDOMNode(Source source) throws TransformerException , ParserConfigurationException , IOException , SAXException { 282 return toDOMSource(source).getNode(); 283 } 284 285 296 public Node toDOMNode(NormalizedMessage message) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 297 Object value = message.getProperty(CONTENT_DOCUMENT_PROPERTY); 298 if (value != null) { 299 if (value instanceof Node ) { 300 return (Node ) value; 301 } 302 else { 303 throw new MessagingException("Invalid property type: Expected W3C DOM node but found: " + value.getClass().getName() + " with value: " + value); 304 } 305 } 306 else { 307 Source content = message.getContent(); 308 if (content != null) { 309 Node node = toDOMNode(content); 310 message.setProperty(CONTENT_DOCUMENT_PROPERTY, node); 311 return node; 312 } 313 else { 314 return null; 315 } 316 } 317 } 318 319 330 public Element toDOMElement(NormalizedMessage message) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 331 Node node = toDOMNode(message); 332 return toDOMElement(node); 333 } 334 335 345 public Element toDOMElement(Source source) throws TransformerException , ParserConfigurationException , IOException , SAXException { 346 Node node = toDOMNode(source); 347 return toDOMElement(node); 348 } 349 350 359 public Element toDOMElement(Node node) throws TransformerException { 360 Element elem; 361 if (node instanceof Document ) { 363 return ((Document ) node).getDocumentElement(); 364 } else if (node instanceof Element ) { 366 return (Element ) node; 367 } else { 369 throw new TransformerException ("Unable to convert DOM node to an Element"); 370 } 371 } 372 373 384 public Document toDOMDocument(NormalizedMessage message) throws MessagingException, TransformerException , ParserConfigurationException , IOException , SAXException { 385 Node node = toDOMNode(message); 386 return toDOMDocument(node); 387 } 388 389 399 public Document toDOMDocument(Source source) throws TransformerException , ParserConfigurationException , IOException , SAXException { 400 Node node = toDOMNode(source); 401 return toDOMDocument(node); 402 } 403 404 416 public Document toDOMDocument(Node node) throws ParserConfigurationException , TransformerException { 417 if (node instanceof Document ) { 419 return (Document ) node; 420 } else if (node instanceof Element ) { 422 Element elem = (Element ) node; 423 if (elem.getOwnerDocument().getDocumentElement() == elem) { 425 return elem.getOwnerDocument(); 426 } else { 428 Document doc = createDocument(); 429 doc.appendChild(doc.importNode(node, true)); 430 return doc; 431 } 432 } else { 434 throw new TransformerException ("Unable to convert DOM node to a Document"); 435 } 436 } 437 438 public DocumentBuilderFactory getDocumentBuilderFactory() { 441 if (documentBuilderFactory == null) { 442 documentBuilderFactory = createDocumentBuilderFactory(); 443 } 444 return documentBuilderFactory; 445 } 446 447 public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) { 448 this.documentBuilderFactory = documentBuilderFactory; 449 } 450 451 452 public DocumentBuilderFactory createDocumentBuilderFactory() { 455 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 456 factory.setNamespaceAware(true); 457 factory.setIgnoringElementContentWhitespace(true); 458 factory.setIgnoringComments(true); 459 return factory; 460 } 461 462 463 public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { 464 DocumentBuilderFactory factory = getDocumentBuilderFactory(); 465 return factory.newDocumentBuilder(); 466 } 467 468 public Document createDocument() throws ParserConfigurationException { 469 DocumentBuilder builder = createDocumentBuilder(); 470 return builder.newDocument(); 471 } 472 473 public TransformerFactory getTransformerFactory() { 474 if (transformerFactory == null) { 475 transformerFactory = createTransformerFactory(); 476 } 477 return transformerFactory; 478 } 479 480 public void setTransformerFactory(TransformerFactory transformerFactory) { 481 this.transformerFactory = transformerFactory; 482 } 483 484 public Transformer createTransfomer() throws TransformerConfigurationException { 485 TransformerFactory factory = getTransformerFactory(); 486 return factory.newTransformer(); 487 } 488 489 public TransformerFactory createTransformerFactory() { 490 TransformerFactory answer = TransformerFactory.newInstance(); 491 return answer; 492 } 493 494 } 495 | Popular Tags |