Search in sources :

Example 1 with DocumentResult

use of org.dom4j.io.DocumentResult in project gocd by gocd.

the class GRDDLTransformer method transform.

public Graph transform(final Document inputDoc, TempGraphFactory graphFactory) throws GrddlTransformException {
    final DocumentResult result = new DocumentResult();
    try {
        xsltTransformerRegistry.transformWithCorrectClassLoader(key, new XSLTTransformerExecutor<Void>() {

            @Override
            public Void execute(Transformer transformer) throws TransformerException {
                DocumentSource source = new DocumentSource(inputDoc);
                transformer.transform(source, result);
                return null;
            }
        });
        // TODO: likely need to optimize with some sort of streaming document reader here
        Graph graph = graphFactory.createTempGraph();
        graph.addTriplesFromRDFXMLAbbrev(new StringReader(result.getDocument().asXML()));
        return graph;
    } catch (ShineRuntimeException e) {
        LOGGER.error("Could not convert to a graph. The document was: \n" + result.getDocument().asXML(), e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.warn("Could not perform grddl transform. The document was: \n" + result.getDocument().asXML(), e);
        throw new GrddlTransformException(e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Graph(com.thoughtworks.studios.shine.semweb.Graph) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) DocumentResult(org.dom4j.io.DocumentResult) DocumentSource(org.dom4j.io.DocumentSource) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

Example 2 with DocumentResult

use of org.dom4j.io.DocumentResult in project gocd by gocd.

the class NUnitRDFizer method importFile.

public Graph importFile(final String parentURI, Document document) throws GrddlTransformException {
    final DocumentResult result = new DocumentResult();
    final DocumentSource source = new DocumentSource(document);
    try {
        return xsltTransformerRegistry.transformWithCorrectClassLoader(XSLTTransformerRegistry.XUNIT_NUNIT_TO_JUNIT_XSL, new XSLTTransformerExecutor<Graph>() {

            @Override
            public Graph execute(Transformer transformer) throws TransformerException, GrddlTransformException {
                transformer.transform(source, result);
                return jUnitRDFizer.importFile(parentURI, result.getDocument());
            }
        });
    } catch (TransformerException e) {
        throw new ShineRuntimeException(e);
    }
}
Also used : Graph(com.thoughtworks.studios.shine.semweb.Graph) Transformer(javax.xml.transform.Transformer) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) DocumentResult(org.dom4j.io.DocumentResult) DocumentSource(org.dom4j.io.DocumentSource) GrddlTransformException(com.thoughtworks.studios.shine.semweb.grddl.GrddlTransformException) TransformerException(javax.xml.transform.TransformerException)

Example 3 with DocumentResult

use of org.dom4j.io.DocumentResult in project gocd by gocd.

the class NUnitRDFizerTest method testShouldTransformFromXSLForNunit2Dot6ToJunitCorrectly.

@Test
public void testShouldTransformFromXSLForNunit2Dot6ToJunitCorrectly() throws Exception {
    String nunitInputXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!--\n" + "This file represents the results of running a test suite\n" + "-->\n" + "<test-results name=\"C:\\Program Files\\NUnit 2.6\\bin\\tests\\mock-assembly.dll\" total=\"21\" errors=\"1\" failures=\"1\" not-run=\"7\" inconclusive=\"1\" ignored=\"4\" skipped=\"0\" invalid=\"3\" date=\"2012-02-04\" time=\"11:46:05\">\n" + "   <environment nunit-version=\"2.6.0.12035\" clr-version=\"2.0.50727.4963\" os-version=\"Microsoft Windows NT 6.1.7600.0\" platform=\"Win32NT\" cwd=\"C:\\Program Files\\NUnit 2.6\\bin\" machine-name=\"CHARLIE-LAPTOP\" user=\"charlie\" user-domain=\"charlie-laptop\" />\n" + "   <culture-info current-culture=\"en-US\" current-uiculture=\"en-US\" />\n" + "   <test-suite type=\"Assembly\" name=\"C:\\Program Files\\NUnit 2.6\\bin\\tests\\mock-assembly.dll\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.094\" asserts=\"0\">\n" + "      <results>\n" + "         <test-suite type=\"Namespace\" name=\"NUnit\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.078\" asserts=\"0\">\n" + "            <results>\n" + "               <test-suite type=\"Namespace\" name=\"Tests\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.078\" asserts=\"0\">\n" + "                  <results>\n" + "                     <test-suite type=\"Namespace\" name=\"Assemblies\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.031\" asserts=\"0\">\n" + "                        <results>\n" + "                           <test-suite type=\"TestFixture\" name=\"MockTestFixture\" description=\"Fake Test Fixture\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.031\" asserts=\"0\">\n" + "                              <categories>\n" + "                                 <category name=\"FixtureCategory\" />\n" + "                              </categories>\n" + "                              <results>\n" + "                                 <test-case name=\"NUnit.Tests.Assemblies.MockTestFixture.TestWithException\" executed=\"True\" result=\"Error\" success=\"False\" time=\"0.000\" asserts=\"0\">\n" + "                                    <failure>\n" + "                                       <message>Intentional Error</message>\n" + "                                       <stack-trace>Some Stack Trace</stack-trace>\n" + "                                    </failure>\n" + "                                 </test-case>\n" + "                                 <test-case name=\"NUnit.Tests.Assemblies.MockTestFixture.FailingTest\" executed=\"True\" result=\"Failure\" success=\"False\" time=\"0.016\" asserts=\"0\">\n" + "                                    <failure>\n" + "                                       <message>Intentional failure</message>\n" + "                                       <stack-trace>Some Stack Trace</stack-trace>\n" + "                                    </failure>\n" + "                                 </test-case>\n" + "                                 <test-case name=\"NUnit.Tests.Assemblies.MockTestFixture.MockTest3\" executed=\"True\" result=\"Success\" success=\"True\" time=\"0.016\" asserts=\"0\">\n" + "                                    <categories>\n" + "                                       <category name=\"AnotherCategory\" />\n" + "                                       <category name=\"MockCategory\" />\n" + "                                    </categories>\n" + "                                    <reason>\n" + "                                       <message>Success</message>\n" + "                                    </reason>\n" + "                                 </test-case>\n" + "                              </results>\n" + "                           </test-suite>\n" + "                        </results>\n" + "                     </test-suite>\n" + "                  </results>\n" + "               </test-suite>\n" + "            </results>\n" + "         </test-suite>\n" + "      </results>\n" + "   </test-suite>\n" + "</test-results>";
    String expectedResultantJunitFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<testsuites>" + "<testsuite name=\"NUnit.Tests.Assemblies.MockTestFixture\" tests=\"3\" time=\"0.031\" failures=\"1\" errors=\"1\" skipped=\"0\">" + "<testcase classname=\"NUnit.Tests.Assemblies.MockTestFixture\" name=\"TestWithException\" time=\"0.000\">" + "<error message=\"Intentional Error\">Some Stack Trace</error><" + "/testcase><testcase classname=\"NUnit.Tests.Assemblies.MockTestFixture\" name=\"FailingTest\" time=\"0.016\">" + "<failure message=\"Intentional failure\">Some Stack Trace</failure>" + "</testcase><testcase classname=\"NUnit.Tests.Assemblies.MockTestFixture\" name=\"MockTest3\" time=\"0.016\"/>" + "</testsuite>" + "</testsuites>";
    try (InputStream xsl = getClass().getClassLoader().getResourceAsStream(XSLTTransformerRegistry.XUNIT_NUNIT_TO_JUNIT_XSL)) {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        DocumentSource source = new DocumentSource(new SAXReader().read(new InputSource(new ByteArrayInputStream(nunitInputXml.getBytes("utf-8")))));
        DocumentResult result = new DocumentResult();
        Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsl));
        transformer.transform(source, result);
        assertThat(result.getDocument().asXML(), isIdenticalTo(expectedResultantJunitFormat));
    }
}
Also used : InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentResult(org.dom4j.io.DocumentResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentSource(org.dom4j.io.DocumentSource) SAXReader(org.dom4j.io.SAXReader) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 4 with DocumentResult

use of org.dom4j.io.DocumentResult in project gocd by gocd.

the class NUnitRDFizerTest method testShouldTransformFromXSLForNunit2Dot5AndEarlierToJunitCorrectly.

@Test
public void testShouldTransformFromXSLForNunit2Dot5AndEarlierToJunitCorrectly() throws Exception {
    String nunitInputXml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>" + "<!--This file represents the results of running a test suite-->" + "<test-results name=\"/home/erik/coding/test/nunittests/Tests.dll\" total=\"4\" failures=\"1\" not-run=\"0\" date=\"2007-07-27\" time=\"11:18:43\">" + "  <environment nunit-version=\"2.2.8.0\" clr-version=\"2.0.50727.42\" os-version=\"Unix 2.6.18.4\" platform=\"Unix\" cwd=\"/home/erik/coding/test/nunittests\" machine-name=\"akira.ramfelt.se\" user=\"erik\" user-domain=\"akira.ramfelt.se\" />" + "  <culture-info current-culture=\"sv-SE\" current-uiculture=\"sv-SE\" />" + "  <test-suite name=\"/home/erik/coding/test/nunittests/Tests.dll\" success=\"False\" time=\"0.404\" asserts=\"0\">" + "    <results>" + "      <test-suite name=\"UnitTests\" success=\"False\" time=\"0.393\" asserts=\"0\">" + "        <results>" + "          <test-suite name=\"UnitTests.MainClassTest\" success=\"False\" time=\"0.289\" asserts=\"0\">" + "            <results>" + "              <test-case name=\"UnitTests.MainClassTest.TestPropertyValue\" executed=\"True\" success=\"True\" time=\"0.146\" asserts=\"1\" />" + "              <test-case name=\"UnitTests.MainClassTest.TestMethodUpdateValue\" executed=\"True\" success=\"True\" time=\"0.001\" asserts=\"1\" />" + "              <test-case name=\"UnitTests.MainClassTest.TestFailure\" executed=\"True\" success=\"False\" time=\"0.092\" asserts=\"1\" result=\"Failure\">" + "                <failure>" + "                  <message><![CDATA[  Expected failure" + "  Expected: 30" + "  But was:  20" + "]]></message>" + "                  <stack-trace><![CDATA[  at UnitTests.MainClassTest.TestFailure () [0x00000] " + "  at <0x00000> <unknown method>" + "  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[])" + "  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] " + "]]></stack-trace>" + "                </failure>" + "              </test-case>" + "            </results>" + "          </test-suite>" + "        </results>" + "      </test-suite>" + "    </results>" + "  </test-suite>" + "</test-results>";
    String expectedResultantJunitFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<testsuites>" + "<testsuite name=\"UnitTests.MainClassTest\" tests=\"3\" time=\"0.289\" failures=\"1\" errors=\"\" skipped=\"0\">" + "<testcase classname=\"UnitTests.MainClassTest\" name=\"TestPropertyValue\" time=\"0.146\"/>" + "<testcase classname=\"UnitTests.MainClassTest\" name=\"TestMethodUpdateValue\" time=\"0.001\"/>" + "<testcase classname=\"UnitTests.MainClassTest\" name=\"TestFailure\" time=\"0.092\">" + "<failure message=\"  Expected failure  Expected: 30  But was:  20\">  " + "at UnitTests.MainClassTest.TestFailure () [0x00000]   at &lt;0x00000&gt; &lt;unknown method&gt;  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[])  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] " + "</failure>" + "</testcase>" + "</testsuite>" + "</testsuites>";
    try (InputStream xsl = getClass().getClassLoader().getResourceAsStream(XSLTTransformerRegistry.XUNIT_NUNIT_TO_JUNIT_XSL)) {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        DocumentSource source = new DocumentSource(new SAXReader().read(new InputSource(new ByteArrayInputStream(nunitInputXml.getBytes("utf-8")))));
        DocumentResult result = new DocumentResult();
        Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsl));
        transformer.transform(source, result);
        assertThat(result.getDocument().asXML(), isIdenticalTo(expectedResultantJunitFormat));
    }
}
Also used : InputSource(org.xml.sax.InputSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentResult(org.dom4j.io.DocumentResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentSource(org.dom4j.io.DocumentSource) SAXReader(org.dom4j.io.SAXReader) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 5 with DocumentResult

use of org.dom4j.io.DocumentResult in project zm-mailbox by Zimbra.

the class JaxbUtil method jaxbToNamedElement.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Element jaxbToNamedElement(String name, String namespace, Object o, Element.ElementFactory factory) throws ServiceException {
    if (Element.JSONElement.mFactory.equals(factory)) {
        return JacksonUtil.jaxbToJSONElement(o, org.dom4j.QName.get(name, namespace));
    }
    try {
        Marshaller marshaller = createMarshaller(o.getClass());
        // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        DocumentResult dr = new DocumentResult();
        marshaller.marshal(new JAXBElement(new QName(namespace, name), o.getClass(), o), dr);
        Document theDoc = dr.getDocument();
        org.dom4j.Element rootElem = theDoc.getRootElement();
        return Element.convertDOM(rootElem, factory);
    } catch (Exception e) {
        throw ServiceException.FAILURE("Unable to convert " + o.getClass().getName() + " to Element", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) DocumentResult(org.dom4j.io.DocumentResult) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) Document(org.dom4j.Document) ServiceException(com.zimbra.common.service.ServiceException) JAXBException(javax.xml.bind.JAXBException)

Aggregations

DocumentResult (org.dom4j.io.DocumentResult)9 Document (org.dom4j.Document)5 Transformer (javax.xml.transform.Transformer)4 DocumentSource (org.dom4j.io.DocumentSource)4 ZMailbox (com.zimbra.client.ZMailbox)3 ServiceException (com.zimbra.common.service.ServiceException)3 Test (org.junit.Test)3 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)2 Graph (com.thoughtworks.studios.shine.semweb.Graph)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 JAXBException (javax.xml.bind.JAXBException)2 Marshaller (javax.xml.bind.Marshaller)2 TransformerException (javax.xml.transform.TransformerException)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 StreamSource (javax.xml.transform.stream.StreamSource)2 SAXReader (org.dom4j.io.SAXReader)2 InputSource (org.xml.sax.InputSource)2 GrddlTransformException (com.thoughtworks.studios.shine.semweb.grddl.GrddlTransformException)1 BrowseResponse (com.zimbra.soap.mail.message.BrowseResponse)1