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 getStackTraceString(Throwable tr) {
    if (tr == null) {
        return "";
    }//from  www  .  j  a  v a2  s .c  o m

    // This is to reduce the amount of log spew that apps do in the non-error
    // condition of the network being unavailable.
    Throwable t = tr;
    while (t != null) {
        if (t instanceof UnknownHostException) {
            return "";
        }
        t = t.getCause();
    }

    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    tr.printStackTrace(pw);
    pw.flush();
    return sw.toString();
}

From source file:Main.java

public static String marshal(Object object) throws Exception {
    StringWriter string = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    marshaller.marshal(object, string);/*  w w w.j a va  2 s. c  o  m*/
    return string.toString();
}

From source file:Main.java

public static String printNode(Node node) {
    String result = "";
    try {/*w  w w  .ja  v a 2 s  .  c  o m*/
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("method", "xml");
        StringWriter sw = new StringWriter();
        DOMSource source = new DOMSource(node);
        transformer.transform(source, new StreamResult(sw));
        result = sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Formatte un XML/*from  w w w.ja  v  a  2 s .  c  o m*/
 * */
public static String prettyFormat(String input, int indent) {
    try {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Throwable e) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = 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", String.valueOf(indent));
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Throwable t) {
            return input;
        }
    }
}

From source file:Main.java

public static String transform(String xmlString, String stylesheetPathname) throws TransformerException {
    try {//from w  w w.j  ava  2s  . c o  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Source stylesheetSource = new StreamSource(new File(stylesheetPathname).getAbsoluteFile());
        Transformer transformer = factory.newTransformer(stylesheetSource);
        Source inputSource = new StreamSource(new StringReader(xmlString));
        Writer outputWriter = new StringWriter();
        Result outputResult = new StreamResult(outputWriter);
        transformer.transform(inputSource, outputResult);
        return outputWriter.toString();
    } catch (TransformerConfigurationException tce) {
        throw new TransformerException(tce.getMessageAndLocation());
    } catch (TransformerException te) {
        throw new TransformerException(te.getMessageAndLocation());
    }
}

From source file:Main.java

public static String convertDocumentToString(Document dom) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer;/*  www. ja va 2s.c om*/
    try {
        transformer = tf.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(dom), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String transform2String(Node node) throws TransformerException {
    final TransformerFactory transFactory = TransformerFactory.newInstance();
    final Transformer transformer = transFactory.newTransformer();
    final StringWriter buffer = new StringWriter();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(node), new StreamResult(buffer));
    return buffer.toString();
}

From source file:Main.java

/**
 * Creates a {@link String} out of a {@link Node}
 * /*from w ww  .  j  a  va  2s  .com*/
 * @param node
 *            never <code>null</code>
 * @return the node as string, never <code>null</code>
 * @throws Exception
 */
public static String asString(Node node) throws Exception {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(node), new StreamResult(writer));
    return writer.toString();
}

From source file:Main.java

public static String getXmlPage(String xmlString, int page, String xslPath) {
    String styledResponse = "";
    StringReader rd = new StringReader(xmlString);
    StringWriter wrt = new StringWriter();
    TransformerFactory tFac = TransformerFactory.newInstance();
    try {/* www .  j  av a  2s .  com*/
        File xsl = new File(xslPath);
        Transformer transformer = tFac.newTransformer(new StreamSource(xsl));
        transformer.setParameter("Page", String.format("%s", page));
        transformer.transform(new StreamSource(rd), new StreamResult(wrt));

        styledResponse = wrt.toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    }

    return styledResponse;
}

From source file:Main.java

/**
 * Get standard xml string of node//  w w  w  . j a  v a2 s .co m
 * 
 * @param node
 * @return
 */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        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));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return sw.toString();
}