本文整理汇总了Java中groovy.text.SimpleTemplateEngine类的典型用法代码示例。如果您正苦于以下问题:Java SimpleTemplateEngine类的具体用法?Java SimpleTemplateEngine怎么用?Java SimpleTemplateEngine使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SimpleTemplateEngine类属于groovy.text包,在下文中一共展示了SimpleTemplateEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initializeEclipseConfig
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
/**
* Initialize the Eclipse configuration
*
* @param eclipse Eclipse configuration to initialize
* @param project Current Gradle project
*/
@Defaults
public void initializeEclipseConfig(EclipseConfig eclipse, ProjectIdentifier project) {
try {
Map<String, Object> context = new HashMap<>();
context.put("project", project);
@NonNull URL resource = Validate
.notNull(Resources.getResource(EclipseConfigPlugin.class, "codetemplates.xml"));
String templateText = Resources.toString(resource, Charsets.UTF_8);
TemplateEngine engine = new SimpleTemplateEngine();
String templates = engine.createTemplate(templateText).make(context).toString();
eclipse.getUi().setCodeTemplates(templates);
}
catch (Exception e) {
throw new RuntimeException("Could not set code templates", e);
}
}
示例2: generate
import groovy.text.SimpleTemplateEngine; //导入依赖的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);
}
}
示例3: expand
import groovy.text.SimpleTemplateEngine; //导入依赖的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);
}
}
});
}
示例4: buildNotifyJson
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
private String buildNotifyJson( @Nonnull final AbstractBuild build,
@Nonnull final Map<String,?> env )
{
Map<String,?> binding = new HashMap<String, Object>(){{
put( "jenkins", notNull( Jenkins.getInstance(), "Jenkins instance" ));
put( "build", notNull( build, "Build instance" ));
put( "env", notNull( env, "Build environment" ));
}};
String json = null;
String template = "<%\n\n" + JSON_FUNCTION + "\n\n%>\n\n" +
notBlank( notifyTemplate, "Notify template" );
try
{
json = notBlank( new SimpleTemplateEngine( getClass().getClassLoader()).
createTemplate( template ).
make( binding ).toString(), "Payload JSON" ).trim();
Asserts.check(( json.startsWith( "{" ) && json.endsWith( "}" )) ||
( json.startsWith( "[" ) && json.endsWith( "]" )),
"Illegal JSON content: should start and end with {} or []" );
Asserts.notNull( new JsonSlurper().parseText( json ), "Parsed JSON" );
}
catch ( Exception e )
{
throwError(( json == null ?
String.format( "Failed to parse Groovy template:%s%s%s",
LINE, template, LINE ) :
String.format( "Failed to validate JSON payload (check with http://jsonlint.com/):%s%s%s",
LINE, json, LINE )), e );
}
return json;
}
示例5: generateStartScriptContentFromTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的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);
}
}
});
}
示例6: findTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
private Template findTemplate(final String templateName) throws SAXException, ParserConfigurationException, ClassNotFoundException, IOException {
TemplateEngine ste = templateName.endsWith(".gxml") ? new XmlTemplateEngine() : new SimpleTemplateEngine();
File sourceTemplate = new File(templatesPath, templateName);
Template template = cachedTemplates.get(templateName);
if (template == null) {
template = ste.createTemplate(new InputStreamReader(new BufferedInputStream(new FileInputStream(sourceTemplate)), config.getString(Keys.TEMPLATE_ENCODING)));
cachedTemplates.put(templateName, template);
}
return template;
}
示例7: getTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
protected Template getTemplate(SimpleTemplateEngine templateEngine, String templateString) {
Template bodyTemplate;
try {
bodyTemplate = templateEngine.createTemplate(templateString);
} catch (Exception e) {
throw new RuntimeException("Unable to compile Groovy template", e);
}
return bodyTemplate;
}
示例8: loadDefaultTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
protected Template loadDefaultTemplate(String templatePath, SimpleTemplateEngine templateEngine) {
String defaultTemplateContent = resources.getResourceAsString(templatePath);
if (defaultTemplateContent == null) {
throw new IllegalStateException("Unable to find default template for reset password link email");
}
//noinspection UnnecessaryLocalVariable
Template template = getTemplate(templateEngine, defaultTemplateContent);
return template;
}
示例9: changePasswordsAtLogonAndSendEmails
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
@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();
}
示例10: loadDefaultTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
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;
}
示例11: generateModuleCodeFromTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
public static String generateModuleCodeFromTemplate(String moduleName, String codeTemplate) {
try {
Map bindings = new HashMap();
bindings.put("moduleName", moduleName);
SimpleTemplateEngine simpleTemplateEngine = new SimpleTemplateEngine();
Template template = simpleTemplateEngine.createTemplate(codeTemplate);
Writable writable = template.make(bindings);
String finalScript = writable.toString();
return finalScript;
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
return null;
}
示例12: fillTemplate
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
public static String fillTemplate(String templateString, Map bindings) throws RuntimeException {
try {
SimpleTemplateEngine simpleTemplateEngine = new SimpleTemplateEngine();
Template template = simpleTemplateEngine.createTemplate(templateString);
Writable writable = template.make(bindings);
String finalScript = writable.toString();
return finalScript;
} catch (CompilationFailedException | ClassNotFoundException | IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
示例13: initialize
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
private void initialize() throws GrafikonException {
TemplateEngine engine = new SimpleTemplateEngine();
try {
templateGString = engine.createTemplate(this.getTemplate());
} catch (Exception e) {
throw new GrafikonException("Cannot create template: " + e.getMessage(), e, GrafikonException.Type.TEXT_TEMPLATE);
}
}
示例14: setupUsers
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
protected void setupUsers() {
try {
TemplateEngine engine = new SimpleTemplateEngine();
Template secTemplate = engine.createTemplate(ApplicationWizard.class.getResource("Security.groovy"));
Map<String, String> bindings = new HashMap<String, String>();
bindings.put("databaseName", connectionProvider.getDatabase().getDatabaseName());
bindings.put("userTableEntityName", userTable.getActualEntityName());
bindings.put("userIdProperty", userIdProperty);
bindings.put("userNameProperty", userNameProperty);
bindings.put("passwordProperty", userPasswordProperty);
bindings.put("userEmailProperty", userEmailProperty);
bindings.put("userTokenProperty", userTokenProperty);
bindings.put("groupTableEntityName", groupTable != null ? groupTable.getActualEntityName() : "");
bindings.put("groupIdProperty", StringUtils.defaultString(groupIdProperty));
bindings.put("groupNameProperty", StringUtils.defaultString(groupNameProperty));
bindings.put("userGroupTableEntityName", userGroupTable != null ? userGroupTable.getActualEntityName() : "");
bindings.put("groupLinkProperty", StringUtils.defaultString(groupLinkProperty));
bindings.put("userLinkProperty", StringUtils.defaultString(userLinkProperty));
bindings.put("adminGroupName", StringUtils.defaultString(adminGroupName));
bindings.put("encryptionAlgorithm", encryptionAlgorithm);
File gcp = (File) context.getServletContext().getAttribute(BaseModule.GROOVY_CLASS_PATH);
FileWriter fw = new FileWriter(new File(gcp, "Security.groovy"));
secTemplate.make(bindings).writeTo(fw);
IOUtils.closeQuietly(fw);
} catch (Exception e) {
logger.warn("Couldn't configure users", e);
SessionMessages.addWarningMessage(ElementsThreadLocals.getText("couldnt.set.up.user.management._", e));
}
}
示例15: testDifferentTemplateEngine
import groovy.text.SimpleTemplateEngine; //导入依赖的package包/类
@Test
public void testDifferentTemplateEngine() throws Exception {
String templateValue = "<% import static org.apache.commons.lang3.text.WordUtils.capitalize %>${capitalize(foo)} ${capitalize(baz)} ${capitalize(moo)}";
PlainTextTestResource resource = new PlainTextTestResource(templateValue);
Map<String, String> variables = new HashMap<>();
variables.put("foo", "a");
variables.put("baz", "b");
variables.put("moo", "c");
GroovyTemplateTestResource testResource = new GroovyTemplateTestResource(new SimpleTemplateEngine(),
resource, variables);
assertEquals("A B C", testResource.getValue());
}