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

public static String node2String(Node node) throws Exception {
    if (node == null)
        return null;
    StringWriter out = null;

    try {/*from   w  w  w .  ja v  a2 s.c o m*/
        Transformer transformer = transformerFactory.newTransformer();
        out = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    } catch (Exception e) {
        throw e;
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:Main.java

private static String getCrashReport(Throwable e, Context application) {

    StringBuffer body = new StringBuffer();
    body.append("Timestamp: " + new Date().toString());
    body.append("\n** Crash Report **\n");
    try {// w ww.j  a  v  a 2  s  . c om
        PackageInfo pi = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
        body.append("Package Name: ").append(pi.packageName).append("\n");
        body.append("Package Version: ").append(pi.versionCode).append("\n");
        body.append("Phone Model: ").append(android.os.Build.MODEL).append("\n");
        body.append("Phone Manufacturer: ").append(android.os.Build.MANUFACTURER).append("\n");
        body.append("Android Version:").append(android.os.Build.VERSION.RELEASE).append("\n");
    } catch (NameNotFoundException e1) {
    }

    StringWriter stack = new StringWriter();
    PrintWriter writer = new PrintWriter(stack);
    e.printStackTrace(writer);

    body.append("\n\nStacktrace:\n\n");
    body.append(stack.toString()).append("\n");

    if (e.getCause() != null) {
        Throwable cause = e.getCause();
        stack = new StringWriter();
        writer = new PrintWriter(stack);
        cause.printStackTrace(writer);

        body.append("\n\nCause Stacktrace:\n\n");
        body.append(stack.toString()).append("\n");
    }

    body.append("** Crash Report **\n");
    return body.toString();
}

From source file:com.androidwhy.modules.mapper.JaxbMapper.java

/**
 * Java Object->Xml with encoding.//from  w ww  .  ja v a 2s.c  o m
 */
public static String toXml(Object root, Class clazz, String encoding) {
    try {
        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(root, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

/**
 * Applies an XSL transformation to <code>xmlString</code> using
 * <code>xsltStr</code> as the stylesheet and returns the transformed
 * string.//from  w w w . j a  va  2 s  .  c  om
 * 
 * @param xmlString
 *            The XML to transform.
 * @param xsltStr
 *            The stylesheet as an XML string (not a file).
 * @return Transformed string.
 * @throws Exception
 */
public static String applyStylesheetAsString(String xmlString, String xsltStr) throws Exception {
    // Use the static TransformerFactory.newInstance() method to instantiate
    // a TransformerFactory. The javax.xml.transform.TransformerFactory
    // system property setting determines the actual class to instantiate --
    // org.apache.xalan.transformer.TransformerImpl.

    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to instantiate a Transformer that will work with
    // the stylesheet you specify. This method call also processes the
    // stylesheet into a compiled Templates object.

    Transformer transformer = tFactory
            .newTransformer(new StreamSource(new ByteArrayInputStream(xsltStr.getBytes())));

    // Use the Transformer to apply the associated Templates object to an
    // XML document (foo.xml) and write the output to a file (foo.out).

    StringWriter swriter = new StringWriter();
    StreamResult sresult = new StreamResult(swriter);
    transformer.transform(new StreamSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))), sresult);
    return swriter.toString();
}

From source file:Main.java

/**
 * Perform an xsl transformation, returning the result as a String.
 * Example use://from   w ww . j  a v  a2 s .  co  m
 * <pre>
 *      String html = XmlUtil.transform(this.getClass().getResourceAsStream("/sample.xslt"), new URL(sampleurl).openStream());
 * </pre>
 */
public static String transform(InputStream xsl, InputStream xml) {
    StringWriter sw = new StringWriter();
    try {
        transform(xsl, xml, sw);
    } catch (Exception ex) {
        throw new RuntimeException("Could not perform transformation", ex);
    }
    return sw.toString();
}

From source file:com.plateform.common.util.JaxbMapper.java

/**
 * Java Object->Xml with encoding./*w ww  . j  ava 2 s . c  o  m*/
 */
public static String toXml(Object root, Class clazz, String encoding) {
    try {
        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(root, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.surevine.ldap2alfresco.TestRichProfiles.java

/**
 * Output an exception's stack trace to the log file.
 * @param level The log level//from w  w  w.ja  v  a  2 s  . c o  m
 * @param e The exception
 */
private static void logException(final Level level, final Exception e) {
    try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        LOGGER.log(level, sw.toString());
    } catch (Exception e2) {
        LOGGER.log(level, "stack trace unavailable");
    }
}

From source file:com.ikon.util.StackTraceUtils.java

/**
 * Convert stack trace to String//from   w  ww. j  a  v a  2  s  .c om
 */
public static String toString(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    IOUtils.closeQuietly(pw);
    IOUtils.closeQuietly(sw);
    return sw.toString();
}

From source file:uk.org.openeyes.oink.it.multi.ITHl7v2ToOpenEyes.java

public static Message loadHl7Message(String path) throws IOException, HL7Exception {
    InputStream is = ITHl7v2ToOpenEyes.class.getResourceAsStream(path);
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);/*from  ww w.  java 2  s  . co  m*/
    String message = writer.toString();
    HapiContext context = new DefaultHapiContext();
    context.setValidationContext(new NoValidation());
    Parser p = context.getGenericParser();
    Message adt = p.parse(message);
    return adt;
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.JaxbMapperHelper.java

/**
 * Java Object->Xml with encoding./*from  w w w.ja  va2 s.  c o  m*/
 */
public static String toXml(Object root, Class<?> clazz, String encoding) {
    try {
        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(root, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionHelper.unchecked(e);
    }
}