Search in sources :

Example 21 with Template

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;
}
Also used : FastStringWriter(org.grails.buffer.FastStringWriter) GrailsDomainClass(grails.core.GrailsDomainClass) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Template(groovy.text.Template)

Example 22 with Template

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();
    }
}
Also used : GroovyPageTemplate(org.grails.gsp.GroovyPageTemplate) ServletException(javax.servlet.ServletException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) GroovyPageTemplate(org.grails.gsp.GroovyPageTemplate) Template(groovy.text.Template)

Example 23 with Template

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);
}
Also used : GrailsTagException(org.grails.taglib.GrailsTagException) GroovyObject(groovy.lang.GroovyObject) Template(groovy.text.Template)

Example 24 with Template

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();
    }
}
Also used : ArrayList(java.util.ArrayList) ResultHandler(org.springframework.test.web.servlet.ResultHandler) MvcResult(org.springframework.test.web.servlet.MvcResult) LinkedHashMap(java.util.LinkedHashMap) Template(groovy.text.Template) MvcEndpoint(org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 25 with Template

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();
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Template(groovy.text.Template)

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