List of usage examples for java.io Writer toString
public String toString()
From source file:ca.digitalface.jasperoo.JasperooOperationsImpl.java
/** * A utility method to convert an input stream to a String. * /*ww w .j a va2 s.com*/ * @param source The source input stream. * @return The source as a String, or an empty String if the source is null * @throws IOException If unable to read stream. */ private String convertStreamToString(InputStream source) throws IOException { /* * To convert the InputStream to String we use the Reader.read(char[] * buffer) method. We iterate until the Reader return -1 which means * there's no more data to read. We use the StringWriter class to * produce the string. */ if (source != null) { Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(source, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { source.close(); } return writer.toString(); } else { return ""; } }
From source file:org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswEndpoint.java
private CswRecordCollection queryCsw(GetRecordsType request) throws CswException { if (LOGGER.isDebugEnabled()) { try {//from w w w . j a v a2 s. co m Writer writer = new StringWriter(); try { Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request); marshaller.marshal(jaxbElement, writer); } catch (JAXBException e) { LOGGER.debug("Unable to marshall {} to XML. Exception {}", GetRecordsType.class, e); } LOGGER.debug(writer.toString()); } catch (Exception e) { LOGGER.debug("Unable to create debug message for getRecordsType: {}", e); } } QueryType query = (QueryType) request.getAbstractQuery().getValue(); CswRecordCollection response = new CswRecordCollection(); response.setRequest(request); response.setOutputSchema(request.getOutputSchema()); response.setMimeType(request.getOutputFormat()); response.setElementName(query.getElementName()); response.setElementSetType( (query.getElementSetName() != null) ? query.getElementSetName().getValue() : null); response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS)); if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) { QueryRequest queryRequest = queryFactory.getQuery(request); try { queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema()); LOGGER.debug("Attempting to execute query: {}", response.getRequest()); QueryResponse queryResponse = framework.query(queryRequest); response.setSourceResponse(queryResponse); } catch (UnsupportedQueryException e) { LOGGER.warn("Unable to query", e); throw new CswException(e); } catch (SourceUnavailableException e) { LOGGER.warn("Unable to query", e); throw new CswException(e); } catch (FederationException e) { LOGGER.warn("Unable to query", e); throw new CswException(e); } } return response; }
From source file:org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.WfsFilterDelegateTest.java
private String getXmlFromMarshaller(FilterType filterType) throws JAXBException { JAXBContext jaxbContext = initJaxbContext(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Writer writer = new StringWriter(); marshaller.marshal(getFilterTypeJaxbElement(filterType), writer); String xml = writer.toString(); LOGGER.debug("XML returned by Marshaller:\n{}", xml); if (LOGGER.isTraceEnabled()) { LOGGER.trace(Arrays.toString(Thread.currentThread().getStackTrace())); }/* www . j a v a2 s. c om*/ return xml; }
From source file:org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.source.TestWfsFilterDelegate.java
private String getXmlFromMarshaller(FilterType filterType) throws JAXBException { JAXBContext jaxbContext = initJaxbContext(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Writer writer = new StringWriter(); marshaller.marshal(getFilterTypeJaxbElement(filterType), writer); String xml = writer.toString(); LOGGER.debug("XML returned by Marshaller:\n{}", xml); LOGGER.trace(Thread.currentThread().getStackTrace().toString()); return xml;//from www . j a va2s . com }
From source file:com.comcast.cns.persistence.CNSSubscriptionCassandraPersistence.java
private String getColumnValuesJSON(CNSSubscription s) throws JSONException { Writer writer = new StringWriter(); JSONWriter jw = new JSONWriter(writer); jw = jw.object();/* w w w. ja v a 2s . c o m*/ if (s.getEndpoint() != null) { jw.key("endPoint").value(s.getEndpoint()); } if (s.getToken() != null) { jw.key("token").value(s.getToken()); } if (s.getArn() != null) { jw.key("subArn").value(s.getArn()); } if (s.getUserId() != null) { jw.key("userId").value(s.getUserId()); } if (s.getConfirmDate() != null) { jw.key("confirmDate").value(s.getConfirmDate().getTime() + ""); } if (s.getProtocol() != null) { jw.key("protocol").value(s.getProtocol().toString()); } if (s.getRequestDate() != null) { jw.key("requestDate").value(s.getRequestDate().getTime() + ""); } jw.key("authenticateOnSubscribe").value(s.isAuthenticateOnUnsubscribe() + ""); jw.key("isConfirmed").value(s.isConfirmed() + ""); jw.key("rawMessageDelivery").value(s.getRawMessageDelivery() + ""); jw.endObject(); return writer.toString(); }
From source file:com.zoffcc.applications.aagtl.HTMLDownloader.java
public String convertStreamToString(InputStream is) throws IOException { /*// w ww .j av a 2s.c o m * To convert the InputStream to String we use the * Reader.read(char[] buffer) method. We iterate until the * Reader return -1 which means there's no more data to * read. We use the StringWriter class to produce the string. */ if (is != null) { Writer writer = new StringWriter(); char[] buffer = new char[default_buffer_size]; try { Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), large_buffer_size); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } return writer.toString(); } else { return null; } }
From source file:com.zoffcc.applications.aagtl.HTMLDownloader.java
public void loadCookies() { String[] ret = new String[2]; // make dirs// w ww . j a va2s .com File dir1 = new File(this.main_aagtl.main_dir + "/config"); dir1.mkdirs(); // load cookies from file File cookie_file = new File(this.main_aagtl.main_dir + "/config/cookie.txt"); FileInputStream fIn = null; InputStreamReader isr = null; char[] inputBuffer = new char[255]; Writer writer = new StringWriter(); String data = null; try { fIn = new FileInputStream(cookie_file); isr = new InputStreamReader(fIn); int n = 0; while ((n = isr.read(inputBuffer)) != -1) { writer.write(inputBuffer, 0, n); } data = writer.toString(); } catch (Exception e) { e.printStackTrace(); System.out.println("loadCookies: Exception1"); return; } finally { try { isr.close(); fIn.close(); } catch (NullPointerException e2) { System.out.println("loadCookies: Exception2"); return; } catch (IOException e) { System.out.println("loadCookies: Exception3"); e.printStackTrace(); return; } } if (cookie_jar == null) { // cookie_jar = new CookieStore(); return; } else { cookie_jar.clear(); } // Log.d("load cookie:", "->" + String.valueOf(data)); // [[version: 0][name: ASP.NET_SessionId] // [value: cyuoctxrwio1x13vivqzlxgi][domain: www.geocaching.com] // [path: /][expiry: null], [version: 0][name: userid] // [value: 8a72e55f-419c-4da7-8de3-7813a3fda9c7][domain: // www.geocaching.com] // [path: /][expiry: Tue Apr 26 15:41:14 Europe/Belgrade 2011]] if (data.length() > 1) { // check for multpile cookies if (data.startsWith("[[")) { // strip [ and ] at begin and end of string data = data.substring(1, data.length() - 1); String s3 = "\\], \\["; String[] a3 = data.split(s3); String data_cookie; for (int j3 = 0; j3 < a3.length; j3++) { data_cookie = a3[j3]; if (j3 == 0) { data_cookie = data_cookie + "]"; } else { data_cookie = "[" + data_cookie; } // System.out.println("parsing cookie #" + j3 + ": " + // data_cookie); String s2 = "]"; String[] a = data_cookie.split(s2); String x = null; String c1, c2 = null; String c_name = null, c_value = null, c_domain = null, c_path = null; String c_version = null; BasicClientCookie this_cookie = null; for (int j = 0; j < a.length; j++) { x = a[j].replace("[", "").trim(); c1 = x.split(":")[0]; c2 = x.split(":")[1].substring(1); // Log.d("load cookie:", "->" + String.valueOf(c1)); // Log.d("load cookie:", "->" + String.valueOf(c2)); if (c1.matches("name") == true) { // Log.d("name:", "->" + String.valueOf(c1)); c_name = c2; } else if (c1.matches("value") == true) { c_value = c2; } else if (c1.matches("domain") == true) { c_domain = c2; } else if (c1.matches("path") == true) { c_path = c2; } else if (c1.matches("version") == true) { c_version = c2; } } this_cookie = new BasicClientCookie(c_name, c_value); this_cookie.setDomain(c_domain); this_cookie.setPath(c_path); // System.out.println("created cookie: ->" + // String.valueOf(this_cookie)); this.cookie_jar.addCookie(this_cookie); } } // single cookie else { String s2 = "]"; String[] a = data.split(s2); String x = null; String c1, c2 = null; String c_name = null, c_value = null, c_domain = null, c_path = null; String c_version = null; BasicClientCookie this_cookie = null; for (int j = 0; j < a.length; j++) { x = a[j].replace("[", "").trim(); c1 = x.split(":")[0]; c2 = x.split(":")[1].substring(1); // Log.d("load cookie:", "->" + String.valueOf(c1)); // Log.d("load cookie:", "->" + String.valueOf(c2)); if (c1.matches("name") == true) { // Log.d("name:", "->" + String.valueOf(c1)); c_name = c2; } else if (c1.matches("value") == true) { c_value = c2; } else if (c1.matches("domain") == true) { c_domain = c2; } else if (c1.matches("path") == true) { c_path = c2; } else if (c1.matches("version") == true) { c_version = c2; } } this_cookie = new BasicClientCookie(c_name, c_value); this_cookie.setDomain(c_domain); this_cookie.setPath(c_path); // System.out.println("created cookie: ->" + // String.valueOf(this_cookie)); this.cookie_jar.addCookie(this_cookie); } } return; }
From source file:com.cyberway.issue.crawler.frontier.AbstractFrontier.java
/** * Load up the seeds.// w w w .j a va 2s . c om * * This method is called on initialize and inside in the crawlcontroller * when it wants to force reloading of configuration. * * @see com.cyberway.issue.crawler.framework.CrawlController#kickUpdate() */ public void loadSeeds() { Writer ignoredWriter = new StringWriter(); logger.info("beginning"); // Get the seeds to refresh. Iterator iter = this.controller.getScope().seedsIterator(ignoredWriter); int count = 0; while (iter.hasNext()) { UURI u = (UURI) iter.next(); CandidateURI caUri = CandidateURI.createSeedCandidateURI(u); caUri.setSchedulingDirective(CandidateURI.MEDIUM); if (((Boolean) getUncheckedAttribute(null, ATTR_SOURCE_TAG_SEEDS)).booleanValue()) { caUri.putString(CoreAttributeConstants.A_SOURCE_TAG, caUri.toString()); caUri.makeHeritable(CoreAttributeConstants.A_SOURCE_TAG); } schedule(caUri); count++; if (count % 1000 == 0) { logger.info(count + " seeds"); } } // save ignored items (if any) where they can be consulted later saveIgnoredItems(ignoredWriter.toString(), controller.getDisk()); logger.info("finished"); }
From source file:velo.ejb.seam.action.HomeActionsBean.java
public String getArray() { log.debug("Get User creation per month for last year method has been invoked!"); try {/* w w w . java2 s . c o m*/ Writer writer1 = new java.io.StringWriter(); XmlWriter xmlwriter1 = new SimpleXmlWriter(writer1); PrettyPrinterXmlWriter pxr = new PrettyPrinterXmlWriter(xmlwriter1); pxr.setIndent("\t"); pxr.setNewline("\n"); pxr.writeXmlVersion("1.0", null, "yes").writeEntity("items"); Calendar c = Calendar.getInstance(); String[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; //move the calendar to 12 months back c.add(Calendar.MONTH, -11); //build dates(1st day) of the last 12 months List<Date> yearlyMonths = new ArrayList<Date>(); log.debug("Building months(1 year) before current month..."); for (int i = 0; i <= 11; i++) { log.trace("Adding monthly date obj: #0", c.getTime()); yearlyMonths.add(c.getTime()); c.add(Calendar.MONTH, 1); } //iterate over months, get the amount of users created per month for (Date currFirstDayOfMonth : yearlyMonths) { c.setTime(currFirstDayOfMonth); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); Long amount = userManager.getCreatedUsersAmount(currFirstDayOfMonth, c.getTime()); pxr.writeEntity("item").writeAttribute("month", monthName[c.get(Calendar.MONTH)]) .writeAttribute("year", c.get(Calendar.YEAR)).writeAttribute("value", amount).endEntity(); } pxr.endEntity(); log.debug("Dump of xml: " + writer1.toString()); return writer1.toString(); } catch (IOException e) { e.printStackTrace(); return null; } /* .writeEntity("item").writeAttribute("month", "Jan").writeAttribute("value", 10).endEntity() .writeEntity("item").writeAttribute("month", "Feb").writeAttribute("value", 20).endEntity() .writeEntity("item").writeAttribute("month", "Mar").writeAttribute("value", 30).endEntity() .writeEntity("item").writeAttribute("month", "Apr").writeAttribute("value", 40).endEntity() .writeEntity("item").writeAttribute("month", "Jun").writeAttribute("value", 50).endEntity() .writeEntity("item").writeAttribute("month", "Jul").writeAttribute("value", 60).endEntity() .writeEntity("item").writeAttribute("month", "Aug").writeAttribute("value", 70).endEntity() */ }
From source file:de.shadowhunt.subversion.internal.PropertiesUpdateOperation.java
@Override protected HttpUriRequest createRequest() { final URI uri = URIUtils.createURI(repository, resource); final DavTemplateRequest request = new DavTemplateRequest("PROPPATCH", uri); if (lockToken != null) { request.addHeader("If", "<" + uri + "> (<" + lockToken + ">)"); }//from w ww . ja v a2 s.c om final Writer body = new StringBuilderWriter(); try { final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body); writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0); writer.writeStartElement("propertyupdate"); writer.writeDefaultNamespace(XmlConstants.DAV_NAMESPACE); writer.setPrefix(XmlConstants.SUBVERSION_CUSTOM_PREFIX, XmlConstants.SUBVERSION_CUSTOM_NAMESPACE); writer.writeNamespace(XmlConstants.SUBVERSION_CUSTOM_PREFIX, XmlConstants.SUBVERSION_CUSTOM_NAMESPACE); writer.setPrefix(XmlConstants.SUBVERSION_DAV_PREFIX, XmlConstants.SUBVERSION_DAV_NAMESPACE); writer.writeNamespace(XmlConstants.SUBVERSION_DAV_PREFIX, XmlConstants.SUBVERSION_DAV_NAMESPACE); writer.setPrefix(XmlConstants.SUBVERSION_SVN_PREFIX, XmlConstants.SUBVERSION_SVN_NAMESPACE); writer.writeNamespace(XmlConstants.SUBVERSION_SVN_PREFIX, XmlConstants.SUBVERSION_SVN_NAMESPACE); writer.writeStartElement(type.action); writer.writeStartElement("prop"); for (final ResourceProperty property : properties) { final String prefix = property.getType().getPrefix(); if (type == Type.SET) { writer.writeStartElement(prefix, ResourcePropertyUtils.escapedKeyNameXml(property.getName())); writer.writeCharacters(property.getValue()); writer.writeEndElement(); } else { writer.writeEmptyElement(prefix, property.getName()); } } writer.writeEndElement(); // prop writer.writeEndElement(); // set || delete writer.writeEndElement(); // propertyupdate writer.writeEndDocument(); writer.close(); } catch (final XMLStreamException e) { throw new SubversionException("could not create request body", e); } final String bodyWithMakers = body.toString(); final String bodyWithoutMakers = ResourcePropertyUtils.filterMarker(bodyWithMakers); request.setEntity(new StringEntity(bodyWithoutMakers, CONTENT_TYPE_XML)); return request; }