List of usage examples for java.io StringWriter close
public void close() throws IOException
From source file:smartrics.rest.fitnesse.fixture.support.Tools.java
public static String fromJSONtoXML(String json) { HierarchicalStreamDriver driver = new JettisonMappedXmlDriver(); StringReader reader = new StringReader(json); HierarchicalStreamReader hsr = driver.createReader(reader); StringWriter writer = new StringWriter(); try {// w ww . j av a2 s.com new HierarchicalStreamCopier().copy(hsr, new PrettyPrintWriter(writer)); return writer.toString(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { Log.warn(e); } } } }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.ElasticSearchDataSetTestBase.java
protected static String getFileAsString(String file) throws Exception { InputStream mappingsFileUrl = Thread.currentThread().getContextClassLoader().getResourceAsStream(file); StringWriter writer = null; String fileContent = null;/* ww w . j av a 2 s. c om*/ try { writer = new StringWriter(); IOUtils.copy(mappingsFileUrl, writer, ENCODING); fileContent = writer.toString(); } finally { if (writer != null) writer.close(); } // Ensure newline characters meet the HTTP specification formatting requirements. return fileContent.replaceAll("\n", "\r\n"); }
From source file:Main.java
public static StringBuffer transformToString(Source xmlSource, Source xslSource) { StringWriter writer = new StringWriter(); Transformer transformer;/*from ww w . j a v a2 s .c o m*/ try { if (xslSource == null) { transformer = TransformerFactory.newInstance().newTransformer(); } else { transformer = TransformerFactory.newInstance().newTransformer(xslSource); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(xmlSource, new StreamResult(writer)); return writer.getBuffer(); } catch (Exception e) { e.printStackTrace(); return writer.getBuffer(); } finally { try { writer.close(); } catch (Exception e) { e.printStackTrace(); } } }
From source file:org.firstopen.singularity.util.XMLUtil.java
public static InputSource createInputSourceFromDocument(Document doc) throws Exception { StringWriter sWriter = null; try {/*from w w w . j av a2s. c om*/ OutputFormat format = new OutputFormat(doc, "UTF-8", true); sWriter = new StringWriter(); XMLSerializer xmlSerializer = new XMLSerializer(sWriter, format); xmlSerializer.setNamespaces(true); xmlSerializer.serialize(doc); InputSource iSource = new InputSource(new StringReader(sWriter.toString())); return iSource; } finally { if (sWriter != null) sWriter.close(); } }
From source file:net.sf.jasperreports.util.CastorUtil.java
/** * @deprecated Replaced by {@link #writeToString(Object)}. *//*from w w w . j a v a 2 s. c o m*/ public static String write(Object object) { StringWriter writer = new StringWriter(); try { write(object, getMappingFileName(object.getClass()), writer); } finally { try { writer.close(); } catch (IOException e) { } } return writer.toString(); }
From source file:Main.java
public static String jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) { // Make sure we are given the correct input. if (xmlClass == null || jaxbElement == null) { return null; }/*from w ww. ja v a 2s.com*/ // We will write the XML encoding into a string. StringWriter writer = new StringWriter(); String result; try { // We will use JAXB to marshal the java objects. final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass); // Marshal the object. Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(jaxbElement, writer); result = writer.toString(); } catch (Exception e) { // Something went wrong so get out of here. return null; } finally { try { writer.close(); } catch (IOException ex) { } } // Return the XML string. return result; }
From source file:Main.java
public static String toXxml(Object bean) { StringWriter stringWriter = null; try {/*from w w w .j a v a 2 s.c om*/ JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); stringWriter = new StringWriter(); marshaller.marshal(bean, stringWriter); String result = stringWriter.toString(); // remove xml declaration result = result.replaceFirst(".*\n", ""); return result; } catch (JAXBException e) { throw new RuntimeException(e); } finally { if (stringWriter != null) try { stringWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:it.tidalwave.northernwind.core.impl.filter.HtmlCleanupFilter.java
/******************************************************************************************************************* * ******************************************************************************************************************/ public static String formatHtml(final @Nonnull String text) throws IOException { final StringWriter sw = new StringWriter(); final BufferedReader br = new BufferedReader(new StringReader(text)); boolean inBody = false; for (;;) {/*from w w w. j a v a2 s . co m*/ final String s = br.readLine(); if (s == null) { break; } if (s.contains("<!-- @nw.HtmlCleanupFilter.enabled=false")) { return text; } if ("</body>".equals(s.trim())) { break; } if (inBody) { sw.write(s + "\n"); } if ("<body>".equals(s.trim())) { inBody = true; } } sw.close(); br.close(); return inBody ? sw.getBuffer().toString() : text; }
From source file:com.athena.peacock.controller.common.component.RHEVMRestTemplate.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private static String marshal(Object obj, String rootElementName) { StringWriter sw = new StringWriter(); try {//from w w w . ja va 2 s .co m JAXBContext jaxbCtx = JAXBContext.newInstance(obj.getClass().getPackage().getName()); Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(new JAXBElement(new QName(rootElementName), obj.getClass(), obj), sw); sw.close(); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sw.toString(); }
From source file:Main.java
public static String toStringFromDoc(Element elem) { String result = null;/* w w w.j av a 2 s. c o m*/ StringWriter strWtr = new StringWriter(); StreamResult strResult = new StreamResult(strWtr); TransformerFactory tfac = TransformerFactory.newInstance(); try { javax.xml.transform.Transformer t = tfac.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, // text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(elem), strResult); } catch (Exception e) { System.err.println("XML.toString(Document): " + e); } result = strResult.getWriter().toString(); try { strWtr.close(); } catch (IOException e) { e.printStackTrace(); } return result; }