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.dianping.lion.util.ThrowableUtils.java

public static String extractStackTrace(Throwable t, int maxLen) {
    StringWriter me = new StringWriter();
    PrintWriter pw = new PrintWriter(me);
    t.printStackTrace(pw);//from   w  w w . j av a 2 s  .c o  m
    pw.flush();
    return StringUtils.substring(me.toString(), 0, maxLen);
}

From source file:Main.java

/** Return a <code>String</code> containing an XML file in serialized form.
 *  @param  doc  the <code>Document</code> object for the XML file to list
 *  @return (<code>String</code>) the XML file in serialized form
 *//*from www .j av  a2 s  .  com*/
public static String getXMLListing(Document doc) {
    // make a serializable version of the XML Document object
    // reference:  www-106.ibm.com/developerworks/xml/library/x-injava2/?dwzone=xml
    OutputFormat of = new OutputFormat(doc);
    of.setIndenting(true); // setIndenting must preceed setIndent
    of.setIndent(2);
    of.setLineSeparator(System.getProperty("line.separator"));
    // of.setPreserveSpace( true ) ;
    StringWriter sw = new StringWriter();
    // use a StringWriter instead of serializing directly to the 
    //  ObjectOutputStream because serializing directly yields a 
    //  java.io.OptionalDataException when reading the data with 
    //  ObjectInputStream.readObject()
    XMLSerializer xmlser = new XMLSerializer(sw, of);
    try {
        xmlser.serialize(doc);
    } catch (IOException ioe) {
        return "<p>IOException Error in getXMLListing( Document ):<br/>" + "&nbsp;&nbsp;&nbsp;"
                + ioe.toString();
    }

    return sw.toString();
}

From source file:Main.java

static public String outputTextDoc(Node outputNode) {
    StringWriter out = new StringWriter();
    PrintWriter pOut = new PrintWriter(out);
    outputNode(outputNode, pOut, 0);// w  ww  .j a va2 s  .co  m
    return (out.toString());
}

From source file:Main.java

/**
 * Gets the node as string./*from w w w  .j a  v a  2s  .c o  m*/
 *
 * @param pNode
 *            the p node
 * @param encoding
 *            the encoding
 * @param pRemoveXmlLine
 *            the p remove xml line
 * @return the node as string
 * @throws Exception
 *             the exception
 */
public static String getNodeAsString(Node pNode, String encoding, boolean pRemoveXmlLine) throws Exception {
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformNode(pNode, encoding, pRemoveXmlLine, result);
    return writer.toString();
}

From source file:Main.java

public static String DocToString(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    if (doc == null)
        return new String("");
    String xmlString = "";
    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());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    xmlString = result.getWriter().toString();

    return xmlString;
}

From source file:Main.java

public static String prettyPrint(File file) {
    Transformer tf;/*  w  w w . j  a va 2  s .  c  o m*/
    try {
        tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult stringResult = new StreamResult(new StringWriter());
        tf.transform(new DOMSource(convertFileToDom(file)), stringResult);
        return stringResult.getWriter().toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String buildXml(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    StringWriter writer = new StringWriter();

    buildXml(node, writer);//w  w  w  .  ja v a2 s.com
    return writer.toString();
}

From source file:Main.java

/**
 * Convert XML Node to String//ww w .  j  ava2  s.  c om
 *
 * @param node
 *            the node to convert
 * @return the String equivalent of the node
 * @throws TransformerException
 */
public static String nodeToString(final Node node) throws TransformerException {
    final Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tf.setOutputProperty(OutputKeys.INDENT, "yes");
    final Writer out = new StringWriter();
    tf.transform(new DOMSource(node), new StreamResult(out));
    return out.toString();
}

From source file:Main.java

/** 
 * Serialized a node to a string. XML declarations will be omitted. 
 * /* w w w.jav a2s . co m*/
 * @param node The node to be serialized.
 * 
 * @return The serialized node.
 * 
 * @throws TransformerFactoryConfigurationError
 * @throws TransformerException
 */
public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * @param thread a thread/*w  w w.  j  a va2s .  c o  m*/
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}