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.apache.olingo.odata2.core.debug.DebugInfoBodyTest.java

private String appendJson(final ODataResponse response) throws IOException {
    Writer writer = new StringWriter();
    DebugInfoBody body = new DebugInfoBody(response, null);
    body.appendJson(new JsonStreamWriter(writer));
    return writer.toString();
}

From source file:com.threewks.thundr.view.velocity.VelocityViewResolver.java

@Override
public void resolve(Request req, Response resp, VelocityView viewResult) {
    String view = viewResult.getView();
    String characterEncoding = StringUtils.defaultString(viewResult.getCharacterEncoding(), "UTF-8");

    try {//from  ww w .  j av  a 2  s.c o  m
        Map<String, Object> model = mergeModels(req, viewResult);
        VelocityContext context = new VelocityContext(model);
        Writer stringWriter = new StringWriter();
        velocityEngine.mergeTemplate(view, StringPool.UTF_8, context, stringWriter);
        String content = stringWriter.toString();
        BaseView.applyToResponse(viewResult, resp);
        byte[] data = content.getBytes(characterEncoding);
        resp.withContentLength(data.length);
        resp.getOutputStream().write(data);
    } catch (Exception e) {
        throw new ViewResolutionException(e, "Failed to render velocity template '%s': %s", view,
                e.getMessage());
    }
}

From source file:org.killbill.billing.plugin.analytics.http.TestReportsServlet.java

@Test(groups = "fast")
public void testDeserializationReserialization() throws Exception {
    final String json = "[" + "{\"name\":\"ultimate\"," + "\"values\":[" + "{\"x\":\"2013-01-01\",\"y\":11.0},"
            + "{\"x\":\"2013-01-02\",\"y\":37.0}," + "{\"x\":\"2013-01-03\",\"y\":16.0},"
            + "{\"x\":\"2013-01-04\",\"y\":29.0}," + "{\"x\":\"2013-01-05\",\"y\":40.0},"
            + "{\"x\":\"2013-01-06\",\"y\":3.0}," + "{\"x\":\"2013-01-07\",\"y\":4.0},"
            + "{\"x\":\"2013-01-08\",\"y\":39.0}," + "{\"x\":\"2013-01-09\",\"y\":34.0},"
            + "{\"x\":\"2013-01-10\",\"y\":31.0}," + "{\"x\":\"2013-01-11\",\"y\":20.0},"
            + "{\"x\":\"2013-01-12\",\"y\":28.0}," + "{\"x\":\"2013-01-13\",\"y\":19.0},"
            + "{\"x\":\"2013-01-14\",\"y\":15.0}," + "{\"x\":\"2013-01-15\",\"y\":31.0},"
            + "{\"x\":\"2013-01-16\",\"y\":16.0}," + "{\"x\":\"2013-01-17\",\"y\":40.0},"
            + "{\"x\":\"2013-01-18\",\"y\":29.0}," + "{\"x\":\"2013-01-19\",\"y\":31.0},"
            + "{\"x\":\"2013-01-20\",\"y\":11.0}," + "{\"x\":\"2013-01-21\",\"y\":36.0},"
            + "{\"x\":\"2013-01-22\",\"y\":18.0}," + "{\"x\":\"2013-01-23\",\"y\":12.0},"
            + "{\"x\":\"2013-01-24\",\"y\":23.0}," + "{\"x\":\"2013-01-25\",\"y\":32.0},"
            + "{\"x\":\"2013-01-26\",\"y\":27.0}," + "{\"x\":\"2013-01-27\",\"y\":33.0},"
            + "{\"x\":\"2013-01-28\",\"y\":34.0}," + "{\"x\":\"2013-01-29\",\"y\":5.0},"
            + "{\"x\":\"2013-01-30\",\"y\":7.0}," + "{\"x\":\"2013-01-31\",\"y\":13.0},"
            + "{\"x\":\"2013-02-01\",\"y\":10.0}," + "{\"x\":\"2013-02-02\",\"y\":43.0},"
            + "{\"x\":\"2013-02-03\",\"y\":15.0}," + "{\"x\":\"2013-02-04\",\"y\":38.0},"
            + "{\"x\":\"2013-02-05\",\"y\":34.0}," + "{\"x\":\"2013-02-06\",\"y\":38.0},"
            + "{\"x\":\"2013-02-07\",\"y\":26.0}," + "{\"x\":\"2013-02-08\",\"y\":27.0},"
            + "{\"x\":\"2013-02-09\",\"y\":1.0}," + "{\"x\":\"2013-02-10\",\"y\":12.0},"
            + "{\"x\":\"2013-02-11\",\"y\":28.0}," + "{\"x\":\"2013-02-12\",\"y\":10.0},"
            + "{\"x\":\"2013-02-13\",\"y\":27.0}" + "]" + "}" + "]";

    final List<NamedXYTimeSeries> obj = jsonMapper.readValue(json.getBytes(),
            new TypeReference<List<NamedXYTimeSeries>>() {
            });//w w  w. j  a  v a 2  s  .  co  m

    final Writer writer = new StringWriter();
    jsonMapper.writeValue(writer, obj);

    Assert.assertEquals(writer.toString(), json);
}

From source file:org.springframework.hateoas.VndErrorsMarshallingTest.java

/**
 * @see #62, #154/*from   w w w  . jav  a  2  s.  co m*/
 */
@Test
public void jaxbMarshalling() throws Exception {

    Writer writer = new StringWriter();
    marshaller.marshal(errors, writer);

    assertThat(new Diff(xmlReference, writer.toString()).similar(), is(true));
}

From source file:org.esigate.tags.BlockRendererTest.java

public void testRenderBlock() throws IOException, HttpErrorPage {
    final String expectedOutput = "abc some"
            + "<!--$beginblock$myblock$-->some text goes here<!--$endblock$myblock$-->" + " cdf hello";
    Writer out = new StringBuilderWriter();
    BlockRenderer tested = new BlockRenderer("myblock", null);
    tested.render(null, expectedOutput, out);
    assertEquals("some text goes here", out.toString());
    // null name means whole page
    out = new StringBuilderWriter();
    tested = new BlockRenderer(null, null);
    tested.render(null, expectedOutput, out);
    assertEquals(expectedOutput, out.toString());
}

From source file:org.killbill.billing.plugin.analytics.http.TestReportsServlet.java

@Test(groups = "fast")
public void testSimpleSerialization() throws Exception {
    final List<DataMarker> res = new ArrayList<DataMarker>();

    final List<XY> xys1 = new ArrayList<XY>();
    xys1.add(new XY("2013-01-01", 11));
    xys1.add(new XY("2013-01-02", 7));
    xys1.add(new XY("2013-01-03", 34));
    final NamedXYTimeSeries serie1 = new NamedXYTimeSeries("serie1", xys1);
    res.add(serie1);//  ww w. ja  v  a 2  s. c  om

    final List<XY> xys2 = new ArrayList<XY>();
    xys2.add(new XY("2013-01-01", 12));
    xys2.add(new XY("2013-01-02", 5));
    xys2.add(new XY("2013-01-03", 3));
    final NamedXYTimeSeries serie2 = new NamedXYTimeSeries("serie2", xys2);
    res.add(serie2);

    final Writer jsonWriter = new StringWriter();
    jsonMapper.writeValue(jsonWriter, res);
    Assert.assertEquals(jsonWriter.toString(),
            "[{\"name\":\"serie1\",\"values\":[{\"x\":\"2013-01-01\",\"y\":11.0},{\"x\":\"2013-01-02\",\"y\":7.0},{\"x\":\"2013-01-03\",\"y\":34.0}]},{\"name\":\"serie2\",\"values\":[{\"x\":\"2013-01-01\",\"y\":12.0},{\"x\":\"2013-01-02\",\"y\":5.0},{\"x\":\"2013-01-03\",\"y\":3.0}]}]");

    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    ReportsServlet.writeAsCSV(Collections.singletonList(new Chart(ReportType.TIMELINE, "foo", res)), out);
    Assert.assertEquals(out.toString(),
            "serie1,2013-01-01,11.0\n" + "serie1,2013-01-02,7.0\n" + "serie1,2013-01-03,34.0\n"
                    + "serie2,2013-01-01,12.0\n" + "serie2,2013-01-02,5.0\n" + "serie2,2013-01-03,3.0\n");
}

From source file:com.erudika.para.utils.Utils.java

/**
 * Compiles a mustache template with a given scope (map of fields and values).
 * @param scope a map of fields and values
 * @param template a Mustache template/*from  www .ja v  a2 s . c o m*/
 * @return the compiled template string
 */
public static String compileMustache(Map<String, Object> scope, String template) {
    if (scope == null || StringUtils.isBlank(template)) {
        return "";
    }
    Writer writer = new StringWriter();
    try {
        mustache.compile(new StringReader(template), MD5(template)).execute(writer, scope);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            logger.error(null, e);
        }
    }
    return writer.toString();
}

From source file:fr.ign.cogit.geoxygene.appli.gl.Shader.java

private void loadShaderSource() {
    try {//from w  ww. j  av  a 2  s.c o  m
        InputStream source_stream = this.location.openStream();
        Writer writer = new StringWriter();
        IOUtils.copy(source_stream, writer);
        this.source = writer.toString();
        this.parseUniforms();
    } catch (IOException e) {
        Logger.getRootLogger()
                .error("In shader " + this.name + " : the shader source cannot be read or parsed.");
        e.printStackTrace();
        this.source = null;
    }
}

From source file:com.jivesoftware.os.upena.uba.service.DeployLog.java

@Override
synchronized public void log(String context, String message, Throwable t) {
    if (t != null) {
        LOG.warn(context + " " + message, t);
        messages.add(message);/* w w w  .ja v a  2  s  .  co m*/
        Writer result = new StringWriter();
        PrintWriter printWriter = new PrintWriter(result);
        t.printStackTrace(printWriter);
        messages.add(result.toString());
        state.set(message + " " + result.toString());
    } else {
        LOG.info(context + " " + message);
        messages.add(message);
        state.set(message);
    }
}

From source file:org.esigate.tags.BlockRendererTest.java

public void testUnknownTag() throws IOException, HttpErrorPage {
    final String input = "abc some<!--$hello$world$-->some text goes here";
    Writer out = new StringBuilderWriter();
    BlockRenderer tested = new BlockRenderer(null, null);
    tested.render(null, input, out);/* ww w .ja  v  a 2s  .  c o  m*/
    // input should remain unchanged
    assertEquals(input, out.toString());
}