Search in sources :

Example 26 with Template

use of groovy.text.Template in project groovy by apache.

the class TemplateServlet method findCachedTemplate.

/**
     * Find a cached template for a given key. If a <code>File</code> is passed then
     * any cached object is validated against the File to determine if it is out of
     * date
     * @param key a unique key for the template, such as a file's absolutePath or a URL.
     * @param file a file to be used to determine if the cached template is stale. May be null.
     * @return The cached template, or null if there was no cached entry, or the entry was stale.
     */
private Template findCachedTemplate(String key, File file) {
    Template template = null;
    /*
         * Test cache for a valid template bound to the key.
         */
    if (verbose) {
        log("Looking for cached template by key \"" + key + "\"");
    }
    TemplateCacheEntry entry = (TemplateCacheEntry) cache.get(key);
    if (entry != null) {
        if (entry.validate(file)) {
            if (verbose) {
                log("Cache hit! " + entry);
            }
            template = entry.template;
        } else {
            if (verbose) {
                log("Cached template " + key + " needs recompilation! " + entry);
            }
        }
    } else {
        if (verbose) {
            log("Cache miss for " + key);
        }
    }
    return template;
}
Also used : Template(groovy.text.Template)

Example 27 with Template

use of groovy.text.Template in project groovy by apache.

the class TemplateServlet method getTemplate.

/**
     * Gets the template created by the underlying engine parsing the request.
     *
     * <p>
     * This method looks up a simple (weak) hash map for an existing template
     * object that matches the source URL. If there is no cache entry, a new one is
     * created by the underlying template engine. This new instance is put
     * to the cache for consecutive calls.
     *
     * @return The template that will produce the response text.
     * @param url The URL containing the template source..
     * @throws ServletException If the request specified an invalid template source URL
     */
protected Template getTemplate(URL url) throws ServletException {
    String key = url.toString();
    Template template = findCachedTemplate(key, null);
    // Template not cached or the source file changed - compile new template!
    if (template == null) {
        try {
            template = createAndStoreTemplate(key, url.openConnection().getInputStream(), null);
        } catch (Exception e) {
            throw new ServletException("Creation of template failed: " + e, e);
        }
    }
    return template;
}
Also used : ServletException(javax.servlet.ServletException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Template(groovy.text.Template)

Example 28 with Template

use of groovy.text.Template in project groovy by apache.

the class BaseTemplate method fragment.

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

Example 29 with Template

use of groovy.text.Template in project groovy by apache.

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) {
        System.out.println("Error processing root doc template");
        e.printStackTrace();
    }
    return templateWithBindingApplied;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Template(groovy.text.Template)

Example 30 with Template

use of groovy.text.Template in project hale by halestudio.

the class Request method init.

/**
 * Initialize the cache.
 */
private void init() {
    File cacheDir = null;
    try {
        // this will throw up in non-OSGi environments
        cacheDir = PlatformUtil.getInstanceLocation();
    } catch (Throwable t) {
        cacheEnabled = false;
    }
    if (cacheEnabled) {
        try {
            if (cacheDir == null) {
                cacheDir = new File(System.getProperty("java.io.tmpdir"));
            }
            // create the configuration from the template
            SimpleTemplateEngine engine = new SimpleTemplateEngine(Request.class.getClassLoader());
            Template template = engine.createTemplate(Request.class.getResource("ehcache.xml"));
            Map<String, Object> binding = new HashMap<>();
            // replace the cache directory
            binding.put("cache_dir", cacheDir.getAbsolutePath());
            ByteArrayOutputStream data = new ByteArrayOutputStream();
            try (Writer writer = new OutputStreamWriter(data, "UTF-8")) {
                template.make(binding).writeTo(writer);
            }
            // initialize the cache manager
            if (HaleCacheManager.create(new ByteArrayInputStream(data.toByteArray())).getCache(CACHE_NAME) != null) {
                return;
            }
            // create a Cache instance - providing cachePath has no effect
            Cache cache = new Cache(CACHE_NAME, 300, MemoryStoreEvictionPolicy.LRU, true, null, true, 0, 0, true, 0, null);
            // add it to CacheManger
            HaleCacheManager.getInstance().addCache(cache);
        } catch (Exception e) {
            log.error("Cache initialization failed", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Template(groovy.text.Template) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) SimpleTemplateEngine(groovy.text.SimpleTemplateEngine) Cache(net.sf.ehcache.Cache)

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