Search in sources :

Example 16 with TransformerFactory

use of javax.xml.transform.TransformerFactory 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)

Example 17 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project OpenAttestation by OpenAttestation.

the class ConverterUtil method formateXMLString.

public static String formateXMLString(String inputXML) {
    StreamResult xmlOutput = null;
    try {
        Source xmlInput = new StreamSource(new StringReader(inputXML));
        StringWriter stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(xmlInput, xmlOutput);
    } catch (Exception e) {
        // simple exception handling, please review it
        throw new RuntimeException(e);
    }
    return xmlOutput.getWriter().toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 18 with TransformerFactory

use of javax.xml.transform.TransformerFactory 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 19 with TransformerFactory

use of javax.xml.transform.TransformerFactory 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 20 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project android-selector-intellij-plugin by importre.

the class AndroidSelectorDialog method createDrawableV21.

private void createDrawableV21(String filename, String color, String pressed) throws Exception {
    VirtualFile child = dir.findChild(drawableV21Dir);
    if (child == null) {
        child = dir.createChildDirectory(null, drawableV21Dir);
    }
    VirtualFile newXmlFile = child.findChild(filename);
    if (newXmlFile != null && newXmlFile.exists()) {
        newXmlFile.delete(null);
    }
    newXmlFile = child.createChildData(null, filename);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element root = doc.createElement("ripple");
    root.setAttributeNS(nsUri, "xmlns:android", androidUri);
    root.setAttribute("android:color", pressed);
    doc.appendChild(root);
    Element item = doc.createElement("item");
    item.setAttribute("android:drawable", color);
    root.appendChild(item);
    OutputStream os = newXmlFile.getOutputStream(null);
    PrintWriter out = new PrintWriter(os);
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(INDENT_SPACE, "4");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    out.println(writer.getBuffer().toString());
    out.close();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) 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)

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