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.kalypso.model.wspm.pdb.ui.internal.wspm.InitPerspectiveJob.java

private String createGmvInput() {
    try {//w  w w  . j  av a2  s  .  c o  m
        final IFile modelFile = m_project.getModelFile();
        final URL modelURL = ResourceUtilities.createQuietURL(modelFile);

        final Gistreeview gistreeview = TemplateUtilities.OF_GISTREEVIEW.createGistreeview();
        final LayerType layerType = TemplateUtilities.OF_TEMPLATE_TYPES.createLayerType();

        layerType.setFeatureXPath("id( 'root' )/wspm:waterBodyMember"); // root feature //$NON-NLS-1$
        layerType.setHref(modelURL.toExternalForm());
        layerType.setLinktype("gml"); //$NON-NLS-1$

        gistreeview.setInput(layerType);

        final Marshaller marshaller = TemplateUtilities.createGistreeviewMarshaller(CharEncoding.UTF_8);
        final StringWriter sw = new StringWriter();
        marshaller.marshal(gistreeview, sw);
        sw.close();
        return sw.toString();
    } catch (final JAXBException e) {
        e.printStackTrace();
        return null;
    } catch (final IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:fr.smile.services.PatchService.java

public void readFile() throws IOException {

    InputStream is = getClass().getResourceAsStream(PATH);

    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);//from   ww  w  . j a  va 2s  . co  m
    String json = writer.toString();
    is.close();
    writer.close();

    JSONArray a = jsonParser(json);

    String version, endVersion, url, instructions, instructionsCluster, warning;
    Boolean reboot, license;

    Patch patch;

    for (Object o : a) {
        JSONObject jsonVersion = (JSONObject) o;

        version = (String) jsonVersion.get("version");
        listVersion.add(version);

        JSONArray patches = (JSONArray) jsonVersion.get("patches");

        for (Object p : patches) {
            JSONObject jsonPatch = (JSONObject) p;
            endVersion = (String) jsonPatch.get("endVersion");
            url = (String) jsonPatch.get("url");
            instructions = (String) jsonPatch.get("instructions");
            instructionsCluster = (String) jsonPatch.get("instructionsCluster");
            warning = (String) jsonPatch.get("warning");
            String r = (String) jsonPatch.get("reboot");
            reboot = r == null || "1".equals(r);
            String l = (String) jsonPatch.get("license");
            license = l != null && "1".equals(l);

            patch = Patch.builder().startVersion(version).endVersion(endVersion).url(url)
                    .instructionsCluster(instructionsCluster).instructions(instructions).warning(warning)
                    .reboot(reboot).license(license).build();

            listPatch.add(patch);
        }
    }
}

From source file:org.metamorfosis.template.freemarker.AbstractFreemarkerTestCase.java

protected void assertEqualsFreemarkerTemplate(Object rootMap, String expectedResult, File template)
        throws Exception {
    String fullPath = FilenameUtils.getFullPath(template.getAbsolutePath());
    cfg.setDirectoryForTemplateLoading(new File(fullPath));

    String relativeTemplate = FilenameUtils.getName(template.getAbsolutePath());
    freemarker.template.Template freeMarkerTemplate = cfg.getTemplate(relativeTemplate);

    StringWriter sw = new StringWriter();
    Environment env = freeMarkerTemplate.createProcessingEnvironment(rootMap, sw);
    env.process(); // process the template

    assertEquals(expectedResult, sw.toString());
    sw.close();
}

From source file:com.jayway.jsonpath.internal.spi.json.JacksonJsonProvider.java

@Override
public String toJson(Object obj) {
    StringWriter writer = new StringWriter();
    try {//from w  ww  . j  av  a  2s .com
        JsonGenerator generator = objectMapper.getFactory().createGenerator(writer);
        objectMapper.writeValue(generator, obj);
        writer.flush();
        writer.close();
        generator.close();
        return writer.getBuffer().toString();
    } catch (IOException e) {
        throw new InvalidJsonException();
    }
}

From source file:org.metamorfosis.template.freemarker.AbstractFreemarkerTestCase.java

protected void assertEqualsFreemarkerTemplate(Object rootMap, String expectedResult, String template)
        throws Exception {
    File fTemplate = File.createTempFile("fmt_", ".ftl");
    FileWriter fw = new FileWriter(fTemplate);
    fw.write(template);/*from ww  w .ja  v a 2s .co m*/
    fw.flush();

    String relativeTemplate = FilenameUtils.getName(fTemplate.getAbsolutePath());
    freemarker.template.Template freeMarkerTemplate = cfg.getTemplate(relativeTemplate);

    StringWriter sw = new StringWriter();
    Environment env = freeMarkerTemplate.createProcessingEnvironment(rootMap, sw);
    env.process(); // process the template

    assertEquals(expectedResult, sw.toString());
    sw.close();
    fw.close();
    fTemplate.delete();
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.edit.MenuManagementEdit.java

public void processCommand(String command, VitroRequest vreq, HttpServletResponse resp) {

    OntModel displayModel = getDisplayModel(vreq);
    if (displayModel == null) {
        //Throw some kind of exception
        log.error("Display model not being retrieved correctly");
    }//from  w ww.  j av a2s .  c  o  m
    //if Add, then create new menu item and new page elements, and use the values above
    String errorMessage = "";
    if (isReorder(command)) {
        errorMessage = processReorder(displayModel, vreq);
    }

    //Edits to model occur here
    displayModel.enterCriticalSection(Lock.WRITE);
    try {
        //Output statements to be removed to log
        log.debug("Statement to be removed are ");
        StringWriter r = new StringWriter();
        removeStatements.write(r, "N3");
        log.debug(r.toString());
        r.close();
        //Output statements to be added to log
        log.debug("Statements to be added are ");
        StringWriter a = new StringWriter();
        addStatements.write(a, "N3");
        log.debug(a.toString());
        a.close();
        //Remove and add statements
        displayModel.remove(removeStatements);
        displayModel.add(addStatements);

    } catch (Exception ex) {
        log.error("An error occurred in processing command", ex);
        errorMessage += "An error occurred and the operation could not be completed successfully.";
    } finally {
        displayModel.leaveCriticalSection();
    }

    //if reorder, need to send back an AJAX response
    if (isReorder(command)) {
        sendReorderResponse(errorMessage, resp);
    }

}

From source file:org.apache.torque.util.SummaryHelperTest.java

public void testSummarizeEntireTable() throws Exception {
    Criteria c = new Criteria();

    SummaryHelper summary = new SummaryHelper();

    summary.setExcludeExprColumns(true);
    List<Class<?>> returnTypes = new ArrayList<Class<?>>();

    summary.addAggregate("COUNT_RECS", new Count(Summarize1Peer.ID));
    returnTypes.add(Integer.class);

    summary.addAggregate("AVG_INT1", new Avg(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("MIN_INT1", new Min(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("MAX_INT1", new Max(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("SUM_INT1", new Sum(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("AVG_FLOAT1", new Avg(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("MIN_FLOAT1", new Min(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("MAX_FLOAT1", new Max(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("SUM_FLOAT1", new Sum(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    List<ListOrderedMapCI> results = summary.summarize(c, returnTypes);

    StringWriter out = new StringWriter();
    summary.dumpResults(out, results, true);
    out.close();
    logger.debug("\n" + out.toString());

    assertTrue("No results returned", results.size() > 0);
    assertTrue("Invalid number of records returned.  Expected 1 but got " + results.size(),
            results.size() == 1);/*from   w w w. j ava 2s.  c o  m*/

    ListOrderedMapCI rec = results.get(0);

    assertTrue("Number of columns incorrect! Did ExcludeExpColumns work? " + "Expected 9 but got " + rec.size(),
            rec.size() == 9);
    assertTrue("COUNT_RECS not correct value", ((Integer) rec.get("COUNT_RECS")).intValue() == 16);
    assertTrue("AVG_INT1 not correct value", ((Integer) rec.get("AVG_INT1")).intValue() == 4);
    assertTrue("MIN_INT1 not correct value", ((Integer) rec.get("MIN_INT1")).intValue() == 1);
    assertTrue("MAX_INT1 not correct value", ((Integer) rec.get("MAX_INT1")).intValue() == 7);
    assertTrue("SUM_INT1 not correct value", ((Integer) rec.get("SUM_INT1")).intValue() == 64);

    assertTrue("AVG_FLOAT1 not correct value", ((Float) rec.get("AVG_FLOAT1")).floatValue() == 8.0f);
    assertTrue("MIN_FLOAT1 not correct value", ((Float) rec.get("MIN_FLOAT1")).floatValue() == 2.0f);
    assertTrue("MAX_FLOAT1 not correct value", ((Float) rec.get("MAX_FLOAT1")).floatValue() == 14.0f);
    assertTrue("SUM_FLOAT1 not correct value", ((Float) rec.get("SUM_FLOAT1")).floatValue() == 128.0f);
}

From source file:org.apache.torque.util.SummaryHelperTest.java

public void testSummarize() throws Exception {
    Criteria c = new Criteria();

    c.addAscendingOrderByColumn(Summarize1Peer.GROUP_BY1);

    SummaryHelper summary = new SummaryHelper();

    summary.setExcludeExprColumns(true);
    List<Class<?>> returnTypes = new ArrayList<Class<?>>();

    summary.addGroupBy(Summarize1Peer.GROUP_BY1);
    returnTypes.add(String.class);

    summary.addAggregate("COUNT_RECS", new Count(Summarize1Peer.ID));
    returnTypes.add(Integer.class);

    summary.addAggregate("AVG_INT1", new Avg(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("MIN_INT1", new Min(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("MAX_INT1", new Max(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("SUM_INT1", new Sum(Summarize1Peer.INT_1));
    returnTypes.add(Integer.class);

    summary.addAggregate("AVG_FLOAT1", new Avg(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("MIN_FLOAT1", new Min(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("MAX_FLOAT1", new Max(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    summary.addAggregate("SUM_FLOAT1", new Sum(Summarize1Peer.FLOAT1));
    returnTypes.add(Float.class);

    List<ListOrderedMapCI> results = summary.summarize(c, returnTypes);

    StringWriter out = new StringWriter();
    summary.dumpResults(out, results, true);
    out.close();
    logger.debug("\n" + out.toString());

    assertTrue("No results returned", results.size() > 0);
    assertTrue("Invalid number of records returned. Expected 4 but got " + results.size(), results.size() == 4);

    ListOrderedMapCI rec = results.get(0);

    assertTrue("GROUP_BY1 valued not correct", "A1".equals(rec.get("GROUP_BY1").toString()));
    assertTrue("COUNT_RECS not correct value", ((Integer) rec.get("COUNT_RECS")).intValue() == 4);
    assertTrue("AVG_INT1 not correct value", ((Integer) rec.get("AVG_INT1")).intValue() == 2);
    assertTrue("MIN_INT1 not correct value", ((Integer) rec.get("MIN_INT1")).intValue() == 1);
    assertTrue("MAX_INT1 not correct value", ((Integer) rec.get("MAX_INT1")).intValue() == 4);
    assertTrue("SUM_INT1 not correct value", ((Integer) rec.get("SUM_INT1")).intValue() == 10);

    rec = results.get(3);/*w w  w.j  a  v a2 s .c  om*/
    assertTrue("GROUP_BY1 valued not correct", "D1".equals(rec.get("GROUP_BY1")));
    assertTrue("COUNT_RECS not correct value", ((Integer) rec.get("COUNT_RECS")).intValue() == 4);
    assertTrue("AVG_FLOAT1 not correct value", ((Float) rec.get("AVG_FLOAT1")).floatValue() == 11.0f);
    assertTrue("MIN_FLOAT1 not correct value", ((Float) rec.get("MIN_FLOAT1")).floatValue() == 8.0f);
    assertTrue("MAX_FLOAT1 not correct value", ((Float) rec.get("MAX_FLOAT1")).floatValue() == 14.0f);
    assertTrue("SUM_FLOAT1 not correct value", ((Float) rec.get("SUM_FLOAT1")).floatValue() == 44.0f);

}

From source file:com.doitnext.swing.widgets.json.JSONJTreeNodeTest.java

@Test
public void pushTest() throws Exception {
    InputStream resource = null;//w  ww.  j a va  2 s.  com
    StringWriter sw = null;
    try {
        resource = this.getClass().getClassLoader().getResourceAsStream(fileName);
        sw = new StringWriter();
        IOUtils.copy(resource, sw);
        String json = sw.toString();
        sw.close();
        sw = null;
        resource.close();
        resource = null;
        JsonElement rootElt = new JsonParser().parse(json);
        JSONJTreeNode root = new JSONJTreeNode(null, -1, rootElt);

        JsonElement converted = root.asJsonElement();
        String expected = rootElt.toString();
        String actual = converted.toString();

        Assert.assertEquals("Json does not match", expected, actual);
        if (expectedError != null)
            throw new IllegalArgumentException(
                    "We didnt' get an " + expectedError.getClass().getName() + " exception as expected.",
                    expectedError);

    } catch (Exception e) {
        if (!e.getClass().isInstance(expectedError))
            throw e;
    } finally {
        if (resource != null)
            resource.close();
        if (sw != null)
            sw.close();
    }
}