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.bootcamp.utils.FreeMarkers.java

public static String renderString(String templateString, Map<String, ?> model) {
    try {//from w w w.  j ava 2  s.  c  om
        StringWriter result = new StringWriter();
        Template t = new Template("name", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

/**
 * Converts an XML {@link org.w3c.dom.Document} to a string.
 *
 * @param doc the XML document//  ww w  .ja va 2 s.  c o  m
 * @return the string representation of the XML document
 */
public static String documentToString(Document doc) {

    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));

        return writer.getBuffer().toString();

    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace(System.err);
    }

    return "";
}

From source file:Main.java

public static String nodeToString(Node n) {
    init();//w  w w.j  a v a 2 s.  c  o m
    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 getXMLString(Node doc) throws TransformerException, TransformerConfigurationException {
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    StringWriter sw = new StringWriter();

    Source src = new DOMSource(doc);
    Result dest = new StreamResult(sw);

    aTransformer.transform(src, dest);//from w ww  .j a v a 2s. co  m

    return sw.toString();
}

From source file:Main.java

public static String newStringFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty("omit-xml-declaration", "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");
    return output;
}

From source file:Main.java

public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException {
    StringWriter writerTo = new StringWriter();
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, writerTo);
    return writerTo.toString();
}

From source file:Main.java

/**
 * Represent xml node as string//  www .java 2  s.c  o m
 * @param node to be represented
 * @return node text
 */
public static String xmlToString(Node node) {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        Transformer transformer = getTransformerFactory().newTransformer();
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();
    } catch (TransformerException e) {
        throw new RuntimeException("Can't transfor XML to String", e);
    }
}

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);/* w  ww. ja va  2s.  com*/
    pw.flush();
    return me.toString();
}

From source file:Main.java

/**
 * Serializes any Object to XML/*from   w  ww  .j  a v  a 2  s. com*/
 *
 * @param object Object to serialize
 * @return XML serialization of the supplied object
 */
public static String toXmlJaxb(Object object) {
    String result = "";
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.marshal(object, writer);
        result = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:Main.java

public static String asString(Document doc) {
    try {//from  w w  w.  j  a  v  a 2 s . com
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (TransformerException e) {
        throw Throwables.propagate(e);
    }
}