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]");
}
}
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;
}
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;
}
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;
}
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()));
}
Aggregations