List of usage examples for java.io Writer toString
public String toString()
From source file:Main.java
/** * Convert XML Node to String// w ww . j a v a 2 s .c om * * @param node * the node to convert * @return the String equivalent of the node * @throws TransformerException */ public static String nodeToString(final Node node) throws TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); final Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String getExceptionStackTrace(Exception e) { Writer writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); e.printStackTrace(printWriter);/*from ww w . j a v a 2 s. com*/ return writer.toString(); }
From source file:Main.java
private static String indent(String xml) { try {//from www. j a v a2 s . c o m InputSource is = new InputSource(new StringReader(xml)); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); OutputFormat format = new OutputFormat(); format.setIndenting(true); format.setIndent(4); Writer out = new StringWriter(); new XMLSerializer(out, format).serialize(document); return out.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }// w ww . j a v a2s . c o m TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *//*from w w w. j a va2 s . c o m*/ public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String getStackTrace(Throwable aThrowable) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); aThrowable.printStackTrace(printWriter); return result.toString(); }
From source file:Main.java
public static String getStackTrace(Throwable aThrowable) { aThrowable.printStackTrace();//from www. ja v a 2s . c om final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); aThrowable.printStackTrace(printWriter); return result.toString(); }
From source file:Main.java
public static String sourceToString(Source source) throws IOException { try {/*from w ww. j a v a2s . c om*/ Transformer trans = transFactory.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); Writer writer = new StringWriter(); trans.transform(source, new StreamResult(writer)); writer.flush(); return writer.toString(); } catch (TransformerException ex) { throw new IOException(ex); } }
From source file:org.gsweb.components.util.ComponentResourceUtils.java
public static String generatorResource(Class<?> c, String type, String metaInfFile, Map<String, Object> params) throws Exception { StringTemplateLoader templateLoader = new StringTemplateLoader(); templateLoader.putTemplate("resourceTemplate", getResourceSrc(c, type, metaInfFile)); Configuration cfg = new Configuration(Configuration.VERSION_2_3_21); cfg.setTemplateLoader(templateLoader); Template template = cfg.getTemplate("resourceTemplate", Constants.BASE_ENCODING); Writer out = new StringWriter(); template.process(params, out);/*from w ww. ja va2 s. c o m*/ return out.toString(); }
From source file:Main.java
public static String formatDocument(Document document) throws TransformerFactoryConfigurationError, TransformerException, IOException { OutputFormat format = new OutputFormat(document, "UTF-8", true); format.setLineWidth(160);/*from w w w . j av a 2s .com*/ // format.setIndenting(true); format.setIndent(2); // format.setEncoding("UTF-8"); Writer out = new StringWriter(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(document); return out.toString(); }