1 17 package org.apache.servicemix.jbi.jaxp; 18 19 import org.w3c.dom.Document ; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.Node ; 22 import org.xml.sax.SAXException ; 23 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.stream.XMLInputFactory; 26 import javax.xml.stream.XMLOutputFactory; 27 import javax.xml.stream.XMLStreamException; 28 import javax.xml.stream.XMLStreamReader; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.dom.DOMResult ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.sax.SAXSource ; 35 36 import java.io.IOException ; 37 38 45 public class StAXSourceTransformer extends SourceTransformer { 46 47 private XMLInputFactory inputFactory; 48 private XMLOutputFactory outputFactory; 49 50 54 public StaxSource toStaxSource(Source source) throws XMLStreamException { 55 if (source instanceof StaxSource) { 56 return (StaxSource) source; 57 } 58 else { 59 XMLInputFactory factory = getInputFactory(); 60 XMLStreamReader reader = factory.createXMLStreamReader(source); 61 return new StaxSource(reader); 62 } 63 } 64 65 public XMLStreamReader toXMLStreamReader(Source source) throws XMLStreamException, TransformerException { 66 if (source instanceof DOMSource ) { 69 Node n = ((DOMSource ) source).getNode(); 70 Element el = n instanceof Document ? ((Document ) n).getDocumentElement() : n instanceof Element ? (Element ) n : null; 71 if (el != null) { 72 return new W3CDOMStreamReader(el); 73 } 74 } 75 XMLInputFactory factory = getInputFactory(); 76 try { 77 return factory.createXMLStreamReader(source); 78 } catch (XMLStreamException e) { 79 return factory.createXMLStreamReader(toReaderFromSource(source)); 80 } 81 } 82 83 public DOMSource toDOMSource(Source source) throws ParserConfigurationException , IOException , SAXException , TransformerException { 84 DOMSource answer = super.toDOMSource(source); 85 if (answer == null && source instanceof StaxSource) { 86 answer = toDOMSourceFromStax((StaxSource) source); 87 } 88 return answer; 89 } 90 91 public SAXSource toSAXSource(Source source) throws IOException , SAXException { 92 SAXSource answer = super.toSAXSource(source); 93 if (answer == null && source instanceof StaxSource) { 94 answer = toSAXSourceFromStax((StaxSource) source); 95 } 96 return answer; 97 } 98 99 public DOMSource toDOMSourceFromStax(StaxSource source) throws TransformerException { 100 Transformer transformer = createTransfomer(); 101 DOMResult result = new DOMResult (); 102 transformer.transform(source, result); 103 return new DOMSource (result.getNode(), result.getSystemId()); 104 } 105 106 public SAXSource toSAXSourceFromStax(StaxSource source) { 107 return (SAXSource ) source; 108 } 109 110 public XMLInputFactory getInputFactory() { 113 if (inputFactory == null) { 114 inputFactory = createInputFactory(); 115 } 116 return inputFactory; 117 } 118 119 public void setInputFactory(XMLInputFactory inputFactory) { 120 this.inputFactory = inputFactory; 121 } 122 123 public XMLOutputFactory getOutputFactory() { 124 if (outputFactory == null) { 125 outputFactory = createOutputFactory(); 126 } 127 return outputFactory; 128 } 129 130 public void setOutputFactory(XMLOutputFactory outputFactory) { 131 this.outputFactory = outputFactory; 132 } 133 134 protected XMLInputFactory createInputFactory() { 137 XMLInputFactory answer = XMLInputFactory.newInstance(); 138 return answer; 139 } 140 141 protected XMLOutputFactory createOutputFactory() { 142 XMLOutputFactory answer = XMLOutputFactory.newInstance(); 143 return answer; 144 } 145 146 } 147 | Popular Tags |