本文整理匯總了Java中org.apache.velocity.app.Velocity.getTemplate方法的典型用法代碼示例。如果您正苦於以下問題:Java Velocity.getTemplate方法的具體用法?Java Velocity.getTemplate怎麽用?Java Velocity.getTemplate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.velocity.app.Velocity
的用法示例。
在下文中一共展示了Velocity.getTemplate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generate
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* 根據模板生成文件
* @param inputVmFilePath 模板路徑
* @param outputFilePath 輸出文件路徑
* @param context
* @throws Exception
*/
public static void generate(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
try {
Properties properties = new Properties();
properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath));
Velocity.init(properties);
//VelocityEngine engine = new VelocityEngine();
Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8");
File outputFile = new File(outputFilePath);
FileWriterWithEncoding writer = new FileWriterWithEncoding(outputFile, "utf-8");
template.merge(context, writer);
writer.close();
} catch (Exception ex) {
throw ex;
}
}
示例2: createTable
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Override
public String createTable(Grammar grammar) {
Velocity.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
Velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("grammar", grammar);
context.put("nonterminalSymbols", grammar.getLhsSymbols());
Template template = Velocity.getTemplate("/grammartools/PredictiveParsingTable.velo");
StringWriter stringWriter = new StringWriter();
template.merge(context, stringWriter);
return stringWriter.toString().replaceAll("\n", "");
}
示例3: outputCode
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public void outputCode(String codePath, String templatePath) throws IOException {
VelocityContext context = new VelocityContext();
context.put("cModule", cModule);
Template template = null;
try {
template = Velocity.getTemplate(templatePath, TEMPLATE_ENCODING);
} catch (ResourceNotFoundException e) {
throw e;
}
File file = new File(codePath);
logger.info("Generating {} ({})", file.getName(), templatePath);
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), OUTPUT_ENCODING)));
template.merge(context, pw);
pw.close();
}
示例4: getTemplate
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* Gets the template by viewname, the viewname is relative path
*
* @param viewname
* the relative view path name
* @param allowEmpty
* if not presented, allow using a empty
* @return Template
* @throws IOException
*/
private Template getTemplate(File f) throws IOException {
String fullname = f.getCanonicalPath();
T t = cache.get(fullname);
if (t == null || t.last != f.lastModified()) {
/**
* get the template from the top
*/
Template t1 = Velocity.getTemplate(fullname, "UTF-8");
t = T.create(t1, f.lastModified());
cache.put(fullname, t);
}
return t == null ? null : t.template;
}
示例5: parseTemplateAndRunExpect
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
protected void parseTemplateAndRunExpect(String expectTemplateName, Map<String, String> contextVars) throws IOException, InterruptedException {
VelocityContext context = new VelocityContext();
for (Map.Entry<String, String> ent : contextVars.entrySet()) {
context.put(ent.getKey(), ent.getValue());
}
Template template = Velocity.getTemplate(expectTemplateName);
String inputPath = EXPECT_DIR + File.separator + expectTemplateName;
String outputPath = inputPath.substring(0, inputPath.length() - 3);
Writer output = new FileWriter(outputPath);
template.merge(context, output);
output.close();
expect(ZIPFILE_EXTRACTED, outputPath);
}
示例6: createTemplatePage
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
private void createTemplatePage(Content content, String templateFileName, Path outFilePath) throws IOException {
// template name
content.put("template", Config.getTemplate());
Properties p = new Properties();
p.setProperty("input.encoding", "UTF-8");
p.setProperty("output.encoding", "UTF-8");
p.setProperty("file.resource.loader.path", Config.getTemplateBaseDir());
Velocity.init(p);
VelocityContext context = content.getVelocityContext();
org.apache.velocity.Template template = Velocity.getTemplate(Config.getTemplateFile(lang, templateFileName).toString(), "UTF-8");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilePath.toFile()), "UTF-8"));
template.merge(context, bw);
bw.close();
}
示例7: compileUristJs
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* Load the urist.js template and compile it with project information.
*/
public static void compileUristJs() {
Log.info("TemplateRenderer", "Writing urist.js");
VelocityContext context = new VelocityContext();
context.put("conf", Uristmaps.conf);
context.put("version",Uristmaps.VERSION);
Template uristJs = Velocity.getTemplate("templates/js/urist.js.vm");
File targetFile = OutputFiles.getUristJs();
targetFile.getParentFile().mkdirs();
try (FileWriter writer = new FileWriter(targetFile)) {
uristJs.merge(context, writer);
} catch (IOException e) {
Log.warn("TemplateRenderer", "Could not write js file: " + targetFile);
if (Log.DEBUG) Log.debug("TemplateRenderer", "Exception", e);
}
}
示例8: createReportFromValidationResult
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
private static void createReportFromValidationResult(ValidationResult result, Path outputPath) {
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
Velocity.init();
try {
Template template = Velocity.getTemplate("reporting/ValidationResult.vm");
VelocityContext context = new VelocityContext();
context.put("validationResult", result);
context.put("resourcesWithWarnings", getResourcesWithWarnings(result));
StringWriter sw = new StringWriter();
template.merge(context, sw);
try (FileWriter fw = new FileWriter(outputPath.toFile())) {
fw.write(sw.toString());
fw.flush();
} catch (IOException ioe) {
LOGGER.error("Creation of HTML Report file {} failed.", outputPath.toString(), ioe);
}
} catch (VelocityException e) {
LOGGER.error("Creation of HTML report failed due to a Velocity Exception", e);
}
}
示例9: parseObj
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
protected void parseObj(
final File base,
final boolean append,
final String pkg,
final String name,
final String out,
final Map<String, Object> objs)
throws MojoExecutionException {
final VelocityContext ctx = newContext();
ctx.put("package", pkg);
if (objs != null) {
for (Map.Entry<String, Object> obj : objs.entrySet()) {
if (StringUtils.isNotBlank(obj.getKey()) && obj.getValue() != null) {
ctx.put(obj.getKey(), obj.getValue());
}
}
}
final Template template = Velocity.getTemplate(name + ".vm");
writeFile(out, base, ctx, template, append);
}
示例10: Test3
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public Test3() throws Exception {
//init
Velocity.init("Velocity/GS_Velocity_1/src/main/java/velocity.properties");
// get Template
Template template = Velocity.getTemplate("Test3.vm");
// getContext
Context context = new VelocityContext();
String name = "Vova";
int age = 21;
boolean flag = true;
context.put("name", name);
context.put("age", age);
context.put("flag", flag);
context.put("today", new Date());
context.put("product", new Product("Book", 12.3));
// get Writer
Writer writer = new StringWriter();
// merge
template.merge(context, writer);
System.out.println(writer.toString());
}
示例11: Test1
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public Test1() throws Exception {
//init
Velocity.init("Velocity/GS_Velocity_1/src/main/java/velocity.properties");
// get Template
Template template = Velocity.getTemplate("Test1.vm");
// getContext
Context context = new VelocityContext();
// get Writer
Writer writer = new StringWriter();
// merge
template.merge(context, writer);
System.out.println(writer.toString());
}
示例12: sendUpdatesEmail
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
private static void sendUpdatesEmail() {
Category news = new Category("News");
news.getContent().add(new Content("News number 1", "Text blah-blah-blah", new Date()));
news.getContent().add(new Content("News number 2", "Text2 blah-blah-blah", new Date()));
news.getContent().add(new Content("News number 3", "Text3 blah-blah-blah", new Date()));
Category events = new Category("Events");
events.getContent().add(new Content("Event number 1", "Text blah-blah-blah", new Date()));
events.getContent().add(new Content("Event number 2", "Text2 blah-blah-blah", new Date()));
events.getContent().add(new Content("Event number 3", "Text3 blah-blah-blah", new Date()));
Template template = Velocity.getTemplate("updates.vm");
Context context = new VelocityContext();
context.put("currentDate", new Date());
context.put("categories", Arrays.asList(news, events));
Writer writer = new StringWriter();
template.merge(context, writer);
EmailUtil.send("[email protected]", "News updates", writer.toString(), EmailUtil.EmailType.HTML);
}
示例13: doGenerate
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Override
public void doGenerate(final Map<String, Object> mapping, final Path output, final Path templatePath) {
try {
Properties p = new Properties();
p.setProperty("input.encoding", "UTF-8");
p.setProperty("file.resource.loader.path", templatePath.getParent().toAbsolutePath().toString());
Velocity.init(p);
final VelocityContext ctx = new VelocityContext();
final BiConsumer<String, Object> _function = (String k, Object v) -> {
ctx.put(k, v);
};
mapping.forEach(_function);
Template template = Velocity.getTemplate(templatePath.getFileName().toString());
File _file = output.toFile();
FileWriter writer = new FileWriter(_file);
template.merge(ctx, writer);
writer.flush();
writer.close();
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
示例14: generateApiImplementation
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Override
public String generateApiImplementation(List<EndpointInfo> endpoints, String className, String packageName,
Map<Long, TypedObject> typeMap)
{
Template template = Velocity.getTemplate("templates/class.vm");
StringWriter stringWriter = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("packageName", packageName);
context.put("clientFactoryClassName", CLASS_NAME_REST_TEMPLATE_FACTORY);
context.put("superInterfaces", new String[]{className.substring(0, className.length() - 4)});
context.put("className", className);
context.put("endpoints", endpoints);
context.put("writer", writer);
context.put("types", typeMap);
template.merge(context, stringWriter);
return stringWriter.toString();
}
示例15: generateClassContents
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
private String generateClassContents(TypedObject typedObject, String targetPackage, String className,
Map<Long, TypedObject> typeMap)
{
TypedObjectType typedObjectType = TypedObjectType.fromString(typedObject.getType());
ContentGenerator<TypedObject> contentGenerator = contentGeneratorMap.get(typedObjectType);
if (contentGenerator != null) {
String content = contentGenerator.generateContent(typedObject, className, typeMap);
if (content == null) {
return null;
}
Template template = Velocity.getTemplate("templates/package_with_contents.vm");
StringWriter stringWriter = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("packageName", targetPackage);
context.put("contents", content);
template.merge(context, stringWriter);
return stringWriter.toString();
}
else {
return null;
}
}