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);
}
}
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);
}
}
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");
}
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);
}
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);
}
Aggregations