use of javax.xml.transform.stax.StAXSource in project tomee by apache.
the class StaxUtils method createXMLStreamReader.
public static XMLStreamReader createXMLStreamReader(Source source) {
try {
if (source instanceof DOMSource) {
DOMSource ds = (DOMSource) source;
Node nd = ds.getNode();
Element el = null;
if (nd instanceof Document) {
el = ((Document) nd).getDocumentElement();
} else if (nd instanceof Element) {
el = (Element) nd;
}
if (null != el) {
return new W3CDOMStreamReader(el, source.getSystemId());
}
} else if (source instanceof StAXSource) {
return ((StAXSource) source).getXMLStreamReader();
} else if (source instanceof StaxSource) {
return ((StaxSource) source).getXMLStreamReader();
} else if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
if (ss.getXMLReader() == null) {
return createXMLStreamReader(((SAXSource) source).getInputSource());
}
}
XMLInputFactory factory = getXMLInputFactory();
try {
XMLStreamReader reader = null;
try {
reader = factory.createXMLStreamReader(source);
} catch (UnsupportedOperationException e) {
// ignore
}
if (reader == null && source instanceof StreamSource) {
// createXMLStreamReader from Source is optional, we'll try and map it
StreamSource ss = (StreamSource) source;
if (ss.getInputStream() != null) {
reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getInputStream());
} else {
reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getReader());
}
}
return reader;
} finally {
returnXMLInputFactory(factory);
}
} catch (XMLStreamException e) {
throw new RuntimeException("Couldn't parse stream.", e);
}
}
Aggregations