Search in sources :

Example 46 with Transformer

use of javax.xml.transform.Transformer in project jersey by jersey.

the class SourceEntityProviderTest method extractContent.

private static String extractContent(Source source) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    // identity transformation
    Transformer transformer = transFactory.newTransformer();
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 47 with Transformer

use of javax.xml.transform.Transformer in project AndroidAsync by koush.

the class DocumentBody method prepare.

private void prepare() {
    if (bout != null)
        return;
    try {
        DOMSource source = new DOMSource(document);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        bout = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(bout, Charsets.UTF_8);
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        writer.flush();
    } catch (Exception 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) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 48 with Transformer

use of javax.xml.transform.Transformer in project jOOQ by jOOQ.

the class XMLDatabase method info.

private InformationSchema info() {
    if (info == null) {
        String xml = getProperties().getProperty(P_XML_FILE);
        String xsl = getProperties().getProperty(P_XSL_FILE);
        InputStream xmlIs = null;
        InputStream xslIs = null;
        log.info("Using XML file", xml);
        try {
            xmlIs = XMLDatabase.class.getResourceAsStream(xml);
            if (xmlIs == null)
                xmlIs = new FileInputStream(xml);
            if (StringUtils.isBlank(xsl)) {
                info = JAXB.unmarshal(new File(xml), InformationSchema.class);
            } else {
                log.info("Using XSL file", xsl);
                xslIs = XMLDatabase.class.getResourceAsStream(xsl);
                if (xslIs == null)
                    xslIs = new FileInputStream(xsl);
                try {
                    StringWriter writer = new StringWriter();
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xslIs));
                    transformer.transform(new StreamSource(xmlIs), new StreamResult(writer));
                    info = JAXB.unmarshal(new StringReader(writer.getBuffer().toString()), InformationSchema.class);
                } catch (TransformerException e) {
                    throw new RuntimeException("Error while transforming XML file " + xml + " with XSL file " + xsl, e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Error while opening files " + xml + " or " + xsl, e);
        } finally {
            if (xmlIs != null) {
                try {
                    xmlIs.close();
                } catch (Exception ignore) {
                }
            }
            if (xslIs != null) {
                try {
                    xslIs.close();
                } catch (Exception ignore) {
                }
            }
        }
    }
    return info;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 49 with Transformer

use of javax.xml.transform.Transformer 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 50 with Transformer

use of javax.xml.transform.Transformer in project Mycat-Server by MyCATApache.

the class FirewallConfig method updateToFile.

public static synchronized void updateToFile(String host, List<UserConfig> userConfigs) throws Exception {
    LOGGER.debug("set white host:" + host + "user:" + userConfigs);
    String filename = SystemConfig.getHomePath() + File.separator + "conf" + File.separator + "server.xml";
    //String filename = "E:\\MyProject\\Mycat-Server\\src\\main\\resources\\server.xml";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new IgnoreDTDEntityResolver());
    Document xmldoc = builder.parse(filename);
    Element whitehost = (Element) xmldoc.getElementsByTagName("whitehost").item(0);
    Element firewall = (Element) xmldoc.getElementsByTagName("firewall").item(0);
    if (firewall == null) {
        firewall = xmldoc.createElement("firewall");
        Element root = xmldoc.getDocumentElement();
        root.appendChild(firewall);
        if (whitehost == null) {
            whitehost = xmldoc.createElement("whitehost");
            firewall.appendChild(whitehost);
        }
    }
    for (UserConfig userConfig : userConfigs) {
        String user = userConfig.getName();
        Element hostEle = xmldoc.createElement("host");
        hostEle.setAttribute("host", host);
        hostEle.setAttribute("user", user);
        whitehost.appendChild(hostEle);
    }
    TransformerFactory factory2 = TransformerFactory.newInstance();
    Transformer former = factory2.newTransformer();
    String systemId = xmldoc.getDoctype().getSystemId();
    if (systemId != null) {
        former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, systemId);
    }
    former.transform(new DOMSource(xmldoc), new StreamResult(new File(filename)));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

Transformer (javax.xml.transform.Transformer)449 StreamResult (javax.xml.transform.stream.StreamResult)354 DOMSource (javax.xml.transform.dom.DOMSource)272 TransformerFactory (javax.xml.transform.TransformerFactory)222 TransformerException (javax.xml.transform.TransformerException)175 StringWriter (java.io.StringWriter)153 Document (org.w3c.dom.Document)114 IOException (java.io.IOException)105 StreamSource (javax.xml.transform.stream.StreamSource)98 Source (javax.xml.transform.Source)97 DocumentBuilder (javax.xml.parsers.DocumentBuilder)70 File (java.io.File)66 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)64 SAXException (org.xml.sax.SAXException)62 Element (org.w3c.dom.Element)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)57 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 StringReader (java.io.StringReader)52 Result (javax.xml.transform.Result)52