KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > jdo > JdoXmlMetadataSubTask


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

5 package xdoclet.modules.jdo;
6
7
8 import java.io.File JavaDoc;
9 import java.text.MessageFormat JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import xjavadoc.*;
15 import xdoclet.XDocletException;
16 import xdoclet.XDocletMessages;
17 import xdoclet.XmlSubTask;
18 import xdoclet.modules.jdo.JdoDocletTask.JdoSpecVersion;
19 import xdoclet.tagshandler.PackageTagsHandler;
20 import xdoclet.template.TemplateException;
21 import xdoclet.util.LogUtil;
22 import xdoclet.util.Translator;
23
24 /**
25  * Generates the XML metadata for the JDO classes.
26  *
27  * @author Ludovic Claude (ludovicc@users.sourceforge.net)
28  * @author David Jencks (d_jencks@users.sourceforge.net)
29  * @created June 11, 20012
30  * @version $Revision: 1.11 $
31  * @ant.element display-name="JDO XML MetaData" name="jdometadata" parent="xdoclet.modules.jdo.JdoDocletTask"
32  * @xdoclet.merge-file file="vendor-extensions.xml" relates-to="generated .jdo files" description="An XML unparsed
33  * entity containing any additional vendor extensions i.e. top-level extension elements."
34  */

35 public class JdoXmlMetadataSubTask extends XmlSubTask
36 {
37     private static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/jdo_xml.xdt";
38
39     private static String JavaDoc PACKAGE_GENERATED_FILE_NAME = "{0}.jdo";
40     private static String JavaDoc CLASS_GENERATED_FILE_NAME = "{0}.jdo";
41
42     private static String JavaDoc JDOXML_PUBLICID_1_0 = "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 1.0//EN";
43
44     private static String JavaDoc JDOXML_SYSTEMID_1_0 = "http://java.sun.com/dtd/jdo_1_0.dtd";
45
46     private static String JavaDoc JDOXML_DTD_FILE_NAME_1_0 = "resources/jdo_1_0.dtd";
47
48     private static String JavaDoc JDOXML_PUBLICID_2_0 = "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN";
49
50     private static String JavaDoc JDOXML_SYSTEMID_2_0 = "http://java.sun.com/dtd/jdo_2_0.dtd";
51
52     private static String JavaDoc JDOXML_DTD_FILE_NAME_2_0 = "resources/jdo_2_0.dtd";
53
54     private String JavaDoc jdoSpec = null;
55     private String JavaDoc generation = "class";
56     private boolean forceGenerationPerPackage;
57     private String JavaDoc project = "metadata";
58
59
60     /**
61      * Describe what the WebXmlSubTask constructor does
62      */

63     public JdoXmlMetadataSubTask()
64     {
65         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
66     }
67
68     /**
69      * Gets the Jdospec attribute of the JdoXmlSubTask object
70      *
71      * @return The Jdospec value
72      */

73     public String JavaDoc getJdoSpec()
74     {
75         if (jdoSpec != null)
76             return jdoSpec;
77
78         // TODO Why the hell doesn't it copy the parameters from JdoDocletTask to JdoXmlMetadataSubTask???
79
// Fortunately, the correct value can be found in getContext().getConfigParam(...):
80
return (String JavaDoc) getContext().getConfigParam("jdospec");
81     }
82
83     public String JavaDoc getGeneration()
84     {
85         return generation;
86     }
87
88     public String JavaDoc getProject()
89     {
90         return project;
91     }
92
93     public void setProject(String JavaDoc project)
94     {
95         this.project = project;
96     }
97
98     public void setGeneration(GenerationOptionTypes value)
99     {
100         generation = value.getValue();
101     }
102
103     /**
104      * Sets the Jdospec attribute of the JdoXmlSubTask object
105      *
106      * @param jdoSpec The new Jdospec value
107      */

108     public void setJdoSpec(JdoSpecVersion jdoSpec)
109     {
110         // TODO Why is this method never called? see getJdoSpec()!
111
Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "setJdoSpec");
112
113         log.info("new jdoSpec: " + jdoSpec.getValue());
114         this.jdoSpec = jdoSpec.getValue();
115     }
116
117     /**
118      * Called to validate configuration parameters.
119      *
120      * @exception XDocletException Description of Exception
121      */

122     public void validateOptions() throws XDocletException
123     {
124         // JDO does not require a template url or a destination file
125
//
126
// super.validateOptions();
127
}
128
129     /**
130      * Describe what the method does
131      *
132      * @exception XDocletException
133      */

134     public void execute() throws XDocletException
135     {
136         String JavaDoc jdoSpec = getJdoSpec();
137         Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "execute");
138
139         log.info("Using jdospec \"" + jdoSpec + "\".");
140
141         if (JdoSpecVersion.JDO_2_0.equals(jdoSpec)) {
142             setPublicId(JDOXML_PUBLICID_2_0);
143             setSystemId(JDOXML_SYSTEMID_2_0);
144             setDtdURL(getClass().getResource(JDOXML_DTD_FILE_NAME_2_0));
145         }
146         else {
147             setPublicId(JDOXML_PUBLICID_1_0);
148             setSystemId(JDOXML_SYSTEMID_1_0);
149             setDtdURL(getClass().getResource(JDOXML_DTD_FILE_NAME_1_0));
150         }
151
152         if (isGenerationPerClass())
153             setDestinationFile(CLASS_GENERATED_FILE_NAME);
154         else if (isGenerationPerPackage())
155             setDestinationFile(PACKAGE_GENERATED_FILE_NAME);
156         else
157             setDestinationFile(getProject() + ".jdo");
158
159         startProcess();
160     }
161
162     public void startProcess() throws XDocletException
163     {
164         Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "startProcess");
165
166         if (log.isDebugEnabled()) {
167             log.debug("destDir.toString()=" + getDestDir());
168             log.debug("getTemplateURL()=" + getTemplateURL());
169             log.debug("getDestinationfile()=" + getDestinationFile());
170             log.debug("getOfType()=" + getOfType());
171             log.debug("getExtent()=" + getExtent());
172             log.debug("getHavingClassTag()=" + getHavingClassTag());
173         }
174
175         if (isGenerationPerClass()) {
176             startProcessPerClass();
177         }
178         else if (isGenerationPerPackage()) {
179             startProcessPerPackage();
180         }
181         else {
182             startProcessForAll();
183         }
184     }
185
186     protected boolean isForceGenerationPerPackage()
187     {
188         return forceGenerationPerPackage;
189     }
190
191     /**
192      * @return
193      * @todo make isGenerationPerClass() in TemplateSubTask protected
194      */

195     protected boolean isGenerationPerClass()
196     {
197         return GenerationOptionTypes.CLASS.equals(generation);
198     }
199
200     protected boolean isGenerationPerPackage()
201     {
202         return GenerationOptionTypes.PACKAGE.equals(generation);
203     }
204
205     /**
206      * Returns class name for the generated file. {0} substituted by package name.
207      *
208      * @param pak
209      * @return The GeneratedClassName value
210      * @exception XDocletException Description of Exception
211      */

212     protected String JavaDoc getGeneratedFileName(XPackage pak) throws XDocletException
213     {
214         Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "getGeneratedFileName");
215         String JavaDoc package_structure = null;
216
217         if (isPrefixWithPackageStructure() == true) {
218             package_structure = PackageTagsHandler.packageNameAsPathFor(pak);
219
220             int lastSlash = package_structure.lastIndexOf('/');
221
222             if (lastSlash != -1)
223                 package_structure = package_structure.substring(0, lastSlash);
224         }
225
226         String JavaDoc packageName = isPackageSubstitutionInheritanceSupported() == true ? package_structure : null;
227
228         String JavaDoc qualifiedName = pak.getName();
229         int lastDot = qualifiedName.lastIndexOf('.');
230         String JavaDoc name = qualifiedName.substring(lastDot + 1);
231
232         String JavaDoc destinationFile = MessageFormat.format(getDestinationFile(), new Object JavaDoc[]{name});
233
234         if (log.isDebugEnabled()) {
235             log.debug("pak=" + pak);
236             log.debug("packageName=" + packageName);
237             log.debug("destinationFile=" + destinationFile);
238         }
239
240         return new File JavaDoc(packageName, destinationFile).toString();
241     }
242
243     protected void setForceGenerationPerPackage(boolean value)
244     {
245         forceGenerationPerPackage = value;
246     }
247
248     protected void startProcessPerPackage() throws XDocletException
249     {
250         Log log = LogUtil.getLog(JdoXmlMetadataSubTask.class, "startProcessPerPackage");
251
252         if (log.isDebugEnabled()) {
253             log.debug("Per package.");
254         }
255
256         Collection JavaDoc packages = getXJavaDoc().getSourcePackages();
257
258         for (Iterator JavaDoc i = packages.iterator(); i.hasNext(); ) {
259             XPackage pakkage = (XPackage) i.next();
260
261             if (log.isDebugEnabled())
262                 log.debug("Working on " + pakkage);
263             generateForPackage(pakkage);
264         }
265     }
266
267     /**
268      * Processed template for pkg and generates output file for pkg.
269      *
270      * @param pkg Description of Parameter
271      * @exception XDocletException Description of Exception
272      */

273     protected void generateForPackage(XPackage pkg) throws XDocletException
274     {
275         Log log = LogUtil.getLog(getClass(), "generateForClass");
276         String JavaDoc generatedFileName = getGeneratedFileName(pkg);
277         File JavaDoc file = new File JavaDoc(getDestDir().toString(), generatedFileName);
278
279         if (log.isDebugEnabled()) {
280             log.debug("destDir.toString()=" + getDestDir().toString());
281             log.debug("getGeneratedFileName()=" + generatedFileName);
282             log.debug("file=" + file);
283         }
284
285         if (file.exists()) {
286             log.debug("File exists.");
287
288             // Check modification timestamps
289

290             // todo: implement isGenerationNeeded in GenerationManager for (sources in a package) -> generated file
291
//boolean isGenerationNeeded = getGenerationManager().isGenerationNeeded(pkg, file, true);
292
boolean isGenerationNeeded = true;
293
294             if (!isGenerationNeeded) {
295                 return;
296             }
297         }
298
299         file.getParentFile().mkdirs();
300
301         try {
302             setCurrentPackage(pkg);
303             startEngine(getTemplateURL(), new File JavaDoc(getDestDir(), generatedFileName));
304         }
305         catch (TemplateException e) {
306             if (e instanceof XDocletException) {
307                 throw (XDocletException) e;
308             }
309             else {
310                 log.debug("generateForClass()");
311                 throw new XDocletException(e, Translator.getString(XDocletMessages.class, XDocletMessages.RUNNING_FAILED));
312             }
313         }
314     }
315
316     /**
317      * Describe what the method does
318      *
319      * @exception XDocletException
320      */

321     protected void engineStarted() throws XDocletException
322     {
323         String JavaDoc generatedFileName = getDestinationFile();
324
325         if (isGenerationPerClass())
326             generatedFileName = getGeneratedFileName(getCurrentClass());
327         else if (isGenerationPerPackage())
328             generatedFileName = getGeneratedFileName(getCurrentPackage());
329         System.out.println(Translator.getString(XDocletMessages.class, XDocletMessages.GENERATING_SOMETHING, new String JavaDoc[]{generatedFileName}));
330     }
331
332     /**
333      * @author Ludovic Claude (ludovicc@users.sourceforge.net)
334      * @created June 14, 20012
335      */

336     public static class GenerationOptionTypes extends org.apache.tools.ant.types.EnumeratedAttribute
337     {
338         public final static String JavaDoc CLASS = "class";
339         public final static String JavaDoc PACKAGE = "package";
340         public final static String JavaDoc PROJECT = "project";
341
342         /**
343          * Gets the Values attribute of the GeneratinOptionTypes object
344          *
345          * @return The Values value
346          */

347         public String JavaDoc[] getValues()
348         {
349             return (new String JavaDoc[]{CLASS, PACKAGE, PROJECT});
350         }
351     }
352 }
353
Popular Tags