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:com.wxxr.mobile.core.tools.VelocityTemplateRenderer.java

@Override
public String renderMacro(String macro, Map<String, Object> attributes, String[] params) {
    checkWasConfigured();/*from w ww .  j  a va 2  s.  c  o m*/
    customize(false);

    VelocityContext context = createContext(attributes);

    TypeUtils typeUtils = createTypeUtils();
    context.put("types", typeUtils);

    Writer writer = new StringWriter();
    String logTag = "\"#" + macro + '"';
    engine.invokeVelocimacro(macro, logTag, params, context, writer);
    String renderedText = writer.toString();

    return postProcess(renderedText, typeUtils);
}

From source file:configuration.GroupAccessBundle.java

/**
 * {@inheritDoc}//from  ww  w  .jav a2 s.com
 */
@Override
public String toString() {
    Writer writer = new JSONPrettyPrintWriter();
    try {
        JSONValue.writeJSONString(this.toMap(), writer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

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

/**
 * get create table statement//  w ww  .  ja v a 2  s  .c  o m
 * @param tableName
 * @param columns
 * @param primaryKeyColumns
 * @return
 */
@Override
public String getCreateTableStatement(String tableName, List<DBAccessor.DBColumnInfo> columns,
        List<String> primaryKeyColumns) {
    Writer stringWriter = new StringWriter();
    writeCreateTableStatement(stringWriter, tableName, columns, primaryKeyColumns);
    return stringWriter.toString();
}

From source file:ca.uhn.fhir.jaxrs.server.util.JaxRsResponse.java

@Override
public Response sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) {
    ResponseBuilder builder = buildResponse(theStatus);
    if (isNotBlank(theContentType)) {
        String charContentType = theContentType + "; charset="
                + StringUtils.defaultIfBlank(theCharset, Constants.CHARSET_NAME_UTF8);
        builder.header(Constants.HEADER_CONTENT_TYPE, charContentType);
    }/*from   w  ww .j av  a 2 s .  c o  m*/
    builder.entity(theWriter.toString());
    Response retVal = builder.build();
    return retVal;
}

From source file:com.zyeeda.framework.template.internal.FreemarkerTemplateServiceProvider.java

@Override
public String render(String template, Map<String, Object> args) throws IOException {
    Reader reader = null;//from  w  ww .j  a va  2 s .  co  m
    Writer writer = null;
    try {
        reader = new StringReader(template);
        Template tpl = new Template(null, reader, this.config);
        writer = new StringWriter();
        this.putBuildinVariables(args);
        tpl.process(args, writer);
        writer.flush();
        return writer.toString();
    } catch (TemplateException e) {
        throw new TemplateServiceException(e);
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.glaf.template.engine.pipeline.FreemarkerPipeline.java

public Writer pipeline(String pipeTemplates[], Object rootMap, Writer writer) throws PipeException {
    try {//from  ww w  . jav a2  s.  co  m
        Map<String, String> globalContext = new java.util.HashMap<String, String>();
        for (int i = 0; i < pipeTemplates.length; i++) {
            String templateName = pipeTemplates[i];
            Template template = conf.getTemplate(templateName);
            if (i == pipeTemplates.length - 1) {
                Environment env = template.createProcessingEnvironment(rootMap, writer);
                env.getCurrentNamespace().putAll(globalContext);
                env.process();
            } else {
                Writer tempOutput = new StringWriter(bufferSize);
                Environment env = template.createProcessingEnvironment(rootMap, tempOutput);
                env.getCurrentNamespace().putAll(globalContext);
                env.process();
                globalContext.putAll(env.getCurrentNamespace().toMap());
                globalContext.put(Pipeline.PIPELINE_CONTENT_VAR_NAME, tempOutput.toString());
            }
        }
        return writer;
    } catch (Exception e) {
        throw new PipeException("process FreeMarker template occer exception,pipeTemplates:"
                + StringUtils.join(pipeTemplates, " | "), e);
    }
}

From source file:org.jannocessor.service.render.VelocityTemplateRenderer.java

@Override
public String renderMacro(String macro, Map<String, Object> attributes, String[] params)
        throws JannocessorException {
    checkWasConfigured();// w ww. j av  a  2 s .c om
    customize(false);

    VelocityContext context = createContext(attributes);

    TypeUtils typeUtils = createTypeUtils();
    context.put("types", typeUtils);

    Writer writer = new StringWriter();
    String logTag = "\"#" + macro + '"';
    engine.invokeVelocimacro(macro, logTag, params, context, writer);
    String renderedText = writer.toString();

    return postProcess(renderedText, typeUtils);
}

From source file:org.apache.jena.osgi.test.JenaOSGITest.java

@Test
public void testJenaCore() throws Exception {
    Model model = makeModel();//ww  w .ja va  2s.  c o  m

    // Does Model's Class.forName() still work?
    model.setWriterClassName("someWriter", "com.hp.hpl.jena.rdf.model.impl.NTripleWriter");
    Writer writer = new StringWriter();
    model.write(writer, "someWriter");
    // yes, but only as long as that classname is accessible within
    // jena-osgi bundle
    assertEquals("<http://example.com/alice> <http://xmlns.com/foaf/0.1/knows> <http://example.com/bob> .",
            writer.toString().trim());

    // Let's also test com.hp.hpl.jena.ontology
    OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
    ObjectProperty knowsObjProp = ontModel.createObjectProperty(knows.getURI());
    ObjectProperty hasFriend = ontModel.createObjectProperty("http://example.com/has_friend");
    hasFriend.addSuperProperty(knowsObjProp);

    Individual aliceIndividual = ontModel.createIndividual(alice);
    Individual bobIndividiual = ontModel.createIndividual(bob);
    ontModel.add(aliceIndividual, hasFriend, bobIndividiual);
    assertTrue(aliceIndividual.hasProperty(knowsObjProp, bobIndividiual));
}

From source file:eu.prestoprime.p4gui.connection.SearchConnection.java

public static String solrSuggest(P4Service service, final String term) {
    Writer writer = new StringWriter();

    String results = "";
    try {/*from w ww .java  2 s.  c  o  m*/
        StringBuilder sb = new StringBuilder();
        sb.append(service.getURL());
        sb.append(SUGGEST_URI);
        sb.append("?term=");
        sb.append(term);
        String path = sb.toString();

        logger.debug("Query to P4WS:\n" + path);

        P4HttpClient client = new P4HttpClient(service.getUserID());

        client.getParams().setParameter("http.protocol.content-charset", "UTF-8");

        HttpRequestBase request = new HttpGet(path);
        HttpResponse response = client.executeRequest(request);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream is = entity.getContent();
            if (is != null) {
                char[] buffer = new char[1024];

                Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
                is.close();
            }
        }
        results = writer.toString();
        EntityUtils.consume(entity);
        writer.close();
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error("Either the query was invalid or P4WS could not be accessed.");
        results = null;
    }
    return results;
}

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

/**
 * Given a string as input, will return an XML string representing the document after
 * transformation./*from www.j a va2s .  c  o  m*/
 *
 * @param description
 * @return string
 * @throws org.ambraproject.ApplicationException
 */
@Override
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;
}