Example usage for java.io StringWriter toString

List of usage examples for java.io StringWriter toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Return the buffer's current value as a string.

Usage

From source file:com.dattack.dbtools.drules.engine.DefaultNotificationActionBeanVisitor.java

private static String formatMessage(final NotificationActionSendMailBean action,
        final Configuration configuration) throws TemplateException, IOException, ConfigurationException {

    final Template template = createTemplate(action);

    final Map<Object, Object> dataModel = new HashMap<>();
    dataModel.putAll(ConfigurationConverter.getMap(configuration));

    final StringWriter outputWriter = new StringWriter();
    template.process(dataModel, outputWriter);
    return outputWriter.toString();
}

From source file:Main.java

public static String nodeToString(Node n, boolean isOmitXmlDeclaration, boolean isIndent)
        throws TransformerException {
    StringWriter sw = new StringWriter();
    Transformer transformer = generateTransformer(isOmitXmlDeclaration, isIndent);
    transformer.transform(new DOMSource(n), new StreamResult(sw));
    return sw.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 w ww.  j  av a 2s . c o  m*/
    printWritter.flush();
    stringWritter.flush();
    return stringWritter.toString();
}

From source file:Main.java

public static String convertToXmlString(Object source) {
    try {/*from  w w w  . ja  va 2 s  .c o m*/
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext = JAXBContext.newInstance(source.getClass());
        Marshaller marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String transform(Source xsl, Document doc) {
    StringWriter writer = new StringWriter();
    try {/*from   w  ww. ja  va2s  .co  m*/
        TransformerFactory.newInstance().newTransformer(xsl).transform(new DOMSource(doc),
                new StreamResult(writer));
    } catch (Exception e) {
    }
    return writer.toString();
}

From source file:Main.java

public static String convertToXml(Object source, Class... type) {
    String result;/*ww  w.  ja  v a  2  s  .c o  m*/
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(source, sw);
        result = sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

public static String getStackTraceAsString(Throwable t) {
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:io.leishvl.core.xml.XmlHelper.java

/**
 * Obtains a String representation of a XML document, omitting the XML declaration.
 * @param document the document to be transformed into String.
 * @return a String representation of the specified XML document.
 *///from  w  w  w.j  a  v  a2  s  . co m
public static String documentToStringOmitXmlDeclaration(final Document document) {
    checkNotNull(document, "Uninitialized document");
    String docStr;
    try {
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(ENCODING, UTF_8.name());
        transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
        final StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        docStr = writer.toString();
    } catch (Exception e) {
        docStr = null;
        LOGGER.warn("Failed to convert DOM document to String", e);
    }
    return docStr;
}

From source file:io.leishvl.core.xml.XmlHelper.java

public static String prettyPrint(final Document document) {
    checkNotNull(document, "Uninitialized document");
    String docStr;//  w  w w  .ja  va 2 s  .  co m
    try {
        final TransformerFactory tf = TransformerFactory.newInstance();
        final Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(ENCODING, UTF_8.name());
        transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(INDENT, "yes");
        final StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        docStr = writer.toString();
    } catch (Exception e) {
        docStr = null;
        LOGGER.warn("Failed to convert DOM document to String", e);
    }
    return docStr;
}

From source file:Main.java

public static String DocumentToString(final Document doc) {
    try {// ww  w.  ja va2 s  . c  om
        StringWriter writer = new StringWriter();
        Result l_s = new StreamResult(writer);

        doc.normalize();

        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        return writer.toString();
    } catch (Exception e) {
        System.err.println(e);

        return null;
    }
}