List of usage examples for java.io StringWriter toString
public String toString()
From source file:com.bsb.cms.commons.template.freemarker.util.FreemarkerUtils.java
/** * ?spring (org.springframework.ui.freemarker.FreeMarkerTemplateUtils) * Process the specified FreeMarker template with the given model and write * the result to the given Writer./*from w w w . j av a 2s . c o m*/ * <p>When using this method to prepare a text for a mail to be sent with Spring's * mail support, consider wrapping IO/TemplateException in MailPreparationException. * @param model the model object, typically a Map that contains model names * as keys and model objects as values * @return the result as String * @throws IOException if the template wasn't found or couldn't be read * @throws freemarker.template.TemplateException if rendering failed * @see org.springframework.mail.MailPreparationException */ public static String processTemplateIntoString(Template template, Object model) throws IOException, TemplateException { StringWriter result = new StringWriter(); template.process(model, result); return result.toString(); }
From source file:com.dianping.lion.util.ThrowableUtils.java
public static String extractStackTrace(Throwable t, int maxLen) { StringWriter me = new StringWriter(); PrintWriter pw = new PrintWriter(me); t.printStackTrace(pw);/* ww w.j a v a 2 s . co m*/ pw.flush(); return StringUtils.substring(me.toString(), 0, maxLen); }
From source file:Main.java
public static String readAsString(@NonNull InputStream is) throws IOException { StringWriter writer = new StringWriter(); InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); copy(reader, writer);//from w w w .j a v a2 s . com return writer.toString(); }
From source file:es.sm2.openppm.core.plugin.PluginTemplate.java
/** * Load template for plugin/*from w w w. j a v a2 s . c o m*/ * * @param templateName * @param data * @return * @throws IOException * @throws TemplateException */ public static String getTemplate(String templateName, HashMap<String, Object> data) throws IOException, TemplateException { Logger log = LogManager.getLog(PluginTemplate.class); // Print data to logger if (log.isDebugEnabled()) { log.debug("Create Template: " + templateName); if (data != null && !data.isEmpty()) { for (String key : data.keySet()) { log.debug("Parameter: " + key + " Value: " + data.get(key)); } } } // Load template InputStream stream = PluginTemplate.class.getResourceAsStream(templateName); String teamplate = IOUtils.toString(stream); // Create loader StringTemplateLoader stringLoader = new StringTemplateLoader(); stringLoader.putTemplate("template", teamplate); // Create configuration Configuration cfg = new Configuration(); cfg.setTemplateLoader(stringLoader); Template template = cfg.getTemplate("template"); StringWriter writer = new StringWriter(); template.process(data, writer); return writer.toString(); }
From source file:Main.java
/** * Copied from "android.util.Log.getStackTraceString()" in order to avoid usage of Android stack * in unit tests./*from w w w. j a va 2 s . co m*/ * * @return Stack trace in form of String */ static String getStackTraceString(Throwable tr) { if (tr == null) { return ""; } // This is to reduce the amount of log spew that apps do in the non-error // condition of the network being unavailable. Throwable t = tr; while (t != null) { if (t instanceof UnknownHostException) { return ""; } t = t.getCause(); } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); tr.printStackTrace(pw); pw.flush(); return sw.toString(); }
From source file:Main.java
public static String getXMLString(Node doc) throws TransformerException, TransformerConfigurationException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); StringWriter sw = new StringWriter(); Source src = new DOMSource(doc); Result dest = new StreamResult(sw); aTransformer.transform(src, dest);// w w w. j a v a2 s . c om return sw.toString(); }
From source file:Main.java
/** * Converts an XML node to a string.//www . jav a 2 s .c om * @param node the XML node * @param outputProperties the output properties * @return the string */ public static String toString(Node node, Map<String, String> outputProperties) { try { StringWriter writer = new StringWriter(); toWriter(node, writer, outputProperties); return writer.toString(); } catch (TransformerException e) { //should never be thrown because we're writing to string throw new RuntimeException(e); } }
From source file:ed.cracken.code.SimpleTestExp1.java
public static void writeAsJson(List<Map<?, ?>> data) throws IOException { StringWriter sw = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(sw, data);//from w w w .ja v a 2 s . c o m System.out.println(sw.toString()); }
From source file:Main.java
public static String documentToString(Document doc) throws TransformerException { // Configure the transformer TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // Transform the document DOMSource source = new DOMSource(doc); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(source, streamResult); return stringWriter.toString(); }
From source file:cz.incad.kramerius.k5.k5velocity.K5APIRetriever.java
public static String getAsString(String queryString) throws IOException { org.apache.velocity.tools.generic.ResourceTool rt = new ResourceTool(); Map<String, String> c = new HashMap<String, String>(); c.put("bundles", "res.configuration"); rt.configure(c);// www . ja va2s.c o m String k5addr = rt.get("k5addr").toString() + queryString; LOGGER.info("requesting url " + k5addr); InputStream inputStream = RESTHelper.inputStream(k5addr); StringWriter sw = new StringWriter(); org.apache.commons.io.IOUtils.copy(inputStream, sw, "UTF-8"); return sw.toString(); }