use of javax.xml.transform.stax.StAXSource in project galley by Commonjava.
the class XMLInfrastructure method fallbackParseDocument.
private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
try {
Source source;
if (safeInputFactory != null) {
xml = repairXmlDeclaration(xml);
final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
source = new StAXSource(eventReader);
} else {
// Deal with ø and other undeclared entities...
xml = escapeNonXMLEntityRefs(xml);
final XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", false);
source = new SAXSource(reader, new InputSource(new StringReader(xml)));
}
final DOMResult result = new DOMResult();
final Transformer transformer = newTransformer();
transformer.transform(source, result);
return (Document) result.getNode();
} catch (final TransformerException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final SAXException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
} catch (final XMLStreamException e1) {
throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
}
}
use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.
the class XSLTImplementation method supportsStAXSource.
public final synchronized boolean supportsStAXSource() {
if (supportsStAXSource == null) {
try {
StAXSource source = new StAXSource(XMLInputFactory.newInstance().createXMLStreamReader(new StringReader("<root/>")));
StreamResult result = new StreamResult(new ByteArrayOutputStream());
newTransformerFactory().newTransformer().transform(source, result);
supportsStAXSource = true;
} catch (Exception ex) {
supportsStAXSource = false;
}
}
return supportsStAXSource;
}
use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.
the class BuilderSpec method from.
static BuilderSpec from(StAXParserConfiguration configuration, Source source) {
if (source instanceof SAXSource) {
return from((SAXSource) source, true);
} else if (source instanceof DOMSource) {
return from(((DOMSource) source).getNode(), true);
} else if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
InputSource is = new InputSource();
is.setByteStream(streamSource.getInputStream());
is.setCharacterStream(streamSource.getReader());
is.setPublicId(streamSource.getPublicId());
is.setSystemId(streamSource.getSystemId());
return from(configuration, is);
} else if (source instanceof StAXSource) {
return from(((StAXSource) source).getXMLStreamReader());
} else {
try {
return new BuilderSpec(new FilteredXmlInput(new StAXPullInput(StAXUtils.getXMLInputFactory().createXMLStreamReader(source), true, null), NamespaceRepairingFilter.DEFAULT), null);
} catch (XMLStreamException ex) {
throw new OMException(ex);
}
}
}
use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.
the class StAXPivotTransformerTest method runTest.
@Override
protected void runTest() throws Throwable {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setExpandEntityReferences(false);
factory.setCoalescing(true);
factory.setIgnoringComments(true);
Document document = factory.newDocumentBuilder().parse(sample.getUrl().toString());
StAXPivot pivot = new StAXPivot(null);
pivot.setReader(new DOMInput(document, false).createReader(pivot));
StringWriter sw = new StringWriter();
xsltImplementation.newTransformerFactory().newTransformer().transform(new StAXSource(pivot), new StreamResult(sw));
assertAbout(xml()).that(sw.toString()).hasSameContentAs(document);
}
use of javax.xml.transform.stax.StAXSource in project cxf by apache.
the class StaxUtils method copy.
public static void copy(Source source, XMLStreamWriter writer) throws XMLStreamException {
if (source instanceof StaxSource) {
StaxSource ss = (StaxSource) source;
if (ss.getXMLStreamReader() == null) {
return;
}
} else if (source instanceof StAXSource) {
StAXSource ss = (StAXSource) source;
if (ss.getXMLStreamReader() == null) {
return;
}
} else if (source instanceof SAXSource) {
SAXSource ss = (SAXSource) source;
InputSource src = ss.getInputSource();
if (src == null || (src.getSystemId() == null && src.getPublicId() == null)) {
if (ss.getXMLReader() != null) {
// OK - reader is OK. We'll use that out
StreamWriterContentHandler ch = new StreamWriterContentHandler(writer);
XMLReader reader = ((SAXSource) source).getXMLReader();
reader.setContentHandler(ch);
try {
try {
reader.setFeature("http://xml.org/sax/features/namespaces", true);
} catch (Throwable t) {
// ignore
}
try {
reader.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
} catch (Throwable t) {
// ignore
}
reader.parse(((SAXSource) source).getInputSource());
return;
} catch (Exception e) {
throw new XMLStreamException(e.getMessage(), e);
}
} else if (ss.getInputSource() == null) {
// nothing to copy, just return
return;
}
}
} else if (source instanceof StreamSource) {
StreamSource ss = (StreamSource) source;
if (ss.getInputStream() == null && ss.getReader() == null && ss.getSystemId() == null) {
// nothing to copy, just return
return;
}
}
XMLStreamReader reader = createXMLStreamReader(source);
copy(reader, writer);
reader.close();
}
Aggregations