Example usage for java.io StringWriter close

List of usage examples for java.io StringWriter close

Introduction

In this page you can find the example usage for java.io StringWriter close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a StringWriter has no effect.

Usage

From source file:StringUtils.java

/**
 * Convert an exception to a String with full stack trace
 * @param ex the exception//from   w  w  w.ja  v  a 2 s . co m
 * @return a String with the full stacktrace error text
 */
public static String getStringFromStackTrace(Throwable ex) {
    if (ex == null) {
        return "";
    }
    StringWriter str = new StringWriter();
    PrintWriter writer = new PrintWriter(str);
    try {
        ex.printStackTrace(writer);
        return str.getBuffer().toString();
    } finally {
        try {
            str.close();
            writer.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:Main.java

/**
 * Converts a dom document to an xml String.
 * /*  ww w . j  ava  2s  . c om*/
 * @param document
 * @return
 * @see XMLSerializer
 */
public static String toXML(final Document document) {
    final XMLSerializer xmlSerializer = new XMLSerializer();

    final OutputFormat outputFormat = new OutputFormat(document);
    outputFormat.setOmitXMLDeclaration(false);
    outputFormat.setEncoding("UTF-8");
    outputFormat.setIndenting(true);

    final StringWriter stringWriter = new StringWriter();
    xmlSerializer.setOutputCharStream(stringWriter);
    xmlSerializer.setOutputFormat(outputFormat);
    try {
        xmlSerializer.serialize(document);
        stringWriter.flush();
        stringWriter.close();
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }

    return stringWriter.toString();
}

From source file:elaborate.util.StringUtil.java

public static String html2xml(String editBody) {
    StringReader in = new StringReader(editBody);
    StringWriter out = new StringWriter();
    TIDY.parse(in, out);/* ww  w.j a v  a  2 s  .  co  m*/
    try {
        in.close();
        out.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return out.toString().replaceAll("\r\n", "").replaceAll("\n", "");
}

From source file:Main.java

/**
 * Helper method to serialize an object to XML. Requires object to be Serializable
 * @param entity Object to serialize//from w w w  .  j  av  a2  s . c  o m
 * @param <T> class that implements Serializable
 * @return String XML representation of object
 * @throws IOException if errors during serialization
 * @throws JAXBException if errors during serialization
 */
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException {
    StringWriter sw = new StringWriter();

    if (null != entity) {
        JAXBContext ctx = JAXBContext.newInstance(entity.getClass());

        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(entity, sw);
    }
    sw.flush();
    sw.close();

    return sw.toString();
}

From source file:Utilities.java

/**
 * Return the stack trace from the passed exception as a string
 *
 * @param  th  The exception to retrieve stack trace for.
 *//*from   w w  w. ja v  a  2  s .com*/
public static String getStackTrace(Throwable th) {
    if (th == null) {
        throw new IllegalArgumentException("Throwable == null");
    }

    StringWriter sw = new StringWriter();
    try {
        PrintWriter pw = new PrintWriter(sw);
        try {
            th.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    } finally {
        try {
            sw.close();
        } catch (IOException ex) {

        }
    }
}

From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java

public static String getBody(JspFragment fragment) throws JspException, IOException {
    if (fragment == null)
        return "";
    StringWriter body = new StringWriter();
    fragment.invoke(body);/*w  ww.j a  va 2s  .  c om*/
    body.close();
    return body.toString().trim();
}

From source file:org.n52.io.geojson.JSONUtils.java

public static String print(final JsonNode node) {
    final StringWriter writer = new StringWriter();
    try {//from w  w w . j a v  a2 s  .c o m
        print(writer, node);
        writer.flush();
    } catch (IOException e) {
        // cannot happen
    } finally {
        try {
            writer.close();
        } catch (IOException ioe) {
            LOGGER.error("Error while colsing closeable!", ioe);
        }
    }
    return writer.toString();
}

From source file:Main.java

/**
 * Converts DOM document to a string// ww w . ja  va2  s .  c o  m
 *
 * @param XMLDocument the document you want to convert
 * @return  String representation of you XML document
 */
public static String XMLToString(Document XMLDocument) {
    String xmlString = null;
    try {
        // Convert DOM to XML string
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        //trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.transform(new DOMSource(XMLDocument), result);
        xmlString = sw.toString();
        sw.close();
    } catch (TransformerConfigurationException tcex) {

    } catch (TransformerException tex) {

    } catch (IOException iex) {

    }
    return xmlString;
}

From source file:Main.java

public static StringBuffer transfer(Element element, StreamSource source) {
    try {//w  w w. j a  va2s  .com
        StringWriter sw = new StringWriter();
        Transformer trans = null;
        if (source != null)
            trans = TransformerFactory.newInstance().newTransformer(source);
        else
            trans = TransformerFactory.newInstance().newTransformer();
        trans.transform(new DOMSource(element), new StreamResult(sw));
        sw.close();
        return sw.getBuffer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.gitblit.utils.MarkdownUtils.java

/**
 * Returns the html version of the markdown source reader. The reader is
 * closed regardless of success or failure.
 *
 * @param markdownReader//w  ww . ja v a  2  s  . c  o m
 * @return html version of the markdown text
 * @throws java.text.ParseException
 */
public static String transformMarkdown(Reader markdownReader) throws IOException {
    // Read raw markdown content and transform it to html
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(markdownReader, writer);
        String markdown = writer.toString();
        return transformMarkdown(markdown);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            // IGNORE
        }
    }
}