use of javax.xml.stream.XMLInputFactory in project cxf 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);
}
}
use of javax.xml.stream.XMLInputFactory in project cxf by apache.
the class JSONUtils method createStreamReader.
public static XMLStreamReader createStreamReader(InputStream is, boolean readXsiType, ConcurrentHashMap<String, String> namespaceMap, String namespaceSeparator, List<String> primitiveArrayKeys, DocumentDepthProperties depthProps, String enc) throws Exception {
if (readXsiType) {
namespaceMap.putIfAbsent(XSI_URI, XSI_PREFIX);
}
Configuration conf = new Configuration(namespaceMap);
if (namespaceSeparator != null) {
conf.setJsonNamespaceSeparator(namespaceSeparator);
}
if (primitiveArrayKeys != null) {
conf.setPrimitiveArrayKeys(new HashSet<>(primitiveArrayKeys));
}
XMLInputFactory factory = depthProps != null ? new JettisonMappedReaderFactory(conf, depthProps) : new MappedXMLInputFactory(conf);
return new JettisonReader(namespaceMap, factory.createXMLStreamReader(is, enc));
}
use of javax.xml.stream.XMLInputFactory in project tomee by apache.
the class Sxc method getXmlInputFactory.
private static XMLInputFactory getXmlInputFactory() {
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
// We don't want to use whatever they have put in the their app as a STAX impl
Thread.currentThread().setContextClassLoader(Sxc.class.getClassLoader());
XMLInputFactory factory = null;
try {
// 1) trying to force jvm one, 2) skipping classloading/SPI mecanism, 3) setting specific property
factory = (XMLInputFactory) Sxc.class.getClassLoader().loadClass("com.sun.xml.internal.stream.XMLInputFactoryImpl").newInstance();
factory.setProperty("http://java.sun.com/xml/stream/properties/ignore-external-dtd", Boolean.TRUE);
} catch (final Exception e) {
// not a big deal, using the default one
factory = XMLInputFactory.newInstance();
}
factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
return factory;
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.
the class BaseXsltDataUpgraderTest method testXsltUpgrade.
protected void testXsltUpgrade(final Resource xslResource, final PortalDataKey dataKey, final Resource inputResource, final Resource expectedResultResource, final Resource xsdResource) throws Exception {
final XmlUtilities xmlUtilities = new XmlUtilitiesImpl() {
@Override
public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
}
};
final XsltDataUpgrader xsltDataUpgrader = new XsltDataUpgrader();
xsltDataUpgrader.setPortalDataKey(dataKey);
xsltDataUpgrader.setXslResource(xslResource);
xsltDataUpgrader.setXmlUtilities(xmlUtilities);
xsltDataUpgrader.afterPropertiesSet();
// Create XmlEventReader (what the JaxbPortalDataHandlerService has)
final XMLInputFactory xmlInputFactory = xmlUtilities.getXmlInputFactory();
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputResource.getInputStream());
final Node sourceNode = xmlUtilities.convertToDom(xmlEventReader);
final DOMSource source = new DOMSource(sourceNode);
final DOMResult result = new DOMResult();
xsltDataUpgrader.upgradeData(source, result);
// XSD Validation
final String resultString = XmlUtilitiesImpl.toString(result.getNode());
if (xsdResource != null) {
final Schema schema = this.loadSchema(new Resource[] { xsdResource }, XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(new StringReader(resultString)));
} catch (Exception e) {
throw new XmlTestException("Failed to validate XSLT output against provided XSD", resultString, e);
}
}
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setNormalizeWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(expectedResultResource.getInputStream()), new StringReader(resultString));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", resultString, e);
}
}
use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.
the class AbstractDom4jImporterExporterTest method testDom4jRoundTripWithComment.
@Test
public void testDom4jRoundTripWithComment() throws Exception {
final TestDom4jImporter importer = new TestDom4jImporter();
final TestDom4jExporter exporter = new TestDom4jExporter();
exporter.setXmlUtilities(new XmlUtilitiesImpl());
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final InputStream resource = this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml");
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(resource);
final Tuple<String, Element> result = importer.unmarshal(new StAXSource(xmlEventReader));
assertNotNull(result);
final StringWriter writer = new StringWriter();
exporter.marshal(result, new StreamResult(writer));
final String marshalResult = writer.toString();
assertNotNull(marshalResult);
XMLUnit.setIgnoreWhitespace(true);
try {
Diff d = new Diff(new InputStreamReader(this.getClass().getResourceAsStream("/org/apereo/portal/io/xml/crn/pilot-lo.fragment-layout.xml")), new StringReader(marshalResult));
assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
} catch (Exception e) {
throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
} catch (Error e) {
throw new XmlTestException("Failed to assert similar between marshall output and expected XML", marshalResult, e);
}
}
Aggregations