private String createXml4HotelRequestBody(HotelRequestBody request, String requestType) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(HotelRequestBody.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); DocumentResult documentResult = new DocumentResult(); m.marshal(request, documentResult); org.dom4j.Document document = documentResult.getDocument(); org.dom4j.Element requestElement = document.getRootElement(); org.dom4j.Document doc1 = DocumentHelper.createDocument(); org.dom4j.Element rootElement = createRequestHeaderElement(doc1, requestType); rootElement.add(requestElement); return doc1.asXML(); }
/** * XML Pretty Printer XSLT Transformation * * @param document the Dom4j Document * @return the transformed document */ public static Document styleDocument( Document document ) { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = factory.newTransformer( new StreamSource( ParserUtils.class .getResourceAsStream( "/org/apache/directory/shared/dsmlv2/DSMLv2.xslt" ) ) ); } catch ( TransformerConfigurationException e1 ) { LOG.warn( "Failed to create the XSLT transformer", e1 ); // return original document return document; } // now lets style the given document DocumentSource source = new DocumentSource( document ); DocumentResult result = new DocumentResult(); try { transformer.transform( source, result ); } catch ( TransformerException e ) { // return original document return document; } // return the transformed document return result.getDocument(); }
public static Document transform(Transformer transformer, Document d) throws TransformerException { DocumentSource source = new DocumentSource(d); DocumentResult result = new DocumentResult(); // TODO: Allow the user to specify stylesheet parameters? transformer.transform(source, result); return result.getDocument(); }
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); } }
private void genEnum(TransformerFactory txFactory, Document doc, String basePkgPath) { try { InputStream xlsInputStream = getClass().getClassLoader().getResourceAsStream("xsl/enum.xsl"); Transformer tx = txFactory.newTransformer(new StreamSource(xlsInputStream)); List enumNodes = doc.selectNodes("root/enums/enum"); for (Object n : enumNodes) { Element enumNode = (Element) n; for (Object n2 : enumNode.selectNodes("field")) { Element fieldNode = (Element) n2; fieldNode.addAttribute("name", fieldNode.attributeValue("name").toUpperCase()); // System.out.println(fieldNode.attributeValue("name")); } } // DocumentSource src = new DocumentSource(doc); DocumentResult rzt = new DocumentResult(); tx.transform(src, rzt); Document rztDoc = rzt.getDocument(); String filePath = FileUtil.combinePath(basePkgPath, "constant"); FileUtil.mkdir(filePath); for (Object jn : rztDoc.selectNodes("enums/enum")) { Element jenumNode = (Element) jn; String enumName = jenumNode.attributeValue("name"); String jenumFilePath = FileUtil.combinePath(filePath, enumName + ".java"); Writer writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(jenumFilePath), "utf-8"); String enumCodes = jenumNode.getText(); enumCodes = enumCodes.replace(",;", ";"); writer.write(enumCodes); } finally { if (writer != null) { writer.close(); } } Log.info("gen enum %s at %s", enumName, jenumFilePath); } } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
@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)); } }
@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 <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] " + "</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)); } }
/** * Applies the transform to the Dom4J document * * @param transformer * @param d * the document to transform * * @return an array containing a single XML string representing the * transformed document * @throws TransformerException * thrown if an XSLT runtime error happens during transformation */ public static Document transform(Transformer transformer, Document d) throws TransformerException { DocumentSource source = new DocumentSource(d); DocumentResult result = new DocumentResult(); // TODO: Allow the user to specify stylesheet parameters? transformer.transform(source, result); return result.getDocument(); }