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.adaptris.util.text.mime.PartIteratorCase.java

protected static String toString(MimeBodyPart p) throws Exception {
    StringWriter out = new StringWriter();
    StreamUtil.copyAndClose(p.getInputStream(), out);
    return out.toString();
}

From source file:Main.java

public static String nodeToString(Node node) throws Exception {
    if (node == null) {
        return null;
    }//from  w w w.j a v a 2 s .  co  m
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    DOMSource source = new DOMSource(node);
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);
    transformer.transform(source, result);
    return sw.toString();
}

From source file:StringUtils.java

/**
 * Make a string representation of the exception.
 * @param e The exception to stringify//from ww  w .  j a v a  2s. co m
 * @return A string with exception name and call stack.
 */
public static String stringifyException(Throwable e) {
    StringWriter stm = new StringWriter();
    PrintWriter wrt = new PrintWriter(stm);
    e.printStackTrace(wrt);
    wrt.close();
    return stm.toString();
}

From source file:Main.java

/**
 * Format the given string as xml content.
 * @param xml/*from  w w  w. j  a v  a  2 s  .  c  om*/
 * @return
 */
public static String formatXml(String xml) {
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        Document doc = domBuilder.parse(is);
        OutputFormat format = new OutputFormat(doc);
        format.setLineWidth(80);
        format.setIndent(2);
        format.setIndenting(true);
        StringWriter out = new StringWriter();
        XMLSerializer xmls = new XMLSerializer(out, format);
        xmls.serialize(doc);
        return out.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xml;
}

From source file:Main.java

/**
 * Converts an XML node to a string.// www .ja va  2  s. c  om
 * @param node the XML node
 * @param outputProperties the output properties
 * @return the string
 */
public static String toString(Node node, Map<String, String> outputProperties) {
    try {
        StringWriter writer = new StringWriter();
        toWriter(node, writer, outputProperties);
        return writer.toString();
    } catch (TransformerException e) {
        //should never be thrown because we're writing to a string
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String getDOMString(Document doc) {

    String s = null;//from   www  .j av a 2s . co  m
    final TransformerFactory tfactory = TransformerFactory.newInstance();
    try {
        final Transformer xform = tfactory.newTransformer();
        final Source src = new DOMSource(doc);
        final StringWriter writer = new StringWriter();
        final Result result = new StreamResult(writer);
        xform.transform(src, result);
        s = writer.toString();
    } catch (final Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static String getXML(NodeList childNodes) {
    try {//  w w  w  .j  a v a2  s .  co m
        StringBuilder builder = new StringBuilder();
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        for (int i = 0; i < childNodes.getLength(); i++) {
            StringWriter sw = new StringWriter();
            t.transform(new DOMSource(childNodes.item(i)), new StreamResult(sw));
            builder.append(sw.toString());
        }
        return builder.toString();
    } catch (Exception ex) {
        return "";
    }
}

From source file:com.cedarsoft.serialization.test.utils.AbstractXmlSerializerTest2.java

@Nonnull
public static String addNameSpace(@Nonnull String nameSpaceUri, @Nonnull byte[] xml)
        throws IOException, SAXException {
    Document document = XmlCommons.parse(xml);

    new XmlNamespaceTranslator().addTranslation(null, nameSpaceUri).translateNamespaces(document, false);

    StringWriter out = new StringWriter();
    XmlCommons.out(document, out);/*from  www. jav  a  2 s  .  c  o m*/
    return out.toString();
}

From source file:com.jivesoftware.os.routing.bird.endpoints.logging.metric.MetricsHelper.java

public static String toJson(Object instance) {
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    try {//w  w  w.  java 2  s.  c  o m
        StringWriter sw = new StringWriter();
        mapper.writeValue(sw, instance);
        return sw.toString();
    } catch (Exception ex) {
        logger.error("Failed to create status!", ex);
        return "{}";
    }
}

From source file:com.mmiagency.knime.nodes.moz.api.util.ConnectionUtil.java

/**
 * //from  w  w  w  . j a  va  2 s .com
 * Method to make a GET HTTP connecton to 
 * the given url and return the output
 * 
 * @param urlToFetch url to be connected
 * @return the http get response
 */
public static String makeRequest(String urlToFetch) throws Exception {

    HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) {
        }
    });
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(urlToFetch.toString()));
    HttpResponse response = request.execute();
    StringWriter writer = new StringWriter();
    IOUtils.copy(response.getContent(), writer, response.getContentEncoding());
    return writer.toString();
}