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

/**
 * dump the exception to string/*from  w  w  w  .java  2s.c o  m*/
 */
public static String dumpException(Throwable throwable) {
    StringWriter stringWriter = new StringWriter(160);
    stringWriter.write(throwable.getClass().getName());
    stringWriter.write(":\n");
    throwable.printStackTrace(new PrintWriter(stringWriter));
    return stringWriter.toString();
}

From source file:Main.java

public static String trans(Element ele) {
    StringWriter writer = new StringWriter();
    try {//  www.  j  av  a2  s .c om
        transformer.transform(new DOMSource(ele), new StreamResult(writer));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

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

/**
 * ?//from  w ww.  j a va2 s  . c o m
 */
public static String renderString(String templateString, Map<String, ?> model) {
    try {
        StringWriter result = new StringWriter();
        Template t = new Template("default", new StringReader(templateString), new Configuration());
        t.process(model, result);
        return result.toString();
    } catch (Exception e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:Main.java

public static String prettyPrintWithTrAX(Document document) throws TransformerException {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    DOMSource domSource = new DOMSource(document);
    transformer.transform(domSource, streamResult);
    return stringWriter.toString();
}

From source file:de.iteratec.iteraplan.businesslogic.service.SavedQueryXmlHelper.java

/**
 * Serializes a JAXB object to an XML string.
 * /*from w w  w .j a v a2  s. c om*/
 * @param schema Schema to use.
 * @param xml JAXB object to serialize.
 * @return the XML representation of the passed object
 */
public static String writeQueryToXMLString(String schema, SerializedQuery<?> xml) {
    try {
        Marshaller marshaller = getMarshaller(xml.getClass(), schema);
        StringWriter sw = new StringWriter();
        marshaller.marshal(xml, sw);

        return sw.toString();
    } catch (JAXBException e) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    } catch (SAXException e) {
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    }
}

From source file:Main.java

public static void dumpElement(Logger log, Element elem) {

    try {//from  w ww .  j a v  a  2 s .c om
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(elem), new StreamResult(buffer));

        log.debug(buffer.toString());
    } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) {
        log.error("Failed to dump element", ex);
    }
}

From source file:Main.java

/**
 * Print all running threads/* www  . j  a  v a 2s. c o m*/
 */
public static void printRunningThreads() {
    // Get the thread listing as a string using a StringWriter stream
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    listAllThreads(out);
    out.close();
    String threadListing = stringWriter.toString();
    System.out.println(threadListing);
}

From source file:org.biopax.validator.api.AbstractAspect.java

private static String getStackTrace(Throwable aThrowable) {
    StringWriter strw = new StringWriter();
    PrintWriter printWriter = new PrintWriter(strw);
    aThrowable.printStackTrace(printWriter);
    return strw.toString();
}

From source file:eu.planets_project.tb.impl.serialization.JaxbUtil.java

/**
* Marshalling via Jaxb: Creates a String Serialization of the requested class for
* the content of Object objectToSerialize. The provided object and the requested class need to be of the same Type
* @param <T>//from  w w w . j a v  a2  s  .c  o m
* @param objectClass
* @param objectToSerialize
* @return
* @throws Exception
*/
public static <T> String marshallObjectwithJAXB(Class<T> objectClass, T objectToSerialize) throws Exception {
    JAXBContext context;
    try {
        context = JAXBContext.newInstance(objectClass);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        //now call the actual marshalling job
        m.marshal(objectToSerialize, sw);
        return sw.toString();
    } catch (Exception e) {
        log.error("marshalWorkflowResult failed for objectClass: " + objectClass + " with " + e);
        throw e;
    }
}

From source file:email.mandrill.SendMail.java

public static void checkPingPong() {
    try {//w  w  w. j a va 2s .  c  om
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("https://mandrillapp.com/api/1.0/users/ping.json");

        JSONObject obj = new JSONObject();
        obj.put("key", SendEmail.MANDRILL_KEY);
        String request = obj.toString();
        HttpEntity entity = new ByteArrayEntity(request.getBytes("UTF-8"));
        httppost.setEntity(entity);
        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity responseEntity = response.getEntity();

        if (responseEntity != null) {
            InputStream instream = responseEntity.getContent();
            try {
                StringWriter writer = new StringWriter();
                IOUtils.copy(instream, writer, "UTF-8");
                String theString = writer.toString();
                logger.log(Level.INFO, theString);
            } finally {
                instream.close();
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, util.Utility.logMessage(e, "Exception while updating org name:", null));
    }
}