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

private static String nodeToString(Node node) throws Exception {
    StringWriter sw = new StringWriter();

    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));

    return sw.toString();
}

From source file:com.doubotis.restwrapper.data.JObjectResponse.java

public static JObjectResponse fromException(Exception exception, boolean showStackTrace) {
    JObjectResponse r = new JObjectResponse(false);

    StringWriter sw = null;/* w ww  .ja  va 2  s. c  om*/
    if (exception instanceof HTTPException && exception.getCause() != null) {
        sw = new StringWriter();
        exception.getCause().printStackTrace(new PrintWriter(sw));
    } else {
        sw = new StringWriter();
        exception.printStackTrace(new PrintWriter(sw));
    }

    if (showStackTrace)
        r.put("stacktrace", sw.toString());
    r.put("message", exception.getMessage());
    r.put("status", "ko");
    return r;
}

From source file:Main.java

public static String formatXML(String unformatted)
        throws SAXException, IOException, TransformerException, ParserConfigurationException {

    if (unformatted == null)
        return null;

    // remove whitespaces between xml tags
    String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><");

    // Instantiate transformer input
    Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces));
    StreamResult xmlOutput = new StreamResult(new StringWriter());

    // Configure transformer
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

    transformer.transform(xmlInput, xmlOutput);
    String formatted = xmlOutput.getWriter().toString();

    return formatted;
}

From source file:Main.java

public static String toString(Collection<Integer> stack, String delimiterChars) {
    if (stack.isEmpty()) {
        return "";
    }/*from www  .  j a  va  2s  .  c om*/
    if (stack.size() == 1) {
        return Integer.toString(stack.iterator().next());
    }
    StringWriter writer = new StringWriter();
    String delimiter = "";
    for (Integer item : stack) {
        writer.append(delimiter);
        writer.append(Integer.toString(item));
        delimiter = delimiterChars;
    }
    return writer.toString();
}

From source file:com.capitaltg.bbcodeguard.StringUtils.java

public static String readResourceAndUpdateText(String resourceName, Map<String, String> values)
        throws IOException {
    InputStream inputStream = StringUtils.class.getClassLoader().getResourceAsStream(resourceName);
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer);/* w ww. ja  va 2s .c  om*/
    inputStream.close();
    return StringUtils.replaceAll(writer.toString(), values);
}

From source file:Main.java

/**
 * Converts an XML node to a string.//  w  w w .j a  v a2 s .co  m
 * @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

/**
 * make throwable to string./*from  w w  w.  j  a va 2 s . c o m*/
 */
public static String toString(Throwable throwable) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    printWriter.print(throwable.getClass().getName() + ": ");
    if (throwable.getMessage() != null) {
        printWriter.print(throwable.getMessage() + "\n");
    }
    printWriter.println();
    try {
        throwable.printStackTrace(printWriter);
        return stringWriter.toString();
    } finally {
        printWriter.close();
    }
}

From source file:Main.java

/** 
 * Returns the XML string representation of a Node.
 * @param node Node to convert into XML string. 
 * @param omitXmlDeclaration <tt>true</tt> to remove the XML declaration from the string. 
 * @return The XML string representation of the input node. 
 * @throws TransformerConfigurationException 
 * @throws TransformerException /*from  w w  w.j  a va 2 s  .com*/
 */
public static String getXMLString(Node node, boolean omitXmlDeclaration)
        throws TransformerConfigurationException, TransformerException {
    StringWriter writer = new StringWriter();
    DOMSource domSource = new DOMSource(node);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no");
    serializer.transform(domSource, result);
    return (writer.toString());
}

From source file:Main.java

public static String printXMLNode(Node node) {
    try {// w  w  w. jav a  2  s.c o m
        TransformerFactory tf = TransformerFactory.newInstance();
        tf.setAttribute("indent-number", 2);
        Transformer transformer = tf.newTransformer();
        String omitDeclaration = node instanceof Document
                || node == node.getOwnerDocument().getDocumentElement() ? "no" : "yes";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration);
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        output = removeEmptyLines(output); // .replaceAll("\n|\r", "");
        return output;
    } catch (TransformerException te) {
        throw new RuntimeException(te);
    }
}

From source file:Main.java

public static String xmltoString(final Document document) throws TransformerException {
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(new DOMSource(document.getDocumentElement()), streamResult);
    return stringWriter.toString();
}