1 /*
2 * Copyright (c) 2001, 2002 The XDoclet team
3 * All rights reserved.
4 */
5 package xdoclet.plugins;
6
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.velocity.VelocityContext;
9 import org.apache.velocity.app.VelocityEngine;
10 import org.apache.velocity.exception.MethodInvocationException;
11 import org.apache.velocity.exception.ResourceNotFoundException;
12 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
13 import org.apache.velocity.runtime.resource.loader.JarResourceLoader;
14
15 import xdoclet.XDocletException;
16 import xdoclet.util.velocity.VelocityConfigurer;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.Map;
23 import java.util.Iterator;
24
25 /***
26 * Plugin backed by <a href="http://jakarta.apache.org/velocity/">Velocity</a>.
27 *
28 * @bean.class name="velocity"
29 * displayName="Velocity Plugin"
30 * shortDescription="A plugin that uses Velocity Scripts"
31 *
32 * @bean.icon color16="velocity16.gif"
33 *
34 * @author <a href="mailto:aslak.hellesoy at netcom.no">Aslak Hellesøy</a>
35 * @version $Revision: 1.12 $
36 */
37 public class VelocityPlugin extends ScriptPlugin {
38 /*** Shared among all instances. */
39 private static VelocityEngine VELOCITY_ENGINE;
40
41 public VelocityPlugin() {
42 }
43
44 private void initEngine()
45 throws Exception {
46 // Initialises Velocity by registering a FileResourceLoader and a JarResourceLoader that
47 // use the directories and jar files supplied by the classpathManager.
48 if( getXDoclet() == null ) {
49 throw new XDocletException("There was no XDoclet for this plugin.");
50 }
51 VelocityConfigurer velocityConfigurer = new VelocityConfigurer(getXDoclet().getClasspathManager());
52
53 // We're adding FileResourceLoader first, so users can easily override templates
54 // by putting them earlier on the classpath
55 velocityConfigurer.addLoader(FileResourceLoader.class, getXDoclet().getClasspathManager().getDirectories());
56 velocityConfigurer.addLoader(JarResourceLoader.class, getXDoclet().getClasspathManager().getJars());
57
58 VELOCITY_ENGINE = velocityConfigurer.initVelocity();
59 }
60
61 /***
62 * Sets the template name. Velocity will search for this template on
63 * the classpath XDoclet was launched with.
64 *
65 * {@inheritDoc}
66 */
67 public void setTemplatePath(String templateName) {
68 super.setTemplatePath(templateName);
69 }
70
71 protected final void generate(File file, Collection metaData) throws IOException, XDocletException {
72 // We're just ignoring the metaData. Our superclass has put it on the context.
73 super.generate( file, metaData );
74 generate(file);
75 }
76
77 protected final void generate(File file, Object metaData) throws IOException, XDocletException {
78 // We're just ignoring the metaData. Our superclass has put it on the context.
79 super.generate( file, metaData );
80 generate(file);
81 }
82
83 /***
84 * Invokes Velocity to render a template.
85 */
86 private final void generate(File file)
87 throws IOException, XDocletException {
88 try {
89 FileWriter writer = new FileWriter(file);
90 VelocityContext context = new VelocityContext();
91 Map props = getContextObjects();
92 Iterator iter = props.keySet().iterator();
93 while (iter.hasNext()) {
94 String key = (String) iter.next();
95 context.put(key, props.get(key));
96 }
97 String templateName = getTemplatePath();
98
99 if (templateName == null) {
100 throw new XDocletException("templateName was not specified");
101 }
102
103 LogFactory.getLog(getClass()).debug("Using templateName:" + templateName);
104
105 if (VELOCITY_ENGINE == null) {
106 initEngine();
107 }
108
109 VELOCITY_ENGINE.mergeTemplate(templateName, getEncoding(), context, writer);
110 writer.flush();
111 writer.close();
112 } catch (ResourceNotFoundException e) {
113 // Unable to find resource 'path/to/template.vm'
114 LogFactory.getLog(getClass()).error(e.getMessage());
115 LogFactory.getLog(getClass()).error("file.resource.loader.path:"
116 + VELOCITY_ENGINE.getProperty("file.resource.loader.path"));
117 LogFactory.getLog(getClass()).error("jar.resource.loader.path:"
118 + VELOCITY_ENGINE.getProperty("jar.resource.loader.path"));
119 throw new XDocletException(e.getMessage(), e);
120 } catch (MethodInvocationException e) {
121 Throwable t = e.getWrappedThrowable();
122
123 throw new XDocletException(t.getMessage(), t);
124 } catch (IOException e) {
125 throw e;
126 } catch (Exception e) {
127 throw new XDocletException(e.getMessage(), e);
128 }
129 }
130 }
This page was automatically generated by Maven