Java Code Examples for groovy.lang.Writable#writeTo()

The following examples show how to use groovy.lang.Writable#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyViewEngine.java    From ozark with Apache License 2.0 5 votes vote down vote up
public void processView(ViewEngineContext context) throws ViewEngineException {

        Map<String, Object> model = new HashMap<>(context.getModels().asMap());
        model.put("request", context.getRequest(HttpServletRequest.class));
        Charset charset = resolveCharsetAndSetContentType(context);
        try (Writer writer = new OutputStreamWriter(context.getOutputStream(), charset);
            InputStream resourceAsStream = servletContext.getResourceAsStream(resolveView(context));
            InputStreamReader in = new InputStreamReader(resourceAsStream, "UTF-8");) {
            Template template = markupTemplateEngine.createTemplate(in);
            Writable output = template.make(model);
            output.writeTo(writer);
        } catch (IOException | CompilationFailedException | ClassNotFoundException e) {
            throw new ViewEngineException(e);
        }
    }
 
Example 2
Source File: StreamingJsonBuilder.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the given Writable as the value of the given attribute name
 *
 * @param name The attribute name 
 * @param json The writable value
 * @throws IOException
 */
public void call(String name, Writable json) throws IOException {
    writeName(name);
    verifyValue();
    if(json instanceof GString) {
        writer.write(generator.toJson(json.toString()));
    }
    else {
        json.writeTo(writer);
    }
}
 
Example 3
Source File: XmlUtil.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static String asString(Writable writable) {
    if (writable instanceof GPathResult) {
        return asString((GPathResult) writable); //GROOVY-4285
    }
    Writer sw = new StringBuilderWriter();
    try {
        writable.writeTo(sw);
    } catch (IOException e) {
        // ignore
    }
    return sw.toString();
}
 
Example 4
Source File: InvokerHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Appends an object to an Appendable using Groovy's default representation for the object.
 */
public static void append(Appendable out, Object object) throws IOException {
    if (object instanceof String) {
        out.append((String) object);
    } else if (object instanceof Object[]) {
        out.append(toArrayString((Object[]) object));
    } else if (object instanceof Map) {
        out.append(toMapString((Map) object));
    } else if (object instanceof Collection) {
        out.append(toListString((Collection) object));
    } else if (object instanceof Writable) {
        Writable writable = (Writable) object;
        Writer stringWriter = new StringBuilderWriter();
        writable.writeTo(stringWriter);
        out.append(stringWriter.toString());
    } else if (object instanceof InputStream || object instanceof Reader) {
        // Copy stream to stream
        try (Reader reader =
                     object instanceof InputStream
                             ? new InputStreamReader((InputStream) object)
                             : (Reader) object) {
            char[] chars = new char[8192];
            for (int i; (i = reader.read(chars)) != -1; ) {
                for (int j = 0; j < i; j++) {
                    out.append(chars[j]);
                }
            }
        }
    } else {
        out.append(toString(object));
    }
}
 
Example 5
Source File: GroovyTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
    try {
        Template template = findTemplate(templateName);
        Writable writable = template.make(wrap(model));
        writable.writeTo(writer);
    } catch (Exception e) {
        throw new RenderingException(e);
    }
}
 
Example 6
Source File: GroovyMarkupTemplateEngine.java    From jbake with MIT License 5 votes vote down vote up
@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
    try {
        Template template = templateEngine.createTemplateByPath(templateName);
        Map<String, Object> wrappedModel = wrap(model);
        Writable writable = template.make(wrappedModel);
        writable.writeTo(writer);
    } catch (Exception e) {
        throw new RenderingException(e);
    }
}
 
Example 7
Source File: GroovyTemplateHandler.java    From myexcel with Apache License 2.0 4 votes vote down vote up
@Override
protected <F> void render(Map<String, F> renderData, Writer out) throws Exception {
    Writable output = templateEngine.make(renderData);
    output.writeTo(out);
}
 
Example 8
Source File: IOGroovyMethods.java    From groovy with Apache License 2.0 2 votes vote down vote up
/**
 * A helper method so that dynamic dispatch of the writer.write(object) method
 * will always use the more efficient Writable.writeTo(writer) mechanism if the
 * object implements the Writable interface.
 *
 * @param self     a Writer
 * @param writable an object implementing the Writable interface
 * @throws IOException if an I/O error occurs.
 * @since 1.0
 */
public static void write(Writer self, Writable writable) throws IOException {
    writable.writeTo(self);
}