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.squale.squaleweb.util.ExceptionWrapper.java

/**
 * Obtention du dtail d'une exception/*from  ww  w  .  j av a  2s .com*/
 * 
 * @param pException exception  dtailler
 * @return dtail de l'exception (au format HTML) en fonction de la configuration (@see #isDumpEnabled())
 */
static public String getExceptionDetail(Throwable pException) {
    // On log l'exception au cas o
    logger.error("Exception survenue ", pException);
    java.io.StringWriter sw = new java.io.StringWriter();
    java.io.PrintWriter pw = new java.io.PrintWriter(sw);
    if (isDumpEnabled()) {
        fillExceptionTrace(pException, pw);
    }
    try {
        sw.close();
    } catch (IOException e) {
        // Impossible  obtenir sur un StringWriter.close
    }
    pw.close();
    return sw.toString();
}

From source file:Main.java

public static String node2String(Node node) throws Exception {
    if (node == null)
        return null;
    StringWriter out = null;

    try {//from ww w .jav  a2  s  . c o  m
        Transformer transformer = transformerFactory.newTransformer();
        out = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    } catch (Exception e) {
        throw e;
    } finally {
        if (out != null)
            out.close();
    }
}

From source file:com.netsteadfast.greenstep.util.Pivot4JUtils.java

public static String rendererHtml(String mondrianUrl, String mdx, boolean showDimensionTitle,
        boolean showParentMembers) throws Exception {
    if (StringUtils.isBlank(mondrianUrl) || StringUtils.isBlank(mdx)) {
        throw new java.lang.IllegalArgumentException("mondrian url and MDX cannot blank.");
    }/* w  w w  . j  a va 2  s.  c o  m*/
    String body = "";
    OlapConnection connection = OlapUtils.getConnection(mondrianUrl);
    OlapDataSource dataSource = new SingleConnectionOlapDataSource(connection);
    try {
        PivotModel model = getPivotModel(dataSource, mdx);
        TableRenderer renderer = getTableRenderer(showDimensionTitle, showParentMembers);
        StringWriter writer = new StringWriter();
        renderer.render(model, new HtmlRenderCallback(writer));

        //writer.write(body);
        body = writer.toString();
        writer.flush();
        writer.close();
    } catch (Exception e) {
        throw e;
    } finally {
        OlapUtils.nullConnection(connection);
        connection = null;
        dataSource = null;
    }
    return body;
}

From source file:com.liferay.portal.util.LuceneFields.java

public static Field getFile(String field, File file, String fileExt) throws IOException {

    fileExt = fileExt.toLowerCase();/* ww w  . j  a v a  2 s  .com*/

    FileInputStream fis = new FileInputStream(file);
    Reader reader = new BufferedReader(new InputStreamReader(fis));

    String text = null;

    if (fileExt.equals(".doc")) {
        try {
            WordDocument wordDocument = new WordDocument(fis);

            StringWriter stringWriter = new StringWriter();

            wordDocument.writeAllText(stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".htm") || fileExt.equals(".html")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
            htmlEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".pdf")) {
        try {
            PDFParser parser = new PDFParser(fis);
            parser.parse();

            PDDocument pdDoc = parser.getPDDocument();

            StringWriter stringWriter = new StringWriter();

            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setLineSeparator("\n");
            stripper.writeText(pdDoc, stringWriter);

            text = stringWriter.toString();

            stringWriter.close();
            pdDoc.close();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".rtf")) {
        try {
            DefaultStyledDocument dsd = new DefaultStyledDocument();

            RTFEditorKit rtfEditorKit = new RTFEditorKit();
            rtfEditorKit.read(reader, dsd, 0);

            text = dsd.getText(0, dsd.getLength());
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    } else if (fileExt.equals(".xls")) {
        try {
            XLSTextStripper stripper = new XLSTextStripper(fis);

            text = stripper.getText();
        } catch (Exception e) {
            _log.error(e.getMessage());
        }
    }

    if (text != null) {
        return new Field(field, text, Field.Store.YES, Field.Index.NOT_ANALYZED);
    } else {
        return new Field(field, reader);
    }
}

From source file:Main.java

protected static Result internalTransformWithParams(Reader doc, Templates templates, Result r, boolean trace,
        String[] params) {//from   w ww.  j ava2 s  .c om
    StringWriter sw = new StringWriter();

    try {
        Transformer transformer = templates.newTransformer();
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++)
                transformer.setParameter("param_" + i, params[i]);
        }

        transformer.transform(new StreamSource(doc), r);

        sw.close();
        return r;
    } catch (Throwable th) {
        th.printStackTrace();
        return r;
    }

}

From source file:org.osaf.cosmo.dav.caldav.report.FreeBusyReport.java

private static String writeCalendar(Calendar calendar) throws DavException {
    try {//from  ww w. ja  va2 s  . c o  m
        StringWriter out = new StringWriter();
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.output(calendar, out);
        String output = out.toString();
        out.close();

        // NB ical4j's outputter generates \r\n line ends but we
        // need only \n, so remove all \r's from the string
        output = output.replaceAll("\r", "");

        return output;
    } catch (Exception e) {
        throw new DavException(e);
    }
}

From source file:org.apache.hive.ptest.execution.TestScripts.java

protected static String getTemplateResult(String command, Map<String, String> keyValues) throws IOException {
    VelocityContext context = new VelocityContext();
    for (String key : keyValues.keySet()) {
        context.put(key, keyValues.get(key));
    }/*from   ww w .  j  a  v  a  2s . c  o  m*/
    StringWriter writer = new StringWriter();
    if (!Velocity.evaluate(context, writer, command, command)) {
        throw new IOException("Unable to render " + command + " with " + keyValues);
    }
    writer.close();
    return writer.toString();
}

From source file:Main.java

/**
 * Using a Reader and a Writer, returns a String from an InputStream.
 *
 * Method based on Hypersonic Code//w ww  .ja  va  2  s  .  co m
 *
 * @param x InputStream to read from
 * @throws IOException
 * @return a Java string
 */
public static String inputStreamToString(InputStream x, String encoding) throws IOException {

    InputStreamReader in = new InputStreamReader(x, encoding);
    StringWriter writer = new StringWriter();
    int blocksize = 8 * 1024;
    char[] buffer = new char[blocksize];

    for (;;) {
        int read = in.read(buffer);

        if (read == -1) {
            break;
        }

        writer.write(buffer, 0, read);
    }

    writer.close();

    return writer.toString();
}

From source file:com.splout.db.common.SploutClient.java

public static String asString(InputStream inputStream) throws IOException {
    StringWriter writer = new StringWriter();
    try {/*from  www  .j a  va2  s .co  m*/
        IOUtils.copy(inputStream, writer);
        return writer.toString();
    } finally {
        inputStream.close();
        writer.close();
    }
}

From source file:Main.java

public static String toXML(Document dom, Properties outputProperties) {
    try {/*from   w w w  .  j  a v a2 s  .c  o m*/
        DOMSource source = new DOMSource(dom);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        Transformer tx = TransformerFactory.newInstance().newTransformer();
        if (outputProperties != null) {
            tx.setOutputProperties(outputProperties);
        }
        tx.transform(source, result);
        String s = writer.toString();
        writer.close();
        return s;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}