Example usage for java.io StringWriter close

List of usage examples for java.io StringWriter close

Introduction

In this page you can find the example usage for java.io StringWriter close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a StringWriter has no effect.

Usage

From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java

@Test
public void testAndEqDeserialize() throws Exception {
    String json = "{\"and\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"eq\":{\"key\":\"k2\",\"type\":\"string\",\"value\":\"v2\"}}]}}";
    Node node = objectMapper.readValue(json, Node.class);
    StringWriter writer = new StringWriter();
    node.accept(new PrintNodeVisitor(writer));
    writer.flush();//from   ww  w  . java  2s  .  c o  m
    writer.close();
    String eq = writer.toString();
    assertEquals("AndNode(Equals[k1,v1],Equals[k2,v2],),", eq);
}

From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java

@Test
public void testOrEqDeserialize() throws Exception {
    String json = "{\"or\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"eq\":{\"key\":\"k2\",\"type\":\"string\",\"value\":\"v2\"}}]}}";
    Node node = objectMapper.readValue(json, Node.class);
    StringWriter writer = new StringWriter();
    node.accept(new PrintNodeVisitor(writer));
    writer.flush();/*ww w.  java 2s.  co m*/
    writer.close();
    String eq = writer.toString();
    assertEquals("OrNode(Equals[k1,v1],Equals[k2,v2],),", eq);
}

From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java

@Test
public void testAndOrEqNeqDeserialize() throws Exception {
    String json = "{\"or\":{\"children\":[{\"and\":{\"children\":[{\"eq\":{\"key\":\"k1\",\"type\":\"string\",\"value\":\"v1\"}},{\"neq\":{\"key\":\"k2\",\"type\":\"ipv4\",\"value\":\"1.2.3.4\"}}]}},{\"and\":{\"children\":[{\"eq\":{\"key\":\"k3\",\"type\":\"integer\",\"value\":\"1234\"}}]}}]}}";
    Node node = objectMapper.readValue(json, Node.class);
    StringWriter writer = new StringWriter();
    node.accept(new PrintNodeVisitor(writer));
    writer.flush();//  w  ww . j  a  va2 s .  c  o m
    writer.close();
    String eq = writer.toString();
    assertEquals("OrNode(AndNode(Equals[k1,v1],NotEquals[k2,1.2.3.4],),AndNode(Equals[k3,1234],),),", eq);
}

From source file:org.calrissian.mango.json.deser.NodeDeserializerTest.java

@Test
public void testRangeDeserialize() throws Exception {

    String json = "{\"range\":{\"key\":\"k1\",\"type\":\"string\",\"start\":\"v1\",\"end\":\"v2\"}}";
    Node node = objectMapper.readValue(json, Node.class);
    StringWriter writer = new StringWriter();
    node.accept(new PrintNodeVisitor(writer));
    writer.flush();/*w ww. jav a  2  s .  c o m*/
    writer.close();
    String eq = writer.toString();
    assertEquals("Range[k1,(v1,v2)],", eq);
}

From source file:com.dhenton9000.jersey.client.velocity.VelocityTests.java

@Test
public void testVelocity() throws Exception {
    VelocityEngineFactory vFactory = new VelocityEngineFactory();
    VelocityEngine ve = vFactory.getEngine();

    VelocityContext context = new VelocityContext();
    context.put("alpha", "alpha");
    context.put("beta", 55);

    StringWriter writer = new StringWriter();

    ve.mergeTemplate("/velocity_templates/test.vm", "UTF-8", context, writer);
    writer.flush();/*from w  w  w .j  av a 2  s.co  m*/
    writer.close();
    String tString = writer.toString();
    LOG.debug("tString " + tString);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode sampleTree = mapper.readTree(tString);

    assertEquals(55, sampleTree.get("beta").asInt());

}

From source file:org.myjerry.mysite.PageProcessor.java

private ProjectFile buildPage(Page page, TemplateProcessor templateProcessor, VelocityContext siteModel) {
    // get the page contents
    String pageContents = null;//  w w  w .  j  a  v a 2s . c  om
    File file = new File(this.parentFolder, page.getFile());
    try {
        pageContents = FileUtils.readFileToString(file);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    // build the page model
    VelocityContext pageModel = new VelocityContext(siteModel);
    pageModel.put("pageTitle", page.getTitle());
    pageModel.put("pageBody", pageContents);

    // process with template
    Template template = templateProcessor.getTemplate(page.getTemplateID());

    StringWriter writer = new StringWriter();
    template.merge(pageModel, writer);
    try {
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // build the page object
    return new ProjectFile(page.getPath(), writer.toString());
}

From source file:org.opentox.jaqpot3.qsar.util.PMMLProcess.java

private static String getTransformationDictionaryXML(byte[] pmmlFile) throws JaqpotException, IOException {
    String res = "";
    PMML pmmlObject = loadPMMLObject(pmmlFile);
    PMML newPmmlObject = new PMML();
    newPmmlObject.setTransformationDictionary(pmmlObject.getTransformationDictionary());
    StringWriter outWriter = new StringWriter();
    try {/*from  w  w w.j av  a2  s. c o  m*/
        JAXBUtil.marshalPMML(newPmmlObject, new StreamResult(outWriter));
        StringBuffer sb = outWriter.getBuffer();
        res = getNodeFromXML(sb.toString());
    } catch (Exception ex) {
        String message = "Unexpected exception was caught while generating"
                + " the PMML representation of a trained model.";
        logger.error(message, ex);
        throw new JaqpotException(message, ex);
    } finally {
        outWriter.close();
    }
    return res;
}

From source file:net.jcreate.e3.resource.handler.CSSMinResourceHandler.java

public void handle(Resource pResource) throws HandleResourceException {
    String charset = pResource.getCharset();
    if (charset == null) {
        charset = HttpHolder.getResponse().getCharacterEncoding();
    }//from  ww  w.ja  va  2s .  c o  m
    logger.info("?:" + pResource.getUri() + "css min...");
    try {
        InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(pResource.getTreatedData()),
                charset);
        CssCompressor cssc = new CssCompressor(isr);
        StringWriter sw = new StringWriter();
        cssc.compress(sw, LINEBREAK_AFTER_CHARACTERS);
        sw.flush();
        sw.toString();
        sw.close();
        logger.info("css min?:" + pResource.getUri() + "?.");
    } catch (Exception ex) {
        final String MSG = "css min?:" + pResource.getUri() + "?";
        logger.warn(MSG, ex);
        return;

    }

}

From source file:de.kaiserpfalzEdv.piracc.about.AboutPageProviderImpl.java

@Override
public String getLicense() {
    String result;//from w w w .  ja v a2s.c om

    try {
        InputStream is = getClass().getResourceAsStream("/LICENSE.html");
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer, "UTF-8");
        result = writer.toString();
        writer.close();
        is.close();
    } catch (IOException e) {
        LOG.error(e.getClass().getSimpleName() + " caught: " + e.getMessage(), e);

        result = i18n.get("about.license.loading-failed");
    }

    return result;
}

From source file:org.geowebcache.rest.controller.GWCSeedingController.java

/**
 * Deserializing a json string is more complicated.
 *
 * XStream does not natively support it. Rather, it uses a JettisonMappedXmlDriver to convert to
 * intermediate xml and then deserializes that into the desired object. At this time, there is a
 * known issue with the Jettison driver involving elements that come after an array in the json
 * string./*from  w w  w.j a  v a2 s.c om*/
 *
 * http://jira.codehaus.org/browse/JETTISON-48
 *
 * The code below is a hack: it treats the json string as text, then converts it to the
 * intermediate xml and then deserializes that into the SeedRequest object.
 */
protected String convertJson(String entityText) throws IOException {
    HierarchicalStreamDriver driver = new JettisonMappedXmlDriver();
    StringReader reader = new StringReader(entityText);
    HierarchicalStreamReader hsr = driver.createReader(reader);
    StringWriter writer = new StringWriter();
    new HierarchicalStreamCopier().copy(hsr, new PrettyPrintWriter(writer));
    writer.close();
    return writer.toString();
}