List of usage examples for java.io StringWriter toString
public String toString()
From source file:Main.java
/** * Creates XML from W3C Document from the xml. * * @param document the xml that needs to be converted. * @return the XML./* w w w . j a v a 2 s . co m*/ * @throws Exception is there is some error during operation. */ public static String createXml(Document document) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); DOMSource source = new DOMSource(document); transformer.transform(source, result); return stringWriter.toString(); }
From source file:inn.eatery.clientTest.ClientHandler.java
public static void pubEvent(Channel ch, JSONObject e) { // Prepare the HTTP request. JSONArray es = new JSONArray(); es.put(e);//ww w .ja v a 2s. co m StringWriter estr = new StringWriter(); es.write(estr); String contentStr = estr.toString(); FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events", ch.alloc().buffer().writeBytes(contentStr.getBytes())); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json"); request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes()); // Set some example cookies. request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder .encode(new DefaultCookie("userId", "ahanda"), new DefaultCookie("sessStart", "kal"))); l.info("readable bytes {}", request.content().readableBytes()); // Send the HTTP request. ch.writeAndFlush(request); }
From source file:Main.java
/** * Transforms a {@link Source} object into a {@link String} representation. * /* www . j a v a 2 s . c om*/ * @param xml the source input. * @param pretty if {@code true} then pretty print with white space. * @return the string output. * @throws TransformerException */ public static String toString(Source xml, boolean pretty) throws TransformerException { StringWriter writer = new StringWriter(); toString(xml, pretty, writer); return writer.toString(); }
From source file:Main.java
/** * Transform a DOM Node to String./*www. j av a2 s . com*/ * @param node The Node to be transformed. * @return a String representation. * @throws ParserConfigurationException Parser confiuration exception * @throws TransformerException Transformer exception */ public static String toString(final Node node) throws ParserConfigurationException, TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter writer = new StringWriter(); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); }
From source file:Main.java
/** * Gets the node as string.//from w w w .j av a2 s .c om * * @param pNode * the p node * @param encoding * the encoding * @param pRemoveXmlLine * the p remove xml line * @return the node as string * @throws Exception * the exception */ public static String getNodeAsString(Node pNode, String encoding, boolean pRemoveXmlLine) throws Exception { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformNode(pNode, encoding, pRemoveXmlLine, result); return writer.toString(); }
From source file:Main.java
public static String getXmlFileToString(String filePath) throws TransformerException { File xmlFile = new File(filePath); StringWriter writer = new StringWriter(); TransformerFactory fac = TransformerFactory.newInstance(); Transformer x = fac.newTransformer(); x.transform(new StreamSource(xmlFile), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String asXml(Object object) throws IOException { try {/*from ww w . ja v a 2s. com*/ StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller m = context.createMarshaller(); m.marshal(object, writer); return writer.toString(); } catch (Exception e) { return ""; } }
From source file:Main.java
/** * Returns a stack trace of the {@code Throwable} as a {@code String}. * /* ww w.j av a 2 s . c om*/ * @param throwable * The throwable for which to create the stack trace. * @param expectNull * True if null should be returned when {@code throwable} is null or * false to return "" when {@code throwable} is null * @return null if {@code throwable} is null and {@code expectNull} is true, * "" if {@code throwable} is null and {@code expectNull} is false, * otherwise the stack trace for {@code throwable} */ public static String stackTraceToString(final Throwable throwable, final boolean expectNull) { if (throwable == null) { if (expectNull == true) { return null; } return ""; } StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); throwable.printStackTrace(printWriter); printWriter.close(); return stringWriter.toString(); }
From source file:Main.java
/** * Converts XML document into the string. * @param document XML//from www .j a v a 2 s .c o m * @return XML as string * @throws TransformerException */ public static String toString(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }
From source file:Main.java
/** * * @param source/*ww w . j av a 2 s . c o m*/ * @param xsltSource * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException */ public static synchronized String getElementValue(InputStream source, InputStream xsltSource) throws TransformerConfigurationException, JAXBException, TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(new StreamSource(source), result); return sw.toString(); }