Search in sources :

Example 6 with DOMDocument

use of org.dom4j.dom.DOMDocument in project gocd by gocd.

the class MaterialXmlRepresenter method toXml.

@Override
public Document toXml(XmlWriterContext ctx) {
    DOMElement rootElement = new DOMElement("material");
    ElementBuilder builder = new ElementBuilder(rootElement);
    populateMaterial(ctx, materialRevision.getMaterial(), builder);
    return new DOMDocument(rootElement);
}
Also used : ElementBuilder(com.thoughtworks.go.server.domain.xml.builder.ElementBuilder) DOMDocument(org.dom4j.dom.DOMDocument) DOMElement(org.dom4j.dom.DOMElement)

Example 7 with DOMDocument

use of org.dom4j.dom.DOMDocument in project openolat by klemens.

the class LocalizedXSLTransformer method render.

/**
 * Render with a localized stylesheet. The localized stylesheet is addressed
 * by its name with appended locale. E.g. mystyle.xsl in DE locale is
 * addressed by mystyle_de.xsl
 *
 * @param node The node to render
 * @param styleSheetName The stylesheet to use.
 * @return Results of XSL transformation
 */
private String render(Element node) {
    try {
        Document doc = node.getDocument();
        if (doc == null) {
            doc = new DOMDocument();
            doc.add(node);
        }
        DocumentSource xmlsource = new DocumentSource(node);
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        templates.newTransformer().transform(xmlsource, result);
        return sw.toString();
    } catch (Exception e) {
        throw new OLATRuntimeException(LocalizedXSLTransformer.class, "Error transforming XML.", e);
    }
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) DocumentSource(org.dom4j.io.DocumentSource) DOMDocument(org.dom4j.dom.DOMDocument) Document(org.dom4j.Document) DOMDocument(org.dom4j.dom.DOMDocument) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 8 with DOMDocument

use of org.dom4j.dom.DOMDocument in project OpenOLAT by OpenOLAT.

the class LocalizedXSLTransformer method render.

/**
 * Render with a localized stylesheet. The localized stylesheet is addressed
 * by its name with appended locale. E.g. mystyle.xsl in DE locale is
 * addressed by mystyle_de.xsl
 *
 * @param node The node to render
 * @param styleSheetName The stylesheet to use.
 * @return Results of XSL transformation
 */
private String render(Element node) {
    try {
        Document doc = node.getDocument();
        if (doc == null) {
            doc = new DOMDocument();
            doc.add(node);
        }
        DocumentSource xmlsource = new DocumentSource(node);
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        templates.newTransformer().transform(xmlsource, result);
        return sw.toString();
    } catch (Exception e) {
        throw new OLATRuntimeException(LocalizedXSLTransformer.class, "Error transforming XML.", e);
    }
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) DocumentSource(org.dom4j.io.DocumentSource) DOMDocument(org.dom4j.dom.DOMDocument) Document(org.dom4j.Document) DOMDocument(org.dom4j.dom.DOMDocument) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 9 with DOMDocument

use of org.dom4j.dom.DOMDocument in project xwiki-platform by xwiki.

the class XWikiDocument method toXMLDocument.

/**
 * Serialize the document to an XML {@link DOMDocument}. You should prefer
 * {@link #toXML(OutputStream, boolean, boolean, boolean, boolean, XWikiContext)} or
 * {@link #toXML(com.xpn.xwiki.internal.xml.XMLWriter, boolean, boolean, boolean, boolean, XWikiContext)} when
 * possible to reduce memory load.
 *
 * @param bWithObjects include XObjects
 * @param bWithRendering include the rendered content
 * @param bWithAttachmentContent include attachments content
 * @param bWithVersions include archived versions
 * @param context current XWikiContext
 * @return a {@link DOMDocument} containing the serialized document.
 * @throws XWikiException when an errors occurs during wiki operations
 * @deprecated since 9.0RC1, use {@link #toXML(OutputTarget, boolean, boolean, boolean, boolean, boolean, String)}
 *             instead
 */
@Deprecated
public Document toXMLDocument(boolean bWithObjects, boolean bWithRendering, boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context) throws XWikiException {
    Document doc = new DOMDocument();
    DOMXMLWriter wr = new DOMXMLWriter(doc, new OutputFormat("", true, context.getWiki().getEncoding()));
    try {
        toXML(wr, bWithObjects, bWithRendering, bWithAttachmentContent, bWithVersions, context);
        return doc;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DOMDocument(org.dom4j.dom.DOMDocument) OutputFormat(org.dom4j.io.OutputFormat) DOMXMLWriter(com.xpn.xwiki.internal.xml.DOMXMLWriter) IOException(java.io.IOException) Document(org.dom4j.Document) DOMDocument(org.dom4j.dom.DOMDocument)

Example 10 with DOMDocument

use of org.dom4j.dom.DOMDocument in project narayana by jbosstm.

the class XMLResultsServlet method doStatus.

public void doStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/xml");
    response.setHeader("Cache-Control", "no-cache");
    HttpSession session = request.getSession();
    final FullTestResult testResult = (FullTestResult) session.getAttribute(TestConstants.ATTRIBUTE_TEST_RESULT);
    DOMDocument report = new DOMDocument();
    DOMElement testsuite = new DOMElement("testsuite");
    report.setRootElement(testsuite);
    if (testResult == null) {
    // No JUnit test results generated.
    } else {
        List passedTests = testResult.getPassedTests();
        List failedTests = testResult.getFailedTests();
        List errorTests = testResult.getErrorTests();
        final int runCount = testResult.runCount();
        final int errorCount = testResult.errorCount();
        final int failureCount = testResult.failureCount();
        testsuite.addAttribute("name", "com.jboss.transaction.wstf.interop.InteropTestSuite");
        testsuite.addAttribute("errors", Integer.toString(errorCount));
        testsuite.addAttribute("failures", Integer.toString(failureCount));
        testsuite.addAttribute("hostname", request.getServerName());
        testsuite.addAttribute("tests", Integer.toString(runCount));
        testsuite.addAttribute("timestamp", new Date().toString());
        DOMElement properties = new DOMElement("properties");
        testsuite.add(properties);
        DOMElement status = newPropertyDOMElement("status");
        properties.add(status);
        status.addAttribute("value", "finished");
        long totalDuration = 0;
        if (!passedTests.isEmpty()) {
            Iterator passedTestsIterator = passedTests.iterator();
            while (passedTestsIterator.hasNext()) {
                FullTestResult.PassedTest passedTest = (FullTestResult.PassedTest) passedTestsIterator.next();
                totalDuration += passedTest.duration;
                final String name = passedTest.test.toString();
                final String description = (String) TestConstants.DESCRIPTIONS.get(name);
                testsuite.add(newTestcase(passedTest.test.getClass().getName(), name + ": " + description, passedTest.duration));
            }
        }
        if (!failedTests.isEmpty()) {
            Iterator failedTestsIterator = failedTests.iterator();
            while (failedTestsIterator.hasNext()) {
                FullTestResult.FailedTest failedTest = (FullTestResult.FailedTest) failedTestsIterator.next();
                totalDuration += failedTest.duration;
                final String name = failedTest.test.toString();
                final String description = (String) TestConstants.DESCRIPTIONS.get(name);
                CharArrayWriter charArrayWriter = new CharArrayWriter();
                PrintWriter printWriter = new PrintWriter(charArrayWriter, true);
                failedTest.assertionFailedError.printStackTrace(printWriter);
                printWriter.close();
                charArrayWriter.close();
                testsuite.add(newFailedTestcase(failedTest.test.getClass().getName(), name + ": " + description, failedTest.duration, failedTest.assertionFailedError.getMessage(), charArrayWriter.toString()));
            }
        }
        if (!errorTests.isEmpty()) {
            Iterator errorTestsIterator = errorTests.iterator();
            while (errorTestsIterator.hasNext()) {
                FullTestResult.ErrorTest errorTest = (FullTestResult.ErrorTest) errorTestsIterator.next();
                totalDuration += errorTest.duration;
                final String name = errorTest.test.toString();
                final String description = (String) TestConstants.DESCRIPTIONS.get(name);
                CharArrayWriter charArrayWriter = new CharArrayWriter();
                PrintWriter printWriter = new PrintWriter(charArrayWriter, true);
                errorTest.throwable.printStackTrace(printWriter);
                printWriter.close();
                charArrayWriter.close();
                System.out.println("charArrayWriter.toString()=" + charArrayWriter.toString());
                testsuite.add(newErrorTestcase(errorTest.test.getClass().getName(), name + ": " + description, errorTest.duration, errorTest.throwable.getMessage(), charArrayWriter.toString()));
            }
        }
        // total time of all tests
        testsuite.addAttribute("time", Float.toString(totalDuration / 1000f));
    }
    String logContent = null;
    final String logName = (String) session.getAttribute(TestConstants.ATTRIBUTE_LOG_NAME);
    if (logName != null) {
        try {
            logContent = TestLogController.readLog(logName);
        } catch (final Throwable th) {
            log("Error reading log file", th);
        }
    }
    testsuite.add(new DOMElement("system-out").addCDATA((logContent != null) ? logContent : ""));
    testsuite.add(new DOMElement("system-err").addCDATA(""));
    XMLWriter outputter = new XMLWriter(response.getWriter(), OutputFormat.createPrettyPrint());
    try {
        outputter.write(testsuite);
        outputter.close();
    } catch (IOException e) {
        throw new ServletException(e);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) DOMDocument(org.dom4j.dom.DOMDocument) IOException(java.io.IOException) DOMElement(org.dom4j.dom.DOMElement) XMLWriter(org.dom4j.io.XMLWriter) Date(java.util.Date) CharArrayWriter(java.io.CharArrayWriter) ServletException(javax.servlet.ServletException) Iterator(java.util.Iterator) List(java.util.List) PrintWriter(java.io.PrintWriter)

Aggregations

DOMDocument (org.dom4j.dom.DOMDocument)13 Document (org.dom4j.Document)8 DOMElement (org.dom4j.dom.DOMElement)8 IOException (java.io.IOException)5 Element (org.dom4j.Element)3 CharArrayWriter (java.io.CharArrayWriter)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 List (java.util.List)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 StreamResult (javax.xml.transform.stream.StreamResult)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 ParseErrorException (org.apache.velocity.exception.ParseErrorException)2 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)2 DocumentSource (org.dom4j.io.DocumentSource)2 OutputFormat (org.dom4j.io.OutputFormat)2