use of org.apache.velocity.app.VelocityEngine in project Asqatasun by Asqatasun.
the class CodeGeneratorMojo method initializeVelocity.
/**
*
* @return
*/
private VelocityEngine initializeVelocity() {
Properties props = new Properties();
props.setProperty("resource.loader", "file");
props.setProperty("file.resource.loader.description", "Velocity File Resource Loader");
props.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
props.setProperty("file.resource.loader.path", "/");
VelocityEngine ve = new VelocityEngine();
try {
ve.init(props);
} catch (Exception ex) {
Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
}
return ve;
}
use of org.apache.velocity.app.VelocityEngine in project jena by apache.
the class SimpleVelocity method process.
/** Process a template */
public static void process(String base, String path, Writer out, VelocityContext context) {
VelocityEngine velocity = new VelocityEngine();
// Turn off logging - catch exceptions and log ourselves
velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogChute);
velocity.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
velocity.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, base);
velocity.init();
try {
Template temp = velocity.getTemplate(path);
temp.merge(context, out);
out.flush();
} catch (ResourceNotFoundException ex) {
velocityLog.error("Resource not found: " + ex.getMessage());
} catch (ParseErrorException ex) {
velocityLog.error("Parse error (" + path + ") : " + ex.getMessage());
} catch (MethodInvocationException ex) {
velocityLog.error("Method invocation exception (" + path + ") : " + ex.getMessage());
} catch (IOException ex) {
velocityLog.warn("IOException", ex);
}
}
use of org.apache.velocity.app.VelocityEngine in project maven-plugins by apache.
the class AnnouncementMojo method processTemplate.
/**
* Create the velocity template
*
* @param context velocity context that has the parameter values
* @param outputDirectory directory where the file will be generated
* @param template velocity template which will the context be merged
* @param announcementFile The file name of the generated announcement
* @throws VelocityException in case of errors.
* @throws MojoExecutionException in case of errors.
*/
public void processTemplate(Context context, File outputDirectory, String template, String announcementFile) throws VelocityException, MojoExecutionException {
File f;
// Use the name of the template as a default value
if (StringUtils.isEmpty(announcementFile)) {
announcementFile = template;
}
try {
f = new File(outputDirectory, announcementFile);
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
VelocityEngine engine = velocity.getEngine();
engine.setApplicationAttribute("baseDirectory", basedir);
if (StringUtils.isEmpty(templateEncoding)) {
templateEncoding = ReaderFactory.FILE_ENCODING;
getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding + ", i.e. build is platform dependent!");
}
Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding);
Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding);
velocityTemplate.merge(context, writer);
writer.flush();
writer.close();
getLog().info("Created template " + f);
} catch (ResourceNotFoundException rnfe) {
throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
} catch (VelocityException ve) {
throw new VelocityException(ve.toString());
} catch (Exception e) {
if (e.getCause() != null) {
getLog().warn(e.getCause());
}
throw new MojoExecutionException(e.toString(), e.getCause());
}
}
use of org.apache.velocity.app.VelocityEngine in project lucene-solr by apache.
the class VelocityResponseWriter method createEngine.
private VelocityEngine createEngine(SolrQueryRequest request) {
VelocityEngine engine = new VelocityEngine();
// route all Velocity logging through Solr's logging facility
engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, velocityLogger);
// Set some engine properties that improve the experience
// - these could be considered in the future for parameterization, but can also be overridden by using
// the init.properties.file setting. (TODO: add a test for this properties set here overridden)
// load the built-in _macros.vm first, then load VM_global_library.vm for legacy (pre-5.0) support,
// and finally allow macros.vm to have the final say and override anything defined in the preceding files.
engine.setProperty(RuntimeConstants.VM_LIBRARY, "_macros.vm,VM_global_library.vm,macros.vm");
// Standard templates autoload, but not the macro one(s), by default, so let's just make life
// easier, and consistent, for macro development too.
engine.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "true");
/*
Set up Velocity resource loader(s)
terminology note: "resource loader" is overloaded here, there is Solr's resource loader facility for plugins,
and there are Velocity template resource loaders. It's confusing, they overlap: there is a Velocity resource
loader that loads templates from Solr's resource loader (SolrVelocityResourceLoader).
The Velocity resource loader order is [params,][file,][solr], intentionally ordered in this manner, and each
one optional and individually enable-able. By default, only "solr" (resource loader) is used, parsing templates
from a velocity/ sub-tree in either the classpath or under conf/.
A common usage would be to enable the file template loader, keeping the solr loader enabled; the Velocity resource
loader path would then be "file,solr" (params is disabled by default). The basic browse templates are built into
this plugin, but can be individually overridden by placing a same-named template in the template.base.dir specified
directory.
*/
ArrayList<String> loaders = new ArrayList<String>();
if (paramsResourceLoaderEnabled) {
loaders.add("params");
engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request));
}
if (fileResourceLoaderBaseDir != null) {
loaders.add("file");
engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, fileResourceLoaderBaseDir.getAbsolutePath());
}
if (solrResourceLoaderEnabled) {
// The solr resource loader serves templates under a velocity/ subtree from <lib>, conf/,
// or SolrCloud's configuration tree. Or rather the other way around, other resource loaders are rooted
// from the top, whereas this is velocity/ sub-tree rooted.
loaders.add("solr");
engine.setProperty("solr.resource.loader.instance", new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader()));
}
// Always have the built-in classpath loader. This is needed when using VM_LIBRARY macros, as they are required
// to be present if specified, and we want to have a nice macros facility built-in for users to use easily, and to
// extend in custom ways.
loaders.add("builtin");
engine.setProperty("builtin.resource.loader.instance", new ClasspathResourceLoader());
engine.setProperty(RuntimeConstants.RESOURCE_LOADER, StringUtils.join(loaders, ','));
engine.setProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8");
// bring in any custom properties too
engine.init(velocityInitProps);
return engine;
}
use of org.apache.velocity.app.VelocityEngine in project tomee by apache.
the class Container method copyTemplateTo.
private void copyTemplateTo(final File targetDir, final String filename) throws Exception {
final File file = new File(targetDir, filename);
if (file.exists()) {
return;
}
// don't break apps using Velocity facade
final VelocityEngine engine = new VelocityEngine();
engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
engine.setProperty(Velocity.RESOURCE_LOADER, "class");
engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
engine.init();
final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename);
final VelocityContext context = new VelocityContext();
context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
final Writer writer = new FileWriter(file);
template.merge(context, writer);
writer.flush();
writer.close();
}
Aggregations