groovy.text.Template Java Examples
The following examples show how to use
groovy.text.Template.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserManagementServiceBean.java From cuba with Apache License 2.0 | 6 votes |
protected void sendResetPasswordEmail(User user, String password, Template subjectTemplate, Template bodyTemplate) { String emailBody; String emailSubject; try (Transaction tx = persistence.createTransaction()) { Map<String, Object> binding = new HashMap<>(); binding.put("user", user); binding.put("password", password); binding.put("persistence", persistence); emailBody = bodyTemplate.make(binding).writeTo(new StringWriter(0)).toString(); emailSubject = subjectTemplate.make(binding).writeTo(new StringWriter(0)).toString(); tx.commit(); } catch (IOException e) { throw new RuntimeException("Unable to write Groovy template content", e); } EmailInfo emailInfo = EmailInfoBuilder.create() .setAddresses(user.getEmail()) .setCaption(emailSubject) .setBody(emailBody) .build(); emailerAPI.sendEmailAsync(emailInfo); }
Example #2
Source File: GroovyDocTemplateEngine.java From groovy with Apache License 2.0 | 6 votes |
public GroovyDocTemplateEngine(GroovyDocTool tool, ResourceManager resourceManager, String[] docTemplates, String[] packageTemplates, String[] classTemplates, Properties properties) { this.resourceManager = resourceManager; this.properties = properties; this.docTemplatePaths = Arrays.asList(docTemplates); this.packageTemplatePaths = Arrays.asList(packageTemplates); this.classTemplatePaths = Arrays.asList(classTemplates); this.docTemplates = new LinkedHashMap<String, Template>(); this.packageTemplates = new LinkedHashMap<String, Template>(); this.classTemplates = new LinkedHashMap<String, Template>(); engine = new GStringTemplateEngine(); }
Example #3
Source File: GroovyDocTemplateEngine.java From groovy with Apache License 2.0 | 6 votes |
String applyClassTemplates(GroovyClassDoc classDoc) { String templatePath = classTemplatePaths.get(0); // todo (iterate) 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 LinkedHashMap<String, Object>(); binding.put("classDoc", classDoc); binding.put("props", properties); templateWithBindingApplied = t.make(binding).writeTo(reasonableSizeWriter()).toString(); } catch (Exception e) { System.out.println("Error processing class template for: " + classDoc.getFullPathName()); e.printStackTrace(); } return templateWithBindingApplied; }
Example #4
Source File: FilterChain.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
public void expand(final Map<String, ?> properties) { transformers.add(new Transformer<Reader, Reader>() { public Reader transform(Reader original) { try { Template template; try { SimpleTemplateEngine engine = new SimpleTemplateEngine(); template = engine.createTemplate(original); } finally { original.close(); } StringWriter writer = new StringWriter(); template.make(properties).writeTo(writer); return new StringReader(writer.toString()); } catch (IOException e) { throw new UncheckedIOException(e); } } }); }
Example #5
Source File: FilterChain.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
public void expand(final Map<String, ?> properties) { transformers.add(new Transformer<Reader, Reader>() { public Reader transform(Reader original) { try { Template template; try { SimpleTemplateEngine engine = new SimpleTemplateEngine(); template = engine.createTemplate(original); } finally { original.close(); } StringWriter writer = new StringWriter(); template.make(properties).writeTo(writer); return new StringReader(writer.toString()); } catch (IOException e) { throw new UncheckedIOException(e); } } }); }
Example #6
Source File: FilterChain.java From pushfish-android with BSD 2-Clause "Simplified" License | 6 votes |
public void expand(final Map<String, ?> properties) { transformers.add(new Transformer<Reader, Reader>() { public Reader transform(Reader original) { try { Template template; try { SimpleTemplateEngine engine = new SimpleTemplateEngine(); template = engine.createTemplate(original); } finally { original.close(); } StringWriter writer = new StringWriter(); template.make(properties).writeTo(writer); return new StringReader(writer.toString()); } catch (IOException e) { throw new UncheckedIOException(e); } } }); }
Example #7
Source File: FilterChain.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 6 votes |
public void expand(final Map<String, ?> properties) { transformers.add(new Transformer<Reader, Reader>() { public Reader transform(Reader original) { try { Template template; try { SimpleTemplateEngine engine = new SimpleTemplateEngine(); template = engine.createTemplate(original); } finally { original.close(); } StringWriter writer = new StringWriter(); template.make(properties).writeTo(writer); return new StringReader(writer.toString()); } catch (IOException e) { throw new UncheckedIOException(e); } } }); }
Example #8
Source File: TemplateServlet.java From groovy with Apache License 2.0 | 6 votes |
/** * 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; }
Example #9
Source File: TemplateServlet.java From groovy with Apache License 2.0 | 6 votes |
/** * 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 file. If the source file didn't change in * length and its last modified stamp hasn't changed compared to a precompiled * template object, this template is used. Otherwise, there is no or an * invalid template object 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 file The file containing the template source. * @throws ServletException If the request specified an invalid template source file */ protected Template getTemplate(File file) throws ServletException { String key = file.getAbsolutePath(); Template template = findCachedTemplate(key, file); // // Template not cached or the source file changed - compile new template! // if (template == null) { try { template = createAndStoreTemplate(key, new FileInputStream(file), file); } catch (Exception e) { throw new ServletException("Creation of template failed: " + e, e); } } return template; }
Example #10
Source File: GroovyDocTemplateEngine.java From groovy with Apache License 2.0 | 6 votes |
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 LinkedHashMap<String, Object>(); binding.put("packageDoc", packageDoc); binding.put("props", properties); templateWithBindingApplied = t.make(binding).toString(); } catch (Exception e) { System.out.println("Error processing package template for: " + packageDoc.name()); e.printStackTrace(); } return templateWithBindingApplied; }
Example #11
Source File: GroovyDocTemplateEngine.java From groovy with Apache License 2.0 | 6 votes |
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 LinkedHashMap<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; }
Example #12
Source File: TemplateServlet.java From groovy with Apache License 2.0 | 6 votes |
public TemplateCacheEntry(File file, Template template, boolean timestamp) { if (template == null) { throw new NullPointerException("template"); } if (timestamp) { this.date = new Date(System.currentTimeMillis()); } else { this.date = null; } this.hit = 0; if (file != null) { this.lastModified = file.lastModified(); this.length = file.length(); } this.template = template; }
Example #13
Source File: UserManagementServiceBean.java From cuba with Apache License 2.0 | 5 votes |
protected Template loadDefaultTemplate(String templatePath, SimpleTemplateEngine templateEngine) { String defaultTemplateContent = resources.getResourceAsString(templatePath); if (defaultTemplateContent == null) { throw new IllegalStateException("Not found default email template for reset passwords operation"); } //noinspection UnnecessaryLocalVariable Template template = getTemplate(templateEngine, defaultTemplateContent); return template; }
Example #14
Source File: GroovyMarkupTemplateEngine.java From jbake with MIT License | 5 votes |
@Override public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException { try { Template template = templateEngine.createTemplateByPath(templateName); Map<String, Object> wrappedModel = wrap(model); Writable writable = template.make(wrappedModel); writable.writeTo(writer); } catch (Exception e) { throw new RenderingException(e); } }
Example #15
Source File: BaseTemplate.java From groovy with Apache License 2.0 | 5 votes |
public BaseTemplate(final MarkupTemplateEngine templateEngine, final Map model, final Map<String,String> modelTypes, final TemplateConfiguration configuration) { this.model = model==null?EMPTY_MODEL:model; this.engine = templateEngine; this.configuration = configuration; this.modelTypes = modelTypes; this.cachedFragments = new LinkedHashMap<String, Template>(); }
Example #16
Source File: TemplateServlet.java From groovy with Apache License 2.0 | 5 votes |
/** * 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; }
Example #17
Source File: TemplateServlet.java From groovy with Apache License 2.0 | 5 votes |
/** * 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)); } // // Last sanity check. // 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(); } } }
Example #18
Source File: GroovyTemplateEngine.java From pippo with Apache License 2.0 | 5 votes |
@Override public void renderString(String templateContent, Map<String, Object> model, Writer writer) { try { Template groovyTemplate = engine.createTemplate(templateContent); PippoGroovyTemplate gt = (PippoGroovyTemplate) groovyTemplate.make(model); gt.setup(getLanguages(), getMessages(), getRouter()); gt.writeTo(writer); } catch (Exception e) { log.error("Error processing Groovy template {} ", templateContent, e); throw new PippoRuntimeException(e); } }
Example #19
Source File: GroovyTemplateEngine.java From jbake with MIT License | 5 votes |
@Override public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException { try { Template template = findTemplate(templateName); Writable writable = template.make(wrap(model)); writable.writeTo(writer); } catch (Exception e) { throw new RenderingException(e); } }
Example #20
Source File: GroovyTemplateEngine.java From jbake with MIT License | 5 votes |
private Template findTemplate(final String templateName) throws SAXException, ParserConfigurationException, ClassNotFoundException, IOException { TemplateEngine ste = templateName.endsWith(".gxml") ? new XmlTemplateEngine() : new SimpleTemplateEngine(); File sourceTemplate = new File(config.getTemplateFolder(), templateName); Template template = cachedTemplates.get(templateName); if (template == null) { template = ste.createTemplate(new InputStreamReader(new BufferedInputStream(new FileInputStream(sourceTemplate)), config.getTemplateEncoding())); cachedTemplates.put(templateName, template); } return template; }
Example #21
Source File: GroovyMarkupView.java From spring-analysis-note with MIT License | 5 votes |
@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())); }
Example #22
Source File: UserManagementServiceBean.java From cuba with Apache License 2.0 | 5 votes |
@Override public Integer changePasswordsAtLogonAndSendEmails(List<UUID> userIds) { checkNotNullArgument(userIds, "Null users list"); checkUpdatePermission(User.class); if (userIds.isEmpty()) return 0; Map<User, String> modifiedUsers = updateUserPasswords(userIds, true); // email templates String resetPasswordBodyTemplate = serverConfig.getResetPasswordEmailBodyTemplate(); String resetPasswordSubjectTemplate = serverConfig.getResetPasswordEmailSubjectTemplate(); SimpleTemplateEngine templateEngine = new SimpleTemplateEngine(scripting.getClassLoader()); Map<String, Template> localizedBodyTemplates = new HashMap<>(); Map<String, Template> localizedSubjectTemplates = new HashMap<>(); // load default Template bodyDefaultTemplate = loadDefaultTemplate(resetPasswordBodyTemplate, templateEngine); Template subjectDefaultTemplate = loadDefaultTemplate(resetPasswordSubjectTemplate, templateEngine); for (Map.Entry<User, String> userPasswordEntry : modifiedUsers.entrySet()) { User user = userPasswordEntry.getKey(); if (StringUtils.isNotEmpty(user.getEmail())) { EmailTemplate template = getResetPasswordTemplate(user, templateEngine, resetPasswordSubjectTemplate, resetPasswordBodyTemplate, subjectDefaultTemplate, bodyDefaultTemplate, localizedSubjectTemplates, localizedBodyTemplates); String password = userPasswordEntry.getValue(); sendResetPasswordEmail(user, password, template.getSubjectTemplate(), template.getBodyTemplate()); } } return modifiedUsers.size(); }
Example #23
Source File: GroovyMarkupView.java From spring-analysis-note with MIT License | 5 votes |
/** * Return a template compiled by the configured Groovy Markup template engine * for the given view URL. */ protected Template getTemplate(String viewUrl) throws Exception { Assert.state(this.engine != null, "No MarkupTemplateEngine set"); try { return this.engine.createTemplateByPath(viewUrl); } catch (ClassNotFoundException ex) { Throwable cause = (ex.getCause() != null ? ex.getCause() : ex); throw new NestedServletException( "Could not find class while rendering Groovy Markup view with name '" + getUrl() + "': " + ex.getMessage() + "'", cause); } }
Example #24
Source File: GroovyMarkupView.java From java-technology-stack with MIT License | 5 votes |
@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())); }
Example #25
Source File: GroovyMarkupView.java From java-technology-stack with MIT License | 5 votes |
/** * Return a template compiled by the configured Groovy Markup template engine * for the given view URL. */ protected Template getTemplate(String viewUrl) throws Exception { Assert.state(this.engine != null, "No MarkupTemplateEngine set"); try { return this.engine.createTemplateByPath(viewUrl); } catch (ClassNotFoundException ex) { Throwable cause = (ex.getCause() != null ? ex.getCause() : ex); throw new NestedServletException( "Could not find class while rendering Groovy Markup view with name '" + getUrl() + "': " + ex.getMessage() + "'", cause); } }
Example #26
Source File: GroovyMarkupView.java From lams with GNU General Public License v2.0 | 5 votes |
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Template template = getTemplate(getUrl()); template.make(model).writeTo(new BufferedWriter(response.getWriter())); }
Example #27
Source File: GroovyMarkupView.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Return a template compiled by the configured Groovy Markup template engine * for the given view URL. */ protected Template getTemplate(String viewUrl) throws Exception { try { return this.engine.createTemplateByPath(viewUrl); } catch (ClassNotFoundException ex) { Throwable cause = (ex.getCause() != null ? ex.getCause() : ex); throw new NestedServletException( "Could not find class while rendering Groovy Markup view with name '" + getUrl() + "': " + ex.getMessage() + "'", cause); } }
Example #28
Source File: ScriptTemplateManagerImpl.java From jira-groovioli with BSD 2-Clause "Simplified" License | 5 votes |
@Override public String renderTemplate(String groovyTemplate, Map<String, Object> parameters) throws TemplateException { try (StringWriter stringWriter = new StringWriter()) { Template template = templateCache.get(groovyTemplate); template.make(fromMap(parameters)).writeTo(stringWriter); return stringWriter.toString(); } catch (Exception ex) { throw new TemplateException("Error executing groovy template", ex); } }
Example #29
Source File: GroovyMarkupView.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Template template = getTemplate(getUrl()); template.make(model).writeTo(new BufferedWriter(response.getWriter())); }
Example #30
Source File: GroovyMarkupView.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Return a template compiled by the configured Groovy Markup template engine * for the given view URL. */ protected Template getTemplate(String viewUrl) throws Exception { try { return this.engine.createTemplateByPath(viewUrl); } catch (ClassNotFoundException ex) { Throwable cause = (ex.getCause() != null ? ex.getCause() : ex); throw new NestedServletException("Could not find class while rendering Groovy Markup view with name '" + getUrl() + "': " + ex.getMessage() + "'", cause); } }