Search in sources :

Example 11 with VelocityEngine

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;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) Properties(java.util.Properties) InvalidParameterException(org.asqatasun.referential.creator.exception.InvalidParameterException) I18NLanguageNotFoundException(org.asqatasun.referential.creator.exception.I18NLanguageNotFoundException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException)

Example 12 with VelocityEngine

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);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ParseErrorException(org.apache.velocity.exception.ParseErrorException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 13 with VelocityEngine

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());
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileOutputStream(java.io.FileOutputStream) VelocityException(org.apache.velocity.exception.VelocityException) OutputStreamWriter(java.io.OutputStreamWriter) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) VelocityException(org.apache.velocity.exception.VelocityException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template)

Example 14 with VelocityEngine

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;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) ArrayList(java.util.ArrayList) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)

Example 15 with VelocityEngine

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();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) NullLogChute(org.apache.velocity.runtime.log.NullLogChute) VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) File(java.io.File) Writer(java.io.Writer) FileWriter(java.io.FileWriter) Template(org.apache.velocity.Template)

Aggregations

VelocityEngine (org.apache.velocity.app.VelocityEngine)29 VelocityContext (org.apache.velocity.VelocityContext)13 StringWriter (java.io.StringWriter)10 Properties (java.util.Properties)9 Template (org.apache.velocity.Template)9 IOException (java.io.IOException)7 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)6 Writer (java.io.Writer)5 File (java.io.File)4 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)4 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Map (java.util.Map)2 ExtendedProperties (org.apache.commons.collections.ExtendedProperties)2