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:de.tudarmstadt.ukp.clarin.webanno.support.JSONUtil.java

public static String toJsonString(MappingJacksonHttpMessageConverter jsonConverter, Object aObject)
        throws IOException {
    StringWriter out = new StringWriter();

    JsonGenerator jsonGenerator = jsonConverter.getObjectMapper().getJsonFactory().createJsonGenerator(out);

    jsonGenerator.writeObject(aObject);/*w  ww.  j ava  2 s . c o m*/
    return out.toString();
}

From source file:Main.java

/**
 * Pulled from apache lang commons project (StringUtils).
 * //from  w  w  w.ja v a2s.c  om
 * <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
 * 
 * @param str String to escape values in, may be null
 * @param escapeSingleQuotes escapes single quotes if <code>true</code>
 * @return the escaped string
 */
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
    if (str == null) {
        return null;
    }
    try {
        StringWriter writer = new StringWriter(str.length() * 2);
        escapeJavaStyleString(writer, str, escapeSingleQuotes);
        return writer.toString();
    } catch (IOException ioe) {
        // this should never ever happen while writing to a StringWriter
        ioe.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String parseObjectToXml(Object obj) {
    if (obj == null) {
        return NO_RESULT;
    }//w ww  .  jav  a 2 s  .c o m
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(obj, sw);
        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:edu.umn.msi.tropix.common.logging.ExceptionUtils.java

public static String toString(@Nullable final Throwable throwable) {
    if (throwable == null) {
        return null;
    }//w w w  . j  av a  2s.  c  om
    final StringWriter stackWriter = new StringWriter();
    throwable.printStackTrace(new PrintWriter(stackWriter));
    return stackWriter.toString();
}

From source file:com.econcept.pingconnectionutility.utility.PingConnectionUtility.java

private static void httpPingable(String targetURI) throws IOException, URISyntaxException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet();
    httpGet.setURI(new URI(targetURI));
    CloseableHttpResponse response = httpClient.execute(httpGet);

    int currentCode = response.getStatusLine().getStatusCode();
    try {/*  ww  w . j  a v  a2 s . c  o  m*/

        if (currentCode >= 200 && currentCode < 300) {
            HttpEntity entity = response.getEntity();

            InputStream responseStream = entity.getContent();

            StringWriter writer = new StringWriter();

            IOUtils.copy(responseStream, writer, "UTF-8");

            System.out.println("Target Server are ok: " + currentCode);
            System.out.println(writer.toString());

            EntityUtils.consume(entity);
        } // if
        else {
            System.out.println("Target Server are not ok: " + currentCode);
        } // else
    } // try
    finally {
        response.close();
    } // finally

}

From source file:models.service.reminder.RemindSender.java

private static String parseTemplate(String template, User oldUser, User newUser, Map<String, Object> params) {
    VelocityContext context = new VelocityContext();
    context.put("oldUser", oldUser);
    context.put("newUser", newUser);

    DateTime now = new DateTime(new Date());
    context.put("now", now);

    context.put("cdnUrl", ConfigFactory.getString("upload.url"));

    if (MapUtils.isNotEmpty(params)) {
        for (Map.Entry<String, Object> e : params.entrySet()) {
            context.put(e.getKey(), e.getValue());
        }//www  .  j  a  va  2 s  .  c  o m
    }

    StringWriter writer = new StringWriter();
    engine.evaluate(context, writer, "safetyRemindTemplate", template);

    return writer.toString();
}

From source file:Main.java

private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {/*from  w  w w.  ja v a2s  .c o m*/
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
        StringWriter writer = new StringWriter();

        char[] chars = new char[256];
        int count = 0;
        while ((count = reader.read(chars)) > 0) {
            writer.write(chars, 0, count);
        }

        return writer.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:Main.java

public static String getXMLString(Document dom, boolean bOmitDeclaration, String sEncoding) {
    String sOmit = (bOmitDeclaration ? "yes" : "no");
    try {//from w w  w.j a v a2 s .  c  o m
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        if (sEncoding != null)
            transformer.setOutputProperty(OutputKeys.ENCODING, sEncoding);
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, sOmit);
        transformer.transform(new DOMSource(dom), new StreamResult(buffer));
        return buffer.toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String nodeToString(Node node) throws TransformerFactoryConfigurationError, TransformerException {
    if (node == null)
        return "";
    StringWriter sw = new StringWriter();
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:com.simiacryptus.util.io.IOUtil.java

/**
 * Write json.//from   w  ww. j  av a  2 s.  co  m
 *
 * @param <T>  the type parameter
 * @param obj  the obj
 * @param file the file
 */
public static <T> void writeJson(T obj, File file) {
    StringWriter writer = new StringWriter();
    try {
        objectMapper.writeValue(writer, obj);
        Files.write(file.toPath(), writer.toString().getBytes());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}