Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

In this page you can find the example usage for java.io StringWriter toString.

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:Main.java

public static String serializeXML(Node e) throws Exception {
    StringWriter result = new StringWriter();
    serializeXML(e, result);/*from w  w w  .  j  av  a2 s. c om*/
    return result.toString();
}

From source file:Main.java

/**
 * Converts an exception to a string.//from w ww  . j a va2 s  .  co  m
 * @param exception
 * @return
 */
public static String getExceptionStackTraceAsString(Exception exception) {
    StringWriter sw = new StringWriter();
    exception.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

public static <T> String generateObject2XMLString(T object) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(object, sw);/*from   www  .ja v  a  2 s  .c o  m*/

    return sw.toString();
}

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;/*from  w ww . jav a  2 s. c o m*/
    try {
        jc = JAXBContext.newInstance(xml.getClass());
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        m.marshal(xml, writer);
        return writer.toString();
    } catch (JAXBException e) {
        return "";
    }
}

From source file:Main.java

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

    buildXml(node, writer);//from   w w w.j  a v a2s. com
    return writer.toString();
}

From source file:Main.java

public static <T> String print(T xml, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);

    //Create marshaller
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(xml, writer);//from  w  w  w  . ja va2  s  . co m

    return writer.toString();
}

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

public static String renderTemplate(Template template, Object model) {
    try {//from   www  . ja  v a2 s.c  om
        StringWriter result = new StringWriter();
        template.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:utils.FreeMarkerUtils.java

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

From source file:Main.java

/**
 * @param thread a thread/*w w  w  .j a va 2 s . c  om*/
 * @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();
}

From source file:Main.java

/**
 * get stack trace message/*from  w ww  . j a  v  a  2s . com*/
 * 
 * @param e the Exception
 * @return stack trace message of the Exception
 */
public static String getStrackTrace(Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw, true));
    String str = sw.toString();
    return str;
}