Search in sources :

Example 1 with XmlUtilities

use of org.apereo.portal.xml.XmlUtilities 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)

Aggregations

IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMResult (javax.xml.transform.dom.DOMResult)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Schema (javax.xml.validation.Schema)1 Validator (javax.xml.validation.Validator)1 XmlUtilities (org.apereo.portal.xml.XmlUtilities)1 XmlUtilitiesImpl (org.apereo.portal.xml.XmlUtilitiesImpl)1 Diff (org.custommonkey.xmlunit.Diff)1 Resource (org.springframework.core.io.Resource)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1