Search in sources :

Example 26 with StreamResult

use of javax.xml.transform.stream.StreamResult 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 StreamResult

use of javax.xml.transform.stream.StreamResult 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 StreamResult

use of javax.xml.transform.stream.StreamResult 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 StreamResult

use of javax.xml.transform.stream.StreamResult 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 StreamResult

use of javax.xml.transform.stream.StreamResult 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

StreamResult (javax.xml.transform.stream.StreamResult)1328 DOMSource (javax.xml.transform.dom.DOMSource)881 Transformer (javax.xml.transform.Transformer)775 StringWriter (java.io.StringWriter)447 TransformerFactory (javax.xml.transform.TransformerFactory)445 Document (org.w3c.dom.Document)415 TransformerException (javax.xml.transform.TransformerException)366 ByteArrayOutputStream (java.io.ByteArrayOutputStream)337 DocumentBuilder (javax.xml.parsers.DocumentBuilder)332 ByteArrayInputStream (java.io.ByteArrayInputStream)288 IOException (java.io.IOException)266 InputStream (java.io.InputStream)249 Test (org.junit.Test)246 StreamSource (javax.xml.transform.stream.StreamSource)213 XMLStreamReader (javax.xml.stream.XMLStreamReader)185 Source (javax.xml.transform.Source)183 File (java.io.File)175 Element (org.w3c.dom.Element)157 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)149 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)149