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:Main.java

/**
 * Convert a DOM tree into a String using transform
 * @param domDoc                  DOM object
 * @throws java.io.IOException    I/O exception
 * @return                        XML as String
 */// w w  w  . j  av  a 2s. c o  m
public static String docToString2(Document domDoc) throws IOException {
    try {
        TransformerFactory transFact = TransformerFactory.newInstance();
        Transformer trans = transFact.newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "no");
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        trans.transform(new DOMSource(domDoc), result);
        return sw.toString();
    } catch (Exception ex) {
        throw new IOException(String.format("Error converting from doc to string %s", ex.getMessage()));
    }
}

From source file:utils.FreeMarkerUtils.java

public static String renderString(String templateString, Map<String, ?> model) {
    try {// www .j  a  v a  2 s.  c  o  m
        StringWriter result = new StringWriter();
        Template t = new Template("name", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        // throw e;
    }
    return "";
}

From source file:onl.area51.httpd.rest.JsonEntity.java

/**
 * Convert a {@link JsonStructure} to a String
 * <p>// w w  w  .  ja v a  2  s  . c  om
 * @param s <p>
 * @return
 */
public static String encode(JsonStructure s) {
    final StringWriter w = new StringWriter();
    try (JsonWriter jw = Json.createWriter(w)) {
        jw.write(s);
        return w.toString();
    }
}

From source file:Main.java

public static int[] fileToIntArray(Context myContext, String filename, int size) {

    int[] array = new int[size];
    int i = 0;//w  w w  .  j  ava 2  s. co  m
    FileInputStream inputStream;
    try {
        int c;
        inputStream = myContext.openFileInput(filename);
        StringWriter writer = new StringWriter();
        while ((c = inputStream.read()) != -1) {
            writer.append((char) c);
        }
        String ints[] = writer.toString().split("\n");
        for (String s : ints) {
            array[i++] = Integer.parseInt(s);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return array;
}

From source file:com.twitter.ambrose.util.JSONUtil.java

/**
 * Serializes object to JSON string.//www .  j  ava 2 s .  c  o  m
 *
 * @param object object to serialize.
 * @return json string.
 * @throws IOException
 */
public static String toJson(Object object) throws IOException {
    StringWriter writer = new StringWriter();
    writeJson(writer, object);
    return writer.toString();
}

From source file:com.opendoorlogistics.studio.dialogs.AboutBoxDialog.java

private static String info(boolean showLicenses) {

    // Use own class loader to prevent problems when jar loaded by reflection
    InputStream is = AboutBoxDialog.class
            .getResourceAsStream(showLicenses ? "/resources/Licences.html" : "/resources/About.html");
    StringWriter writer = new StringWriter();
    try {//  w w w .  ja v  a 2 s . c om
        IOUtils.copy(is, writer, Charsets.UTF_8);
        is.close();
    } catch (Throwable e) {
    }

    String s = writer.toString();

    s = replaceVersionNumberTags(s);
    return s;
}

From source file:Main.java

public static String lsSerializePretty(Document doc) {
    DOMImplementation domImplementation = doc.getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
        DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
        if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
            lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");
            StringWriter stringWriter = new StringWriter();
            lsOutput.setCharacterStream(stringWriter);
            lsSerializer.write(doc, lsOutput);
            return stringWriter.toString();
        } else {/*from w w w. jav  a 2s .  co m*/
            throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
        }
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:Main.java

public static String NodeToStr(Node node) throws Exception, TransformerFactoryConfigurationError {
    StringWriter sw = new StringWriter();
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:com.qspin.qtaste.util.PythonHelper.java

/**
 * Gets message of a PyException.//w  w w  .  j a va 2 s.  c o  m
 * If internal message is null, uses printStackTrace() method to build message.
 *
 * @param e PyException
 * @return message string
 */
public static String getMessage(PyException e) {
    if (e.getMessage() != null) {
        return e.getMessage();
    } else {
        StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }
}

From source file:com.github.ibole.infrastructure.common.exception.MoreThrowables.java

/**
 * ErrorStackString./*from  ww  w  .  ja va2  s  .  c om*/
 */
public static String getStackTraceAsString(Throwable e) {
    if (e == null) {
        return "";
    }
    StringWriter stringWriter = new StringWriter();
    e.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}