/** * 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); }
public Writer writeTo(final Writer out) throws IOException { if (this.replacementNodeStack.empty()) { for (Object child : this.children) { if (child instanceof Writable) { ((Writable) child).writeTo(out); } else { out.write(child.toString()); } } return out; } else { return ((Writable) this.replacementNodeStack.peek()).writeTo(out); } }
public void process(OutputStream out) throws IOException { Writer w = new OutputStreamWriter(out, charset); c.writeTo(w); w.flush(); w.close(); } });
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { String url = getUrl(); Assert.state(url != null, "'url' not set"); Template template = getTemplate(url); template.make(model).writeTo(new BufferedWriter(response.getWriter())); }
public void process(OutputStream out) throws IOException { Writer w = new OutputStreamWriter(out, charset); c.writeTo(w); w.flush(); w.close(); } });
private static String asString(Writable writable) { if (writable instanceof GPathResult) { return asString((GPathResult) writable); //GROOVY-4285 } Writer sw = new StringWriter(); try { writable.writeTo(sw); } catch (IOException e) { // ignore } return sw.toString(); }
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { String url = getUrl(); Assert.state(url != null, "'url' not set"); Template template = getTemplate(url); template.make(model).writeTo(new BufferedWriter(response.getWriter())); }
/** * Renders an embedded template as a fragment. Fragments are cached in a template, meaning that * if you use the same fragment in a template, it will only be compiled once, but once <b>per template * instance</b>. This is less performant than using {@link #layout(java.util.Map, String)}. * * @param model model to be passed to the template * @param templateText template body * @return this template instance * @throws IOException * @throws ClassNotFoundException */ public Object fragment(Map model, String templateText) throws IOException, ClassNotFoundException { Template template = cachedFragments.get(templateText); if (template==null) { template = engine.createTemplate(new StringReader(templateText)); cachedFragments.put(templateText, template); } template.make(model).writeTo(out); return this; }
public String encode(final ClassNode node) { ByteArrayOutputStream baos = new ByteArrayOutputStream(128); DataOutputStream dos = new DataOutputStream(baos); Writer wrt = new StringBuilderWriter(); String encoded = null; try { doEncode(node, dos); EncodingGroovyMethods.encodeBase64(baos.toByteArray()).writeTo(wrt); encoded = wrt.toString(); } catch (IOException e) { throw new GroovyRuntimeException("Unable to serialize type information", e); } return encoded; }
} else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out;
/** * Includes another template inside this template. * @param templatePath the path to the included resource. * @throws IOException * @throws ClassNotFoundException */ public void includeGroovy(String templatePath) throws IOException, ClassNotFoundException { URL resource = engine.resolveTemplate(templatePath); engine.createTypeCheckedModelTemplate(resource, modelTypes).make(model).writeTo(out); }
} else if (object instanceof Writable) { Writable writable = (Writable) object; writable.writeTo(out); } else if (object instanceof InputStream || object instanceof Reader) {
Writable writable = (Writable) object; Writer stringWriter = new StringBuilderWriter(); writable.writeTo(stringWriter); out.append(stringWriter.toString()); } else if (object instanceof InputStream || object instanceof Reader) {
template.make(binding.getVariables()).writeTo(out); makeMillis = System.currentTimeMillis() - makeMillis;
/** * Imports a template and renders it using the specified model, allowing fine grained composition of templates and * layouting. This works similarily to a template include but allows a distinct model to be used. If the layout * inherits from the parent model, a new model is created, with the values from the parent model, eventually * overriden with those provided specifically for this layout. * * @param model model to be passed to the template * @param templateName the name of the template to be used as a layout * @param inheritModel a boolean indicating if we should inherit the parent model * @return this template instance * @throws IOException * @throws ClassNotFoundException */ public Object layout(Map model, String templateName, boolean inheritModel) throws IOException, ClassNotFoundException { Map submodel = inheritModel ? forkModel(model) : model; URL resource = engine.resolveTemplate(templateName); engine.createTypeCheckedModelTemplate(resource, modelTypes).make(submodel).writeTo(out); return this; }
@Get(value = "/welcome", produces = MediaType.TEXT_PLAIN) Writable render() { // <2> return writer -> template.make( // <3> CollectionUtils.mapOf( "firstName", "Fred", "lastName", "Flintstone" ) ).writeTo(writer); }
/** * 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); } }
} else if (data instanceof Writable) { StringWriter out = new StringWriter(); ((Writable) data).writeTo(out); out.flush(); data = out;
@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); } }
@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); } }