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

static public String document2String(Node document) {
    if (document == null)
        return null;

    StringWriter stringWriter = new StringWriter();
    try {//  w ww. ja v  a  2s . c  o  m
        identityTransformer.transform(new DOMSource(document), new StreamResult(stringWriter));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return stringWriter.toString();
}

From source file:FileCopyUtils.java

/**
 * Copy the contents of the given Reader into a String.
 * Closes the reader when done./*from   w ww .  j a v a  2s  .co  m*/
 * @param in the reader to copy from
 * @return the String that has been copied to
 * @throws IOException in case of I/O errors
 */
public static String copyToString(Reader in) throws IOException {
    StringWriter out = new StringWriter();
    copy(in, out);
    return out.toString();
}

From source file:Main.java

public static String nodeToString(final Node node) {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer;//from  w  w w.  ja v a  2 s.co m
    try {
        transformer = transFactory.newTransformer();
        final StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(node), new StreamResult(buffer));
        buffer.append("\n");
        return buffer.toString();
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gitblit.utils.MarkdownUtils.java

/**
 * Returns the html version of the markdown source reader. The reader is
 * closed regardless of success or failure.
 *
 * @param markdownReader//  www  . ja v a  2s  .  c o m
 * @return html version of the markdown text
 * @throws java.text.ParseException
 */
public static String transformMarkdown(Reader markdownReader) throws IOException {
    // Read raw markdown content and transform it to html
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(markdownReader, writer);
        String markdown = writer.toString();
        return transformMarkdown(markdown);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            // IGNORE
        }
    }
}

From source file:com.rsmart.rfabric.logging.FormattedLogger.java

/**
 * Applies a pattern with parameters to create a {@link String} used as a logging message
 *  /*from w w w. j  a  va2 s . c o  m*/
 * 
 * @param pattern to format against
 * @param objs an array of objects used as parameters to the <code>pattern</code>
 * @return Logging Message
 */
private static final String getMessage(String pattern, Object... objs) {
    StringWriter retval = new StringWriter();
    PrintWriter writer = new PrintWriter(retval);

    writer.printf(pattern, objs);

    return retval.toString();
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

public static String readFileAsStringFromBundle(String filename) throws IOException {
    InputStream is = Utils.class.getResourceAsStream(("/" + filename).replace('/', File.separatorChar));
    if (is == null) {
        throw new IOException(String.format("Could not find %s", filename));
    }/*from w ww . ja  v  a  2 s.co m*/
    StringWriter sw = new StringWriter();
    IOUtils.copy(is, sw, "utf-8"); //this uses Apache commons io
    return sw.toString();
}

From source file:io.wcm.caravan.pipeline.impl.JacksonFunctions.java

/**
 * Serializes the given POJO into a JSON string using the default factory
 * @param pojo an object that can be serialized with Jackson's default {@link ObjectMapper}
 * @return JSON string//w  w w .  j  a v  a 2 s.  c o m
 * @throws JsonPipelineOutputException if the given object can not be serialized
 */
public static String pojoToString(Object pojo) {
    try {
        StringWriter writer = new StringWriter();
        objectMapper.writeValue(writer, pojo);
        return writer.toString();
    } catch (IOException ex) {
        throw new JsonPipelineOutputException("Failed to serialize entity to JSON string", ex);
    }
}

From source file:org.urbanstew.soundcloudapi.test.SoundCloudApiTest.java

public static void printXML(String title, HttpResponse response) throws Exception {
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document dom = db.parse(response.getEntity().getContent());

    System.out.println(title + " response XML:");
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(dom), result);
    System.out.println(writer.toString());
}

From source file:com.sportpm.mapper.JaxbMapper.java

/**
 * Java Object->Xml with encoding.//from  w  ww  .  j a  v a 2 s.com
 */
@SuppressWarnings("rawtypes")
public static String toXml(Object root, Class clazz, String encoding) {
    try {
        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(root, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:eu.morfeoproject.fast.catalogue.util.Util.java

/**
 * helper method to retrieve the contents of a resource in the classpath .
 *//*  w  w w. j  ava  2s  . c o  m*/
public static String getResource(Log log, String resourceName) {
    InputStream infRulesStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(resourceName);
    if (infRulesStream == null) {
        log.error(resourceName + ": resource not found -- check classpath");
        return null;
    }
    StringWriter output = new StringWriter();
    try {
        IOUtils.copy(infRulesStream, output);
        return output.toString();
    } catch (IOException e) {
        log.error(resourceName + ": cannot read resource", e);
        return null;
    } finally {
        IOUtils.closeQuietly(infRulesStream);
    }
}