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

/**
 * DOM to string.//from   w w w.  j av  a2 s .c  o  m
 *
 * @param doc
 *            the doc
 * @return the string
 */
/*
 * from:
 * http://www.journaldev.com/71/utility-java-class-to-format-xml-document
 * -to-xml-string-and-xml-to-document
 */
public static String DOMToString(Document doc) {
    String xmlString = "";
    if (doc != null) {
        try {
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            xmlString = sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return xmlString;
}

From source file:Main.java

/**
 * Creates a string representation of a {@link Node} instance. This method
 * does not introduce any character to the string representation of the
 * {@link Node} (eg. \n or \r characters)
 *
 * @param node A {@link Node} instance/*  w w w.  j a  v a2  s  .c o  m*/
 * @return A string representation of the node instance
 * @throws RequestSecurityTokenException
 */
public static String xmlToString(Node node) throws Exception {
    try {
        Source source = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        Result result = new StreamResult(stringWriter);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(source, result);
        return stringWriter.getBuffer().toString();

    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    } catch (TransformerException e) {
        e.printStackTrace();
        throw new Exception("Cannot build a string representation of the assertion.");
    }
}

From source file:utils.FreeMarkerUtils.java

public static String renderTemplate(Template template, Object model) {
    try {/*  w w  w  . ja  v  a  2s  .co  m*/
        StringWriter result = new StringWriter();
        template.process(model, result);
        return result.toString();
    } catch (Exception e) {
        // throw Exceptions.unchecked(e);
    }
    return "";
}

From source file:Main.java

public static String marshalToString(Class<?> klass, Object obj) throws JAXBException {
    try {//  w  ww.  jav  a 2s. co  m
        context = JAXBContext.newInstance(klass);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        unmarshaller = context.createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(
                "There was a problem creating a JAXBContext object for formatting the object to XML.");
    }
    StringWriter sw = new StringWriter();
    String result = null;
    try {
        marshaller.marshal(obj, sw);
        result = sw == null ? null : sw.toString();
    } catch (JAXBException jaxbe) {
        jaxbe.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

/**
 * Method to get formatted Xml string from Xml source
 *
 * @param payload Source/*  w  ww. ja  va  2 s . com*/
 * @return a formatted Xml string
 */
public static String getXmlStringFromSource(Source payload) {
    String result = null;
    StreamResult strResult = new StreamResult(new StringWriter());

    if (payload != null) {
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            //factory.setAttribute("indent-number", new Integer(2));
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
            transformer.transform(payload, strResult);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        result = strResult.getWriter().toString();
    }
    return result;
}

From source file:Main.java

/**
 * Format the given string as xml content.
 * @param xml/*from w w  w .  j  a v a  2 s .  com*/
 * @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

public static String getXMLAsString(Document doc) throws Exception {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);

    return result.getWriter().toString();
}

From source file:Main.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWritter = new StringWriter();
    PrintWriter printWritter = new PrintWriter(stringWritter, true);
    t.printStackTrace(printWritter);/*from  ww w  .  j a  va 2  s  .  c o  m*/
    printWritter.flush();
    stringWritter.flush();
    return stringWritter.toString();
}

From source file:Main.java

/**
 * @param xml/*  w  ww .  j av a  2s .  co m*/
 * @return pretty xml
 */
public static String prettyXml(String xml) {
    Transformer serializer;
    try {
        Source source = new StreamSource(new StringReader(xml));
        StringWriter writer = new StringWriter();

        serializer = transFactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        serializer.transform(source, new StreamResult(writer));
        return writer.toString();
    } catch (Exception e) {
        return xml;
    }
}