List of usage examples for java.io StringWriter toString
public String toString()
From source file:Main.java
public static String transformXmlToString(Document doc, String encoding) throws ParserConfigurationException, TransformerException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.newDocument(); Element e = doc.getDocumentElement(); Node n = document.importNode(e, true); document.appendChild(n);/* w ww . ja v a2s .co m*/ // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(document); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
public static String toXMLString(Document doc) throws TransformerException { 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);// ww w .j a va 2 s. c om String xmlString = sw.toString(); return xmlString; }
From source file:org.jboss.tools.livereload.core.internal.util.ReloadCommandGenerator.java
private static String buildRefreshCommand(final String location, final boolean liveCSS) throws IOException, URISyntaxException { final Map<String, Object> reloadArgs = new LinkedHashMap<String, Object>(); reloadArgs.put("command", "reload"); reloadArgs.put("path", location); reloadArgs.put("liveCSS", liveCSS); final StringWriter commandWriter = new StringWriter(); objectMapper.writeValue(commandWriter, reloadArgs); return commandWriter.toString(); }
From source file:Main.java
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);/*w w w .jav a 2 s . co m*/ String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
From source file:org.pivotal.sqlfire.HibernateTransactionalBatchInsertsTest.java
protected static SessionFactory createSessionFactoryUsingLocalSessionFactoryBean() { System.out.printf("Using Spring's LocalSessionFactoryBean class to configure Hibernate!%n"); try {//from w ww . j a v a 2 s .c o m LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setAnnotatedClasses(User.class); factoryBean.setHibernateProperties(createHibernateConfigurationSettings()); factoryBean.afterPropertiesSet(); return factoryBean.getObject(); } catch (IOException e) { StringWriter writer = new StringWriter(); e.printStackTrace(new PrintWriter(writer)); fail(writer.toString()); throw new RuntimeException(e); } }
From source file:Main.java
/** * Converts a document object to an xml string * @param document the document to convert * @param documentTransformer the DOM document transformer * @return the xml string/*from ww w.j a v a 2 s . c om*/ */ public static String documentToString(Document document, Transformer documentTransformer) throws TransformerException { StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(document); documentTransformer.transform(source, result); return sw.toString(); }
From source file:eu.seaclouds.platform.dashboard.util.ObjectMapperHelpers.java
/** * Transforms an annotated Object to a XML string using javax.xml.bind.Marshaller * * @param object to transform to a XML String * @return a XML string representing the object * @throws IOException if is not possible to parse the object *///from w w w.ja v a2s. com public static String ObjectToXml(Object object) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); StringWriter sw = new StringWriter(); marshaller.marshal(object, sw); return sw.toString(); }
From source file:Main.java
/** * Serialise the supplied W3C DOM subtree. * * @param node The DOM node to be serialized. * @param format Format the output.//from w w w.jav a 2s .c o m * @return The subtree in serailised form. * @throws DOMException Unable to serialise the DOM. */ public static String serialize(final Node node, boolean format) throws DOMException { StringWriter writer = new StringWriter(); serialize(node, format, writer); return writer.toString(); }
From source file:Main.java
private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(new DOMSource(document), streamResult); return stringWriter.toString(); }
From source file:com.heliosdecompiler.helios.Helios.java
public static void displayError(Throwable t) { t.printStackTrace();/*from w ww .ja va 2 s . c o m*/ StringWriter writer = new StringWriter(); t.printStackTrace(new PrintWriter(writer)); JOptionPane.showMessageDialog(null, writer.toString(), t.getClass().getSimpleName(), JOptionPane.INFORMATION_MESSAGE); }