KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > entity > ValueObjectSubTask


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

5 package xdoclet.modules.ejb.entity;
6
7 import java.util.*;
8
9 import xjavadoc.*;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13
14 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
15 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
16 import xdoclet.tagshandler.PackageTagsHandler;
17 import xdoclet.util.Translator;
18
19 /**
20  * Creates "value objects" for Entity EJBs. This task replaces <a HREF="DataObjectSubTask.html">Data Object</a> .
21  *
22  * @author Vincent Harcq (vincent.harcq@hubmethods.com)
23  * @created Feb 5, 2002
24  * @ant.element display-name="Value Object" name="valueobject" parent="xdoclet.modules.ejb.EjbDocletTask"
25  * @version $Revision: 1.16 $
26  * @xdoclet.merge-file file="valueobject-custom.xdt" relates-to="{0}Value.java" description="A text file containing
27  * custom template and/or java code to include in the value object class."
28  */

29 public class ValueObjectSubTask extends AbstractEjbCodeGeneratorSubTask
30 {
31     public final static String JavaDoc DEFAULT_DATAOBJECT_CLASS_PATTERN = "{0}Value";
32
33     protected final static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/valueobject.xdt";
34
35     private static String JavaDoc currentValueObjectClass;
36
37     private static String JavaDoc currentValueObjectName;
38
39     private static String JavaDoc currentValueObjectMatch;
40     private static String JavaDoc currentValueObjectImplements;
41     private static String JavaDoc currentValueObjectExtends;
42
43     /**
44      * A configuration parameter for specifying the data object class name pattern. By default the value is used for
45      * deciding the entity data object class name. The placeholder "{0}" in the value means the current class's symbolic
46      * name which for an EJBean is the EJB name.
47      *
48      * @see #getValueObjectClassPattern()
49      */

50     protected String JavaDoc valueObjectClassPattern;
51
52     /**
53      * Form tag being processed right now
54      */

55     protected XTag currentDataObjectTag;
56
57     /**
58      * Whether to generate a single-parameter constructor, only setting up the PK
59      */

60     private boolean generatePKConstructor = false;
61
62     /**
63      * Describe what the ValueObjectSubTask constructor does
64      */

65     public ValueObjectSubTask()
66     {
67         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
68         setDestinationFile(getValueObjectClassPattern() + ".java");
69         addOfType("javax.ejb.EntityBean");
70     }
71
72     /**
73      * Gets the CurrentValueObjectClass attribute of the ValueObjectSubTask class
74      *
75      * @return The CurrentValueObjectClass value
76      */

77     public static String JavaDoc getCurrentValueObjectClass()
78     {
79         return currentValueObjectClass;
80     }
81
82     public static String JavaDoc getCurrentValueObjectImplements()
83     {
84         return currentValueObjectImplements;
85     }
86
87     public static String JavaDoc getCurrentValueObjectExtends()
88     {
89         return currentValueObjectExtends;
90     }
91
92     /**
93      * Gets the CurrentValueObjectName attribute of the ValueObjectSubTask class
94      *
95      * @return The CurrentValueObjectName value
96      */

97     public static String JavaDoc getCurrentValueObjectName()
98     {
99         return currentValueObjectName;
100     }
101
102     /**
103      * Gets the CurrentValueObjectMatch attribute of the ValueObjectSubTask class
104      *
105      * @return The CurrentValueObjectMatch value
106      */

107     public static String JavaDoc getCurrentValueObjectMatch()
108     {
109         return currentValueObjectMatch;
110     }
111
112     /**
113      * Returns the configuration parameter for specifying the data object class name pattern. By default the value is
114      * used for deciding the entity data object class name. The placeholder "{0}" in the value means the current class's
115      * symbolic name which for an EJBean is the EJB name. If nothing explicitly specified by user then "{0}Value" is
116      * used by default.
117      *
118      * @return The ValueObjectClassPattern value
119      * @see #valueObjectClassPattern
120      */

121     public String JavaDoc getValueObjectClassPattern()
122     {
123         if (valueObjectClassPattern != null) {
124             return valueObjectClassPattern;
125         }
126         else {
127             return DEFAULT_DATAOBJECT_CLASS_PATTERN;
128         }
129     }
130
131     public boolean getGeneratePKConstructor()
132     {
133         return generatePKConstructor;
134     }
135
136     /**
137      * The pattern by which the value object classes are named. The placeholder "{0}" designates the EJB name.
138      *
139      * @param new_pattern The new Pattern value
140      * @ant.not-required No, default is "{0}Value"
141      */

142     public void setPattern(String JavaDoc new_pattern)
143     {
144         valueObjectClassPattern = new_pattern;
145     }
146
147     /**
148      * Whether to generate a single-parameter constructor in the ValueObject, which only sets up the PK.
149      *
150      * @param generatePKConstructor The new GeneratePKConstructor value
151      * @ant.not-required No, default is false
152      */

153     public void setGeneratePKConstructor(String JavaDoc generatePKConstructor)
154     {
155         this.generatePKConstructor = generatePKConstructor.substring(0, 1).equalsIgnoreCase("t");
156     }
157
158     /**
159      * Called to validate configuration parameters.
160      *
161      * @exception XDocletException
162      */

163     public void validateOptions() throws XDocletException
164     {
165         super.validateOptions();
166
167         if (getValueObjectClassPattern() == null || getValueObjectClassPattern().trim().equals("")) {
168             throw new XDocletException(
169                 Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
170         }
171
172         if (getValueObjectClassPattern().indexOf("{0}") == -1) {
173             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
174         }
175     }
176
177     /**
178      * Gets the GeneratedFileName attribute of the ValueObjectSubTask object
179      *
180      * @param clazz Describe what the parameter does
181      * @return The GeneratedFileName value
182      * @exception XDocletException
183      */

184     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
185     {
186         return PackageTagsHandler.packageNameAsPathFor(getCurrentValueObjectClass()) + ".java";
187     }
188
189     /**
190      * Describe what the method does
191      *
192      * @param clazz Describe what the parameter does
193      * @return Describe the return value
194      * @exception XDocletException
195      */

196     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
197     {
198         if (super.matchesGenerationRules(clazz) == false) {
199             return false;
200         }
201
202         if (ValueObjectTagsHandler.isGenerationNeeded(getCurrentClass())) {
203             return true;
204         }
205         else {
206             return false;
207         }
208     }
209
210     /**
211      * Describe what the method does
212      *
213      * @param clazz Describe what the parameter does
214      * @exception XDocletException
215      */

216     protected void generateForClass(XClass clazz) throws XDocletException
217     {
218
219         Collection dos = getCurrentClass().getDoc().getTags("ejb:value-object");
220
221 // System.out.println( "Generating Value Object classes for '" + getCurrentClass().qualifiedName() + "'." );
222

223         for (Iterator i = dos.iterator(); i.hasNext(); ) {
224             XTag tag = (XTag) i.next();
225
226             currentValueObjectClass = ValueObjectTagsHandler.getCurrentValueObjectClass(getCurrentClass(), tag);
227             currentValueObjectName = ValueObjectTagsHandler.getCurrentValueObjectName(tag);
228             currentValueObjectMatch = ValueObjectTagsHandler.getCurrentValueObjectMatch(tag);
229             currentValueObjectImplements = ValueObjectTagsHandler.getCurrentValueObjectImplements(tag);
230             currentValueObjectExtends = ValueObjectTagsHandler.getCurrentValueObjectExtends(tag);
231             super.generateForClass(clazz);
232         }
233     }
234
235     /**
236      * Describe what the method does
237      *
238      * @exception XDocletException
239      */

240     protected void engineStarted() throws XDocletException
241     {
242         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_VALUEOBJECT_FOR,
243             new String JavaDoc[]{getCurrentClass().getQualifiedName() + "--> " + getCurrentValueObjectClass()}));
244     }
245
246 }
247
Popular Tags