Example usage for java.io StringWriter getBuffer

List of usage examples for java.io StringWriter getBuffer

Introduction

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

Prototype

public StringBuffer getBuffer() 

Source Link

Document

Return the string buffer itself.

Usage

From source file:com.pentaho.repository.importexport.PDIImportUtil.java

public static String asXml(Document document) {
    try {//  ww  w . ja v  a  2 s .c om
        Source source = new DOMSource(document.getParentNode());
        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();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
        return null;
    } catch (TransformerException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:dk.nsi.haiba.minipasconverter.executor.MinipasPreprocessor.java

public static String getStackTrace(final Throwable throwable) {
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw, true);
    throwable.printStackTrace(pw);/*from w  w  w  . j  a va  2 s.co  m*/
    return sw.getBuffer().toString();
}

From source file:Main.java

public static String toString(Document doc, String encoding, boolean indent)
        throws TransformerFactoryConfigurationError, TransformerException {
    Source source = new DOMSource(doc);
    StringWriter sw = new StringWriter();
    Result result = new StreamResult(sw);

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

    if (indent) {
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    }/*from ww w .  j ava 2s  .co m*/

    xformer.transform(source, result);

    return sw.getBuffer().toString();
}

From source file:de.tudarmstadt.ukp.shibhttpclient.Utils.java

public static String xmlToString(XMLObject aObject) throws IOException {
    Document doc;//w w w .j a v a2  s .  co m
    try {
        doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument();
    } catch (MarshallingException e) {
        throw new IOException(e);
    }

    try {
        Source source = new DOMSource(doc);
        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();
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}

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 . java  2s  .c om
 * @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:com.netflix.hystrix.serial.SerialHystrixConfiguration.java

public static String toJsonString(HystrixConfiguration config) {
    StringWriter jsonString = new StringWriter();

    try {//from   ww  w .  j a  v a  2  s.c  o m
        JsonGenerator json = jsonFactory.createGenerator(jsonString);

        serializeConfiguration(config, json);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return jsonString.getBuffer().toString();
}

From source file:com.haulmont.cuba.core.entity.LocaleHelper.java

public static String convertPropertiesToString(Properties properties) {
    StringWriter writer = new StringWriter();
    String result = null;/*  w w  w .  jav a 2  s  .c o m*/
    boolean written = false;
    try {
        properties.store(writer, "");
        written = true;
    } catch (IOException ignored) {
    }

    if (written) {
        StringBuffer buffer = writer.getBuffer();
        Pattern pattern = Pattern.compile("(?m)^#.*\\s\\s");
        result = pattern.matcher(buffer).replaceAll("");
    }
    return result;
}

From source file:de.micromata.genome.util.runtime.AssertUtils.java

/**
 * Gets the file lines.//  ww w .  jav a  2  s.com
 *
 * @param f the f
 * @return the file lines
 */
private static List<String> getFileLines(final File f) {
    final StringWriter sout = new StringWriter();
    try {
        IOUtils.copy(new FileInputStream(f), sout, Charset.defaultCharset());
    } catch (final Exception ex) {
        return null;
    }
    final String fc = sout.getBuffer().toString();
    final List<String> s = getLines(fc);
    return s;
}

From source file:io.uengine.util.StringUtils.java

/**
 * Properties? key value ? ./*from   w  w  w.j a v a2  s.com*/
 *
 * @param properties Properties
 * @return key value ?
 */
public static String propertiesToString(Properties properties) {
    StringWriter writer = new StringWriter();
    PrintWriter out = new PrintWriter(writer);
    Set<Object> keys = properties.keySet();
    for (Object key : keys) {
        out.println(key + "=" + properties.get(key));
    }
    return writer.getBuffer().toString();
}

From source file:uk.ac.diamond.shibbolethecpauthclient.Utils.java

/**
 * Helper method that turns the message into a formatted XML string.
 * //w w  w  .j a v a  2  s .c  o  m
 * @param aObject
 *The XML object to turn into a formatted XML string
 * 
 * @return The XML string
 * 
 * @throws IOException
 * thrown if there is a problem turning the XML object into a string
 */
static String xmlToString(XMLObject aObject) throws IOException {

    Document doc;
    try {
        doc = Configuration.getMarshallerFactory().getMarshaller(aObject).marshall(aObject).getOwnerDocument();
    } catch (MarshallingException e) {
        throw new IOException(e);
    }

    try {
        Source source = new DOMSource(doc);
        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();
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}