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:cc.sion.core.utils.Exceptions.java

/**
 * ErrorStackString.//from   www.j ava 2  s  .c  o m
 */
public static String getStackTraceAsString(Throwable ex) {
    StringWriter stringWriter = new StringWriter();
    ex.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:Main.java

public static String getStringFromFile(String pathUrlName, boolean encode) {
    Writer writer = null;//from  ww w  .ja va  2 s  .co  m
    try {
        InputStream is = new FileInputStream(new File(pathUrlName));

        writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader;
            if (encode) {
                reader = new BufferedReader(new InputStreamReader(is, HTML_ENCODING));
            } else {
                reader = new BufferedReader(new InputStreamReader(is));
            }
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:com.bootcamp.utils.FreeMarkers.java

public static String renderTemplate(Template template, Object model) {
    try {/*from  w w w  .j av  a 2 s.c  o  m*/
        StringWriter result = new StringWriter();
        template.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

/**
 * Converts an XML document to a formatted XML string.
 * //w w  w  .ja va 2 s  .  co m
 * @param doc The document to format.
 * @return Formatted XML document.
 */
public static String toString(Document doc) {
    if (doc == null) {
        return "";
    }

    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

/**
 * Convert the document to an array of bytes.
 *
 * @param doc The XML document./*w w  w  . j a  va2s .  c  o  m*/
 * @param encoding The encoding of the output data.
 *
 * @return The XML document as an array of bytes.
 *
 * @throws TransformerException If there is an error transforming to text.
 */
public static byte[] asByteArray(Document doc, String encoding) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return writer.getBuffer().toString().getBytes();
}

From source file:Main.java

public static String toXmlString(Document document) {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    Transformer transformer;//from   ww  w. jav  a  2s.co  m
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document.getLastChild()), result);
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}

From source file:Main.java

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();
    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));
    return sw.toString();
}

From source file:com.ontotext.s4.multiThreadRequest.utils.Util.java

public static String convertSreamToString(InputStream inputStream) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, UTF_8_ENCODING);
    String theString = writer.toString();
    return theString;
}

From source file:Main.java

/**
 * Serialize an XML document into a String (for debug purpose only!). the
 * returned String could contains an error message instead if a problem as
 * occurred during the serialization. This method is intended for debug /
 * trace purpose because you really don't need to serialize XML at all in
 * your code unless your planing to output it to a file. In this case, use
 * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
 *//*from   w ww.ja  va 2s  .  c  o m*/
public static String dumpXml(Document document) {
    TransformerFactory factory = TransformerFactory.newInstance();
    try {
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        return "XML dump failed: " + e.getMessage();
    }
}

From source file:com.tera.common.util.ExceptionUtil.java

/**
 * Simplified version of getting stacktracer from {@link Throwable} For
 * advanced use - see {@link org.apache.commons.lang.exception.ExceptionUtils}
 * // ww w.  ja  va 2 s  . com
 * @param throwable
 * @return
 */
public static String getStackTrace(Throwable throwable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    throwable.printStackTrace(printWriter);
    return result.toString();
}