KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package xdoclet.modules.ejb.dao;
6
7 import java.text.MessageFormat JavaDoc;
8 import java.util.Properties JavaDoc;
9
10 import org.apache.commons.logging.Log;
11
12 import xjavadoc.XClass;
13 import xjavadoc.XMethod;
14
15 import xdoclet.DocletContext;
16 import xdoclet.DocletTask;
17 import xdoclet.XDocletException;
18 import xdoclet.modules.ejb.EjbTagsHandler;
19 import xdoclet.modules.ejb.home.HomeTagsHandler;
20 import xdoclet.util.LogUtil;
21
22 /**
23  * @author <a HREF="mailto:stevensa@users.sourceforge.net">Andrew Stevens</a>
24  * @created February 8, 2002
25  * @xdoclet.taghandler namespace="EjbDao"
26  * @version $Revision: 1.5 $
27  */

28 public class DaoTagsHandler extends EjbTagsHandler
29 {
30     /**
31      * Similar to {@link xdoclet.modules.ejb.intf.InterfaceTagsHandler#getComponentInterface}. Relies on the ejb.dao
32      * tag, which has the following relevant properties:
33      * <ul>
34      * <li> class: The fully qualified name of the DAO class - overrides all set patterns
35      * <li> pattern: The pattern to be used to determine the unqualified name of the DAO class
36      * <li> package: The package the DAO is to be placed in
37      * </ul>
38      *
39      *
40      * @param clazz Description of Parameter
41      * @return The DAO value
42      */

43     public static String JavaDoc getDaoClassFor(XClass clazz)
44     {
45         Log log = LogUtil.getLog(DaoTagsHandler.class, "getDaoClassFor");
46
47         String JavaDoc fileName = clazz.getContainingPackage().getName();
48         String JavaDoc daoPattern = null;
49
50         if (log.isDebugEnabled()) {
51             log.debug("dao for " + clazz.getQualifiedName());
52         }
53
54         daoPattern = getDaoClassPattern();
55
56         String JavaDoc daoClass = clazz.getDoc().getTagAttributeValue("ejb.dao", "class", false);
57
58         if (daoClass != null) {
59             return daoClass;
60         }
61
62         String JavaDoc ejbName = null;
63         String JavaDoc packagePattern = null;
64
65         if (daoPattern.indexOf("{0}") != -1) {
66             ejbName = MessageFormat.format(daoPattern, new Object JavaDoc[]{getShortEjbNameFor(clazz)});
67         }
68         else {
69             ejbName = daoPattern;
70         }
71
72         // Fix package name
73
fileName = choosePackage(fileName, packagePattern, DocletTask.getSubTaskName(DaoSubTask.class));
74         fileName += "." + ejbName;
75
76         return fileName;
77     }
78
79     /**
80      * Returns true if method should be added to the DAO, false otherwise. The method should be included if it has a
81      * dao.call tag, unless it's a finder or create method which has already been added automatically.
82      *
83      * @param method XMethod to be evaluated
84      * @return Whether to include in the DAO
85      * @exception XDocletException
86      */

87     public static boolean isDaoMethod(XMethod method) throws XDocletException
88     {
89         boolean include;
90         Log log = LogUtil.getLog(DaoTagsHandler.class, "isDaoMethod");
91
92         include = method.getDoc().hasTag("dao.call");
93         if (log.isDebugEnabled()) {
94             log.debug("method " + method.getName() + " has " + (include ? "a" : "no") + " dao.call tag");
95         }
96         if (HomeTagsHandler.isCreateMethod(method)) {
97             String JavaDoc createMethods = getTagValue(FOR_CLASS, "ejb.dao", "create-methods", null, "true", false, false);
98
99             log.debug("createMethods=" + createMethods);
100             if ("true".equals(createMethods)) {
101                 include = false;
102             }
103         }
104         if (HomeTagsHandler.isFinderMethod(method)) {
105             String JavaDoc finderMethods = getTagValue(FOR_CLASS, "ejb.dao", "finder-methods", null, "true", false, false);
106
107             log.debug("finderMethods=" + finderMethods);
108             if ("true".equals(finderMethods)) {
109                 include = false;
110             }
111         }
112         return include;
113     }
114
115     /**
116      * Gets the DaoClassPattern attribute of the DaoTagsHandler class
117      *
118      * @return The DaoClassPattern value
119      */

120     protected static String JavaDoc getDaoClassPattern()
121     {
122         DaoSubTask daoSubtask = ((DaoSubTask) DocletContext.getInstance().getSubTaskBy(DocletTask.getSubTaskName(DaoSubTask.class)));
123
124         if (daoSubtask != null) {
125             return daoSubtask.getDaoClassPattern();
126         }
127         else {
128             return DaoSubTask.DEFAULT_DAO_CLASS_PATTERN;
129         }
130     }
131
132     /**
133      * Gets the DaoSubTaskActive attribute of the DaoTagsHandler class
134      *
135      * @return The DaoSubTaskActive value
136      */

137     private static boolean isDaoSubTaskActive()
138     {
139         return DocletContext.getInstance().isSubTaskDefined(DocletTask.getSubTaskName(DaoSubTask.class));
140     }
141
142     /**
143      * Returns the full qualified dao class name for the bean
144      *
145      * @param attributes The attributes of the template tag
146      * @return DAO class name
147      * @exception XDocletException
148      * @doc.tag type="content"
149      */

150     public String JavaDoc daoClass(Properties JavaDoc attributes) throws XDocletException
151     {
152         return getDaoClassFor(getCurrentClass());
153     }
154
155     /**
156      * Evaluate the body block if ejb.dao tag present and DAO subtask being used.
157      *
158      * @param template
159      * @exception XDocletException
160      * @doc.tag type="block"
161      */

162     public void ifUsingDao(String JavaDoc template) throws XDocletException
163     {
164         if (isDaoSubTaskActive() && getCurrentClass().getDoc().hasTag("ejb.dao", false)) {
165             generate(template);
166         }
167     }
168
169     /**
170      * Evaluates the body block if current method is a DAO method.
171      *
172      * @param template The body of the block tag
173      * @exception XDocletException
174      * @see #isDaoMethod(xjavadoc.XMethod)
175      * @doc.tag type="block"
176      */

177     public void ifDaoMethod(String JavaDoc template) throws XDocletException
178     {
179         if (isDaoMethod(getCurrentMethod())) {
180             generate(template);
181         }
182     }
183
184 }
185
Popular Tags