List of usage examples for java.io StringWriter getBuffer
public StringBuffer getBuffer()
From source file:Main.java
public static String convertResultSetToXML(ResultSet rs) throws SQLException, ParserConfigurationException, TransformerException { ResultSetMetaData rsmd = rs.getMetaData(); int colCount = rsmd.getColumnCount(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Element results = doc.createElement("Results"); doc.appendChild(results);// w w w . ja v a 2s .c om while (rs.next()) { Element row = doc.createElement("Row"); results.appendChild(row); for (int i = 1; i <= colCount; i++) { String columnName = rsmd.getColumnName(i); Object value = rs.getObject(i); if (value != null) { Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_")); node.appendChild(doc.createTextNode(value.toString())); row.appendChild(node); } } } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
private static byte[] serializeXML(Transformer transformer, Source input) throws UnsupportedEncodingException, TransformerException { StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); transformer.transform(input, result); String streamString = stringWriter.getBuffer().toString(); byte[] byteArray = streamString.getBytes("UTF-8"); return byteArray; }
From source file:StringUtils.java
/** * Convert an exception to a String with full stack trace * @param ex the exception/* ww w . j a va 2 s .c o m*/ * @return a String with the full stacktrace error text */ public static String getStringFromStackTrace(Throwable ex) { if (ex == null) { return ""; } StringWriter str = new StringWriter(); PrintWriter writer = new PrintWriter(str); try { ex.printStackTrace(writer); return str.getBuffer().toString(); } finally { try { str.close(); writer.close(); } catch (IOException e) { //ignore } } }
From source file:Main.java
public static String toXMLString(Node node) { if (node == null) { return ""; }//from w w w . j a va 2s. com try { Transformer tran = tf.newTransformer(); tran.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter swriter = new StringWriter(); Source src = new DOMSource(node); Result res = new StreamResult(swriter); tran.transform(src, res); return swriter.getBuffer().toString(); } catch (Exception e) { e.printStackTrace(); return ""; } }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./*from ww w .j a v a2s. co m*/ * @param encoding The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:com.maddyhome.idea.copyright.util.VelocityHelper.java
public static String evaluate(PsiFile file, Project project, Module module, String template) { VelocityEngine engine = getEngine(); VelocityContext vc = new VelocityContext(); vc.put("today", new DateInfo()); vc.put("file", new FileInfo(file)); vc.put("project", new ProjectInfo(project)); vc.put("module", new ModuleInfo(module)); vc.put("username", System.getProperty("user.name")); try {/*from ww w . j a va 2s . com*/ StringWriter sw = new StringWriter(); engine.evaluate(vc, sw, CopyrightProjectPlugin.class.getName(), template); return sw.getBuffer().toString(); } catch (Exception e) { return ""; } }
From source file:com.thinkbiganalytics.feedmgr.nifi.NifiTemplateParser.java
public static String documentToString(Document doc) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
public static String getDtdAsString(String uri) throws IOException { Reader in = null;//w w w . ja v a 2 s . com if ((uri.startsWith("http")) || (uri.startsWith("ftp")) || (uri.startsWith("file:"))) in = new InputStreamReader(new URL(uri).openStream()); else { in = new FileReader(uri); } StringWriter out = new StringWriter(); char[] buffer = new char[4096]; for (int count = in.read(buffer); count != -1; count = in.read(buffer)) { out.write(buffer, 0, count); } return out.getBuffer().toString(); }
From source file:Main.java
/** * Convert the document to an array of bytes. * //from w w w. j a va2s .c o m * @param doc * The XML document. * @param encoding * The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException * If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./* www . ja va 2 s . co m*/ * @param encoding The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); try { return writer.getBuffer().toString().getBytes(encoding); } catch (UnsupportedEncodingException e) { throw new TransformerException("Unsupported Encoding", e); } }