KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > wsee > WseeTagsHandler


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

5 package xdoclet.modules.wsee;
6
7 import java.io.File JavaDoc;
8 import java.text.MessageFormat JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Properties JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15 import xjavadoc.XClass;
16 import xjavadoc.XPackage;
17 import xjavadoc.XTag;
18
19 import xdoclet.ConfigParamIntrospector;
20 import xdoclet.DocletContext;
21 import xdoclet.DocletSupport;
22 import xdoclet.XDocletException;
23
24 import xdoclet.XDocletTagSupport;
25 import xdoclet.tagshandler.PackageTagsHandler;
26
27 /**
28  * Tags handler for dealing with wsee extensions.
29  *
30  * @author Christoph G. Jung (christoph.jung@infor.de)
31  * @author Jason Essington (jasone@greenrivercomputing.com)
32  * @created 23.12.03
33  * @version $Revision: 1.5 $
34  * @xdoclet.taghandler namespace="Wsee"
35  */

36
37 public class WseeTagsHandler extends XDocletTagSupport
38 {
39     /**
40      * name of javadoc tag indicating a port component
41      */

42     public final static String JavaDoc PORT_COMPONENT = "wsee.port-component";
43     /**
44      * name of javadoc tag indicating a handler
45      */

46     public final static String JavaDoc HANDLER = "wsee.handler";
47
48     /**
49      * state
50      */

51     public XTag currentHandler;
52
53     /**
54      * check whether the given class is a port component
55      *
56      * @param clazz class
57      * @return
58      * @exception XDocletException
59      */

60     public static boolean isPortComponent(XClass clazz)
61          throws XDocletException
62     {
63         return clazz.getDoc().hasTag(PORT_COMPONENT);
64     }
65
66     /**
67      * Gets the namespace for the specified package.
68      *
69      * @param pak package
70      * @return namespace
71      * @see #getNamespaceForPackage(java.lang.String)
72      */

73     public static String JavaDoc getNamespaceForPackage(XPackage pak)
74     {
75         return getNamespaceForPackage(pak.getName());
76     }
77
78     /**
79      * Gets the namespace for the specified package.
80      *
81      * @param pak package
82      * @return namespace
83      */

84     public static String JavaDoc getNamespaceForPackage(String JavaDoc pak)
85     {
86         List JavaDoc packageSubstitutions = getPackageNamespaceMappings();
87
88         for (int i = 0; i < packageSubstitutions.size(); i++) {
89             WseeDocletTask.PackageNamespaceMapping ps = (WseeDocletTask.PackageNamespaceMapping) packageSubstitutions.get(i);
90             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(ps.getPackages(), ",", false);
91
92             while (st.hasMoreTokens()) {
93                 String JavaDoc packages = st.nextToken();
94
95                 if (pak.startsWith(packages)) {
96                     return ps.getNamespace() + pak.substring(packages.length()).replace('.', '-');
97                 }
98             }
99         }
100
101         return "urn-" + pak.replace('.', '-');
102     }
103
104     /**
105      * Gets the package-namespace mappings for the subtask.
106      *
107      * @return List of packageNamespaceMapping config params
108      */

109     public static List JavaDoc getPackageNamespaceMappings()
110     {
111         // SubTask's packageNamespaceMappings has precedence over
112
// the global packageNamespaceMappings defined in DocletTask
113
List JavaDoc packageNamespaceMappings = (List JavaDoc) DocletContext.getInstance().getConfigParam("packageNamespaceMappings");
114
115         return packageNamespaceMappings;
116     }
117
118     /**
119      * return the namespace of the specified class
120      *
121      * @param clazz class
122      * @return namespace URI
123      * @exception XDocletException
124      */

125     public String JavaDoc getNamespaceURI(XClass clazz)
126          throws XDocletException
127     {
128         XTag wsTag = clazz.getDoc().getTag("wsee.port-component");
129         String JavaDoc nameSpace = null;
130
131         if (wsTag == null) {
132             wsTag = clazz.getDoc().getTag("wsee.jaxrpc-mapping");
133         }
134         if (wsTag != null) {
135             nameSpace = wsTag.getAttributeValue("namespace-uri");
136         }
137         if (nameSpace == null) {
138             nameSpace = getNamespaceForPackage(clazz.getContainingPackage());
139         }
140         return nameSpace;
141     }
142
143     /**
144      * Iterates over all classes loaded by javadoc and being a port component.
145      *
146      * @param template The body of the block tag
147      * @param attributes The attributes of the template tag
148      * @exception XDocletException Description of Exception
149      * @doc.tag type="block"
150      * @doc.param name="abstract" optional="true" values="true,false" description="If true then accept
151      * abstract classes also; otherwise don't."
152      * @doc.param name="type" optional="true" description="For all classes by the type."
153      */

154     public void forAllPortComponents(String JavaDoc template, Properties JavaDoc attributes)
155          throws XDocletException
156     {
157         Collection JavaDoc classes = getXJavaDoc().getSourceClasses();
158
159         for (Iterator JavaDoc i = classes.iterator(); i.hasNext(); ) {
160             XClass clazz = (XClass) i.next();
161
162             setCurrentClass(clazz);
163
164             if (DocletSupport.isDocletGenerated(getCurrentClass())) {
165                 continue;
166             }
167
168             if (isPortComponent(getCurrentClass())) {
169                 generate(template);
170             }
171         }
172     }
173
174     /**
175      * returns the service endpoint interface name belonging to the current class
176      *
177      * @param props The attributes of the template tag
178      * @return
179      * @exception XDocletException
180      * @doc.tag type="content"
181      */

182     public String JavaDoc serviceEndpoint(Properties JavaDoc props) throws XDocletException
183     {
184         XClass clazz = getCurrentClass();
185         String JavaDoc pkg = PackageTagsHandler.getPackageNameFor(clazz.getContainingPackage(), true);
186         XTag ejbTag = null;
187         String JavaDoc spec = null;
188
189         if (clazz.getDoc().hasTag("ejb.bean")) {
190             ejbTag = clazz.getDoc().getTag("ejb.interface");
191             if (ejbTag != null) {
192                 spec = ejbTag.getAttributeValue("service-endpoint-class");
193             }
194             // if we haven't explicitly defined a service interface name, try to build the default name.
195
if (spec == null || "".equals(spec)) {
196                 spec = pkg + "." + clazz.getName();
197                 if (spec.endsWith("Bean"))
198                     spec = spec.substring(0, spec.length() - 4);
199             }
200         }
201         else {
202
203             ejbTag = clazz.getDoc().getTag("web.servlet");
204             if (ejbTag != null) {
205                 spec = ejbTag.getAttributeValue("service-endpoint-class");
206             }
207             if (spec == null || "".equals(spec)) {
208                 spec = pkg + "." + clazz.getName();
209                 spec += "Service";
210             }
211         }
212
213         return spec;
214     }
215
216     /**
217      * returns the service endpoint link pointing to the current class
218      *
219      * @param props The attributes of the template tag
220      * @return
221      * @exception XDocletException
222      * @doc.tag type="content"
223      */

224     public String JavaDoc serviceEndpointLink(Properties JavaDoc props)
225          throws XDocletException
226     {
227         XClass clazz = getCurrentClass();
228         XTag ejbTag = clazz.getDoc().getTag("ejb.bean");
229
230         if (ejbTag != null) {
231             return "<ejb-link>" + ejbTag.getAttributeValue("name") + "</ejb-link>";
232         }
233         ejbTag = clazz.getDoc().getTag("web.servlet");
234         if (ejbTag != null) {
235             return "<servlet-link>"
236                 + ejbTag.getAttributeValue("name")
237                 + "</servlet-link>";
238         }
239         return null;
240     }
241
242     /**
243      * return the namespace of the current clazz/package
244      *
245      * @return
246      * @exception XDocletException
247      * @doc.tag type="content"
248      */

249     public String JavaDoc namespaceURI()
250          throws XDocletException
251     {
252         String JavaDoc ns = "";
253
254         if (getCurrentClass() != null) {
255             ns = getNamespaceURI(getCurrentClass());
256         }
257         else if (getCurrentPackage() != null) {
258             ns = getNamespaceForPackage(getCurrentPackage());
259         }
260         else {
261             // we don't have a current package or class, so just get the first namespace.
262
List JavaDoc nsmappings = getPackageNamespaceMappings();
263
264             if (!nsmappings.isEmpty()) {
265                 WseeDocletTask.PackageNamespaceMapping ps = (WseeDocletTask.PackageNamespaceMapping) nsmappings.get(0);
266
267                 ns = ps.getNamespace();
268             }
269         }
270         return ns;
271     }
272
273     /**
274      * Iterates over all handler tags annotating the current class.
275      *
276      * @param template The body of the block tag
277      * @param attributes The attributes of the template tag
278      * @throws XDocletException
279      * @doc.tag type="block"
280      */

281     public void forAllHandlers(String JavaDoc template, Properties JavaDoc attributes)
282          throws XDocletException
283     {
284         XClass clazz = getCurrentClass();
285         Iterator JavaDoc allTags = clazz.getDoc().getTags(HANDLER).iterator();
286
287         while (allTags.hasNext()) {
288             currentHandler = (XTag) allTags.next();
289             generate(template);
290         }
291
292     }
293
294     /**
295      * conditional checking presence of a handler tag
296      *
297      * @param template The body of the block tag
298      * @param props The attributes of the template tag
299      * @exception XDocletException
300      * @doc.tag type="block"
301      * @doc.param name="paramName" optional="false" description="The name of the parameter"
302      */

303     public void ifHasHandlerTag(String JavaDoc template, Properties JavaDoc props)
304          throws XDocletException
305     {
306         if (handlerTagValue(props) != null) {
307             generate(template);
308         }
309     }
310
311     /**
312      * extract the value of the current handler tag
313      *
314      * @param props The attributes of the template tag
315      * @return
316      * @exception XDocletException
317      * @doc.tag type="content"
318      * @doc.param name="paramName" optional="false" description="The name of the parameter"
319      */

320     public String JavaDoc handlerTagValue(Properties JavaDoc props) throws XDocletException
321     {
322         return currentHandler.getAttributeValue(props.getProperty("paramName"));
323     }
324
325     /**
326      * conditional to handle per class wsdl
327      *
328      * @param template The body of the block tag
329      * @param props The attributes of the template tag
330      * @throws XDocletException
331      * @doc.tag type="block"
332      */

333     public void ifWsdlPerClass(String JavaDoc template, Properties JavaDoc props) throws XDocletException
334     {
335         if (isWsdlPerClass())
336             generate(template);
337     }
338
339     /**
340      * conditional to handle single wsdl generation
341      *
342      * @param template The body of the block tag
343      * @param props The attributes of the template tag
344      * @throws XDocletException
345      * @doc.tag type="block"
346      */

347     public void ifNotWsdlPerClass(String JavaDoc template, Properties JavaDoc props) throws XDocletException
348     {
349         if (!isWsdlPerClass())
350             generate(template);
351     }
352
353     /**
354      * Constructs a guestimated filename for the wsdl file. It also attempts to decide if the file should be in META-INF
355      * or WEB-INF. This should yield a filename that will be correct for use within the webservices.xml file.
356      *
357      * @param props If prefixWithPackageStructure is specified for the wsdl sub task, the property
358      * prefixWithPackage="true" will need to be specified.
359      * @return filename
360      * @doc.tag type="content"
361      * @doc.param name="prefixWithPackage" optional="true" values="true,false" description="Whether to prefix the
362      * filename with the package hierarchy."
363      */

364     public String JavaDoc wsdlFilename(Properties JavaDoc props)
365     {
366         XClass clazz = getCurrentClass();
367         String JavaDoc wsdlPattern = getWsdlFilePattern();
368
369         String JavaDoc packageName = null;
370         String JavaDoc file = null;
371
372         if (isWsdlPerClass()) {
373
374             boolean prefixWithPackage = false;
375             String JavaDoc hasPrefix = props.getProperty("prefixWithPackage");
376
377             if (hasPrefix != null && !"".equals(hasPrefix)) {
378                 prefixWithPackage = Boolean.getBoolean(hasPrefix);
379             }
380
381             if (prefixWithPackage) {
382                 packageName = PackageTagsHandler.packageNameAsPathWithoutSubstitutionFor(clazz.getContainingPackage());
383             }
384
385             String JavaDoc serviceName = getCurrentClass().getDoc().getTagAttributeValue(WseeTagsHandler.PORT_COMPONENT, "name");
386
387             file = new File JavaDoc(packageName, serviceName).toString();
388         }
389
390         // assume our wsdl files will start in WEB-INF/ unless the current class has an ejb.bean tag
391
String JavaDoc prefix = "WEB-INF/";
392
393         if (clazz != null && clazz.getDoc().hasTag("ejb.bean")) {
394             prefix = "META-INF/";
395         }
396         return prefix + MessageFormat.format(wsdlPattern, new Object JavaDoc[]{file});
397     }
398
399     /**
400      * Constructs a guestimated filename for the jaxrpc file.
401      *
402      * @param props If prefixWithPackageStructure is specified for the wsdl sub task, the property
403      * prefixWithPackage="true" will need to be specified.
404      * @return filename
405      * @doc.tag type="content"
406      * @doc.param name="prefixWithPackage" optional="true" values="true,false" description="Whether to prefix the
407      * filename with the package hierarchy."
408      */

409     public String JavaDoc jaxrpcMappingFilename(Properties JavaDoc props)
410     {
411         XClass clazz = getCurrentClass();
412         String JavaDoc jaxrpcPattern = getJaxrpcFilePattern();
413
414         String JavaDoc packageName = null;
415         String JavaDoc file = null;
416
417         if (isJaxrpcPerClass()) {
418
419             boolean prefixWithPackage = true;
420             String JavaDoc hasPrefix = props.getProperty("prefixWithPackage");
421
422             if (hasPrefix != null && !"".equals(hasPrefix)) {
423                 prefixWithPackage = Boolean.getBoolean(hasPrefix);
424             }
425
426             if (prefixWithPackage) {
427                 packageName = PackageTagsHandler.packageNameAsPathWithoutSubstitutionFor(clazz.getContainingPackage());
428             }
429
430             file = new File JavaDoc(packageName, getCurrentClass().getName()).toString();
431         }
432
433
434         // assume our wsdl files will start in WEB-INF/ unless the current class has an ejb.bean tag
435
String JavaDoc prefix = "WEB-INF/";
436
437         if (clazz != null && clazz.getDoc().hasTag("ejb.bean")) {
438             prefix = "META-INF/";
439         }
440         return prefix + MessageFormat.format(jaxrpcPattern, new Object JavaDoc[]{file});
441     }
442
443     /**
444      * Is wsdl generation by class or as a single file?
445      *
446      * @return true if by class
447      */

448     protected boolean isWsdlPerClass()
449     {
450         return getWsdlFilePattern().indexOf("{0}") != -1;
451     }
452
453     /**
454      * Is jaxrpc generation by class or as a single file?
455      *
456      * @return true if by class
457      */

458     protected boolean isJaxrpcPerClass()
459     {
460         return getJaxrpcFilePattern().indexOf("{0}") != -1;
461     }
462
463     /**
464      * Get the value of the wsdl file pattern
465      *
466      * @return pattern
467      */

468     protected String JavaDoc getWsdlFilePattern()
469     {
470         String JavaDoc pattern = null;
471         Object JavaDoc wsdlFile = DocletContext.getInstance().getConfigParam("wsdlFile");
472
473         if (wsdlFile == ConfigParamIntrospector.NULL || "".equals(wsdlFile)) {
474             pattern = WsdlSubTask.DEFAULT_WSDL_FILE_PATTERN;
475         }
476         else {
477             pattern = (String JavaDoc) wsdlFile;
478         }
479         return pattern;
480     }
481
482     /**
483      * Get the value of the jaxrpc file pattern
484      *
485      * @return pattern
486      */

487     protected String JavaDoc getJaxrpcFilePattern()
488     {
489         String JavaDoc pattern = "";
490         Object JavaDoc jaxrpcFile = DocletContext.getInstance().getConfigParam("jaxrpcMappingFile");
491
492         if (jaxrpcFile != ConfigParamIntrospector.NULL) {
493             pattern = (String JavaDoc) jaxrpcFile;
494         }
495         return pattern;
496     }
497 }
498
Popular Tags