use of groovy.text.Template in project grails-core by grails.
the class GroovyPagesTemplateRenderer method generateScaffoldedTemplate.
private Template generateScaffoldedTemplate(GrailsWebRequest webRequest, String uri) throws IOException {
Template t = null;
Collection<String> controllerActions = scaffoldedActionMap.get(webRequest.getControllerName());
if (controllerActions != null && controllerActions.contains(webRequest.getActionName())) {
GrailsDomainClass domainClass = controllerToScaffoldedDomainClassMap.get(webRequest.getControllerName());
if (domainClass != null) {
int i = uri.lastIndexOf('/');
String scaffoldedtemplateName = i > -1 ? uri.substring(i) : uri;
if (scaffoldedtemplateName.toLowerCase().endsWith(".gsp")) {
scaffoldedtemplateName = scaffoldedtemplateName.substring(0, scaffoldedtemplateName.length() - 4);
}
FastStringWriter sw = new FastStringWriter();
ReflectionUtils.invokeMethod(generateViewMethod, scaffoldingTemplateGenerator, domainClass, scaffoldedtemplateName, sw);
t = groovyPagesTemplateEngine.createTemplate(new ByteArrayResource(sw.toString().getBytes("UTF-8"), uri), false);
}
}
return t;
}
use of groovy.text.Template in project grails-core by grails.
the class GroovyPagesServlet method renderPageWithEngine.
/**
* Attempts to render the page with the given arguments
*
*
* @param engine The GroovyPagesTemplateEngine to use
* @param request The HttpServletRequest
* @param response The HttpServletResponse
* @param scriptSource The template
*
* @throws IOException Thrown when an I/O exception occurs rendering the page
*/
protected void renderPageWithEngine(GroovyPagesTemplateEngine engine, HttpServletRequest request, HttpServletResponse response, GroovyPageScriptSource scriptSource) throws Exception {
request.setAttribute(GrailsLayoutDecoratorMapper.RENDERING_VIEW, Boolean.TRUE);
GSPResponseWriter out = createResponseWriter(response);
try {
Template template = engine.createTemplate(scriptSource);
if (template instanceof GroovyPageTemplate) {
((GroovyPageTemplate) template).setAllowSettingContentType(true);
}
template.make().writeTo(out);
} catch (Exception e) {
out.setError();
throw e;
} finally {
if (out != null)
out.close();
}
}
use of groovy.text.Template in project grails-core by grails.
the class GroovyPagesTemplateRenderer method render.
public void render(GrailsWebRequest webRequest, TemplateVariableBinding pageScope, Map<String, Object> attrs, Object body, Writer out) throws IOException {
Assert.state(groovyPagesTemplateEngine != null, "Property [groovyPagesTemplateEngine] must be set!");
String templateName = getStringValue(attrs, "template");
if (GrailsStringUtils.isBlank(templateName)) {
throw new GrailsTagException("Tag [render] is missing required attribute [template]");
}
String uri = webRequest.getAttributes().getTemplateUri(templateName, webRequest.getRequest());
String contextPath = getStringValue(attrs, "contextPath");
String pluginName = getStringValue(attrs, "plugin");
final Object controller = webRequest.getAttribute(GrailsApplicationAttributes.CONTROLLER, GrailsWebRequest.SCOPE_REQUEST);
Template t = findAndCacheTemplate(controller, pageScope, templateName, contextPath, pluginName, uri);
if (t == null) {
throw new GrailsTagException("Template not found for name [" + templateName + "] and path [" + uri + "]");
}
makeTemplate(webRequest, t, attrs, body, out);
}
use of groovy.text.Template in project spring-boot by spring-projects.
the class EndpointDocumentation method endpoints.
@Test
public void endpoints() throws Exception {
final File docs = new File("src/main/asciidoc");
final Map<String, Object> model = new LinkedHashMap<>();
final List<EndpointDoc> endpoints = new ArrayList<>();
model.put("endpoints", endpoints);
for (MvcEndpoint endpoint : getEndpoints()) {
final String endpointPath = (StringUtils.hasText(endpoint.getPath()) ? endpoint.getPath() : "/");
if (!SKIPPED.contains(endpointPath)) {
String output = endpointPath.substring(1);
output = output.length() > 0 ? output : "./";
this.mockMvc.perform(get(endpointPath).accept(ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON)).andExpect(status().isOk()).andDo(document(output)).andDo(new ResultHandler() {
@Override
public void handle(MvcResult mvcResult) throws Exception {
EndpointDoc endpoint = new EndpointDoc(docs, endpointPath);
endpoints.add(endpoint);
}
});
}
}
File file = new File(RESTDOCS_OUTPUT_DIR + "/endpoints.adoc");
file.getParentFile().mkdirs();
PrintWriter writer = new PrintWriter(file, "UTF-8");
try {
Template template = this.templates.createTemplate(new File("src/restdoc/resources/templates/endpoints.adoc.tpl"));
template.make(model).writeTo(writer);
} finally {
writer.close();
}
}
use of groovy.text.Template in project groovy by apache.
the class TemplateServlet method createAndStoreTemplate.
/**
* Compile the template and store it in the cache.
* @param key a unique key for the template, such as a file's absolutePath or a URL.
* @param inputStream an InputStream for the template's source.
* @param file a file to be used to determine if the cached template is stale. May be null.
* @return the created template.
* @throws Exception Any exception when creating the template.
*/
private Template createAndStoreTemplate(String key, InputStream inputStream, File file) throws Exception {
if (verbose) {
log("Creating new template from " + key + "...");
}
Reader reader = null;
try {
String fileEncoding = (fileEncodingParamVal != null) ? fileEncodingParamVal : System.getProperty(GROOVY_SOURCE_ENCODING);
reader = fileEncoding == null ? new InputStreamReader(inputStream) : new InputStreamReader(inputStream, fileEncoding);
Template template = engine.createTemplate(reader);
cache.put(key, new TemplateCacheEntry(file, template, verbose));
if (verbose) {
log("Created and added template to cache. [key=" + key + "] " + cache.get(key));
}
//
if (template == null) {
throw new ServletException("Template is null? Should not happen here!");
}
return template;
} finally {
if (reader != null) {
reader.close();
} else if (inputStream != null) {
inputStream.close();
}
}
}
Aggregations