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:com.joliciel.jochre.search.webClient.SearchWebClientUtils.java

public static String getJson(URL url) {
    try {/*  www .jav  a2  s.c  o m*/
        URLConnection con = url.openConnection();
        InputStream in = con.getInputStream();
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        String json = writer.toString();
        return json;
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String toString(Node n) throws IOException, TransformerException {
    StringWriter w = new StringWriter();
    serialize(n, new StreamResult(w));
    return w.toString();
}

From source file:Main.java

/**
 * get stack trace as string/*from w  w  w  . ja  v a 2s. c o  m*/
 * 
 * @param traceElements
 *            - stack trace elements
 * @return built string
 */
public static String getStackTrace(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    String string = sw.toString();
    return string;
}

From source file:net.fizzl.redditengine.data.GsonTemplate.java

/**
 * Returns an instance of class T from {@link InputStream} that contains JSON data
 * /* www  . j  a v  a 2  s. c o  m*/
 * @param is   JSON as InputStream
 * @param type   {@link Class}<T> for conversion from JSON to POJO
 * @return      POJO representation of the JSON data
 * @throws IOException
 * @see Gson
 */
public static <T> T fromInputStream(InputStream is, Class<T> type) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString(), type);
}

From source file:net.fizzl.redditengine.data.SubredditListing.java

/**
 * Returns a {@link #SubredditListing} from an InputStream
 * // w  w  w .j  a  v  a 2  s  .  c om
 * @param is   InputStream containing a subreddit listing as JSON
 * @return      SubredditListing
 * @throws       IOException
 */
public static SubredditListing fromInputStream(InputStream is) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromString(writer.toString());
}

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:Main.java

/**
 * Pretty print an XML. Use with caution as this drains the source if it is
 * a StreamSource.//from w  w  w .  j av  a  2  s .  com
 * @param source the XML source
 * @return a String with readable XML
 */
public static String prettyPrint(final Source source) {
    try {
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        return e.getMessage();
    }
}

From source file:net.fizzl.redditengine.data.ListMapValue.java

public static List<String> fromInputStream(InputStream is, String name) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return fromInputStream(writer.toString(), name);
}

From source file:Main.java

/**
 * Return the xml translation of an object
 * /*from   w  w w  .  ja  v a 2s . c o m*/
 * @param cls
 * @param entity
 * @return
 */
public static String objectToXML(Class<?> cls, Object entity) {

    try {
        Marshaller m = JAXBContext.newInstance(cls).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        m.marshal(entity, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String documentToString(Document document) {
        try {/*from   w w  w. ja  v  a 2s. c  o m*/
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer trans = tf.newTransformer();
            StringWriter sw = new StringWriter();
            trans.transform(new DOMSource(document), new StreamResult(sw));
            return sw.toString();
        } catch (TransformerException tEx) {
            tEx.printStackTrace();
        }
        return null;
    }