Search in sources :

Example 61 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class IdentityImportExportTestUtilities method testIdentityImportExport.

public static <T> void testIdentityImportExport(TransactionOperations transactionOperations, final IDataImporter<T> dataImporter, final IDataExporter<?> dataExporter, Resource resource, Function<T, String> getName) throws Exception {
    final String importData = toString(resource);
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new StringReader(importData));
    // Unmarshall from XML
    final Unmarshaller unmarshaller = dataImporter.getUnmarshaller();
    final StAXSource source = new StAXSource(xmlEventReader);
    @SuppressWarnings("unchecked") final T dataImport = (T) unmarshaller.unmarshal(source);
    // Make sure the data was unmarshalled
    assertNotNull("Unmarshalled import data was null", dataImport);
    // Import the data
    dataImporter.importData(dataImport);
    // Export the data
    final String name = getName.apply(dataImport);
    final Object dataExport = transactionOperations.execute(new TransactionCallback<Object>() {

        /* (non-Javadoc)
                             * @see org.springframework.transaction.support.TransactionCallback#doInTransaction(org.springframework.transaction.TransactionStatus)
                             */
        @Override
        public Object doInTransaction(TransactionStatus status) {
            return dataExporter.exportData(name);
        }
    });
    // Make sure the data was exported
    assertNotNull("Exported data was null", dataExport);
    // Marshall to XML
    final Marshaller marshaller = dataExporter.getMarshaller();
    final StringWriter result = new StringWriter();
    marshaller.marshal(dataExport, new StreamResult(result));
    // Compare the exported XML data with the imported XML data, they should match
    final String resultString = result.toString();
    try {
        XMLUnit.setIgnoreWhitespace(true);
        Diff d = new Diff(new StringReader(importData), new StringReader(resultString));
        assertTrue("Export result differs from import" + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between import XML and export XML", resultString, e);
    }
}
Also used : Marshaller(org.springframework.oxm.Marshaller) StreamResult(javax.xml.transform.stream.StreamResult) Diff(org.custommonkey.xmlunit.Diff) TransactionStatus(org.springframework.transaction.TransactionStatus) StAXSource(javax.xml.transform.stax.StAXSource) IOException(java.io.IOException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(org.springframework.oxm.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 62 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class AbstractDom4jImporterExporterTest method testDom4jCommentFiltering.

@Test
public void testDom4jCommentFiltering() 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 Element document = result.getSecond();
    final List<org.dom4j.Node> comments = document.selectNodes("//comment()");
    for (final org.dom4j.Node comment : comments) {
        comment.detach();
    }
    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/filtered-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 63 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class AbstractDom4jImporter method convertToElement.

protected Element convertToElement(Source source) {
    if (source instanceof StAXSource) {
        final StAXSource staxSource = (StAXSource) source;
        final DOMConverter domConverter = new DOMConverter();
        final Document document;
        try {
            XMLStreamReader xmlStreamReader = staxSource.getXMLStreamReader();
            if (xmlStreamReader == null) {
                final XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
                xmlStreamReader = new FixedXMLEventStreamReader(xmlEventReader);
            }
            document = domConverter.buildDocument(xmlStreamReader);
        } catch (XMLStreamException e) {
            throw new RuntimeException("Failed to parse StAX Reader into Dom4J Element", e);
        }
        final DOMReader domReader = new DOMReader();
        final org.dom4j.Document dom4JDocument = domReader.read(document);
        dom4JDocument.setName(source.getSystemId());
        return dom4JDocument.getRootElement();
    }
    throw new IllegalArgumentException("Source of type " + source.getClass() + " is not supported");
}
Also used : DOMReader(org.dom4j.io.DOMReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMConverter(org.codehaus.staxmate.dom.DOMConverter)

Example 64 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class FragmentDefinitionImporter method unmarshal.

/* (non-Javadoc)
     * @see org.springframework.oxm.Unmarshaller#unmarshal(javax.xml.transform.Source)
     */
@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
    if (source instanceof StAXSource) {
        source = fixStAXSource((StAXSource) source);
    }
    final Transformer identityTransformer;
    try {
        identityTransformer = this.xmlUtilities.getIdentityTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create identity Transformer", e);
    }
    final DOMResult domResult = new DOMResult();
    try {
        identityTransformer.transform(source, domResult);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform " + source + " into Document", e);
    }
    final Document resultDoc = (Document) domResult.getNode();
    return new Tuple<String, Document>(source.getSystemId(), resultDoc);
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) Tuple(org.apereo.portal.utils.Tuple)

Example 65 with StAXSource

use of javax.xml.transform.stax.StAXSource in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method createSourceXmlEventReader.

private BufferedXMLEventReader createSourceXmlEventReader(final Source source) {
    // If it is a StAXSource see if we can do better handling of it
    if (source instanceof StAXSource) {
        final StAXSource staxSource = (StAXSource) source;
        XMLEventReader xmlEventReader = staxSource.getXMLEventReader();
        if (xmlEventReader != null) {
            if (xmlEventReader instanceof BufferedXMLEventReader) {
                final BufferedXMLEventReader bufferedXMLEventReader = (BufferedXMLEventReader) xmlEventReader;
                bufferedXMLEventReader.reset();
                bufferedXMLEventReader.mark(-1);
                return bufferedXMLEventReader;
            }
            return new BufferedXMLEventReader(xmlEventReader, -1);
        }
    }
    final XMLInputFactory xmlInputFactory = this.xmlUtilities.getXmlInputFactory();
    final XMLEventReader xmlEventReader;
    try {
        xmlEventReader = xmlInputFactory.createXMLEventReader(source);
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to create XML Event Reader for data Source", e);
    }
    return new BufferedXMLEventReader(xmlEventReader, -1);
}
Also used : BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedXMLEventReader(org.apereo.portal.xml.stream.BufferedXMLEventReader) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6