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:jp.sourceforge.reflex.core.ResourceMapper.java

public String toXML(Object obj, boolean printns) {
    ((RXMapper) this.getClassMapper()).setJo_packagemap(new LinkedHashMap<String, String>(jo_packages0),
            printns);/*from   w ww .ja  v  a  2s  . c  o m*/
    //      ((RXMapper)this.getClassMapper()).clearJo_namespacemap();
    Writer stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = hierarchicalStreamDriver.createWriter(stringWriter);
    marshal(obj, writer);
    writer.flush();
    writer.close();
    return stringWriter.toString();
}

From source file:org.easyrec.taglib.ProfileRenderer.java

/**
 * This function will format the XML profile with intends and new lines.
 *
 * @param xmlToFormat the xml String you want to format
 * @return the formated version of teh given XML string
 *//*  w  w w  . ja v a2s.  c  o  m*/
private String formatXml(String xmlToFormat) {
    try {
        final Document document = generateXmlDocument(xmlToFormat);

        OutputFormat format = new OutputFormat(document);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        format.setOmitXMLDeclaration(true);
        Writer out = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(out, format);
        serializer.serialize(document);

        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        return xmlToFormat;
    }
}

From source file:org.ambraproject.service.XMLServiceImpl.java

/**
 * Given a string as input, will return an XML string representing the document after
 * transformation./*w  w  w.ja  v a  2s  .  c o  m*/
 *
 * @param description
 * @return string
 * @throws org.ambraproject.ApplicationException
 */
public String getTransformedDescription(String description) throws ApplicationException {
    String transformedString;
    try {
        final DocumentBuilder builder = createDocBuilder();
        Document desc = builder.parse(new InputSource(new StringReader("<desc>" + description + "</desc>")));
        final DOMSource domSource = new DOMSource(desc);
        final Transformer transformer = getTranslet(desc);
        final Writer writer = new StringWriter();

        transformer.transform(domSource, new StreamResult(writer));
        transformedString = writer.toString();
    } catch (Exception e) {
        throw new ApplicationException(e);
    }

    // Ambra stylesheet leaves "END_TITLE" as a marker for other processes
    transformedString = transformedString.replace("END_TITLE", "");
    return transformedString;
}

From source file:org.ambraproject.service.XMLServiceImpl.java

/**
 * Given an XML Document as input, will return an XML string representing the document after
 * transformation.//from  w w  w . jav a2 s  . co m
 *
 * @param doc
 * @return XML String of transformed document
 * @throws org.ambraproject.ApplicationException
 */
public String getTransformedDocument(Document doc) throws ApplicationException {
    String transformedString;
    try {
        if (log.isDebugEnabled())
            log.debug("Applying XSLT transform to the document...");

        final DOMSource domSource = new DOMSource(doc);
        final Transformer transformer = getTranslet(doc);
        final Writer writer = new StringWriter(1000);

        transformer.transform(domSource, new StreamResult(writer));
        transformedString = writer.toString();
    } catch (Exception e) {
        throw new ApplicationException(e);
    }
    return transformedString;
}

From source file:im.vector.util.BugReporter.java

/**
 * Read the file content as String/* www.ja  v  a  2  s .c o  m*/
 *
 * @param fin the input file
 * @return the file content as String
 */
private static String convertStreamToString(File fin) {
    Reader reader = null;

    try {
        Writer writer = new StringWriter();
        InputStream inputStream = new FileInputStream(fin);
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;

            char[] buffer = new char[2048];
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
            }
        }
        return writer.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + e.getMessage());
    } catch (OutOfMemoryError oom) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + oom.getMessage());
    } finally {
        try {
            if (null != reader) {
                reader.close();
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
        }
    }

    return "";
}

From source file:org.apache.ambari.server.orm.helpers.dbms.GenericDbmsHelper.java

@Override
public String getDropSequenceStatement(String sequenceName) {
    StringWriter writer = new StringWriter();
    String defaultStmt = String.format("DROP sequence %s", sequenceName);

    try {/*  w  ww .  ja  va  2  s  .  c  o  m*/
        Writer w = databasePlatform.buildSequenceObjectDeletionWriter(writer, sequenceName);
        return w != null ? w.toString() : defaultStmt;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return defaultStmt;
}

From source file:nl.ru.cmbi.vase.web.rest.JobRestResource.java

@MethodMapping(value = "/hsspresult/{id}", httpMethod = HttpMethod.GET, produces = RestMimeTypes.TEXT_PLAIN)
public String hsspResult(String id) {

    if (Config.isXmlOnly()) {

        log.warn("rest/hsspresult was requested, but not enabled");

        // hssp job submission is not allowed if hssp is turned off
        throw new AbortWithHttpErrorCodeException(HttpURLConnection.HTTP_NOT_FOUND);
    }/*from  w  ww .  ja va2  s  . com*/

    File hsspFile = new File(Config.getHSSPCacheDir(), id + ".hssp.bz2");

    String jobStatus = this.status(id);

    try {

        if (jobStatus.equals("SUCCESS") && hsspFile.isFile()) {

            StringWriter sw = new StringWriter();
            InputStream hsspIn = new BZip2CompressorInputStream(new FileInputStream(hsspFile));
            IOUtils.copy(hsspIn, sw);
            hsspIn.close();
            sw.close();

            return sw.toString();
        }

        URL url = new URL(hsspRestURL + "/result/pdb_file/hssp_stockholm/" + id + "/");

        Writer writer = new StringWriter();
        IOUtils.copy(url.openStream(), writer);
        writer.close();

        JSONObject output = new JSONObject(writer.toString());

        String result = output.getString("result");

        if (jobStatus.equals("SUCCESS") && Config.hsspPdbCacheEnabled()) {

            // Write it to the cache:
            OutputStream fileOut = new BZip2CompressorOutputStream(new FileOutputStream(hsspFile));
            IOUtils.write(result, fileOut);
            fileOut.close();
        } else
            return "";

        return result;

    } catch (Exception e) {

        log.error(e.getMessage(), e);
        throw new AbortWithHttpErrorCodeException(HttpURLConnection.HTTP_INTERNAL_ERROR);
    }
}

From source file:de.jaide.courier.email.SmtpConfiguration.java

/**
 * Returns the SMTP configuration as a JSON string.
 * /*from   w  ww  . ja v  a2  s  .  co m*/
 * @return The SMTP configuration as a JSON string.
 */
@SuppressWarnings("unchecked")
@Override
public String toJSONString() {
    JSONObject entry = new JSONObject();

    Map<String, Object> configuration = new LinkedHashMap<String, Object>();
    configuration.put("smtpHostname", smtpHostname);
    configuration.put("smtpPort", new Integer(smtpPort));
    configuration.put("tls", tls);
    configuration.put("ssl", ssl);
    configuration.put("username", username);
    configuration.put("password", password);
    configuration.put("fromEMail", fromEMail);
    configuration.put("fromSenderName", fromSenderName);

    entry.put(configurationName, configuration);

    /*
     * The JSONWriter will pretty-print the output
     */
    Writer jsonWriter = new JSONWriter();
    try {
        entry.writeJSONString(jsonWriter);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    return jsonWriter.toString();
}

From source file:com.devnexus.ting.core.service.impl.CfpToMailTransformer.java

public String applyMustacheTemplate(CfpSubmission cfpSubmission, String template) {
    Map<String, Object> context = new HashMap<String, Object>();

    context.put("salutation", cfpSubmission.getSpeakersAsString(true));
    context.put("description", cfpSubmission.getDescription());
    context.put("presentationType", cfpSubmission.getPresentationType().getName());
    context.put("skillLevel", cfpSubmission.getSkillLevel().getName());
    context.put("comments", cfpSubmission.getSlotPreference());
    context.put("topic", cfpSubmission.getTopic());
    context.put("title", cfpSubmission.getTitle());
    context.put("sessionRecordingApproved", cfpSubmission.isSessionRecordingApproved() ? "Yes" : "No");
    context.put("speakers", cfpSubmission.getSpeakers());

    Writer writer = new StringWriter();
    MustacheFactory mf = new DefaultMustacheFactory();
    Mustache mustache = mf.compile(new StringReader(template), "email-notification");

    try {//  w w w.  j  ava 2 s . c  o m
        mustache.execute(writer, context).flush();
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    return writer.toString();
}

From source file:com.migratebird.script.repository.impl.ArchiveScriptLocation.java

/**
 * @param properties A properties map//from w w w  . ja  v a 2  s  .co m
 * @return The given properties as a reader to a properties file
 * @throws IOException if a problem occurs opening the reader
 */
protected Reader getPropertiesAsFile(Properties properties) throws IOException {
    Writer propertiesFileWriter = new StringWriter();
    properties.store(new WriterOutputStream(propertiesFileWriter), null);
    return new StringReader(propertiesFileWriter.toString());
}