Example usage for java.io StringWriter StringWriter

List of usage examples for java.io StringWriter StringWriter

Introduction

In this page you can find the example usage for java.io StringWriter StringWriter.

Prototype

public StringWriter() 

Source Link

Document

Create a new string writer using the default initial string-buffer size.

Usage

From source file:Main.java

/**
 * Converts a dom to a String/*from   w  w  w. ja  v  a  2s . co  m*/
 *
 * @param dom dom to convert
 * @param outputProperties the properties for the String representation of
 * the XML
 * @return the dom as a String
 */
public static String writeDomToString(Document dom, Properties outputProperties) {
    try {
        StringWriter ret = new StringWriter();
        TransformerFactory transFact = TransformerFactory.newInstance();
        // transFact.setAttribute("indent-number", 2);
        Transformer transformer = transFact.newTransformer();
        if (outputProperties != null) {
            transformer.setOutputProperties(outputProperties);
        }
        DOMSource source = new DOMSource(dom);
        StreamResult result = new StreamResult(ret);
        transformer.transform(source, result);
        return ret.toString();
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom to string!", e);
    }
}

From source file:org.pathirage.freshet.utils.ExpressionSerde.java

public static String serialize(Expression expression) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    StringWriter sw = new StringWriter();
    objectMapper.writeValue(sw, expression);

    return sw.toString();
}

From source file:Main.java

public static String getFormattedXml(String xmlString) {

    // ///////////////////////////////////////////////////////////////
    //   Declarations
    // ///////////////////////////////////////////////////////////////

    Source xmlInput = null;//from www  . ja  va2s . c  o m
    StringWriter stringWriter = null;
    StreamResult xmlOutput = null;

    TransformerFactory transformerFactory = null;
    Transformer transformer = null;

    String formattedXml = null;

    // ///////////////////////////////////////////////////////////////
    //   Code
    // ///////////////////////////////////////////////////////////////

    try {

        xmlInput = new StreamSource(new StringReader(xmlString));
        stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);

        transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", 4);

        transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);

        formattedXml = xmlOutput.getWriter().toString();
    } catch (Exception e) {

        // To Do: Handle Exception..
    }

    return formattedXml;
}

From source file:Main.java

/**
 * src:http://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string
 * @param doc/*w w  w  .  j  av a2 s  .  co  m*/
 * @return
 */
public static String convertDocumentToString(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;
    try {
        transformer = tf.newTransformer();
        // below code to remove XML declaration
        // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String lsSerializePretty(Document doc) {
    DOMImplementation domImplementation = doc.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            lsSerializer.write(doc, lsOutput);
            return stringWriter.toString();
        } else {/*from w ww.  j  av  a 2s .c o m*/
            throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
        }
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:Main.java

public static String prettyPrintWithDOM3LS(Document document) {
    DOMImplementation domImplementation = document.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            lsSerializer.write(document, lsOutput);
            return stringWriter.toString();
        } else {/*from   ww  w . jav a2  s  .c  om*/
            throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
        }
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {/*from ww w  .  ja va 2  s.c o  m*/
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:helper.SerializationHelper.java

public static String serializeObject(Object object) {
    ObjectMapper mapper = new ObjectMapper();
    Writer strWriter = new StringWriter();
    try {/*from  ww w .  j  a va  2 s  .c  o  m*/
        mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        mapper.writeValue(strWriter, object);
    } catch (Exception e) {
        System.out.println(e);
    }
    return strWriter.toString();
}

From source file:org.uiautomation.ios.communication.Helper.java

public static JSONObject extractObject(HttpResponse resp) {
    String str = "";
    try {//  w  ww  .  ja  v  a2s. c o  m
        InputStream is = resp.getEntity().getContent();
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer, "UTF-8");

        str = writer.toString();
        return new JSONObject(str);
    } catch (Exception e) {
        throw new WebDriverException(str, e);
    }
}

From source file:Main.java

/**
 * Converts a document object to an xml string
 * @param document the document to convert
 * @param documentTransformer the DOM document transformer
 * @return the xml string/*  w  ww .j a v  a  2 s .  co  m*/
 */
public static String documentToString(Document document, Transformer documentTransformer)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    DOMSource source = new DOMSource(document);
    documentTransformer.transform(source, result);
    return sw.toString();
}