1 package xdoclet.plugins;
2
3 import xdoclet.Plugin;
4 import xdoclet.XDocletException;
5 import xdoclet.XDoclet;
6 import xdoclet.Property;
7 import xdoclet.util.TypeConversionUtil;
8
9 import java.io.File;
10 import java.io.IOException;
11 import java.util.Collection;
12 import java.util.Map;
13 import java.util.HashMap;
14 import java.util.Collections;
15 import java.util.Iterator;
16
17 import org.apache.commons.collections.CollectionUtils;
18 import org.apache.commons.collections.Predicate;
19
20 /***
21 * Base class for plugins based on scripting.
22 *
23 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a>
24 * @version $Revision: 1.10 $
25 */
26 public abstract class ScriptPlugin extends Plugin {
27 /*** Extra objects that are not available via the MetadataProvider. */
28 private final Map _contextObjects = new HashMap();
29
30 /*** Relative path to template (from one of the files/zips on the classpath). */
31 private String _templatePath;
32
33 /***
34 * Gets the template name.
35 *
36 * @return the template name
37 */
38 public final String getTemplatePath() {
39 return _templatePath;
40 }
41
42 /***
43 * Sets the path to the template. This should be a relative path. The template
44 * will be searched for on the classpath, using the relative path inside
45 * jar files or directories.
46 *
47 * @param templateName relative path to the template to use.
48 *
49 * @bean.property
50 * shortDescription="Relative path to the template. Resolved from classpath."
51 * displayName="Template Path"
52 * default="true"
53 */
54 public void setTemplatePath(String templateName) {
55 _templatePath = templateName;
56 }
57
58 protected void validate()
59 throws XDocletException {
60 super.validate();
61
62 if (getTemplatePath() == null) {
63 throw new XDocletException("templatePath was not specified.");
64 }
65 }
66
67 private final void setDynamicProperties() {
68 for (Iterator i = getProperties().iterator(); i.hasNext();) {
69 Property property = (Property) i.next();
70
71 setContextObject(property.getName(), property.getValue());
72 }
73 }
74
75 public void execute() throws IOException, XDocletException {
76 setContextObject("XDOCLET_VERSION", XDoclet.XDOCLET_VERSION);
77 setContextObject("plugin", this);
78 setDynamicProperties();
79 super.execute();
80 }
81
82 /***
83 * @bean.method shortDescription="Set a property that will be able to the template."
84 * displayName="New Property"
85 */
86 public final Property createProperty() {
87 Property property = new Property();
88
89 add(property);
90
91 return property;
92 }
93
94 private Collection getProperties() {
95 return CollectionUtils.select(this,
96 new Predicate() {
97 public boolean evaluate(Object o) {
98 return o instanceof Property;
99 }
100 });
101 }
102
103 protected void generate(File destinationFile, Collection metaData) throws IOException, XDocletException {
104 setContextObject("collection", metaData);
105 setContextObject("classes", getContextObjects().get("collection"));
106 setContextObject("packageName", getPackageName());
107 }
108
109 protected void generate( File destinationFile, Object metaData ) throws IOException, XDocletException {
110 setContextObject("object", metaData);
111
112 // Convenience properties for class information
113 if( metaData.getClass().getName().equals("xjavadoc.SourceClass") ) {
114 setContextObject("class", metaData);
115 }
116
117 setContextObject("destinationFileName", destinationFile.getName());
118
119 String destinationPackageName = getMetadataProvider().getPackageName(metaData);
120
121 setContextObject("destinationPackageName", destinationPackageName);
122
123 String destinationUnqualifiedClassName = destinationFile.getName().substring(0,
124 destinationFile.getName().length() - 5);
125
126 setContextObject("destinationUnqualifiedClassName", destinationUnqualifiedClassName);
127 setContextObject("destinationQualifiedClassName",
128 TypeConversionUtil.getQualifiedClassName(destinationPackageName, destinationUnqualifiedClassName));
129
130 }
131
132 /***
133 * Subclasses can call this method prior to generation to put additional objects on the context.
134 *
135 * @return a Map that will be put on Velocity's context.
136 */
137 protected final Map getContextObjects() {
138 return Collections.unmodifiableMap(_contextObjects);
139 }
140
141 /***
142 * Allows subclasses to set an additional object in the context.
143 */
144 protected final void setContextObject(String name, Object obj) {
145 _contextObjects.put(name, obj);
146 }
147
148 }
This page was automatically generated by Maven