Example usage for javax.xml.transform.stream StreamResult getWriter

List of usage examples for javax.xml.transform.stream StreamResult getWriter

Introduction

In this page you can find the example usage for javax.xml.transform.stream StreamResult getWriter.

Prototype

public Writer getWriter() 

Source Link

Document

Get the character stream that was set with setWriter.

Usage

From source file:org.apache.juddi.adminconsole.hub.JUDDIRequestsAsXML.java

private static String PrettyPrintXML(String input) {
    if (input == null || input.length() == 0) {
        return "";
    }/*from  www . ja  v a2 s. c o  m*/
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        StreamSource source = new StreamSource(new StringReader(input.trim()));
        transformer.transform(source, result);
        String xmlString = result.getWriter().toString();
        return (xmlString);
    } catch (Exception ex) {
    }
    return null;
}

From source file:org.apache.juddi.adminconsole.hub.UddiAdminHub.java

private static String PrettyPrintXML(String input) {
    try {//from  w  ww .j  av  a2s. c o  m
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        StreamSource source = new StreamSource(new StringReader(input));
        transformer.transform(source, result);
        String xmlString = result.getWriter().toString();
        return (xmlString);
    } catch (Exception ex) {
    }
    return null;
}

From source file:org.apache.olingo.odata2.core.debug.DebugInfoBody.java

private String formatXml(final String xml) throws IOException {
    try {/*  w w  w . j a v  a  2  s.c  o m*/
        final TransformerFactory transformerFactory = XmlHelper.getTransformerFactory();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StreamResult outputTarget = new StreamResult(new StringWriter());
        transformer.transform(new StreamSource(new StringReader(xml)), outputTarget);
        return outputTarget.getWriter().toString();
    } catch (final TransformerException e) {
        return xml;
    }
}

From source file:org.apache.solr.common.cloud.SolrZkClient.java

public static String prettyPrint(String input, int indent) {
    try {//  w  w  w  .  ja  va2 s.  c om
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException("Problem pretty printing XML", e);
    }
}

From source file:org.apache.sysml.conf.DMLConfig.java

public synchronized String serializeDMLConfig() throws DMLRuntimeException {
    String ret = null;//from   w w w .  j  av a2 s  . c o m
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(_xmlRoot);
        transformer.transform(source, result);
        ret = result.getWriter().toString();
    } catch (Exception ex) {
        throw new DMLRuntimeException("Unable to serialize DML config.", ex);
    }

    return ret;
}

From source file:org.automagic.deps.doctor.Utils.java

public static Node prettyFormat(Node node, int indentSize, boolean indentWithTabs) {

    if (indentSize < 1) {
        throw new IllegalArgumentException("Indentation size must be greater or equal to 1");
    }//from  ww w .j  a  v a2  s  .co m

    try {
        Source xmlInput = new DOMSource(node);

        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indentSize);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        String result = xmlOutput.getWriter().toString();

        if (indentWithTabs) {
            StringBuilder sb = new StringBuilder(result);
            Matcher matcher = INDENT_PATTERN.matcher(result);
            while (matcher.find()) {
                sb.setCharAt(matcher.start(), '\t');
            }
            result = sb.toString();
        }

        Document parse = DOC_BUILDER.get().parse(new ByteArrayInputStream(result.getBytes()));
        Node importNode = node.getOwnerDocument().importNode(parse.getDocumentElement(), true);

        return importNode;

    } catch (TransformerException | SAXException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.belio.service.gateway.SafcomGateway.java

/**
 * Method used to print the SOAP Response
 *///  ww  w .j  a  v a2  s . c om
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    Source sourceContent = soapResponse.getSOAPPart().getContent();
    // System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(sourceContent, result);
    String xmlString = result.getWriter().toString();
    Launcher.LOG.info(xmlString);
}

From source file:org.broad.igv.util.Utilities.java

public static String getString(Document document) {
    StreamResult streamResult;
    try {// w ww.j  av  a 2  s  . c om

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        streamResult = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(document);
        transformer.transform(source, streamResult);
    } catch (TransformerException e) {
        return null;
    }

    return streamResult.getWriter().toString();
}

From source file:org.codice.ddf.catalog.transformer.html.RecordViewHelpers.java

public CharSequence buildMetadata(String metadata, Options options) {
    if (metadata == null) {
        return "";
    }//from www.j  a  v  a  2s .com
    try {
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        StreamResult result = new StreamResult(new StringWriter());

        transformer.transform(new DOMSource(builder.parse(new InputSource(new StringReader(metadata)))),
                result);
        StringBuilder sb = new StringBuilder();
        sb.append("<pre>").append(escapeHtml(result.getWriter().toString())).append("</pre>");
        return new Handlebars.SafeString(sb.toString());
    } catch (TransformerConfigurationException e) {
        LOGGER.warn("Failed to convert metadata to a pretty string", e);
    } catch (TransformerException e) {
        LOGGER.warn("Failed to convert metadata to a pretty string", e);
    } catch (SAXException e) {
        LOGGER.warn("Failed to convert metadata to a pretty string", e);
    } catch (ParserConfigurationException e) {
        LOGGER.warn("Failed to convert metadata to a pretty string", e);
    } catch (IOException e) {
        LOGGER.warn("Failed to convert metadata to a pretty string", e);
    }
    return metadata;
}

From source file:org.dhatim.delivery.AbstractParser.java

protected Writer getWriter(Result result, ExecutionContext executionContext) {
    if (!(result instanceof StreamResult)) {
        return new NullWriter();
    }//w w w  . j  a v a  2 s .  c om

    StreamResult streamResult = (StreamResult) result;
    if (streamResult.getWriter() != null) {
        return streamResult.getWriter();
    } else if (streamResult.getOutputStream() != null) {
        try {
            if (executionContext != null) {
                return new OutputStreamWriter(streamResult.getOutputStream(),
                        executionContext.getContentEncoding());
            } else {
                return new OutputStreamWriter(streamResult.getOutputStream(), "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            throw new SmooksException("Unable to encode output stream.", e);
        }
    } else {
        throw new SmooksException(
                "Invalid " + StreamResult.class.getName() + ".  No OutputStream or Writer instance.");
    }
}