Search in sources :

Example 36 with Template

use of groovy.text.Template in project groovy-core by groovy.

the class TemplateServlet method service.

/**
 * Services the request with a response.
 * <p>
 * First the request is parsed for the source file uri. If the specified file
 * could not be found or can not be read an error message is sent as response.
 *
 * @param request  The http request.
 * @param response The http response.
 * @throws IOException      if an input or output error occurs while the servlet is handling the HTTP request
 * @throws ServletException if the HTTP request cannot be handled
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (verbose) {
        log("Creating/getting cached template...");
    }
    // 
    // Get the template source file handle.
    // 
    Template template;
    long getMillis;
    String name;
    File file = super.getScriptUriAsFile(request);
    if (file != null) {
        name = file.getName();
        if (!file.exists()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            // throw new IOException(file.getAbsolutePath());
            return;
        }
        if (!file.canRead()) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Can not read \"" + name + "\"!");
            // throw new IOException(file.getAbsolutePath());
            return;
        }
        getMillis = System.currentTimeMillis();
        template = getTemplate(file);
        getMillis = System.currentTimeMillis() - getMillis;
    } else {
        name = super.getScriptUri(request);
        URL url = servletContext.getResource(name);
        getMillis = System.currentTimeMillis();
        template = getTemplate(url);
        getMillis = System.currentTimeMillis() - getMillis;
    }
    // 
    // Create new binding for the current request.
    // 
    ServletBinding binding = new ServletBinding(request, response, servletContext);
    setVariables(binding);
    // 
    // Prepare the response buffer content type _before_ getting the writer.
    // and set status code to ok
    // 
    response.setContentType(CONTENT_TYPE_TEXT_HTML + "; charset=" + encoding);
    response.setStatus(HttpServletResponse.SC_OK);
    // 
    // Get the output stream writer from the binding.
    // 
    Writer out = (Writer) binding.getVariable("out");
    if (out == null) {
        out = response.getWriter();
    }
    // 
    if (verbose) {
        log("Making template \"" + name + "\"...");
    }
    // String made = template.make(binding.getVariables()).toString();
    // log(" = " + made);
    long makeMillis = System.currentTimeMillis();
    template.make(binding.getVariables()).writeTo(out);
    makeMillis = System.currentTimeMillis() - makeMillis;
    if (generateBy) {
        StringBuilder sb = new StringBuilder(100);
        sb.append("\n<!-- Generated by Groovy TemplateServlet [create/get=");
        sb.append(Long.toString(getMillis));
        sb.append(" ms, make=");
        sb.append(Long.toString(makeMillis));
        sb.append(" ms] -->\n");
        out.write(sb.toString());
    }
    // 
    // flush the response buffer.
    // 
    response.flushBuffer();
    if (verbose) {
        log("Template \"" + name + "\" request responded. [create/get=" + getMillis + " ms, make=" + makeMillis + " ms]");
    }
}
Also used : File(java.io.File) URL(java.net.URL) Writer(java.io.Writer) Template(groovy.text.Template)

Example 37 with Template

use of groovy.text.Template in project groovy-core by groovy.

the class GroovyDocTemplateEngine method applyPackageTemplate.

String applyPackageTemplate(String template, GroovyPackageDoc packageDoc) {
    String templateWithBindingApplied = "";
    try {
        Template t = packageTemplates.get(template);
        if (t == null) {
            t = engine.createTemplate(resourceManager.getReader(template));
            packageTemplates.put(template, t);
        }
        Map<String, Object> binding = new HashMap<String, Object>();
        binding.put("packageDoc", packageDoc);
        binding.put("props", properties);
        templateWithBindingApplied = t.make(binding).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return templateWithBindingApplied;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Template(groovy.text.Template)

Example 38 with Template

use of groovy.text.Template in project groovy-core by groovy.

the class GroovyDocTemplateEngine method applyClassTemplates.

String applyClassTemplates(GroovyClassDoc classDoc) {
    // todo (iterate)
    String templatePath = classTemplatePaths.get(0);
    String templateWithBindingApplied = "";
    try {
        Template t = classTemplates.get(templatePath);
        if (t == null) {
            t = engine.createTemplate(resourceManager.getReader(templatePath));
            classTemplates.put(templatePath, t);
        }
        Map<String, Object> binding = new HashMap<String, Object>();
        binding.put("classDoc", classDoc);
        binding.put("props", properties);
        templateWithBindingApplied = t.make(binding).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return templateWithBindingApplied;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Template(groovy.text.Template)

Example 39 with Template

use of groovy.text.Template in project groovy-core by groovy.

the class GroovyDocTemplateEngine method applyRootDocTemplate.

String applyRootDocTemplate(String template, GroovyRootDoc rootDoc) {
    String templateWithBindingApplied = "";
    try {
        Template t = docTemplates.get(template);
        if (t == null) {
            t = engine.createTemplate(resourceManager.getReader(template));
            docTemplates.put(template, t);
        }
        Map<String, Object> binding = new HashMap<String, Object>();
        binding.put("rootDoc", rootDoc);
        binding.put("props", properties);
        templateWithBindingApplied = t.make(binding).toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return templateWithBindingApplied;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Template(groovy.text.Template)

Example 40 with Template

use of groovy.text.Template in project spring-framework by spring-projects.

the class GroovyMarkupView method renderMergedTemplateModel.

@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()));
}
Also used : Template(groovy.text.Template) BufferedWriter(java.io.BufferedWriter)

Aggregations

Template (groovy.text.Template)41 IOException (java.io.IOException)19 HashMap (java.util.HashMap)7 ServletException (javax.servlet.ServletException)7 Writable (groovy.lang.Writable)6 SimpleTemplateEngine (groovy.text.SimpleTemplateEngine)6 Writer (java.io.Writer)6 File (java.io.File)5 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 GroovyObject (groovy.lang.GroovyObject)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 GrailsTagException (org.grails.taglib.GrailsTagException)2 Test (org.junit.Test)2 WebConfig (com.haulmont.cuba.web.WebConfig)1 TestExplanation1 (eu.esdihumboldt.hale.common.align.model.impl.mdexpl.test.TestExplanation1)1