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.unitedinternet.cosmo.dav.caldav.report.FreeBusyReport.java

private static String writeCalendar(Calendar calendar) throws CosmoDavException {
    try {//  www . j av  a  2s .com
        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 (IOException | ValidationException e) {
        throw new CosmoDavException(e);
    }
}

From source file:Main.java

public static String writeDocumentToString(Document document) {
    try {// w w  w.  j av  a2s. c o  m
        TransformerFactory transfac = TransformerFactory.newInstance();
        Transformer trans;
        trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StringWriter writer = new StringWriter();
        try {
            StreamResult streamResult = new StreamResult(writer);
            DOMSource source = new DOMSource(document);
            trans.transform(source, streamResult);
        } catch (TransformerException e) {
            e.printStackTrace();
            return null;
        } finally {
            writer.close();
        }
        return writer.toString();

    } catch (TransformerConfigurationException | IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:org.sakaiproject.bbb.impl.util.XmlUtil.java

public static String convertPropsToXml(Props props) throws Exception {
    String xml = "";
    StringWriter outputWriter = null;
    try {//from  ww  w  .j  a va  2  s  .c  om
        outputWriter = new StringWriter();
        outputWriter.write("<?xml version='1.0' ?>");
        BeanWriter beanWriter = getBeanWriter(outputWriter);
        beanWriter.write("MeetingProperties", props);
        xml = outputWriter.toString();
    } finally {
        outputWriter.close();
    }
    return xml;
}

From source file:Main.java

public static String getStackTrace(Exception e) {
    if (e == null) {
        return "";
    }//from  w  w  w . j  a v  a  2  s.  c  o  m
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:Main.java

public static String getStackTrace(@Nullable Throwable e) {
    if (e == null) {
        return "";
    }/*from   ww w .  ja  va  2  s  .  c o  m*/
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
    } finally {
        if (sw != null) {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
    return sw.toString();
}

From source file:org.kalypso.ogc.sensor.request.RequestFactory.java

public static String buildXmlString(final Request requestType, final boolean includeXmlHeader)
        throws JAXBException, IOException {
    StringWriter writer = null;
    try {/*w w  w. j a va2 s .  c o  m*/
        writer = new StringWriter();

        final Marshaller marshaller = JaxbUtilities.createMarshaller(JC);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.marshal(requestType, writer);
        writer.close();
    } finally {
        IOUtils.closeQuietly(writer);
    }
    final String xmlStr = writer.toString();
    if (!includeXmlHeader)
        return xmlStr.replaceAll("<\\?xml.*\\?>", "").replaceAll("\n", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    return xmlStr;
}

From source file:org.jumpmind.metl.core.runtime.component.XsltProcessor.java

public static String getBatchXml(Model model, ArrayList<EntityData> inputRows, boolean outputAllAttributes) {
    SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
    Element root = new Element("batch");
    Document doc = new Document(root);

    for (ModelEntity entity : getModelEntities(model, inputRows)) {
        Element entityElement = new Element("entity");
        entityElement.setAttribute("name", entity.getName());
        root.addContent(entityElement);/*from ww  w  .  java  2  s.c  o  m*/

        for (EntityData entityData : inputRows) {
            List<ModelAttribute> attributes = null;
            if (outputAllAttributes) {
                attributes = entity.getModelAttributes();
            } else {
                attributes = getModelAttributes(model, entity.getId(), entityData.keySet());
            }

            Element recordElement = new Element("record");
            if (attributes.size() > 0) {
                entityElement.addContent(recordElement);
            }

            for (ModelAttribute attribute : attributes) {
                if (attribute != null && attribute.getEntityId().equals(entity.getId())) {
                    Element attributeElement = new Element("attribute");
                    attributeElement.setAttribute("name", attribute.getName());
                    Object object = entityData.get(attribute.getId());
                    String value = null;
                    DataType type = attribute.getDataType();

                    if (object != null) {
                        if (type.isTimestamp() && object instanceof Date) {
                            value = df.format(object);
                        } else {
                            value = object.toString();
                        }
                    }
                    attributeElement.setAttribute("value", value == null ? "" : value);
                    recordElement.addContent(attributeElement);
                }
            }
        }
    }

    StringWriter writer = new StringWriter();
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    try {
        xmlOutput.output(doc, writer);
        writer.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return writer.toString();
}

From source file:UploadUtils.PomfUpload.java

/**
 * Get a response from Uguu.//  w w  w.j  a  va 2  s.  co m
 * @param conn the connection to use to listen to response.
 * @throws IOException during reading GZip response
 */
private static String getResponse(HttpURLConnection conn) throws IOException {
    String charset = "UTF-8";
    InputStream gzippedResponse = conn.getInputStream();
    InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse);
    Reader reader = new InputStreamReader(ungzippedResponse, charset);
    StringWriter writer = new StringWriter();
    char[] buffer = new char[10240];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        writer.write(buffer, 0, length);
    }
    String response = writer.toString();
    writer.close();
    reader.close();
    reader.close();
    for (ImagelinkListener ll : listeners) {
        ll.onImageLink(response);
    }
    return response;
}

From source file:com.medlog.webservice.lifecycle.Security.java

public static String getStackTrace(Throwable t) {
    String stackTrace = null;/*  w  w  w. j ava 2 s  .c  om*/
    try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        pw.close();
        sw.close();
        stackTrace = sw.getBuffer().toString();
    } catch (Exception ex) {
    }
    return stackTrace;
}

From source file:com.code.savemarks.utils.Utils.java

public static String stackTraceToString(Throwable e) {
    String retValue = null;//from   w w w  . j a v a2s .  c  o  m
    StringWriter sw = null;
    PrintWriter pw = null;
    try {
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        retValue = sw.toString();
    } finally {
        try {
            if (pw != null)
                pw.close();
            if (sw != null)
                sw.close();
        } catch (IOException ignore) {
        }
    }
    return retValue;
}