本文整理汇总了Java中groovy.text.Template类的典型用法代码示例。如果您正苦于以下问题:Java Template类的具体用法?Java Template怎么用?Java Template使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Template类属于groovy.text包,在下文中一共展示了Template类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generate
import groovy.text.Template; //导入依赖的package包/类
@Override
public void generate() {
try {
target.getParentFile().mkdirs();
SimpleTemplateEngine templateEngine = new SimpleTemplateEngine();
String templateText = Resources.asCharSource(templateURL, CharsetToolkit.getDefaultSystemCharset()).read();
Template template = templateEngine.createTemplate(templateText);
Writer writer = Files.asCharSink(target, Charsets.UTF_8).openStream();
try {
template.make(bindings).writeTo(writer);
} finally {
writer.close();
}
} catch (Exception ex) {
throw new GradleException("Could not generate file " + target + ".", ex);
}
}
示例2: expand
import groovy.text.Template; //导入依赖的package包/类
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);
}
}
});
}
示例3: sendResetPasswordEmail
import groovy.text.Template; //导入依赖的package包/类
protected void sendResetPasswordEmail(User user, String link, Template subjectTemplate, Template bodyTemplate) {
Transaction tx = persistence.getTransaction();
String emailBody;
String emailSubject;
try {
Map<String, Object> binding = new HashMap<>();
binding.put("user", user);
binding.put("link", link);
binding.put("lifetimeHours", forgotPasswordConfig.getResetPasswordTokenLifetimeMinutes() / 60);
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);
} finally {
tx.end();
}
EmailInfo emailInfo = new EmailInfo(user.getEmail(), emailSubject, emailBody);
emailerAPI.sendEmailAsync(emailInfo);
}
示例4: getTemplate
import groovy.text.Template; //导入依赖的package包/类
private static Template getTemplate(TemplateEngine engine, String name)
throws CompilationFailedException, ClassNotFoundException, IOException {
File file = new File("templates", name);
if (file.exists()) {
return engine.createTemplate(file);
}
ClassLoader classLoader = GroovyTemplate.class.getClassLoader();
URL resource = classLoader.getResource("templates/" + name);
if (resource != null) {
return engine.createTemplate(resource);
}
return engine.createTemplate(name);
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:17,代码来源:GroovyTemplate.java
示例5: sendResetPasswordEmail
import groovy.text.Template; //导入依赖的package包/类
protected void sendResetPasswordEmail(User user, String password, Template subjectTemplate, Template bodyTemplate) {
Transaction tx = persistence.getTransaction();
String emailBody;
String emailSubject;
try {
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);
} finally {
tx.end();
}
EmailInfo emailInfo = new EmailInfo(user.getEmail(), emailSubject, emailBody);
emailerAPI.sendEmailAsync(emailInfo);
}
示例6: GroovyDocTemplateEngine
import groovy.text.Template; //导入依赖的package包/类
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();
}
示例7: applyClassTemplates
import groovy.text.Template; //导入依赖的package包/类
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).toString();
} catch (Exception e) {
System.out.println("Error processing class template for: " + classDoc.getFullPathName());
e.printStackTrace();
}
return templateWithBindingApplied;
}
示例8: applyPackageTemplate
import groovy.text.Template; //导入依赖的package包/类
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;
}
示例9: applyRootDocTemplate
import groovy.text.Template; //导入依赖的package包/类
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;
}
示例10: TemplateCacheEntry
import groovy.text.Template; //导入依赖的package包/类
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;
}
示例11: getTemplate
import groovy.text.Template; //导入依赖的package包/类
/**
* 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;
}
示例12: loadView
import groovy.text.Template; //导入依赖的package包/类
@Override
protected View loadView(final String viewName, final Locale locale) throws Exception {
Map<String, String> model = viewModels.get(viewName);
Template template;
if (model != null) {
template = factory.getEngine(locale).createTypeCheckedModelTemplateByPath(createTemplatePath(viewName, locale), model);
} else {
template = factory.getEngine(locale).createTemplateByPath(createTemplatePath(viewName, locale));
}
return new GroovyTemplateEngineView(template);
}
示例13: createChildCrudPage
import groovy.text.Template; //导入依赖的package包/类
protected void createChildCrudPage(File dir, Template template, String parentName,
Collection<Reference> references, Reference ref, ArrayList<ChildPage> pages) throws Exception {
Column fromColumn = ref.getActualFromColumn();
Table fromTable = fromColumn.getTable();
String entityName = fromTable.getActualEntityName();
String parentProperty = ref.getActualToColumn().getActualPropertyName();
String linkToParentProperty = fromColumn.getActualPropertyName();
String childQuery = "from " + entityName + " where " + linkToParentProperty + " = %{#" + parentName + "."
+ parentProperty + "}" + " order by id desc";
String childDirName = entityName;
boolean multipleRoles = isMultipleRoles(fromTable, ref, references);
if (multipleRoles) {
childDirName += "-as-" + linkToParentProperty;
}
File childDir = new File(new File(dir, PageInstance.DETAIL), childDirName);
String childTitle = Util.guessToWords(childDirName);
Map<String, String> bindings = new HashMap<String, String>();
bindings.put("parentName", parentName);
bindings.put("parentProperty", parentProperty);
bindings.put("linkToParentProperty", linkToParentProperty);
createCrudPage(childDir, fromTable, childQuery, pages, template, bindings, childTitle);
}
示例14: service
import groovy.text.Template; //导入依赖的package包/类
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String groovyFile = (String) request.getAttribute("javax.servlet.include.path_info");
if (null == groovyFile) {
groovyFile = request.getPathInfo();
}
if (groovyFile.startsWith("/")) {
groovyFile = groovyFile.substring(1);
}
String templateSource = getTemplateSource(groovyFile);
try {
Template groovyTemplate = templateEngine.createTemplate(templateSource);
groovyTemplate.make(Application.get().getParameters()).writeTo(response.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: generateStartScriptContentFromTemplate
import groovy.text.Template; //导入依赖的package包/类
private String generateStartScriptContentFromTemplate(final Map<String, String> binding) {
return IoUtils.get(getTemplate().asReader(), new Transformer<String, Reader>() {
@Override
public String transform(Reader reader) {
try {
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Template template = engine.createTemplate(reader);
String output = template.make(binding).toString();
return TextUtil.convertLineSeparators(output, lineSeparator);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
});
}