List of usage examples for java.io CharArrayWriter toString
public String toString()
From source file:org.dhatim.SmooksUtil.java
/** * Utility method to filter the content in the specified {@link InputStream} for the specified {@link org.dhatim.container.ExecutionContext}. * <p/>//from w ww . j a v a2 s .com * Useful for testing purposes. In a real scenario, use * {@link Smooks#filter(org.dhatim.container.ExecutionContext,javax.xml.transform.Source,javax.xml.transform.Result)}. * <p/> * The content of the returned String is totally dependent on the configured * {@link org.dhatim.delivery.dom.DOMElementVisitor} and {@link org.dhatim.delivery.dom.serialize.SerializationUnit} * implementations. * * @param executionContext Execution context for the filter. * @param stream Stream to be processed. Will be closed before returning. * @param smooks The {@link Smooks} instance through which to perform the filter and serialize operations. * @return The Smooks processed content buffer. * @throws IOException Exception using or closing the supplied InputStream. * @throws SmooksException Excepting processing content stream. */ public static String filterAndSerialize(ExecutionContext executionContext, InputStream stream, Smooks smooks) throws SmooksException { String responseBuf = null; CharArrayWriter writer = new CharArrayWriter(); try { smooks.filterSource(executionContext, new StreamSource(stream), new StreamResult(writer)); responseBuf = writer.toString(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { new SmooksException("Failed to close stream...", e); } } writer.close(); } return responseBuf; }
From source file:org.opennaas.extensions.ofertie.ncl.provisioner.components.UserManager.java
private static String readXMLFile(String filePath) throws IOException { log.debug("Reading content of file " + filePath); InputStreamReader reader = null; BufferedReader bufferReader = null; try {/*from w ww . j a va 2 s. c om*/ URL url = new URL(filePath); reader = new InputStreamReader(url.openStream()); bufferReader = new BufferedReader(reader); } catch (MalformedURLException ignore) { // They try a file File file = new File(filePath); bufferReader = new BufferedReader(new FileReader(file)); } CharArrayWriter w = new CharArrayWriter(); int n; char[] buf = new char[8192]; while ((n = bufferReader.read(buf)) > 0) { w.write(buf, 0, n); } bufferReader.close(); String fileContent = w.toString(); log.debug("Readed content of file " + filePath); return fileContent; }
From source file:opendap.aws.glacier.BesMetadataExtractor.java
private static String runSysCommand(String sysCmd) throws IOException { Process p = null;/*from w w w. j a va 2 s. c o m*/ try { System.out.println("COMMAND: " + sysCmd); Runtime rt = Runtime.getRuntime(); p = rt.exec(sysCmd.toString()); InputStream in = p.getInputStream(); OutputStream out = p.getOutputStream(); InputStream err = p.getErrorStream(); CharArrayWriter caw = new CharArrayWriter(); IOUtils.copy(in, caw); IOUtils.copy(err, System.err); return caw.toString(); } finally { if (p != null) p.destroy(); } }
From source file:Main.java
/** * * @param document//from w ww .ja va 2 s . c o m * @return * @throws TransformerException * @throws UnsupportedEncodingException */ private static String write(Document document) throws TransformerException, UnsupportedEncodingException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); Source source = new DOMSource(document); CharArrayWriter os = new CharArrayWriter(); Result result = new StreamResult(os); transformer.transform(source, result); return os.toString(); }
From source file:org.jbpm.bpel.wsdl.xml.WsdlUtil.java
public static Definition writeAndRead(Definition definition) throws WSDLException { // write the definition to an in-memory sink CharArrayWriter memorySink = new CharArrayWriter(); factory.newWSDLWriter().writeWSDL(definition, memorySink); // read the definition back from memory try {//from w ww. ja va2 s.c o m return factory.newWSDLReader().readWSDL(definition.getDocumentBaseURI(), XmlUtil.parseText(memorySink.toString())); } catch (SAXException e) { throw new WSDLException(WSDLException.PARSER_ERROR, "could not read the WSDL definitions back", e); } }
From source file:Main.java
public static String encodeAttribute(String value) { CharArrayWriter writer = new CharArrayWriter(); int size = value.length(); for (int i = 0; i < size; i++) { char c = value.charAt(i); switch (c) { case '&': writer.append(ENCODED_AMPERSAND); break; case '"': writer.append(ENCODED_DOUBLE_QUOTE); break; case '<': writer.append(ENCODED_LESS_THAN); break; case '>': writer.append(ENCODED_GREATER_THAN); break; default:// ww w . j a va 2 s. c om writer.append(c); break; } } return writer.toString(); }
From source file:com.ephesoft.dcma.util.XMLUtil.java
/** * This method should eventually replace the toXMLString(Document doc) method. * /*from w ww . j av a 2 s. c om*/ * @param xmlNode Node * @return String * @throws TransformerException * @throws Exception */ public static String xmlNode2String(Node xmlNode) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(PROPERTY_INDENT, VALUE_YES); javax.xml.transform.dom.DOMSource src = new javax.xml.transform.dom.DOMSource(xmlNode); java.io.CharArrayWriter writer = new java.io.CharArrayWriter(WRITER_CONST); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(writer); transformer.transform(src, result); return writer.toString(); }
From source file:com.ephesoft.dcma.util.XMLUtil.java
/** * toXMLString./*ww w . jav a 2 s. c om*/ * * @param document * @return * @throws TransformerException */ public static String toXMLString(Document document) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(PROPERTY_INDENT, VALUE_YES); javax.xml.transform.dom.DOMSource src = new javax.xml.transform.dom.DOMSource(document); java.io.CharArrayWriter writer = new java.io.CharArrayWriter(WRITER_CONST); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(writer); transformer.transform(src, result); return writer.toString(); }
From source file:com.ephesoft.dcma.util.XMLUtil.java
/** * To apply Transformation.// ww w.jav a2s . c om * * @param doc Document * @param xsltPath String * @return String * @throws TransformerException */ public static String applyTransformation(Document doc, String xsltPath) throws TransformerException { InputStream xsltFile = XMLUtil.class.getClassLoader().getResourceAsStream(xsltPath); TransformerFactory xsltFactory = TransformerFactory.newInstance(); StreamSource inputSource = new StreamSource(xsltFile); Transformer transformer = xsltFactory.newTransformer(inputSource); javax.xml.transform.dom.DOMSource src = new javax.xml.transform.dom.DOMSource(doc); java.io.CharArrayWriter writer = new java.io.CharArrayWriter(WRITER_CONST); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(writer); transformer.transform(src, result); return writer.toString(); }
From source file:com.ephesoft.dcma.util.XMLUtil.java
/** * To apply XSL Transformation./*from ww w . jav a2 s. co m*/ * * @param xmlDocument Document * @param stylesheetFileLocation String * @return byte[] * @throws TransformerException */ public static byte[] applyXSLTransformation(Document xmlDocument, String stylesheetFileLocation) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheetFileLocation)); transformer.setOutputProperty(PROPERTY_INDENT, VALUE_YES); javax.xml.transform.dom.DOMSource src = new javax.xml.transform.dom.DOMSource(xmlDocument); java.io.CharArrayWriter writer = new java.io.CharArrayWriter(WRITER_CONST); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult(writer); transformer.transform(src, result); return writer.toString().getBytes(); }