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:com.github.triceo.robozonky.notifications.email.AbstractEmailingListener.java

protected static String stackTraceToString(final Throwable t) {
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);/*  w w  w .j  a  v a  2s  .  co  m*/
    return sw.toString();
}

From source file:Main.java

/**
 * //  w w  w . j  a v  a2  s . c o m
 * @param object
 * @return
 * @throws JAXBException
 */
public static String pojoToXml(Object object) throws JAXBException {

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // marshaller.setProperty(Marshaller.JAXB_ENCODING, "U");
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    String xmlData = writer.toString();
    return xmlData;
}

From source file:net.fizzl.redditengine.data.LinkListing.java

public static LinkListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:Main.java

public static String getXMLStringFromDocument(Document doc) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {/*from  w  ww .  j ava 2s . co m*/
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    }
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
    } catch (TransformerException e) {
        throw new RuntimeException("Error while transforming XML document to String : ", e);
    }
    String output = writer.getBuffer().toString();
    return output;
}

From source file:Utilities.java

/**
 * Return the stack trace from the passed exception as a string
 *
 * @param  th  The exception to retrieve stack trace for.
 *//*from  www .j a v  a2  s .c o  m*/
public static String getStackTrace(Throwable th) {
    if (th == null) {
        throw new IllegalArgumentException("Throwable == null");
    }

    StringWriter sw = new StringWriter();
    try {
        PrintWriter pw = new PrintWriter(sw);
        try {
            th.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    } finally {
        try {
            sw.close();
        } catch (IOException ex) {

        }
    }
}

From source file:Main.java

public static String marshal(Object object) throws JAXBException {
    StringWriter writer = new StringWriter();
    marshal(null, object, writer, null);
    return writer.toString();
}

From source file:Main.java

/**
 * Converts DOM document to a string//  ww w .ja v  a2s .  co m
 *
 * @param XMLDocument the document you want to convert
 * @return  String representation of you XML document
 */
public static String XMLToString(Document XMLDocument) {
    String xmlString = null;
    try {
        // Convert DOM to XML string
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        //trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.transform(new DOMSource(XMLDocument), result);
        xmlString = sw.toString();
        sw.close();
    } catch (TransformerConfigurationException tcex) {

    } catch (TransformerException tex) {

    } catch (IOException iex) {

    }
    return xmlString;
}

From source file:Main.java

public static String getXMLString(Node doc) throws TransformerException {
    DOMSource domSource = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    return writer.toString();
}

From source file:Main.java

protected static Result internalTransform(Reader doc, Templates templates, Result r, boolean trace) {
    StringWriter sw = new StringWriter();

    try {/*  w  ww.  j  a  va  2 s  . c  o m*/
        Transformer transformer = templates.newTransformer();

        transformer.transform(new StreamSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }

}

From source file:Main.java

public static String toString(Node element) throws Exception {
    if (element == null) {
        return "null";
    }//ww  w . ja v a2  s.c  o  m
    Source source = new DOMSource(element);

    StringWriter stringWriter = new StringWriter();
    try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
        Result result = new StreamResult(printWriter);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    }
    return stringWriter.toString();
}