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.zaproxy.zap.extension.saml.SAMLMessage.java

/**
 * Get the saml message as a formatted XML
 *
 * @return/*from  www. j av  a 2  s  .  c o m*/
 */
public String getSamlMessageString() {
    try {
        Source xmlInput;
        xmlInput = new StreamSource(new StringReader(samlMessageString));

        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);
        Transformer transformer = transformerFactory.newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        log.warn("error in parsing saml message.", e);
        return samlMessageString;
    }
}

From source file:ru.codeinside.gws3572c.GMPClientSignTest.java

private static String convertElementToString(Element signElement) {
    Transformer transformer;//from w  ww .jav  a 2s .c om
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        //            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(signElement);
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (Exception e) {
        return null;
    }
}

From source file:specminers.referenceparser.graphvizdot.PradelsDotFilesToJffConverter.java

public String getAsJffFormat() throws ParserConfigurationException, IOException, TransformerException {
    Document jff = getJffXMLDocument();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(jff);
    transformer.transform(source, result);

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

    return xmlString;
}

From source file:telecom.sudparis.eu.paas.client.APIClient.java

private static String prettyFormat(String input, int indent) {
    try {/*  ww w .  ja v  a2  s . co m*/
        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.setOutputProperty(OutputKeys.STANDALONE, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:uk.bl.wa.indexer.WARCIndexerCommand.java

public static void prettyPrintXML(String doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    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(doc));
    transformer.transform(source, result);
    String xmlString = result.getWriter().toString();
    System.out.println(xmlString);
}

From source file:uk.me.jeffsutton.pojogen.SimplePOJO.java

public String pretyFormat(String src) {
    try {//  w w w .  j  a  v  a2s . c  om
        Source xmlInput = new StreamSource(new StringReader(src));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        throw new RuntimeException(e); // simple exception handling, please review it
    }
}

From source file:xmlconverter.controller.logic.GetFileCount.java

/**
 *
 * Writes Document object to specified path as file
 *
 * @param doc//w ww. ja  va  2s.  c om
 * @param path
 * @throws TransformerException
 * @throws IOException
 * @throws DOMException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private void writeDocument(Document doc, File path)
        throws TransformerException, IOException, DOMException, ParserConfigurationException, SAXException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(source, result);

    File toCheck = path;
    if (!path.getAbsolutePath().endsWith(".xml")) {
        toCheck = new File(path.getAbsolutePath() + slash + path.getName() + ".xml");
    }

    try (FileOutputStream xmlfile = new FileOutputStream(toCheck);) {
        if (toCheck.exists()) {
            xmlfile.write(result.getWriter().toString().getBytes());
        } else {
            toCheck.createNewFile();
            xmlfile.write(result.getWriter().toString().getBytes());
        }
    }
}