Search in sources :

Example 26 with Transformer

use of javax.xml.transform.Transformer in project rhino by PLOS.

the class AbstractXpathReader method recoverXml.

private static String recoverXml(Node node) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(outputStream));
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
    // TODO: Encoding?
    return new String(outputStream.toByteArray());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 27 with Transformer

use of javax.xml.transform.Transformer in project rhino by PLOS.

the class AuthorsXmlExtractor method getAsXMLString.

private static String getAsXMLString(Node node) throws TransformerException {
    final Transformer tf = TransformerFactory.newInstance().newTransformer();
    final StringWriter stringWriter = new StringWriter();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.transform(new DOMSource(node), new StreamResult(stringWriter));
    return stringWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult)

Example 28 with Transformer

use of javax.xml.transform.Transformer in project ACS by ACS-Community.

the class ExtraDataFeatureUtil method getExtraDataMap.

public String getExtraDataMap(DAOOperations dao, String path, Set<String> expectedAttributes, Set<String> expectedElements) throws CDBFieldDoesNotExistEx, WrongCDBDataTypeEx, ParserConfigurationException, TransformerException {
    Document xmldoc;
    DocumentBuilder builder = documentBuilderObjectPool.borrowObject();
    try {
        DOMImplementation impl = builder.getDOMImplementation();
        xmldoc = impl.createDocument(null, "data", null);
    } finally {
        documentBuilderObjectPool.returnObject(builder);
    }
    Element root = xmldoc.getDocumentElement();
    if (getExtraDataMap(root, dao, path, expectedAttributes, expectedElements)) {
        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.transform(new DOMSource(xmldoc), streamResult);
        String ret = stringWriter.toString();
        // Oracle XMLTYPE attributes don't like empty XML, thus we convert it to null
        if (ret != null && ret.trim().isEmpty()) {
            ret = null;
        }
        return ret;
    } else {
        return null;
    }
}
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) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) Document(org.w3c.dom.Document)

Example 29 with Transformer

use of javax.xml.transform.Transformer in project ACS by ACS-Community.

the class DumpXML method main.

/**
	 * @param args
	 */
public static void main(String[] args) throws Throwable {
    if (args.length != 1) {
        System.err.println("Usage: java " + DumpXML.class.getName() + " <xml file>");
        System.exit(1);
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    // XercesJ 2.9.1 rejects this, it supports it by default
    //factory.setXIncludeAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xmldoc = builder.parse(new InputSource(new FileReader(args[0])));
    Source src = new DOMSource(xmldoc);
    Result dest = new StreamResult(System.out);
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer transformer = tranFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(src, dest);
}
Also used : InputSource(org.xml.sax.InputSource) 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) FileReader(java.io.FileReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 30 with Transformer

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

the class ReportGenerator method stringToHtml.

public static String stringToHtml(String inxml, String infilexsl) {
    Document doc = null;
    // factory.setNamespaceAware(true);
    // factory.setValidating(true);
    File stylesheet = null;
    StringReader inReader = new StringReader(inxml);
    StringWriter writer = new StringWriter();
    try {
        stylesheet = new File(infilexsl);
        DocumentBuilder builder = XmlUtils.newXxeDisabledDocumentBuilderFactory().newDocumentBuilder();
        doc = builder.parse(new InputSource(inReader));
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
    } catch (TransformerException | SAXException | ParserConfigurationException | IOException e) {
        showDialogForGUI();
        logger.error(e.getMessage(), e);
    } finally {
    }
    // we should really adopt something other than XSLT ;)
    return writer.toString().replace("&lt;p&gt;", "<p>").replace("&lt;/p&gt;", "</p>");
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Transformer (javax.xml.transform.Transformer)1034 StreamResult (javax.xml.transform.stream.StreamResult)799 DOMSource (javax.xml.transform.dom.DOMSource)644 TransformerFactory (javax.xml.transform.TransformerFactory)511 TransformerException (javax.xml.transform.TransformerException)398 StringWriter (java.io.StringWriter)322 IOException (java.io.IOException)264 Document (org.w3c.dom.Document)260 StreamSource (javax.xml.transform.stream.StreamSource)204 Source (javax.xml.transform.Source)188 File (java.io.File)162 DocumentBuilder (javax.xml.parsers.DocumentBuilder)149 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)145 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)142 Element (org.w3c.dom.Element)136 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)124 SAXException (org.xml.sax.SAXException)112 StringReader (java.io.StringReader)108 Result (javax.xml.transform.Result)106 ByteArrayOutputStream (java.io.ByteArrayOutputStream)104