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 String marshaller(Object o, Class<?> T) {
    JAXBContext jc;/*  w ww.j a v a2s. co  m*/
    Marshaller marshaller;
    StringWriter writer = new StringWriter();
    try {
        jc = JAXBContext.newInstance(T);
        marshaller = jc.createMarshaller();
        marshaller.marshal(o, writer);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return writer.toString();

}

From source file:Main.java

/**
 * Print all running threads/*from  w  w w.j  av  a2s  .co m*/
 */
public static void printRunningThreads() {
    // Get the thread listing as a string using a StringWriter stream
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    listAllThreads(out);
    out.close();
    String threadListing = stringWriter.toString();
    System.out.println(threadListing);
}

From source file:Main.java

public static String transform(Source xsl, Document doc) {
    StringWriter writer = new StringWriter();
    try {/*from w  ww.  j  a  v  a2s.c  o m*/
        TransformerFactory.newInstance().newTransformer(xsl).transform(new DOMSource(doc),
                new StreamResult(writer));
    } catch (Exception e) {
    }
    return writer.toString();
}

From source file:Main.java

/**
 * This return exception as string.//w w  w  .j a v a 2s. com
 *
 * @param throwable Throwable
 * @return exception string
 */
public static String exceptionToString(Throwable throwable) {
    StringWriter errors = new StringWriter();
    throwable.printStackTrace(new PrintWriter(errors));
    return errors.toString();
}

From source file:Main.java

public static String toXml(Class className, Object object) {
    String strXml = "";
    StringWriter writer = null;/*from  www.ja  v  a2 s .  co  m*/
    try {
        writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(className);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(object, writer);
        strXml = writer.toString();
        writer.flush();

        strXml = strXml.replace("&lt;", "<");
        strXml = strXml.replace("&gt;", ">");
    } catch (Exception e) {
    } finally {
        if (writer != null) {
            try {
                writer.close();
                writer = null;
            } catch (Exception e) {
            }
        }
    }
    return strXml;
}

From source file:Main.java

public static String xmlToString(Node node) {
    try {// ww w  .j  av  a 2  s .  c  o m
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static <T> String obj2Xml(T c) throws RuntimeException {
    String returnXml = "";
    Writer writer = null;/*from w  w w .  j av a 2s  .  co m*/
    try {
        writer = new StringWriter();
        JAXBContext.newInstance(c.getClass()).createMarshaller().marshal(c, writer);
        returnXml = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return returnXml;
}

From source file:Main.java

/**
 * Get the Stack trace from throwable instance.
 *
 * @param t Exception to be print.//from  w ww  . java 2  s.  com
 * @return String formed stack Trace.
 */
public static String getStack(Throwable t) {
    if (t == null)
        return null;
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

public static String documentToString(Node document) {
    try {/*  ww  w. j  av a  2 s . c  om*/
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString().replace("\r\n", "\n");
    } catch (TransformerException e) {
        throw new IllegalAccessError("Couldn't transform document to string");
    }
}

From source file:Main.java

/**
 * get stack trace as string//  ww  w.  j a v a  2  s  .  c  o  m
 * 
 * @param traceElements
 *            - stack trace elements
 * @return built string
 */
public static String getStackTrace(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    String string = sw.toString();
    return string;
}