Search in sources :

Example 81 with XMLInputFactory

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Node(org.w3c.dom.Node) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 82 with XMLInputFactory

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));
}
Also used : Configuration(org.codehaus.jettison.mapped.Configuration) XMLInputFactory(javax.xml.stream.XMLInputFactory) BadgerFishXMLInputFactory(org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory) MappedXMLInputFactory(org.codehaus.jettison.mapped.MappedXMLInputFactory) MappedXMLInputFactory(org.codehaus.jettison.mapped.MappedXMLInputFactory)

Example 83 with XMLInputFactory

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);
    }
}
Also used : XMLInputFactory(javax.xml.stream.XMLInputFactory) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) MarshalException(javax.xml.bind.MarshalException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException)

Example 84 with XMLInputFactory

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) DOMResult(javax.xml.transform.dom.DOMResult) InputStreamReader(java.io.InputStreamReader) Diff(org.custommonkey.xmlunit.Diff) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) Schema(javax.xml.validation.Schema) Resource(org.springframework.core.io.Resource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) XmlUtilities(org.apereo.portal.xml.XmlUtilities) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Validator(javax.xml.validation.Validator)

Example 85 with XMLInputFactory

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);
    }
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) InputStreamReader(java.io.InputStreamReader) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) Element(org.dom4j.Element) StAXSource(javax.xml.transform.stax.StAXSource) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)182 XMLStreamReader (javax.xml.stream.XMLStreamReader)114 XMLStreamException (javax.xml.stream.XMLStreamException)74 InputStream (java.io.InputStream)54 StringReader (java.io.StringReader)45 IOException (java.io.IOException)40 XMLEventReader (javax.xml.stream.XMLEventReader)36 ByteArrayInputStream (java.io.ByteArrayInputStream)30 Test (org.junit.Test)29 InputStreamReader (java.io.InputStreamReader)19 Unmarshaller (javax.xml.bind.Unmarshaller)18 XMLEvent (javax.xml.stream.events.XMLEvent)15 StAXSource (javax.xml.transform.stax.StAXSource)15 StreamSource (javax.xml.transform.stream.StreamSource)15 ArrayList (java.util.ArrayList)14 JAXBException (javax.xml.bind.JAXBException)14 HashMap (java.util.HashMap)13 DOMSource (javax.xml.transform.dom.DOMSource)12 StartElement (javax.xml.stream.events.StartElement)10 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10