KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ejb > dao > DaoSubTask


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

5 package xdoclet.modules.ejb.dao;
6
7 import org.apache.commons.logging.Log;
8
9 import xjavadoc.XClass;
10
11 import xdoclet.XDocletException;
12 import xdoclet.XDocletMessages;
13 import xdoclet.modules.ejb.AbstractEjbCodeGeneratorSubTask;
14 import xdoclet.modules.ejb.XDocletModulesEjbMessages;
15 import xdoclet.tagshandler.PackageTagsHandler;
16 import xdoclet.util.LogUtil;
17 import xdoclet.util.Translator;
18
19 /**
20  * @author <a HREF="mailto:stevensa@users.sourceforge.net">Andrew Stevens</a>
21  * @created February 8, 2002
22  * @ant.element display-name="Data Access Object" name="dao" parent="xdoclet.modules.ejb.EjbDocletTask"
23  * @version $Revision: 1.3 $
24  * @xdoclet.merge-file file="dao-custom.xdt" relates-to="{0}DAO.java" description="A text file containing custom
25  * template and/or java code to include in the data access object interface."
26  */

27 public class DaoSubTask extends AbstractEjbCodeGeneratorSubTask
28 {
29     public final static String JavaDoc DEFAULT_DAO_CLASS_PATTERN = "{0}DAO";
30
31     private static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/dao.xdt";
32
33     /**
34      * A configuration parameter for specifying the DAO class name pattern. By default the value is used for deciding
35      * the DAO class name. {0} in the value mean current class's symbolic name which for an EJBean is the EJB name.
36      *
37      * @see #getDaoClassPattern()
38      */

39     private String JavaDoc daoClassPattern;
40
41     /**
42      * Describe what the DaoSubTask constructor does
43      */

44     public DaoSubTask()
45     {
46         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
47         setDestinationFile(getDaoClassPattern() + ".java");
48         addOfType("javax.ejb.EntityBean");
49         addOfType("javax.ejb.SessionBean");
50         setHavingClassTag("ejb.dao");
51         setPackageSubstitutionInheritanceSupported(false);
52     }
53
54     /**
55      * Returns the configuration parameter for specifying the DAO class name pattern. By default the value is used for
56      * deciding the DAO class name. {0} in the value mean current class's symbolic name which for an EJBean is the EJB
57      * name. If nothing explicitly specified by user then "{0}DAO" is used by default.
58      *
59      * @return The daoClassPattern value
60      */

61     public String JavaDoc getDaoClassPattern()
62     {
63         if (daoClassPattern != null) {
64             return daoClassPattern;
65         }
66         else {
67             return DEFAULT_DAO_CLASS_PATTERN;
68         }
69     }
70
71     /**
72      * Sets the Pattern attribute of the DaoSubTask object
73      *
74      * @param new_pattern The new Pattern value
75      */

76     public void setPattern(String JavaDoc new_pattern)
77     {
78         daoClassPattern = new_pattern;
79     }
80
81     /**
82      * Called to validate configuration parameters.
83      *
84      * @exception XDocletException
85      */

86     public void validateOptions() throws XDocletException
87     {
88         super.validateOptions();
89
90         if (getDaoClassPattern() == null || getDaoClassPattern().trim().equals("")) {
91             throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"pattern"}));
92         }
93
94         if (getDaoClassPattern().indexOf("{0}") == -1) {
95             throw new XDocletException(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.PATTERN_HAS_NO_PLACEHOLDER));
96         }
97     }
98
99     /**
100      * Gets the GeneratedFileName attribute of the DaoSubTask object
101      *
102      * @param clazz Describe what the parameter does
103      * @return The GeneratedFileName value
104      * @exception XDocletException
105      */

106     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
107     {
108         return PackageTagsHandler.packageNameAsPathFor(DaoTagsHandler.getDaoClassFor(getCurrentClass())) + ".java";
109     }
110
111     /**
112      * Describe what the method does
113      *
114      * @exception XDocletException
115      */

116     protected void engineStarted() throws XDocletException
117     {
118         System.out.println(Translator.getString(XDocletModulesEjbMessages.class, XDocletModulesEjbMessages.GENERATING_DAO_FOR,
119             new String JavaDoc[]{getCurrentClass().getQualifiedName()}));
120     }
121
122     /**
123      * Describe what the method does
124      *
125      * @param clazz Describe what the parameter does
126      * @return Describe the return value
127      * @exception XDocletException
128      */

129     protected boolean matchesGenerationRules(XClass clazz) throws XDocletException
130     {
131         Log log = LogUtil.getLog(DaoSubTask.class, "matchesGenerationRules");
132
133         if (super.matchesGenerationRules(clazz) == false) {
134             log.debug("Skip bean " + clazz.getQualifiedName() + " because super.matchesGenerationRules() returned false.");
135             return false;
136         }
137
138         String JavaDoc generate = clazz.getDoc().getTagAttributeValue("ejb.dao", "generate", false);
139
140         if ((generate != null) && (generate.equals("false") || generate.equals("no"))) {
141             log.debug("Skip DAO class for " + clazz.getQualifiedName() + " because of generate=" + generate + " flag.");
142             return false;
143         }
144
145         return true;
146     }
147
148 }
149
Popular Tags