List of usage examples for java.io StringWriter toString
public String toString()
From source file:it.cnr.ilc.ilcioutils.IlcInputToString.java
/** * Convert an inputstream into a string/* www . ja v a 2 s .c o m*/ * * @param is the inputstream * @return the string from the input */ public static String convertInputStreamToString(InputStream is) { StringWriter writer = new StringWriter(); String encoding = "UTF-8"; String message = ""; String theString = ""; try { IOUtils.copy(is, writer, encoding); theString = writer.toString(); } catch (Exception e) { message = "IOException in coverting the stream into a string " + e.getMessage(); Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message); } //System.err.println("DDDD " + theString); IOUtils.closeQuietly(is); IOUtils.closeQuietly(writer); return theString; }
From source file:com.plateform.common.util.JaxbMapper.java
/** * Java Collection->Xml with encoding, ?Root ElementCollection. *///from w w w .j a va 2 s. com public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) { try { CollectionWrapper wrapper = new CollectionWrapper(); wrapper.collection = root; JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper); StringWriter writer = new StringWriter(); createMarshaller(clazz, encoding).marshal(wrapperElement, writer); return writer.toString(); } catch (JAXBException e) { throw ExceptionUtils.unchecked(e); } }
From source file:Main.java
public static String convertToString(Node node) { boolean withXMLDeclaration = true; String result;//from w ww .j a va 2 s . c o m if (withXMLDeclaration) { Document document = node.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); result = serializer.writeToString(node); } else { try { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); result = buffer.toString(); } catch (TransformerConfigurationException e) { result = ""; } catch (TransformerException e) { result = ""; } } return result; }
From source file:Main.java
/** * This method performs XSL Transformation. <br> * <b>Deprecated use XmlTransformer.transform</b> * //from ww w.ja va 2s. c om * @param source * The input XML document * @param stylesheet * The XSL stylesheet * @param params * parameters to apply to the XSL Stylesheet * @param outputProperties * properties to use for the xsl transform. Will overload the xsl output definition. * @return The output document transformed * @throws Exception * The exception */ @Deprecated public static String transform(Source source, Source stylesheet, Map<String, String> params, Properties outputProperties) throws Exception { try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(stylesheet); if (outputProperties != null) { transformer.setOutputProperties(outputProperties); } if (params != null) { transformer.clearParameters(); for (Entry<String, String> entry : params.entrySet()) { String name = entry.getKey(); String value = entry.getValue(); transformer.setParameter(name, value); } } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); } catch (TransformerConfigurationException e) { String strMessage = e.getMessage(); if (e.getLocationAsString() != null) { strMessage += ("- location : " + e.getLocationAsString()); } throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause()); } catch (TransformerFactoryConfigurationError e) { throw new Exception("Error transforming document XSLT : " + e.getMessage(), e); } catch (TransformerException e) { String strMessage = e.getMessage(); if (e.getLocationAsString() != null) { strMessage += ("- location : " + e.getLocationAsString()); } throw new Exception("Error transforming document XSLT : " + strMessage, e.getCause()); } catch (Exception e) { throw new Exception("Error transforming document XSLT : " + e.getMessage(), e); } }
From source file:Main.java
/** * Renders an XML node to a string/*from ww w.j a va 2 s.c o m*/ * @param node The xml node to render * @param stripHeader If true, strips the auto-generated XML header from the generated string * @return the rendered string or null if it failed conversion */ public static String renderNode(final Node node, final boolean stripHeader) { if (node == null) return null; try { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(node), new StreamResult(writer)); final String s = writer.toString(); if (stripHeader) { return s.replace(XML_HEADER, ""); } return s; } catch (Throwable e) { return null; } }
From source file:com.aerospike.example.cache.AsACache.java
/** * Write usage to console./* w ww. j a v a 2 s . co m*/ */ private static void logUsage(Options options) { HelpFormatter formatter = new HelpFormatter(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); String syntax = AsACache.class.getName() + " [<options>]"; formatter.printHelp(pw, 100, syntax, "options:", options, 0, 2, null); log.info(sw.toString()); }
From source file:com.aerospike.utility.SetDelete.java
private static void logUsage(Options options) { HelpFormatter formatter = new HelpFormatter(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); String syntax = SetDelete.class.getName() + " [<options>]"; formatter.printHelp(pw, 100, syntax, "options:", options, 0, 2, null); log.info(sw.toString()); }
From source file:com.googlecode.osde.internal.utils.ResourceUtil.java
/** * Retrieve the content in the resource file which you specify. * * @param path The resource file path.//from ww w . ja va 2 s . c o m * @param encoding File encoding. * @return The Content. * @throws IOException When some errors occurred. */ public static String loadTextResourceFile(String path, String encoding) throws IOException { InputStreamReader in = null; StringWriter out = null; try { in = new InputStreamReader(ResourceUtil.class.getResourceAsStream(path), encoding); out = new StringWriter(); IOUtils.copy(in, out); String result = out.toString(); return result; } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:com.thinkbiganalytics.hive.util.HiveUtils.java
/** * Quotes the specified string for use in a Hive query. * * @param string the string to be quoted * @return the quoted string/*from ww w .j a va 2 s .c o m*/ */ @Nonnull public static String quoteString(@Nonnull final String string) { try { final StringWriter writer = new StringWriter(string.length() + 2); writer.append('"'); StringEscapeUtils.ESCAPE_JAVA.translate(string, writer); writer.append('"'); return writer.toString(); } catch (final IOException e) { throw new IllegalArgumentException("String contains invalid characters: " + string, e); } }
From source file:Main.java
public static String marshaller(Object o, Class<?> T) { JAXBContext jc;/*w w w . j a v a2s. c o m*/ Marshaller marshaller; StringWriter writer = new StringWriter(); try { jc = JAXBContext.newInstance(T); marshaller = jc.createMarshaller(); marshaller.marshal(o, writer); } catch (JAXBException e) { e.printStackTrace(); } return writer.toString(); }