Search in sources :

Example 6 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project camel by apache.

the class XmlFixture method transform.

protected static Document transform(Document aDocument, String aResourcePath) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    InputStream in = XmlFixture.class.getResourceAsStream(aResourcePath);
    Source src = new StreamSource(in);
    src.setSystemId(XmlFixture.class.getResource(aResourcePath).toExternalForm());
    Transformer t = tf.newTransformer(src);
    DOMResult result = new DOMResult();
    t.transform(new DOMSource(aDocument), result);
    return (Document) result.getNode();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 7 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project hadoop by apache.

the class Configuration method writeXml.

/**
   * Write out the non-default properties in this configuration to the
   * given {@link Writer}.
   *
   * <li>
   * When property name is not empty and the property exists in the
   * configuration, this method writes the property and its attributes
   * to the {@link Writer}.
   * </li>
   * <p>
   *
   * <li>
   * When property name is null or empty, this method writes all the
   * configuration properties and their attributes to the {@link Writer}.
   * </li>
   * <p>
   *
   * <li>
   * When property name is not empty but the property doesn't exist in
   * the configuration, this method throws an {@link IllegalArgumentException}.
   * </li>
   * <p>
   * @param out the writer to write to.
   */
public void writeXml(String propertyName, Writer out) throws IOException, IllegalArgumentException {
    Document doc = asXmlDocument(propertyName);
    try {
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        // Important to not hold Configuration log while writing result, since
        // 'out' may be an HDFS stream which needs to lock this configuration
        // from another thread.
        transformer.transform(source, result);
    } catch (TransformerException te) {
        throw new IOException(te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 8 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project hadoop by apache.

the class TestQueueConfigurationParser method testQueueConfigurationParser.

/**
 * test xml generation 
 * @throws ParserConfigurationException
 * @throws Exception 
 */
@Test(timeout = 5000)
public void testQueueConfigurationParser() throws ParserConfigurationException, Exception {
    JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
    JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
    JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");
    info.addChild(infoChild1);
    info.addChild(infoChild2);
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
    Document document = builder.newDocument();
    // test QueueConfigurationParser.getQueueElement 
    Element e = QueueConfigurationParser.getQueueElement(document, info);
    // transform result to string for check
    DOMSource domSource = new DOMSource(e);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    String str = writer.toString();
    assertTrue(str.endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 9 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project cucumber-jvm by cucumber.

the class JUnitFormatter method done.

@Override
public void done() {
    try {
        // set up a transformer
        rootElement.setAttribute("name", JUnitFormatter.class.getName());
        rootElement.setAttribute("failures", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
        rootElement.setAttribute("skipped", String.valueOf(rootElement.getElementsByTagName("skipped").getLength()));
        rootElement.setAttribute("time", sumTimes(rootElement.getElementsByTagName("testcase")));
        if (rootElement.getElementsByTagName("testcase").getLength() == 0) {
            // to avoid failed Jenkins jobs
            addDummyTestCase();
        }
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(out);
        DOMSource source = new DOMSource(doc);
        trans.transform(source, result);
    } catch (TransformerException e) {
        throw new CucumberException("Error while transforming.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) CucumberException(cucumber.runtime.CucumberException) TransformerException(javax.xml.transform.TransformerException)

Example 10 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project che by eclipse.

the class BuildFileGenerator method documentToString.

/** Convert document to formatted XML string. */
private String documentToString(Document doc) throws TransformerException {
    StringWriter writer = new StringWriter();
    Source source = new DOMSource(doc);
    Result result = new StreamResult(writer);
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.setAttribute("indent-number", "4");
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
    return writer.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)188 Transformer (javax.xml.transform.Transformer)158 StreamResult (javax.xml.transform.stream.StreamResult)137 DOMSource (javax.xml.transform.dom.DOMSource)113 TransformerException (javax.xml.transform.TransformerException)63 StreamSource (javax.xml.transform.stream.StreamSource)60 StringWriter (java.io.StringWriter)58 Document (org.w3c.dom.Document)53 IOException (java.io.IOException)42 Source (javax.xml.transform.Source)41 DocumentBuilder (javax.xml.parsers.DocumentBuilder)37 File (java.io.File)36 Element (org.w3c.dom.Element)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)29 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)27 Result (javax.xml.transform.Result)24 SAXException (org.xml.sax.SAXException)24 StringReader (java.io.StringReader)23 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)23 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20