Search in sources :

Example 31 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 32 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project tinker by Tencent.

the class JavaXmlUtil method saveDocument.

/**
     * save document
     *
     * @param document
     * @param outputFullFilename
     */
public static void saveDocument(final Document document, final String outputFullFilename) {
    OutputStream outputStream = null;
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, Constant.Encoding.UTF8);
        outputStream = new FileOutputStream(outputFullFilename);
        StreamResult result = new StreamResult(outputStream);
        transformer.transform(domSource, result);
    } catch (Exception e) {
        throw new JavaXmlUtilException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                throw new JavaXmlUtilException(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) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 33 with TransformerFactory

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

the class Launching method serializeDocumentInt.

/**
     * Serializes a XML document into a string - encoded in UTF8 format,
     * with platform line separators.
     *
     * @param doc document to serialize
     * @return the document as a string
     * @throws TransformerException if an unrecoverable error occurs during the serialization
     * @throws IOException if the encoding attempted to be used is not supported
     */
private static String serializeDocumentInt(Document doc) throws TransformerException, IOException {
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult outputTarget = new StreamResult(s);
    transformer.transform(source, outputTarget);
    //$NON-NLS-1$
    return s.toString("UTF8");
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 34 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project goci by EBISPOT.

the class ChromosomeRenderlet method render.

public void render(RenderletNexus nexus, C context, E entity) {
    int position = getPosition();
    int height = SVGCanvas.canvasHeight;
    int width = SVGCanvas.canvasWidth;
    double chromWidth = (double) width / 12;
    double chromHeight = (double) height / 2;
    double xCoordinate;
    double yCoordinate = 0;
    if (position < 12) {
        xCoordinate = position * chromWidth;
    } else {
        xCoordinate = (position - 12) * chromWidth;
        yCoordinate = (double) height / 2;
    }
    InputStream in = null;
    try {
        in = getSVGFile().openStream();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document chromSVG = db.parse(in);
        Element root = chromSVG.getDocumentElement();
        Element g = (Element) root.getElementsByTagName("g").item(0).cloneNode(true);
        setChromBands(g, nexus);
        StringBuilder builder = new StringBuilder();
        builder.append("translate(");
        builder.append(Double.toString(xCoordinate));
        builder.append(",");
        builder.append(Double.toString(yCoordinate));
        builder.append(")");
        g.setAttribute("transform", builder.toString());
        g.setAttribute("gwasname", getName());
        g.removeAttribute("title");
        SVGArea currentArea = new SVGArea(xCoordinate, yCoordinate, chromWidth, chromHeight, 0);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(g), new StreamResult(buffer));
        String str = buffer.toString();
        RenderingEvent<E> event = new RenderingEvent<E>(entity, str, currentArea, this);
        nexus.renderingEventOccurred(event);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (SAXException e) {
        throw new RuntimeException("Failed to read in template chromosome SVG from original resource", e);
    } catch (IOException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to render chromosome SVG", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // tried our best!
            }
        }
    }
}
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) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) RenderingEvent(uk.ac.ebi.spot.goci.pussycat.renderlet.RenderingEvent) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SVGArea(uk.ac.ebi.spot.goci.pussycat.layout.SVGArea) TransformerException(javax.xml.transform.TransformerException)

Example 35 with TransformerFactory

use of javax.xml.transform.TransformerFactory in project OpenAM by OpenRock.

the class ValidationErrorHandler method print.

/**
     * Prints a Node tree recursively.
     * @param node A DOM tree Node
     * @param encoding character encoding
     * @return An xml String representation of the DOM tree.
     */
public static String print(Node node, String encoding) {
    if (node == null) {
        return null;
    }
    try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        transformer.setOutputProperty("encoding", encoding);
        DOMSource source = new DOMSource(node);
        ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
        StreamResult result = new StreamResult(os);
        transformer.transform(source, result);
        return os.toString(encoding);
    } catch (Exception e) {
        return null;
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28