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