本文整理匯總了Java中org.apache.velocity.app.Velocity.init方法的典型用法代碼示例。如果您正苦於以下問題:Java Velocity.init方法的具體用法?Java Velocity.init怎麽用?Java Velocity.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.velocity.app.Velocity
的用法示例。
在下文中一共展示了Velocity.init方法的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: usage1
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public void usage1(String inputFile) throws FileNotFoundException {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("author", "Elliot A.");
context.put("address", "217 E Broadway");
context.put("phone", "555-1337");
FileInputStream file = new FileInputStream(inputFile);
//Evaluate
StringWriter swOut = new StringWriter();
Velocity.evaluate(context, swOut, "test", file);
String result = swOut.getBuffer().toString();
System.out.println(result);
}
示例3: 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", "");
}
示例4: VelocityViewProcessor
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Inject
public VelocityViewProcessor(final javax.ws.rs.core.Configuration config, final ServiceLocator serviceLocator,
@Optional final ServletContext servletContext) {
super(config, servletContext, "velocity", "vm");
this.factory = getTemplateObjectFactory(serviceLocator, VelocityConfigurationFactory.class,
new Value<VelocityConfigurationFactory>() {
@Override
public VelocityConfigurationFactory get() {
Configuration configuration = getTemplateObjectFactory(serviceLocator, Configuration.class,
Values.<Configuration>empty());
if (configuration == null) {
return new VelocityDefaultConfigurationFactory(servletContext);
} else {
return new VelocitySuppliedConfigurationFactory(configuration);
}
}
});
Velocity.init();
}
示例5: __doViewInit
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
@Override
protected void __doViewInit(IWebMvc owner) {
super.__doViewInit(owner);
// 初始化Velocity模板引擎配置
if (!__inited) {
__velocityConfig.setProperty(Velocity.ENCODING_DEFAULT, owner.getModuleCfg().getDefaultCharsetEncoding());
__velocityConfig.setProperty(Velocity.INPUT_ENCODING, owner.getModuleCfg().getDefaultCharsetEncoding());
__velocityConfig.setProperty(Velocity.OUTPUT_ENCODING, owner.getModuleCfg().getDefaultCharsetEncoding());
//
if (__baseViewPath.startsWith("/WEB-INF")) {
__velocityConfig.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, new File(RuntimeUtils.getRootPath(), StringUtils.substringAfter(__baseViewPath, "/WEB-INF/")).getPath());
} else {
__velocityConfig.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, __baseViewPath);
}
//
Velocity.init(__velocityConfig);
//
__inited = true;
}
}
示例6: doMerge
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static String doMerge(String strToMerge, Map map) {
if (strToMerge == null)
return null;
try {
// ask Velocity to evaluate it.
Velocity.init();
StringWriter w = new StringWriter();
VelocityContext context = new VelocityContext(map);
Velocity.evaluate(context, w, "logTag", strToMerge);
return w.getBuffer().toString();
} catch (Exception e) {
return null;
}
}
示例7: start
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* Simply initialize the Velocity engine
*/
@PostConstruct
public void start() throws Exception
{
try
{
Velocity.setProperty(Velocity.RESOURCE_LOADER, "cp");
Velocity.setProperty("cp.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.setProperty("cp.resource.loader.cache", "true");
Velocity.setProperty("cp.resource.loader.modificationCheckInterval ", "0");
Velocity.setProperty("input.encoding", "UTF-8");
Velocity.setProperty("output.encoding", "UTF-8");
// Very busy servers should increase this value. Default: 20
// Velocity.setProperty("velocity.pool.size", "20");
Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.JdkLogChute");
Velocity.init();
log.log(Level.FINE,"Velocity initialized!");
}
catch (Exception ex)
{
log.log(Level.SEVERE,"Unable to initialize Velocity", ex);
throw new RuntimeException(ex);
}
}
示例8: generateCodeByVelocity
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* 生成代碼 by velocity
* @param map 變量
* @param destPath 目的地址
* @param destFile 目的文件名
* @param tmpPath 模版地址
* @param tmpFile 模版文件名
* @return
*/
public static boolean generateCodeByVelocity(Map<String, Object> map, String destPath, String destFile, String tmpPath, String tmpFile){
try {
// 1.初始化
Properties properties = new Properties();
properties.put("file.resource.loader.path", tmpPath);
properties.put("input.encoding", "UTF-8");
properties.put("output.encoding", "UTF-8");
Velocity.init(properties);
VelocityContext context = new VelocityContext(map);
// 2.生成代碼
FileUtil.mkdir(destPath);
BufferedWriter sw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(destPath, destFile)), "UTF-8"));
Velocity.getTemplate(tmpFile).merge(context, sw);
sw.flush();
sw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
示例9: 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();
}
示例10: configure
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static boolean configure(File file) {
boolean result = false;
try {
if (file != null) {
Velocity.init(file.getAbsolutePath());
} else {
// default
// org/apache/velocity/runtime/defaults/velocity.properties
Velocity.init();
}
result = true;
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
示例11: init
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public static void init() {
if (_isInitialized) {
return;
}
Properties veloProp = new Properties();
try {
veloProp.load(instance().getClass().getResourceAsStream("/velocity.properties")); //$NON-NLS-1$
Velocity.init(veloProp);
_isInitialized = true;
} catch (Exception ex) {
StatusUtil.log(ex);
}
}
示例12: 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);
}
}
示例13: initializeVelocity
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* Intializes the Apache Velocity template engine.
*
* @throws ConfigurationException thrown if there is a problem initializing Velocity
*/
protected static void initializeVelocity() throws ConfigurationException {
try {
log.debug("Initializing Velocity template engine");
Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.NullLogChute");
Velocity.setProperty(RuntimeConstants.ENCODING_DEFAULT, "UTF-8");
Velocity.setProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8");
Velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
Velocity.setProperty("classpath.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init();
} catch (Exception e) {
throw new ConfigurationException("Unable to initialize Velocity template engine", e);
}
}
示例14: AbstractReporter
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
/**
* @param classpathPrefix Where in the classpath to load templates from.
*/
protected AbstractReporter(String classpathPrefix) {
this.classpathPrefix = classpathPrefix;
Velocity.setProperty("resource.loader", "classpath");
Velocity.setProperty("classpath.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
if (!META.shouldGenerateVelocityLog()) {
Velocity.setProperty("runtime.log.logsystem.class",
"org.apache.velocity.runtime.log.NullLogSystem");
}
try {
Velocity.init();
} catch (Exception | AssertionError ex) {
throw new ReportNGException("Failed to initialise Velocity.", ex);
}
}
示例15: Test2
import org.apache.velocity.app.Velocity; //導入方法依賴的package包/類
public Test2() throws Exception {
//init
Velocity.init("Velocity/GS_Velocity_1/src/main/java/velocity.properties");
// get Template
Template template = Velocity.getTemplate("Test2.vm");
// getContext
Context context = new VelocityContext();
String name = "Vova";
context.put("name", name);
// get Writer
Writer writer = new StringWriter();
// merge
template.merge(context, writer);
System.out.println(writer.toString());
}