List of usage examples for java.io StringWriter toString
public String toString()
From source file:de.innovationgate.wga.common.beans.hdbmodel.ModelDefinition.java
public static ModelDefinition read(InputStream in) throws Exception { // First read XML manually and validate against the DTD provided in this OpenWGA distribution (to prevent it being loaded from the internet, which might not work, see #00003612) SAXReader reader = new SAXReader(); reader.setIncludeExternalDTDDeclarations(true); reader.setIncludeInternalDTDDeclarations(true); reader.setEntityResolver(new EntityResolver() { @Override// w w w .j a v a 2s . c o m public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { if (systemId.equals("http://doc.openwga.com/hdb-model-definition-6.0.dtd")) { return new InputSource(ModelDefinition.class.getClassLoader().getResourceAsStream( WGUtils.getPackagePath(ModelDefinition.class) + "/hdb-model-definition-6.0.dtd")); } else { // use the default behaviour return null; } } }); org.dom4j.Document domDoc = reader.read(in); // Remove doctype (we already have validated) and provide the resulting XML to the serializer domDoc.setDocType(null); StringWriter out = new StringWriter(); XMLWriter writer = new XMLWriter(out); writer.write(domDoc); String xml = out.toString(); return _serializer.read(ModelDefinition.class, new StringReader(xml)); }
From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java
public static String marshalToString(Object obj) throws JAXBException { StringWriter writer = new StringWriter(); JAXBContext jc = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = jc.createMarshaller(); marshaller.marshal(obj, writer);// ww w . j a v a 2 s . c o m log.info("Marshal: " + writer.toString()); return writer.toString(); }
From source file:Main.java
public static String toString(Throwable e) { StringWriter w = new StringWriter(); PrintWriter p = new PrintWriter(w); p.print(e.getClass().getName() + ": "); if (e.getMessage() != null) { p.print(e.getMessage() + "\n"); }//from w w w.j a v a2 s . c o m p.println(); try { e.printStackTrace(p); return w.toString(); } finally { p.close(); } }
From source file:uk.ac.cam.cl.dtg.picky.client.analytics.Analytics.java
private static void upload(Properties properties, String datasetURL) { if (datasetURL == null) return;/*from w w w .j av a 2 s.com*/ URL url; try { url = new URL(new URL(datasetURL), URL); } catch (MalformedURLException e1) { e1.printStackTrace(); return; } try { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url.toURI()); post.setHeader("Content-Type", "text/plain; charset=utf-8"); StringWriter stringWriter = new StringWriter(); properties.store(stringWriter, ""); HttpEntity entity = new StringEntity(stringWriter.toString()); post.setEntity(entity); HttpResponse response = client.execute(post); LOG.info("Uploading analytics: " + response.getStatusLine()); } catch (IOException | URISyntaxException e) { LOG.error(e.getMessage()); } }
From source file:Main.java
public static String xmlDocumentToString(final Document doc) { try {/*from ww w . java 2s . c o m*/ final DOMSource domSource = new DOMSource(doc); final StringWriter writer = new StringWriter(); final StreamResult result = new StreamResult(writer); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (final TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:org.apache.solr.client.solrj.util.ClientUtils.java
public static String toXML(SolrInputDocument doc) { StringWriter str = new StringWriter(); try {/*from ww w . j a v a 2s . c o m*/ writeXML(doc, str); } catch (Exception ex) { } return str.toString(); }
From source file:kltn.geocoding.Geocoding.java
private static void prinRaw(String s) throws MalformedURLException, IOException { String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM"; link = link + "&address=" + URLEncoder.encode(s); URL url = new URL(link); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); InputStream is = httpsCon.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String jsonString = writer.toString(); System.out.println(jsonString); }
From source file:Util.java
/** * Applies a stylesheet to a given xml document. * /*from w ww . java2 s. c o m*/ * @param xmlDocument * the xml document to be transformed * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static String transformDocumentAsString(Document xmlDocument, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), streamResult); // Now you can get the output Node from the DOMResult. return stringWriter.toString(); }
From source file:com.splout.db.common.SploutClient.java
public static String asString(InputStream inputStream) throws IOException { StringWriter writer = new StringWriter(); try {/*from w ww . j a va 2 s . c o m*/ IOUtils.copy(inputStream, writer); return writer.toString(); } finally { inputStream.close(); writer.close(); } }
From source file:com.hazelcast.qasonar.utils.GitHubUtils.java
public static String getFileContentsFromGitHub(GHRepository repo, String fileName) throws IOException { long started = System.nanoTime(); try {// w w w .ja v a 2 s.c om IOException exception = null; for (int i = 0; i < GITHUB_FILE_DOWNLOAD_RETRIES; i++) { try { GHContent fileContent = repo.getFileContent(fileName); StringWriter writer = new StringWriter(); copy(fileContent.read(), writer); return writer.toString(); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { exception = e; } sleepMillis(GITHUB_EXCEPTION_DELAY_MILLIS * (i + 1)); } throw exception; } finally { record(TimeTrackerLabel.GET_FILE_CONTENTS_FROM_GITHUB, System.nanoTime() - started); } }