KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > web > WebTagsHandler


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

5 package xdoclet.modules.web;
6
7 import java.text.MessageFormat JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Properties JavaDoc;
10 import java.util.StringTokenizer JavaDoc;
11 import xjavadoc.XClass;
12 import xjavadoc.XTag;
13
14 import xdoclet.DocletContext;
15 import xdoclet.DocletTask;
16 import xdoclet.XDocletException;
17
18 import xdoclet.XDocletTagSupport;
19 import xdoclet.tagshandler.ClassTagsHandler;
20 import xdoclet.tagshandler.PackageTagsHandler;
21
22 /**
23  * @author Marcus Brito (pazu@animegaiden.com.br)
24  * @author Christoph G. Jung (christoph.jung@infor.de)
25  * @created Jun 28, 2002
26  * @version $Revision: 1.4 $
27  * @xdoclet.taghandler namespace="Web"
28  */

29 public class WebTagsHandler extends XDocletTagSupport
30 {
31
32     /**
33      * Returns short version of the servlet name of the clazz.
34      *
35      * @param clazz the class we want its short servlet name
36      * @return The shortName value
37      * @see #shortName()
38      */

39     public static String JavaDoc shortServletName(XClass clazz)
40     {
41         // Find the last part of the name
42
StringTokenizer JavaDoc ejbNameTokens = new StringTokenizer JavaDoc(servletName(clazz), ":./\\-");
43         String JavaDoc name;
44
45         do {
46             name = ejbNameTokens.nextToken();
47         } while (ejbNameTokens.hasMoreTokens());
48
49         return name;
50     }
51
52     public static String JavaDoc serviceEndpoint(XClass clazz) throws XDocletException
53     {
54         String JavaDoc fileName = clazz.getContainingPackage().getName();
55         String JavaDoc name_pattern = null;
56         String JavaDoc package_pattern = null;
57         String JavaDoc component_interface = null;
58
59         component_interface = clazz.getDoc().getTagAttributeValue(ServiceEndpointSubTask.WEB_SERVLET, ServiceEndpointSubTask.SERVICE_ENDPOINT + "-class");
60         if (component_interface != null) {
61             return component_interface;
62         }
63
64         name_pattern = clazz.getDoc().getTagAttributeValue(ServiceEndpointSubTask.WEB_SERVLET, ServiceEndpointSubTask.SERVICE_ENDPOINT + "-pattern");
65         if (name_pattern == null) {
66             ServiceEndpointSubTask seintf_subtask = ((ServiceEndpointSubTask) DocletContext.getInstance().
67                 getSubTaskBy(DocletTask.getSubTaskName(ServiceEndpointSubTask.class)));
68
69             if (seintf_subtask != null) {
70                 name_pattern = seintf_subtask.getServiceEndpointClassPattern();
71             }
72             else {
73                 name_pattern = ServiceEndpointSubTask.DEFAULT_SERVICE_ENDPOINT_CLASS_PATTERN;
74             }
75         }
76
77         package_pattern = clazz.getDoc().getTagAttributeValue(ServiceEndpointSubTask.WEB_SERVLET, ServiceEndpointSubTask.SERVICE_ENDPOINT + "-package");
78
79         String JavaDoc ejb_name = null;
80
81         if (name_pattern.indexOf("{0}") != -1) {
82             ejb_name = MessageFormat.format(name_pattern, new Object JavaDoc[]{shortServletName(clazz)});
83         }
84         else {
85             ejb_name = name_pattern;
86         }
87
88         String JavaDoc subtask_name = null;
89
90         // Fix package name
91
fileName = choosePackage(fileName, package_pattern, subtask_name);
92         fileName += "." + ejb_name;
93         return fileName;
94     }
95
96     public static String JavaDoc serviceEndpoint(Properties JavaDoc properties) throws XDocletException
97     {
98         XClass clazz = getCurrentClass();
99
100         return serviceEndpoint(clazz);
101     }
102
103     public static String JavaDoc servletName(Properties JavaDoc properties)
104     {
105         return servletName(getCurrentClass());
106     }
107
108     public static String JavaDoc servletName(XClass clazz)
109     {
110         XTag beanTag = clazz.getDoc().getTag(ServiceEndpointSubTask.WEB_SERVLET);
111         String JavaDoc paramValue = null;
112
113         if (beanTag != null) {
114             paramValue = beanTag.getAttributeValue("name");
115         }
116
117         if (paramValue == null) {
118             paramValue = clazz.getName();
119         }
120         return paramValue;
121     }
122
123     public static String JavaDoc choosePackage(String JavaDoc packageName, String JavaDoc packagePattern, String JavaDoc subtask)
124     {
125
126         ArrayList JavaDoc packageSubstitutions = PackageTagsHandler.getPackageSubstitutions(subtask);
127
128         if (packagePattern != null) {
129             // later we may do some parametric {0} fancy stuff here
130
return packagePattern;
131         }
132         else {
133             for (int i = 0; i < packageSubstitutions.size(); i++) {
134                 PackageTagsHandler.PackageSubstitution ps = (PackageTagsHandler.PackageSubstitution) packageSubstitutions.get(i);
135                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ps.getPackages(), ",", false);
136
137                 while (st.hasMoreTokens()) {
138                     String JavaDoc packages = st.nextToken();
139                     String JavaDoc suffix = "." + packages;
140
141                     if (packageName.endsWith(suffix)) {
142                         packageName = packageName.substring(0, packageName.length() - suffix.length()) + '.' + ps.getSubstituteWith();
143                         break;
144                     }
145                 }
146             }
147         }
148
149         return packageName;
150     }
151
152     /**
153      * Process the tag body for each web:ejb-ref tag in all source files. Please note that this tag already iterates
154      * over all available sources; it should <em>not</em> be enclosed by a &lt;XDtClass:forAllClasses> tag or any other
155      * that process classes. This tag does not process tags with duplicated name attributes. If such tags occurs, only
156      * the first tag is processed, and further tags will only emit a warning message.
157      *
158      * @param template The body of the block tag
159      * @throws XDocletException if something goes wrong
160      * @doc.tag type="block"
161      */

162     public void forAllEjbRefs(String JavaDoc template) throws XDocletException
163     {
164         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:ejb-ref", "name");
165     }
166
167     /**
168      * Process the tag body for each web:ejb-local-ref tag in all source files. Look at forAllEjbRefs for some notes
169      * about the behavior of this tag.
170      *
171      * @param template The body of the blocktag
172      * @throws XDocletException if something goes wrong
173      * @doc.tag type="block"
174      */

175     public void forAllEjbLocalRefs(String JavaDoc template) throws XDocletException
176     {
177         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:ejb-local-ref", "name");
178     }
179
180     /**
181      * Process the tag body for each web:resource-ref tag in all source files. Look at forAllEjbRefs for some notes
182      * about the behavior of this tag.
183      *
184      * @param template The body of the blocktag
185      * @throws XDocletException if something goes wrong
186      * @doc.tag type="block"
187      */

188     public void forAllResourceRefs(String JavaDoc template) throws XDocletException
189     {
190         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:resource-ref", "name");
191     }
192
193     /**
194      * Process the tag body for each web:resource-env-ref tag in all source files. Look at forAllEjbRefs for some notes
195      * about the behavior of this tag.
196      *
197      * @param template The body of the blocktag
198      * @throws XDocletException if something goes wrong
199      * @doc.tag type="block"
200      */

201     public void forAllResourceEnvRefs(String JavaDoc template) throws XDocletException
202     {
203         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:resource-env-ref", "name");
204     }
205
206     /**
207      * Process the tag body for each web:security-role tag in all source files. Look at forAllEjbRefs for some notes
208      * about the behavior of this tag.
209      *
210      * @param template The body of the blocktag
211      * @throws XDocletException if something goes wrong
212      * @doc.tag type="block"
213      */

214     public void forAllSecurityRoles(String JavaDoc template) throws XDocletException
215     {
216         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:security-role", "role-name");
217     }
218
219     /**
220      * Process the tag body for each web:env-entry tag in all source files. Look at forAllEjbRefs for some notes about
221      * the behavior of this tag.
222      *
223      * @param template The body of the blocktag
224      * @throws XDocletException if something goes wrong
225      * @doc.tag type="block"
226      */

227     public void forAllEnvEntries(String JavaDoc template) throws XDocletException
228     {
229         ClassTagsHandler.forAllDistinctClassTags(getEngine(), template, "web:env-entry", "name");
230     }
231
232     /**
233      * Returns the name of the class home interface extends.
234      *
235      * @param attributes
236      * @return The name of generated PK class.
237      * @exception XDocletException
238      * @doc.tag type="content"
239      */

240     public String JavaDoc extendsFrom(Properties JavaDoc attributes) throws XDocletException
241     {
242         String JavaDoc def_base_class_name = getTagValue(
243             FOR_CLASS,
244             getCurrentClass().getDoc(),
245             ServiceEndpointSubTask.WEB_SERVLET,
246             "service-endpoint-extends",
247             null,
248             "java.rmi.Remote",
249             true,
250             false);
251
252         return def_base_class_name;
253     }
254
255 }
256
Popular Tags