List of usage examples for java.io StringWriter toString
public String toString()
From source file:Main.java
/** * Convert an XML node to a string. Node can be a document or an element. * /*from ww w.ja v a 2s .co m*/ * @param node * @return */ public static String toXMLString(Node node) { Transformer transformer = null; try { transformer = tFactory.newTransformer(); // System.err.format("Using transformer: %s%n", transformer); } catch (TransformerConfigurationException e) { e.printStackTrace(); } DOMSource source = new DOMSource(node); StringWriter xmlWriter = new StringWriter(); StreamResult result = new StreamResult(xmlWriter); try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } return xmlWriter.toString(); }
From source file:com.cats.version.utils.Utils.java
public static String getException(Exception e) { StringWriter sw = new StringWriter(); PrintWriter printWriter = new PrintWriter(sw); e.printStackTrace(printWriter);/* w w w . ja v a 2 s . c o m*/ return sw.toString(); }
From source file:com.aerospike.examples.pk.StorePrimaryKey.java
/** * Write usage to console./* www . j a va 2 s . co m*/ */ private static void logUsage(Options options) { HelpFormatter formatter = new HelpFormatter(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); String syntax = StorePrimaryKey.class.getName() + " [<options>]"; formatter.printHelp(pw, 100, syntax, "options:", options, 0, 2, null); log.info(sw.toString()); }
From source file:com.splunk.shuttl.testutil.TUtilsTestNG.java
private static String getStackTrace(Exception exception) { StringWriter stackTraceStringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stackTraceStringWriter); exception.printStackTrace(printWriter); printWriter.flush();//w w w. j a va2s .co m return stackTraceStringWriter.toString(); }
From source file:hudson.console.HyperlinkNoteTest.java
private static String annotate(String text) throws IOException { StringWriter writer = new StringWriter(); try (ConsoleAnnotationOutputStream out = new ConsoleAnnotationOutputStream(writer, null, null, StandardCharsets.UTF_8)) { IOUtils.copy(new StringReader(text), out); }/* w w w. j a va 2s . c o m*/ return writer.toString(); }
From source file:jlotoprint.model.Template.java
public static Model load(File templateFile, boolean showFeedback) { Model modelRef = null;/*from w ww . j av a 2 s.c o m*/ try { StringWriter writer = new StringWriter(); IOUtils.copy(new FileInputStream(templateFile), writer, "UTF-8"); Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); modelRef = g.fromJson(writer.toString(), Model.class); } catch (Exception ex) { //Logger.getLogger(Template.class.getName()).log(Level.SEVERE, null, ex); if (showFeedback) { Alert dialog = new Alert(Alert.AlertType.ERROR, "The template you are trying to load is invalid.", ButtonType.OK); dialog.initModality(Modality.APPLICATION_MODAL); dialog.showAndWait(); } } return modelRef; }
From source file:Main.java
public static String xmlToString(Document doc) { try {//w w w .ja v a 2 s .c o m DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }/* w ww . j a v a 2 s. c om*/ cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }
From source file:com.code.savemarks.utils.Utils.java
public static String stackTraceToString(Throwable e) { String retValue = null;//from w ww . j a v a 2s. c o m StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw); e.printStackTrace(pw); retValue = sw.toString(); } finally { try { if (pw != null) pw.close(); if (sw != null) sw.close(); } catch (IOException ignore) { } } return retValue; }
From source file:br.gov.lexml.parser.documentoarticulado.LexMLUtil.java
public static String formatLexML(String xml) { String retorno = null;// w w w . j a va 2s. co m try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(new DOMSource(document), streamResult); retorno = stringWriter.toString(); } catch (Exception e) { e.printStackTrace(); } return retorno; }