KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > SubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet;
6
7 import java.io.File JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import xjavadoc.XJavaDoc;
15
16 import xdoclet.util.LogUtil;
17
18 /**
19  * <p>
20  *
21  * An abstract base class for all sub-tasks. Common code and the contract is defined here.</p> <p>
22  *
23  * Because of the way Ant is designed all setter methods automatically are settable config parameters. Note that by
24  * default init() method inherits default setting from the containing task via DocletContext. Setter methods in sub-task
25  * gives the user finer control over config parameters of the sub-task. </p>
26  *
27  * @author Ara Abrahamian (ara_e@email.com)
28  * @created June 16, 2001
29  * @version $Revision: 1.75 $
30  */

31 public abstract class SubTask extends DocletSupport implements Serializable JavaDoc
32 {
33     private XJavaDoc _xJavaDoc;
34
35     /**
36      * Destination directory where generated files will be stored.
37      */

38     private File JavaDoc destDir = null;
39
40     /**
41      * Merge directory where XDoclet searches for external files that are to be merged. Merge files internal to
42      * xdoclet.jar are picked from xdoclet.jar's root instead of using mergeDir which is for external files. By default
43      * it inherits its value from the shared DocletContext which is itself filled with values set by user for the task.
44      */

45     private File JavaDoc mergeDir = null;
46
47     private ArrayList JavaDoc configParams = new ArrayList JavaDoc();
48
49     /**
50      * You can explicitly give the task a distinct name. It's useful for <template/> where if you're going to use config
51      * parameters you have to prefix the parameter with a unique subtask name, otherwise it's assumed to be a global
52      * config parameter and it means that you can't use the same parameter name in two <template/> instances.
53      * Overwriting it with this field explicitly gives you the chance to make the config param a local and solve the
54      * above problem.
55      */

56     private String JavaDoc subTaskName;
57
58     /**
59      * Gets the SubTaskName attribute of the SubTask object
60      *
61      * @return The SubTaskName value
62      */

63     public final String JavaDoc getSubTaskName()
64     {
65         if (subTaskName != null) {
66             return subTaskName;
67         }
68
69         return DocletTask.getSubTaskName(getClass());
70     }
71
72     /**
73      * Gets the ConfigParams attribute of the SubTask object
74      *
75      * @return The ConfigParams value
76      */

77     public List JavaDoc getConfigParams()
78     {
79         return configParams;
80     }
81
82     public Map JavaDoc getConfigParamsAsMap()
83     {
84         return DocletTask.getConfigParamsAsMap(getConfigParams());
85     }
86
87
88     /**
89      * Gets the DestDir attribute of the SubTask object
90      *
91      * @return The DestDir value
92      */

93     public File JavaDoc getDestDir()
94     {
95         return destDir;
96     }
97
98     /**
99      * Gets the MergeDir attribute of the SubTask object
100      *
101      * @return The MergeDir value
102      */

103     public File JavaDoc getMergeDir()
104     {
105         return mergeDir;
106     }
107
108     /**
109      * Sets an optional name for the subtask that will be seen in XDoclet's debug messages.
110      *
111      * @param subTaskName
112      */

113     public void setSubTaskName(String JavaDoc subTaskName)
114     {
115         this.subTaskName = subTaskName;
116     }
117
118     /**
119      * Sets the directory where the generated file(s) will be written.
120      *
121      * @param destDir The new DestDir value
122      */

123     public void setDestDir(File JavaDoc destDir)
124     {
125         this.destDir = destDir;
126     }
127
128     /**
129      * Specifies the location of the merge directory. This is where XDoclet will look for merge files.
130      *
131      * @param mergeDir The new MergeDir value
132      */

133     public void setMergeDir(File JavaDoc mergeDir)
134     {
135         this.mergeDir = mergeDir;
136     }
137
138     /**
139      * Specifies a configuration parameter for the subtask.
140      *
141      * @param configParam Describe the method parameter
142      */

143     public void addConfigParam(ConfigParameter configParam)
144     {
145         configParams.add(configParam);
146     }
147
148     /**
149      * Describe what the method does
150      *
151      * @param src Describe what the parameter does
152      */

153     public void copyAttributesFrom(TemplateSubTask src)
154     {
155         setDestDir(src.getDestDir());
156         setMergeDir(src.getMergeDir());
157         for (int i = 0; i < src.getConfigParams().size(); i++) {
158             addConfigParam((ConfigParameter) src.getConfigParams().get(i));
159         }
160         setSubTaskName(src.getSubTaskName());
161     }
162
163     /**
164      * Initializes SubTask. It inherits values of the config parameters if not explicitly defined for this sub-task.
165      *
166      * @param xJavaDoc
167      * @exception XDocletException Description of Exception
168      * @see #execute()
169      */

170     public void init(XJavaDoc xJavaDoc) throws XDocletException
171     {
172         if (xJavaDoc == null) {
173             throw new XDocletException("xJavaDoc can't be null");
174         }
175         _xJavaDoc = xJavaDoc;
176
177         Log log = LogUtil.getLog(SubTask.class, "init");
178
179         log.debug("mergeDir = " + mergeDir);
180
181         if (destDir == null) {
182             // not explicitly set by user, then inherit it from task
183
log.debug("destDir inherited it from task");
184             destDir = new File JavaDoc(getContext().getDestDir());
185         }
186
187         log.debug("destDir = " + destDir);
188
189         if (mergeDir == null && getContext().getMergeDir() != null) {
190             // not explicitly set by user, then inherit it from task
191
log.debug("mergeDir inherited it from task");
192             mergeDir = new File JavaDoc(getContext().getMergeDir());
193         }
194
195         log.debug("mergeDir = " + mergeDir);
196     }
197
198     /**
199      * Called to start execution of the sub-task.
200      *
201      * @exception XDocletException Description of Exception
202      */

203     public abstract void execute() throws XDocletException;
204
205     /**
206      * Called to validate configuration parameters.
207      *
208      * @exception XDocletException Description of Exception
209      */

210     public void validateOptions() throws XDocletException
211     {
212     }
213
214     /**
215      * A utility method that deleges the call to DocletContext.getSingleInstance().
216      *
217      * @return the singleton context object
218      */

219     protected DocletContext getContext()
220     {
221         return DocletContext.getInstance();
222     }
223
224     protected XJavaDoc getXJavaDoc()
225     {
226         return _xJavaDoc;
227     }
228 }
229
Popular Tags