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

/**
 * Converts a {@link Node node} to an XML string
 *
 * @param node the first element/*from   w  w w .ja  va2  s .  c  o  m*/
 * @return the XML String representation of the node, never null
 */
public static String nodeToString(Node node) {
    try {
        StringWriter writer = new StringWriter();
        createIndentingTransformer().transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

/**
 * Convert XML {@link Document} to its string representation.
 *
 * @param document for conversion//  w  w w  .j  av a 2 s. com
 * @return - string representation of XML {@link Document}
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static String xmlToString(Document document) throws Exception {
    if (transformer == null) {
        throw new Exception("Transformer is null");
    }
    Source xmlSource = new DOMSource(document);
    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    transformer.transform(xmlSource, result);
    return stringWriter.toString();
}

From source file:Main.java

/**
 * <p>Use this to grab things like the Plays title, or the characters in the play.</p>
 * <p>You will have to specify the XSLT file to do this</p>
 *
 * @param  xmlFile           the name and path of the xml file
 * @param  xslFile           the name and path of the xslt file
 *@param  delim             the delimiter you wish to split with.
 * @return                   A string delimited of the transformed document
 * @see    MimeConstants/*from  w  w w  .  j  a v a 2  s.  co  m*/
 */
public static String simpleTransformToStr(String xmlFile, String xslFile) {
    // --------------------------------------------------------------------
    // http://www.oreillynet.com/pub/a/oreilly/java/news/javaxslt_0801.html
    // --------------------------------------------------------------------

    String returnValue = "";
    try {
        // JAXP reads data using the Source interface
        Source xmlSource = new StreamSource(xmlFile);
        Source xsltSource = new StreamSource(xslFile);

        // the factory pattern supports different XSLT processors
        TransformerFactory transFact = TransformerFactory.newInstance();

        Transformer trans;

        trans = transFact.newTransformer(xsltSource);

        // ----------------------------------------------------------
        // http://forum.java.sun.com/thread.jspa?threadID=636335&messageID=3709470
        //      Paul, you had alot of trouble figuring out how to pass the output of the transformation, the url above is hwere you got the following chunk
        // ----------------------------------------------------------

        StringWriter output = new StringWriter();
        trans.transform(xmlSource, new StreamResult(output));

        return output.toString();

    } catch (TransformerException ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:edu.unc.lib.dl.util.SOAPUtil.java

public static String getString(SOAPMessage msg) {
    StringWriter w = new StringWriter();
    StreamResult result = new StreamResult(w);
    print(msg, result);//from   w  ww. j  a  va2  s .c o  m
    w.flush();
    return w.toString();
}

From source file:fm.last.util.StringUtils.java

/**
 * Extracts the stack trace from the passed exception into a string.
 * //from  www . ja v  a  2 s .c  om
 * @param t Throwable to extract stack trace from.
 * @return String representation of the stack trace.
 */
public static String extractStackTraceString(Throwable t) {
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:Main.java

public static String printNode(Node node) {
    String result = "";
    try {/* w w w .j  a  v  a 2s . co  m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("method", "xml");
        StringWriter sw = new StringWriter();
        DOMSource source = new DOMSource(node);
        transformer.transform(source, new StreamResult(sw));
        result = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static <T> String marshal(T object) throws JAXBException {
    final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller();
    final StringWriter stringWriter = new StringWriter();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, stringWriter);

    return stringWriter.toString();
}

From source file:com.capitaltg.bbcodeguard.StringUtils.java

public static String readResourceAndUpdateText(String resourceName, Map<String, String> values)
        throws IOException {
    InputStream inputStream = StringUtils.class.getClassLoader().getResourceAsStream(resourceName);
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer);/*ww  w .  j ava 2s  .c  o m*/
    inputStream.close();
    return StringUtils.replaceAll(writer.toString(), values);
}

From source file:cz.incad.kramerius.k5.k5velocity.K5APIRetriever.java

public static String get(String url) throws IOException {

    LOGGER.info("requesting url " + url);
    InputStream inputStream = RESTHelper.inputStream(url);
    StringWriter sw = new StringWriter();
    org.apache.commons.io.IOUtils.copy(inputStream, sw, "UTF-8");
    return sw.toString();
}

From source file:Main.java

public static String dumpException(Throwable e) {
    StringWriter sw = new StringWriter(160);
    sw.write(e.getClass().getName());/*from   w w w. j  a v  a 2s .c om*/
    sw.write(":\n");
    e.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}