Search in sources :

Example 51 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 52 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)

Example 53 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.

the class BufferedXMLEventReaderTest method testBufferSomeEvents.

@Test
public void testBufferSomeEvents() throws Exception {
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final InputStream xmlStream = this.getClass().getResourceAsStream("document.xml");
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(xmlStream);
    final BufferedXMLEventReader reader = new BufferedXMLEventReader(xmlEventReader, 10);
    int eventCount = 0;
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
    reader.reset();
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(132, eventCount);
}
Also used : InputStream(java.io.InputStream) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 54 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.

the class BufferedXMLEventReaderTest method testBufferAllEvents.

@Test
public void testBufferAllEvents() throws Exception {
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final InputStream xmlStream = this.getClass().getResourceAsStream("document.xml");
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(xmlStream);
    final BufferedXMLEventReader reader = new BufferedXMLEventReader(xmlEventReader, -1);
    final XMLEvent firstEvent = reader.peek();
    int eventCount = 0;
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(122, eventCount);
    reader.reset();
    final XMLEvent firstEventAgain = reader.peek();
    assertEquals(firstEvent, firstEventAgain);
    while (reader.hasNext()) {
        reader.nextEvent();
        eventCount++;
    }
    assertEquals(244, eventCount);
}
Also used : InputStream(java.io.InputStream) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 55 with XMLInputFactory

use of javax.xml.stream.XMLInputFactory in project uPortal by Jasig.

the class Dom2StAXTest method testDom2StAXEventReader.

@Test
public void testDom2StAXEventReader() throws Exception {
    final XMLInputFactory newFactory = XMLInputFactory.newFactory();
    final DOMSource source = new DOMSource(this.document);
    newFactory.createXMLEventReader(source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

XMLInputFactory (javax.xml.stream.XMLInputFactory)154 XMLStreamReader (javax.xml.stream.XMLStreamReader)98 XMLStreamException (javax.xml.stream.XMLStreamException)63 StringReader (java.io.StringReader)43 InputStream (java.io.InputStream)41 IOException (java.io.IOException)33 XMLEventReader (javax.xml.stream.XMLEventReader)30 Test (org.junit.Test)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStreamReader (java.io.InputStreamReader)14 JAXBException (javax.xml.bind.JAXBException)14 StAXSource (javax.xml.transform.stax.StAXSource)14 StreamSource (javax.xml.transform.stream.StreamSource)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 ArrayList (java.util.ArrayList)12 XMLEvent (javax.xml.stream.events.XMLEvent)12 DOMSource (javax.xml.transform.dom.DOMSource)11 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)10 JAXBContext (javax.xml.bind.JAXBContext)9 HashMap (java.util.HashMap)8