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

public static void error(Component parent, Exception e, String title) {
    StringWriter out = new StringWriter();
    e.printStackTrace(new PrintWriter(out));
    out.flush();/*ww  w  . j  av  a2  s. co m*/
    error(parent, out.getBuffer().toString(), title);
}

From source file:Main.java

public static String returnDocAsString(Document doc) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tfFac = TransformerFactory.newInstance();
    // use null trandformation
    Transformer tf = tfFac.newTransformer();
    tf.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static String saveObjectToXmlString(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    final StringWriter writer = new StringWriter();
    m.marshal(obj, writer);//from ww  w  . j a v a  2 s.  c o m

    return writer.toString();
}

From source file:Main.java

public static String documentToString(Document doc) {
    StreamResult sr = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    try {/*w w w  .ja va 2  s.  c om*/
        _transformer.transform(source, sr);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sr.getWriter().toString();
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {/*  ww  w.  j a  va2  s .c o  m*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
    return sw.toString();
}

From source file:Main.java

public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {//from  w  ww  .  j  a v  a2s . c o m
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.err.println("nodeToString Transformer Exception");
    }
    return sw.toString();
}

From source file:Main.java

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

From source file:Main.java

public static String domToText(Document inDocument) throws TransformerException {

    StringWriter output = new StringWriter();
    DOMSource source = new DOMSource(inDocument);
    StreamResult result = new StreamResult(output);
    transformer.transform(source, result);
    //this.getWatcher().setProgressText("saved " + getEditedFile().getAbsolutePath());

    return output.toString();

}

From source file:Main.java

private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {//from   w  ww .  j a  va2  s.com
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
        StringWriter writer = new StringWriter();

        char[] chars = new char[256];
        int count = 0;
        while ((count = reader.read(chars)) > 0) {
            writer.write(chars, 0, count);
        }

        return writer.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:Main.java

public static String toStringFromDoc(Element elem) {
    String result = null;//from w ww  .  j a  va 2 s  .  c o m

    StringWriter strWtr = new StringWriter();
    StreamResult strResult = new StreamResult(strWtr);
    TransformerFactory tfac = TransformerFactory.newInstance();
    try {
        javax.xml.transform.Transformer t = tfac.newTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
        // text
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(new DOMSource(elem), strResult);
    } catch (Exception e) {
        System.err.println("XML.toString(Document): " + e);
    }
    result = strResult.getWriter().toString();
    try {
        strWtr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}