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 obj2Xml(Object obj, String encoding) {
    String result = null;//from  www  .  j av  a 2s  . com
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

static void printE(String tag, Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);/*w  w  w . j a  va 2  s  .co m*/
    Log.e(tag, sw.toString());
}

From source file:Main.java

/**
 * @param <T>/* ww w. jav a 2 s  .c o m*/
 *           the type to serialize
 * @param c
 *           the class of the type to serialize
 * @param o
 *           the instance containing the data to serialize
 * @return a string representation of the data.
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static <T> String marshal(Class<T> c, Object o) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(c);
    Marshaller marshaller = ctx.createMarshaller();
    StringWriter entityXml = new StringWriter();
    marshaller.marshal(o, entityXml);
    String entityString = entityXml.toString();
    return entityString;
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//from   www . j ava 2 s  .  c  o  m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException {
    /* TODO: Certain HTML entities, like "&#8658;", currently seem impossible to represent, as the
         HTML mode will output them in a named form unsupported by the Swing HTML parser
         (e.g. "&lArr;"). *///www.  ja v  a  2 s  . co m
    Transformer t;
    t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml");
    t.setOutputProperty(OutputKeys.INDENT, "no");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no");
    StringWriter ret = new StringWriter();
    t.transform(new DOMSource(elm), new StreamResult(ret));
    return ret.toString();
}

From source file:Main.java

public static String marshal(Object object) throws JAXBException {
    StringWriter sw = new StringWriter();
    marshal(object, sw);//from   ww w. j  ava  2  s. c o  m
    return sw.toString();
}

From source file:com.mirth.connect.connectors.http.HttpUtil.java

public static String uncompressGzip(byte[] content, String charset) throws IOException {
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(content));
    StringWriter writer = new StringWriter();
    IOUtils.copy(gzis, writer, charset);
    return writer.toString();
}

From source file:Main.java

public static String marshal(Object object) throws JAXBException {
    StringWriter writer = new StringWriter();
    marshal(null, object, writer, null);
    return writer.toString();
}

From source file:Main.java

public static String documentToString(Document doc) throws IOException {
    StringWriter writer = new StringWriter();
    writeDocument(doc, writer);// www. jav a 2 s  . c  o m
    return writer.toString();
}

From source file:com.mac.holdempoker.socket.JsonConverter.java

public static String toJsonString(Object obj) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    //configure Object mapper for pretty print
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);

    //writing to console, can write to any output stream such as file
    StringWriter jsonString = new StringWriter();
    objectMapper.writeValue(jsonString, obj);
    return jsonString.toString();
}