List of usage examples for java.io StringWriter toString
public String toString()
From source file:com.trusolve.ant.filters.JsonToYamlFilter.java
public static Reader readDocument(Reader in) throws JsonProcessingException, IOException { ObjectMapper jsonIn = new ObjectMapper(); JsonNode jn = jsonIn.readTree(in);//from w w w.j a v a2 s. co m YAMLFactory yf = new YAMLFactory(); StringWriter sw = new StringWriter(); yf.createGenerator(sw).setCodec(new ObjectMapper(yf)).writeObject(jn); return new StringReader(sw.toString()); }
From source file:Main.java
public static String writeXMLtoString(org.w3c.dom.Document doc) throws Exception { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result);/*from w w w .jav a 2 s. c om*/ String xmlString = sw.toString(); return xmlString; }
From source file:de.tudarmstadt.ukp.dkpro.tc.core.ExperimentStarter.java
/** * Method which executes Groovy script provided in the pathToScript. * //w ww . ja v a 2 s .co m * @param pathToScript * path to Groovy script. */ public static void start(String pathToScript) throws InstantiationException, IllegalAccessException, IOException { ClassLoader parent = ExperimentStarter.class.getClassLoader(); GroovyClassLoader loader = new GroovyClassLoader(parent); StringWriter writer = new StringWriter(); IOUtils.copy(parent.getResourceAsStream(pathToScript), writer, "UTF-8"); Class<?> groovyClass = loader.parseClass(writer.toString()); GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); Object[] a = {}; groovyObject.invokeMethod("run", a); loader.close(); }
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {/*from ww w. j a va 2 s . co m*/ // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags 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); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException { StringWriter writerTo = new StringWriter(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writerTo); return writerTo.toString(); }
From source file:Main.java
public static String prettyPrintWithDOM3LS(Document document) { DOMImplementation domImplementation = document.getImplementation(); if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) { DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0"); LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) { lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); LSOutput lsOutput = domImplementationLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); lsSerializer.write(document, lsOutput); return stringWriter.toString(); } else {/*w w w . java 2s. c o m*/ throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable."); } } else { throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported."); } }
From source file:Main.java
public static String marshalToString(Class<?> klass, Object obj) throws JAXBException { try {//from w w w. j ava 2s . c o m context = JAXBContext.newInstance(klass); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException( "There was a problem creating a JAXBContext object for formatting the object to XML."); } StringWriter sw = new StringWriter(); String result = null; try { marshaller.marshal(obj, sw); result = sw == null ? null : sw.toString(); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } return result; }
From source file:com.floragunn.searchguard.test.helper.file.FileHelper.java
public static final String loadFile(final String file) throws IOException { final StringWriter sw = new StringWriter(); IOUtils.copy(FileHelper.class.getResourceAsStream("/" + file), sw, StandardCharsets.UTF_8); return sw.toString(); }
From source file:com.joliciel.csvLearner.utils.LogUtils.java
/** Return the current exception & stack trace as a String. *//*from w w w . j ava2s . co m*/ public static String getErrorString(Throwable e) { String s = null; try { StringWriter sw = new StringWriter(); PrintWriter ps = new PrintWriter((Writer) sw); e.printStackTrace(ps); sw.flush(); s = sw.toString(); sw.close(); } catch (IOException ioe) { // do nothing! } return s; }
From source file:Main.java
public static String nodeToString(Node node) throws TransformerException { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString().replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim(); }