Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

In this page you can find the example usage for java.io Writer toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.geosdi.geoplatform.gui.server.command.publish.cas.CasPublishLayerPreviewCommand.java

@Override
public PublishLayerPreviewResponse execute(CasPublishLayerPreviewRequest request,
        HttpServletRequest httpServletRequest) {
    try {//from  w ww  .  jav  a  2s .c o  m
        sessionUtility.getLoggedAccount(httpServletRequest);
    } catch (GPSessionTimeout timeout) {
        throw new GeoPlatformException(timeout);
    }
    String result = null;
    try {
        List<String> layerList = request.getLayerList();
        casPublisherService.publishAll(httpServletRequest.getSession().getId(), "previews", "dataTest",
                layerList);
        boolean reloadCluster = request.isReloadCluster();
        if (reloadCluster) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localContext);
            //                HttpResponse response = httpclient.execute(get, localContext);
            InputStream is = response.getEntity().getContent();
            Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            result = writer.toString();
        }
    } catch (ResourceNotFoundFault ex) {
        logger.error("Error on publish shape: " + ex);
        throw new GeoPlatformException("Error on publish shape.");
    } catch (FileNotFoundException ex) {
        logger.error("Error on publish shape: " + ex);
        throw new GeoPlatformException("Error on publish shape.");
    } catch (MalformedURLException e) {
        logger.error("Error on cluster url: " + e);
        throw new GeoPlatformException(new GPReloadURLException("Error on cluster url."));
    } catch (IOException e) {
        logger.error("Error on reloading cluster: " + e);
        throw new GeoPlatformException(new GPReloadURLException("Error on reloading cluster."));
    }
    return new PublishLayerPreviewResponse(result);

}

From source file:com.mirth.connect.plugins.datatypes.xml.XMLBatchAdaptor.java

private String toXML(Node node) throws Exception {
    Writer writer = new StringWriter();

    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.transform(new DOMSource(node), new StreamResult(writer));

    return writer.toString();
}

From source file:org.ambraproject.service.XMLServiceImpl.java

public InputStream getTransformedInputStream(InputStream xml) throws ApplicationException {
    try {// w  ww.j  av a2s  . co  m
        final Writer writer = new StringWriter(1000);

        Document doc = createDocBuilder().parse(xml);
        Transformer transformer = this.getTranslet(doc);
        DOMSource domSource = new DOMSource(doc);
        transformer.transform(domSource, new StreamResult(writer));
        return new ByteArrayInputStream(writer.toString().getBytes("UTF-8"));
    } catch (Exception e) {
        throw new ApplicationException(e);
    }
}

From source file:com.yahoo.ycsb.db.CouchbaseClient.java

/**
 * Encode the object for couchbase storage.
 *
 * @param source the source value./*from   w  ww.  j av  a 2  s. co m*/
 * @return the storable object.
 */
private Object encode(final HashMap<String, ByteIterator> source) {
    HashMap<String, String> stringMap = StringByteIterator.getStringMap(source);
    if (!useJson) {
        return stringMap;
    }

    ObjectNode node = JSON_MAPPER.createObjectNode();
    for (Map.Entry<String, String> pair : stringMap.entrySet()) {
        node.put(pair.getKey(), pair.getValue());
    }
    JsonFactory jsonFactory = new JsonFactory();
    Writer writer = new StringWriter();
    try {
        JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);
        JSON_MAPPER.writeTree(jsonGenerator, node);
    } catch (Exception e) {
        throw new RuntimeException("Could not encode JSON value");
    }
    return writer.toString();
}

From source file:org.exoplatform.ecms.xcmis.sp.BaseTest.java

protected String convertStreamToString(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {//from  w ww.ja v  a  2s  .  c o  m
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:org.apache.velocity.tools.view.VelocityViewServlet.java

protected void mergeTemplate(Template template, Context context, HttpServletResponse response)
        throws IOException {
    Writer writer;
    if (this.bufferOutput) {
        writer = new StringWriter();
    } else {//from w w w .j  a va2  s . c  o  m
        writer = response.getWriter();
    }

    getVelocityView().merge(template, context, writer);

    if (this.bufferOutput) {
        response.getWriter().write(writer.toString());
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.common.util.BeanToTextExporter.java

public static String beanListToText(final String type, final Writer out, final Map<String, String> columns,
        final List data, final DateFormat dateFormat) {
    CSVWriter writer = new CSVWriter(out);
    if ("tab".equals(type)) {
        writer = new CSVWriter(out, '\t', CSVWriter.NO_QUOTE_CHARACTER);
    }/*from   w w  w .ja  v a  2 s.c  o m*/
    List<String> cols = new LinkedList<String>();
    try {
        //Writing the column headers
        for (Map.Entry<String, String> e : columns.entrySet()) {
            cols.add(e.getValue());
        }
        writer.writeNext((String[]) cols.toArray(new String[columns.size()]));
        cols.clear();
        //Writing the data
        if (data != null) {
            for (Object o : data) {
                for (Map.Entry<String, String> e : columns.entrySet()) {
                    final Object obj = getAndInvokeGetter(o, e.getKey());
                    String value = getExportString(obj, dateFormat);

                    if ("tab".equals(type)) {
                        value = value.replace("\t", "   ");
                    }
                    cols.add(value);
                }
                writer.writeNext((String[]) cols.toArray(new String[columns.size()]));
                cols.clear();
            }
        }
        writer.close();
        out.flush();
        out.close();
    } catch (Exception e) {
        logger.debug(FancyExceptionLogger.printException(e));
        return "An error occurred.";
    }
    return out.toString();
}

From source file:configuration.ClientConfiguration.java

/**
 * {@inheritDoc}// w w w . j av a2 s  . co m
 */
@Override
public String toString() {
    Writer writer = new JSONPrettyPrintWriter();
    try {
        JSONValue.writeJSONString(toMap(), writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:ddf.catalog.registry.transformer.RegistryServiceConverter.java

/**
 * Read gml and return a wkt.//from  w w w  .  j  a v  a 2  s . c  o m
 *
 * @param reader
 * @return
 */
private void readGml(HierarchicalStreamReader reader, MetacardImpl meta, String attributeKey) {

    safeMove(reader, 3, () -> {

        // buffer up the current gml node and all its children
        Writer out = new StringWriter();
        HierarchicalStreamWriter writer = new CompactWriter(out);
        HierarchicalStreamCopier copier = new HierarchicalStreamCopier();
        copier.copy(reader, writer);
        String xml = out.toString();

        xml = xml.replaceFirst(">", " xmlns:gml=\"http://www.opengis.net/gml\">");

        meta.setAttribute(attributeKey, gml3ToWkt.convert(xml));
    });
}

From source file:com.dsh105.nexus.hook.github.GitHub.java

public String createGist(Exception e) {
    Nexus.LOGGER.info("Creating a Gist for " + e.getClass().getName());
    Writer writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    e.printStackTrace(printWriter);/*from w w  w. j  ava2 s.  c  o m*/
    Gist gist = new Gist(new GistFile(writer.toString()));
    try {
        writer.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return gist.create();
}