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:Main.java

public static String tranform(String xslSource, String original) {
    StringReader sr = new StringReader(xslSource);
    StringReader sro = new StringReader(original);
    StringWriter result = new StringWriter();
    doTransform(new StreamSource(sr), new StreamSource(sro), new StreamResult(result));
    return result.toString();
}

From source file:Main.java

public static String formatDocument(Document document)
        throws TransformerFactoryConfigurationError, TransformerException, IOException {
    OutputFormat format = new OutputFormat(document, "UTF-8", true);
    format.setLineWidth(160);/*from  w  ww. ja va  2 s . com*/
    //        format.setIndenting(true);
    format.setIndent(2);
    //        format.setEncoding("UTF-8");
    Writer out = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(out, format);
    serializer.serialize(document);

    return out.toString();
}

From source file:Main.java

public static String getErrorInfo(Throwable arg1) {
    Writer writer = new StringWriter();
    PrintWriter pw = new PrintWriter(writer);
    arg1.printStackTrace(pw);/*from w  w  w .j a v  a2s.  co  m*/
    pw.close();
    String error = writer.toString();
    return error;
}

From source file:Main.java

public static String toString(Element element) {
    try {/* ww  w.j  ava  2 s  . c o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter buffer = new StringWriter();
        transformer.transform(new DOMSource(element), new StreamResult(buffer));

        return buffer.toString();

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:it.cnr.ilc.tokenizer.utils.InputToString.java

/**
 * Convert an inputstream into a string/*from w ww  .ja v  a 2s.co m*/
 * @param is the inputstream
 * @return the string from the input
 */
public static String convertInputStreamToString(InputStream is) {
    StringWriter writer = new StringWriter();
    String encoding = "UTF-8";
    String message = "";
    String theString = "";
    try {
        IOUtils.copy(is, writer, encoding);
        theString = writer.toString();
    } catch (Exception e) {
        message = "IOException in coverting the stream into a string " + e.getMessage();
        Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, message);
    }

    //System.err.println("DDDD " + theString);
    IOUtils.closeQuietly(is);
    IOUtils.closeQuietly(writer);
    return theString;
}

From source file:Main.java

public static String formatDOM(Node node) throws TransformerException {
    StringWriter writer = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/*from  ww  w.ja va2s.  com*/
    transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    // encoding doesn't matter since we stick with strings
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String sourceToString(Source source) throws IOException {

    try {//from   w w w .  ja v a  2 s. co  m
        Transformer trans = transFactory.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        Writer writer = new StringWriter();
        trans.transform(source, new StreamResult(writer));
        writer.flush();
        return writer.toString();
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static String xmlToString(Document xml) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;

    try {/*  ww w .  j  av a  2s  . c o m*/
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
    DOMSource source = new DOMSource(xml);
    StringWriter writer = new StringWriter();
    try {
        transformer.transform(source, new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return writer.toString();

}

From source file:Main.java

/**
 * Returns a string representation of {@code node}. This method should only
 * be used for debugging purposes, at it creates a new transformer factory
 * and transformer upon each call.//from  ww  w  . j a  va2  s .  co  m
 * 
 * @param n
 *            the node
 * @return a string representation of {@code node}
 */
public static String toString(Node n) {
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(n), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException e) {
        return null;
    }
}

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();
}