View Javadoc

1   package org.tinyjee.maven.dim.sources;
2   
3   import org.apache.velocity.app.VelocityEngine;
4   import org.apache.velocity.context.Context;
5   import org.apache.velocity.tools.config.EasyFactoryConfiguration;
6   import org.tinyjee.maven.dim.spi.Globals;
7   import org.tinyjee.maven.dim.spi.VelocityConfigurator;
8   
9   import java.util.Map;
10  
11  import static org.apache.velocity.runtime.RuntimeConstants.*;
12  
13  /**
14   * Is the default implementation for {@link VelocityConfigurator} which sets various defaults.
15   *
16   * @author Juergen_Kellerer, 2011-10-14
17   */
18  public class DefaultVelocityConfigurator implements VelocityConfigurator {
19  	public void configureEngine(VelocityEngine engine) {
20  		// Register a custom resource loader that uses DIM's path / url resolve logic
21  		engine.setProperty(RESOURCE_LOADER, "dim-resource");
22  		engine.setProperty("dim-resource.resource.loader.class",
23  				"org.tinyjee.maven.dim.sources.TemplateSource$UrlResourceLoader");
24  
25  		// Enable template-local macro definitions
26  		if (engine.getProperty(VM_PERM_ALLOW_INLINE) == null) engine.setProperty(VM_PERM_ALLOW_INLINE, true);
27  
28  		// Setup macro libraries
29  		String existingLibrary = (String) engine.getProperty(VM_LIBRARY);
30  		engine.setProperty(VM_LIBRARY, "classpath:/templates/dim/macro-library.vm," + VM_LIBRARY_DEFAULT +
31  				(existingLibrary == null ? "" : ',' + existingLibrary));
32  	}
33  
34  	public void configureTools(EasyFactoryConfiguration toolsConfiguration) {
35  		toolsConfiguration.toolbox("application").
36  				tool("org.apache.velocity.tools.generic.AlternatorTool").
37  				tool("org.apache.velocity.tools.generic.ClassTool").
38  				tool("org.apache.velocity.tools.generic.ComparisonDateTool").
39  				tool("org.apache.velocity.tools.generic.ConversionTool").
40  				tool("org.apache.velocity.tools.generic.DateTool").
41  				tool("org.apache.velocity.tools.generic.DisplayTool").
42  				tool("org.apache.velocity.tools.generic.EscapeTool").
43  				tool("org.apache.velocity.tools.generic.FieldTool").
44  				tool("org.apache.velocity.tools.generic.MathTool").
45  				tool("org.apache.velocity.tools.generic.NumberTool").
46  				tool("org.apache.velocity.tools.generic.RenderTool").
47  				tool("org.apache.velocity.tools.generic.SortTool").
48  				tool("org.apache.velocity.tools.generic.ListTool").
49  				tool("org.apache.velocity.tools.generic.ResourceTool").
50  				tool("org.apache.velocity.tools.generic.MarkupTool").
51  				tool(VelocityHighlightingTool.class).
52  				tool(VelocitySourceClassInvocationTool.class);
53  	}
54  
55  	public void configureContext(Map<String, Object> parameters, Context templateContext) {
56  		// Note: The given parameters and tools configuration is already present, applying
57  		//       additional customizations:
58  		if (!templateContext.containsKey("globals")) templateContext.put("globals", Globals.getInstance());
59  		if (!templateContext.containsKey("macroParameters")) templateContext.put("macroParameters", parameters);
60  		if (!templateContext.containsKey("context")) templateContext.put("context", templateContext);
61  
62  		// 'sourceUrl' is set when the context is constructed and points to the actual template file,
63  		// there's no need to construct and set it here.
64  	}
65  }