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 nodeToString(Node node) {
    try {//from ww  w  .  j ava  2  s  . c o m
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(node);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        transformer.transform(source, result);
        return writer.toString();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String toStringE(Node element) {
    try {/*from  w w w. j  a v  a2  s.  c o  m*/
        if (element == null) {
            return "null";
        }
        Source source = new DOMSource(element);

        StringWriter stringWriter = new StringWriter();
        try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
            Result result = new StreamResult(printWriter);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(source, result);
        }
        return stringWriter.toString();
    } catch (IllegalArgumentException | TransformerException ex) {
        throw new Error(ex);
    }
}

From source file:com.xyxy.platform.modules.extension.tools.FreeMarkers.java

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

From source file:com.cprassoc.solr.auth.util.Utils.java

public static String streamToString(InputStream in) {
    String result = "";
    try {//from w  w w.j av a  2 s  . c o  m
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, "UTF-8");
        result = writer.toString();
    } catch (Exception e) {
        //e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * Serialise the supplied W3C DOM subtree.
 *
 * @param node The DOM node to be serialized.
 * @param format Format the output./*from  www .j  a va 2s  . c  o  m*/
 * @return The subtree in serailised form.
 * @throws DOMException Unable to serialise the DOM.
 */
public static String serialize(final Node node, boolean format) throws DOMException {
    StringWriter writer = new StringWriter();
    serialize(node, format, writer);
    return writer.toString();
}

From source file:Main.java

License:asdf

public static String documentToString(Document document) {
    try {//from w  w w  .  j a  va 2  s . com
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        StringWriter sw = new StringWriter();
        trans.transform(new DOMSource(document), new StreamResult(sw));
        return sw.toString();
    } catch (TransformerException tEx) {
        tEx.printStackTrace();
    }
    return null;
}

From source file:org.thymeleaf.spring4.view.FragmentRenderer.java

public static String render(final ThymeleafView view, final IFragmentSpec fragmentSpec, ModelMap modelMap,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    final StringWriter htmlStringWriter = new StringWriter();

    new ThymeleafView() {

        @Override//from   w w w.  j a  v a 2 s.  c  o  m
        public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(response) {

                @Override
                public PrintWriter getWriter() throws IOException {
                    return new PrintWriter(htmlStringWriter);
                }

            };

            view.renderFragment(fragmentSpec, model, request, wrapper);
        }

    }.render(modelMap, request, response);

    return htmlStringWriter.toString();
}

From source file:Main.java

/**
 *
 * @param obj/* w  ww .j a  v  a2s .c  o  m*/
 * @return
 * @throws JAXBException
 */
public static String serializeToString(Object obj) throws JAXBException {
    java.io.StringWriter sw = new StringWriter();
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.setProperty(JAXB_FORMATTED_OUTPUT, TRUE);
    m.marshal(obj, sw);
    return sw.toString();
}

From source file:Main.java

private static String getNodeValue(NodeList nodeList)
        throws TransformerFactoryConfigurationError, TransformerException {

    final Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setURIResolver(null);/*from w w w.  j a v a2s.com*/

    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final StringWriter sw = new StringWriter();
        serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw));
        String xml = sw.toString();
        final Matcher matcher = HEADER_PATTERN.matcher(xml);
        if (matcher.matches()) {
            xml = matcher.group(1);
        }
        buf.append(xml);
        buf.append("\n");
    }

    return buf.toString();
}

From source file:Main.java

public static String documentToString(Document document) throws TransformerException {
    Node rootNode = (Node) document.getDocumentElement();
    Source source = new DOMSource(rootNode);

    StringWriter stringWriter = new StringWriter();
    Result result = new StreamResult(stringWriter);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.transform(source, result);

    return stringWriter.getBuffer().toString().replace("\n", "");
}