List of usage examples for java.io StringWriter getBuffer
public StringBuffer getBuffer()
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;// w w w .j a v a 2 s .c o m try { transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static String documentToString(Document document) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {// w w w .j a v a2 s . co m transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException e) { throw new RuntimeException(e); } String output = writer.getBuffer().toString().replaceAll("\n|\r", ""); return output; }
From source file:Main.java
public static String XMLToString(Document doc, Boolean singleLine) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {/* ww w .ja v a 2 s. co m*/ transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String string = writer.getBuffer().toString(); if (!singleLine) { return string; } return string.replaceAll("\n|\r", ""); }
From source file:Main.java
public static String formatXMLStr(String xml) { String output = null;/*from w w w .j a v a 2 s .com*/ try { Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); // output = writer.getBuffer().toString().replaceAll("\n|\r", ""); output = writer.getBuffer().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return output; }
From source file:Main.java
/** * Represent xml node as string/* www. jav a2 s . co m*/ * @param node to be represented * @return node text */ public static String xmlToString(Node node) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); Transformer transformer = getTransformerFactory().newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerException e) { throw new RuntimeException("Can't transfor XML to String", e); } }
From source file:com.netflix.hystrix.contrib.requests.stream.HystrixRequestEventsJsonStream.java
public static String convertRequestToJson(HystrixRequestEvents request) throws IOException { StringWriter jsonString = new StringWriter(); JsonGenerator json = jsonFactory.createGenerator(jsonString); writeRequestAsJson(json, request);/*from w w w. j a v a 2s . c o m*/ json.close(); return jsonString.getBuffer().toString(); }
From source file:com.github.lothar.security.acl.jpa.query.AclJpaQuery.java
private static String getStackTrace(Throwable throwable) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw);/*from w ww . j a v a 2 s . c o m*/ return sw.getBuffer().toString(); }
From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java
/** * Method that converts a W3C {@link Document} object to a String * @param document the document to convert * @param indent whether lines must be indented or not * @param omitDeclaration whether the XML declaration should be omitted or not. * @param encoding the encoding placed at the XML declaration. Be carefully: Specifying here an encoding only takes effect at the XML declaration. If you are going to write the string to an output stream (e.g. to a file), you must also take care of the encoding there, for example, by means of {@link org.apache.commons.io.output.XmlStreamWriter}. * @return the {@link String} representation of the document * @throws TransformerException if there are problems during the conversion */// ww w . j a v a 2s . c o m public static String convertW3CDocumentToString(Document document, boolean indent, boolean omitDeclaration, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
public static String documentToString(Document d) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {/*ww w .ja va 2 s .c om*/ transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return null; } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(d), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); return null; } return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:com.kaaprotech.satu.util.CmdLineUtil.java
public static String getCommandLineUsageMessage() { final HelpFormatter formatter = new HelpFormatter(); final StringWriter sw = new StringWriter(); final PrintWriter pw = new PrintWriter(sw, true); pw.println();/*from ww w . j a v a 2 s . co m*/ formatter.printHelp(pw, 120, "Satu", null, getOptions(), 2, 10, null); return sw.getBuffer().toString(); }