Search in sources :

Example 21 with DOMSource

use of javax.xml.transform.dom.DOMSource in project groovy by apache.

the class XmlUtil method serialize.

/**
     * Return a pretty String version of the Element.
     *
     * @param element the Element to serialize
     * @return the pretty String representation of the Element
     */
public static String serialize(Element element) {
    StringWriter sw = new StringWriter();
    serialize(new DOMSource(element), sw);
    return sw.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource)

Example 22 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 23 with DOMSource

use of javax.xml.transform.dom.DOMSource 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 24 with DOMSource

use of javax.xml.transform.dom.DOMSource in project hive by apache.

the class GenHiveTemplate method writeToFile.

private void writeToFile(File template, Document document) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(template);
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult)

Example 25 with DOMSource

use of javax.xml.transform.dom.DOMSource in project tomcat by apache.

the class DefaultServlet method secureXslt.

private Source secureXslt(InputStream is) {
    // Need to filter out any external entities
    Source result = null;
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(secureEntityResolver);
        Document document = builder.parse(is);
        result = new DOMSource(document);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        if (debug > 0) {
            log(e.getMessage(), e);
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            // Ignore
            }
        }
    }
    return result;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXException(org.xml.sax.SAXException)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35