List of usage examples for java.io StringWriter toString
public String toString()
From source file:Main.java
public static String convertXMLFileToString(String fileName) { try {// www . j a v a 2 s . co m DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(stw)); return stw.toString(); } catch (Exception e) { System.out.println("Unable to load xml file: " + e.toString()); } return null; }
From source file:net.alpha.velocity.spring.support.VelocityEngineUtils.java
/** * Merge the specified Velocity template with the given model into a String. * <p>When using this method to prepare a text for a mail to be sent with Spring's * mail support, consider wrapping VelocityException in MailPreparationException. * * @param velocityEngine VelocityEngine to work with * @param templateLocation the location of template, relative to Velocity's resource loader path * @param encoding the encoding of the template file * @param model the Map that contains model names as keys and model objects as values * @return the result as String//from w ww . java 2 s . com * @throws VelocityException if the template wasn't found or rendering failed * @see org.springframework.mail.MailPreparationException */ public static String mergeTemplateIntoString(VelocityEngine velocityEngine, String templateLocation, String encoding, Map<String, Object> model) throws VelocityException { StringWriter result = new StringWriter(); mergeTemplate(velocityEngine, templateLocation, encoding, model, result); return result.toString(); }
From source file:mpq.MpqUtil.java
public static String convertMpqFileToString(MpqFile file) throws IOException { if (file == null) return null; StringWriter writer = new StringWriter(); IOUtils.copy(file.getInputStream(), writer); String theString = writer.toString(); return theString; }
From source file:Main.java
public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver, Properties transformerProperties, HashMap<String, String> params, String transformerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = null; if (transformerClassName == null) transformerFactory = TransformerFactory.newInstance(); else {//from w w w . j a v a2 s. c om Class transformerClass = Class.forName(transformerClassName); Constructor defaultConstructor = transformerClass.getConstructor(null); transformerFactory = (TransformerFactory) transformerClass.newInstance(); } if (resolver != null) transformerFactory.setURIResolver(resolver); Transformer transformer = transformerFactory.newTransformer(xslt); if (transformerFactory != null) transformer.setOutputProperties(transformerProperties); if (params != null) { for (Map.Entry<String, String> cursor : params.entrySet()) { transformer.setParameter(cursor.getKey(), cursor.getValue()); } } StringWriter strWriter = new StringWriter(); StreamResult result = new StreamResult(strWriter); transformer.transform(doc, result); return (strWriter.toString()); }
From source file:Main.java
/** * Serialize XML Node to string/*from ww w. j ava 2s .co m*/ * <p> * Note: this method is supposed to be faster than the Transform version but the output control * is limited. If node is Document node, it will output XML PI which sometimes we want to avoid. * * @param doc XML document * @param node Node to be serialized * @param encoding encoding for the output * @return String representation of the Document * @throws IOException */ public static String serializeToStringLS(Document doc, Node node, String encoding) throws IOException { DOMImplementationLS domImpl = (DOMImplementationLS) doc.getImplementation(); LSSerializer lsSerializer = domImpl.createLSSerializer(); LSOutput output = domImpl.createLSOutput(); output.setEncoding(encoding); StringWriter writer = new StringWriter(); output.setCharacterStream(writer); lsSerializer.write(node, output); writer.flush(); return writer.toString(); }
From source file:Main.java
private static String transformToStringResult(Document document, Transformer transformer) throws TransformerConfigurationException, TransformerException { StringWriter stringWriter = new StringWriter(); synchronized (transformer) { transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); }/*from www. j a v a 2s. co m*/ return stringWriter.toString(); }
From source file:Main.java
/** * Converts a Node into a String using the DOM, level 3, Load/Save serializer. * /*from www .j a v a 2 s. c o m*/ * @param node the node to be written to a string * * @return the string representation of the node */ public static String nodeToString(Node node) { StringWriter writer = new StringWriter(); writeNode(node, writer); return writer.toString(); }
From source file:Utilities.java
/** * Return the stack trace from the passed exception as a string * * @param th The exception to retrieve stack trace for. *//*from w w w .java2 s. c om*/ public static String getStackTrace(Throwable th) { if (th == null) { throw new IllegalArgumentException("Throwable == null"); } StringWriter sw = new StringWriter(); try { PrintWriter pw = new PrintWriter(sw); try { th.printStackTrace(pw); return sw.toString(); } finally { pw.close(); } } finally { try { sw.close(); } catch (IOException ex) { } } }
From source file:Main.java
public static String createPrintableString(Document doc) { String s = ""; try {//from w w w . j a va 2s .c o m // set up a transformer 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 StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); s = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
/** * Serialise the supplied W3C DOM subtree. * * @param nodeList The DOM subtree as a NodeList. * @param format Format the output./*from www . j a v a 2 s . com*/ * @return The subtree in serailised form. * @throws DOMException Unable to serialise the DOM. */ public static String serialize(NodeList nodeList, boolean format) throws DOMException { StringWriter writer = new StringWriter(); serialize(nodeList, format, writer); return writer.toString(); }