List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:org.apache.kylin.jdbc.TestUtil.java
public static String getResourceContent(String path) { ClassLoader loader = Thread.currentThread().getContextClassLoader(); StringWriter writer = new StringWriter(); InputStream is = loader.getResourceAsStream(path); if (is == null) { throw new IllegalArgumentException(new FileNotFoundException(path + " not found in class path")); }// w ww. j av a 2 s . c o m try { IOUtils.copy(is, writer, "utf-8"); return writer.toString(); } catch (IOException e) { IOUtils.closeQuietly(is); throw new RuntimeException(e); } }
From source file:com.mlo55.utils.XMLUtils.java
/** * Prettifies XML data, e.g. add indentation. * @throws Exception// ww w . j a v a 2s . c om */ public static String formatXml(String result) throws Exception { Source source = new StringSource(result); Transformer transformer = getTransformer(); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(source, streamResult); final String xmlOut = streamResult.getWriter().toString(); return xmlOut; }
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 ava 2 s . com * * @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
/** * Format the provided XML input./*from w ww . j a v a 2 s .co m*/ * * @param input * XML input to format. * @param indent * Indentation to use on formatted XML. * @return Formatted XML. * @throws TransformerException * if some problem occur while processing the xml * @see #prettyFormat(String) */ public static String prettyFormat(String input, Integer indent) throws TransformerException { if (input != null) { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent == null ? 2 : indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } return input; }
From source file:StringUtils.java
/** * Make a string representation of the exception. * @param e The exception to stringify// w w w . java 2s . com * @return A string with exception name and call stack. */ public static String stringifyException(Throwable e) { StringWriter stm = new StringWriter(); PrintWriter wrt = new PrintWriter(stm); e.printStackTrace(wrt); wrt.close(); return stm.toString(); }
From source file:Main.java
public static String getString(Node doc) { StringWriter sw = new StringWriter(); try {//from w ww .ja v a 2s .c o m TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree trans.transform(new DOMSource(doc), new StreamResult(sw)); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String toXMLString(final DOMSource value) { final StringWriter writer = new StringWriter(); try {/* w ww .j a v a 2 s. c om*/ final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(value, new StreamResult(writer)); } catch (final Throwable t) { //todo throw flash error throw new RuntimeException(t); } final String result = writer.toString(); return result.substring(0, result.length() - 1); }
From source file:Main.java
public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory .newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:com.joliciel.jochre.search.webClient.SearchWebClientUtils.java
public static String getJson(URL url) { try {/*w w w . ja va 2 s . c o m*/ URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); String json = writer.toString(); return json; } catch (IOException e) { LogUtils.logError(LOG, e); throw new RuntimeException(e); } }
From source file:Main.java
public static String toString(Element e) { try {// w w w .j a v a2 s .co m TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer xform = tfactory.newTransformer(); Source src = new DOMSource(e); java.io.StringWriter writer = new StringWriter(); Result result = new javax.xml.transform.stream.StreamResult(writer); xform.transform(src, result); return writer.toString(); } catch (Exception ex) { return "Unable to convert to string: " + ex.toString(); } }