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 getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);/*from  ww w . j a v a2  s .  c o m*/
    pw.flush();
    sw.flush();
    return sw.toString();
}

From source file:Main.java

/**
 * Transforms an XML to a String// w  w  w . j a  va  2s.c  o  m
 * @param node XML node
 * @return String represenation of XML
 */
public static String printXML(Node node) {
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer serializer;
    try {
        serializer = tfactory.newTransformer();

        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        StringWriter output = new StringWriter();
        serializer.transform(new DOMSource(node), new StreamResult(output));
        return output.toString();
    } catch (TransformerException e) {

        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String nodeToString(Node n) {
    init();//from w  ww  . ja  v  a  2  s .c om
    try {
        DOMSource src = new DOMSource(n);
        StringWriter sr = new StringWriter();
        Result res = new StreamResult(sr);
        tx.transform(src, res);
        return sr.toString();
    } catch (Exception e) {
        return (e.getMessage());
    }
}

From source file:Main.java

public static String tranform(String xslSource, String original) {
    StringReader sr = new StringReader(xslSource);
    StringReader sro = new StringReader(original);
    StringWriter result = new StringWriter();
    doTransform(new StreamSource(sr), new StreamSource(sro), new StreamResult(result));
    return result.toString();
}

From source file:com.krawler.portal.tools.FreeMarkerUtil.java

public static String process(String name, Object context) throws Exception {

    StringWriter writer = new StringWriter();

    process(name, context, writer);//w w w .  j a v a 2  s.c om

    return writer.toString();
}

From source file:Main.java

public static String getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }//from  w ww.java2s  .  com

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

From source file:io.opentracing.contrib.elasticsearch.common.SpanDecorator.java

private static Map<String, Object> errorLogs(Throwable throwable) {
    Map<String, Object> errorLogs = new HashMap<>(4);
    errorLogs.put("event", Tags.ERROR.getKey());
    errorLogs.put("error.kind", throwable.getClass().getName());
    errorLogs.put("error.object", throwable);

    errorLogs.put("message", throwable.getMessage());

    StringWriter sw = new StringWriter();
    throwable.printStackTrace(new PrintWriter(sw));
    errorLogs.put("stack", sw.toString());

    return errorLogs;
}

From source file:Main.java

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();

    // Client assumes/requires UTF-8 response.
    LSOutput lsOutput = domImplLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");

    StringWriter stringWriter = new StringWriter();
    lsOutput.setCharacterStream(stringWriter);
    serializer.write(element, lsOutput);

    return stringWriter.toString();
}

From source file:Main.java

/**
 * Marshals a bean to XML./*  w w  w  .ja  v  a 2 s .  co m*/
 * 
 * @param bean
 * @return XML {@link String}
 * @throws JAXBException
 */
public static <T> String marshal(T bean) throws JAXBException {
    StringWriter stw = new StringWriter();
    createMarshaller(bean.getClass()).marshal(bean, stw);

    return stw.toString();
}

From source file:com.dianping.lion.util.ThrowableUtils.java

public static String extractStackTrace(Throwable t) {
    StringWriter me = new StringWriter();
    PrintWriter pw = new PrintWriter(me);
    t.printStackTrace(pw);/*from w ww .j  a  v a2  s .  c  o m*/
    pw.flush();
    return me.toString();
}