KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > mvcsoft > ejb > MVCSoftTagsHandler


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

5 package xdoclet.modules.mvcsoft.ejb;
6
7 import java.util.*;
8
9 import xjavadoc.*;
10
11 import xdoclet.XDocletException;
12 import xdoclet.tagshandler.ClassTagsHandler;
13
14 import xdoclet.util.Translator;
15
16 /**
17  * @author Daniel OConnor (docodan@mvcsoft.com)
18  * @created November 1, 2001
19  * @xdoclet.taghandler namespace="MVCSoft"
20  */

21 public class MVCSoftTagsHandler extends ClassTagsHandler
22 {
23     protected static XParameter currentQueryMethodParameter;
24
25     protected static StringTokenizer currentAliases;
26
27     protected static String JavaDoc currentRoleName;
28
29     protected static String JavaDoc currentFieldName;
30
31     protected static String JavaDoc currentColName;
32
33     protected static String JavaDoc nested;
34     private final static String JavaDoc CMP_FIELDS = "CMP Fields";
35
36     /**
37      * Describe what the method does
38      *
39      * @return Describe the return value
40      * @exception XDocletException Describe the exception
41      */

42     public String JavaDoc methodParamType() throws XDocletException
43     {
44         StringBuffer JavaDoc type = new StringBuffer JavaDoc(currentQueryMethodParameter.getType().getQualifiedName());
45
46         for (int iter = 0; iter < currentQueryMethodParameter.getDimension(); iter++) {
47             type.append("[]");
48         }
49         return type.toString();
50     }
51
52     /**
53      * Describe what the method does
54      *
55      * @param template Describe what the parameter does
56      * @exception XDocletException Describe the exception
57      */

58     public void forAllQueryMethodParams(String JavaDoc template) throws XDocletException
59     {
60         List parameters = Arrays.asList(getCurrentMethod().getParameters().toArray());
61
62         for (int i = 0; i < (parameters.size() - 2); i++) {
63             currentQueryMethodParameter = (XParameter) parameters.get(i);
64
65             generate(template);
66         }
67     }
68
69     /**
70      * Describe what the method does
71      *
72      * @return Describe the return value
73      * @exception XDocletException Describe the exception
74      */

75     public String JavaDoc fieldName() throws XDocletException
76     {
77         String JavaDoc token = currentToken(new Properties());
78         int idx = token.indexOf(' ');
79
80         if (idx == -1) {
81             return token;
82         }
83         else {
84             return token.substring(0, idx).trim();
85         }
86     }
87
88     /**
89      * Describe what the method does
90      *
91      * @return Describe the return value
92      * @exception XDocletException Describe the exception
93      */

94     public String JavaDoc sortDirection() throws XDocletException
95     {
96         String JavaDoc token = currentToken(new Properties()).trim();
97         int idx = token.indexOf(' ');
98
99         if (idx == -1) {
100             return "";
101         }
102         else {
103             String JavaDoc sort = token.substring(idx, token.length()).trim();
104
105             if (sort.equalsIgnoreCase("desc")) {
106                 return "Descending";
107             }
108             else {
109                 return "Ascending";
110             }
111         }
112     }
113
114     /**
115      * Describe what the method does
116      *
117      * @param template Describe what the parameter does
118      * @exception XDocletException Describe the exception
119      */

120     public void ifHasSortDirection(String JavaDoc template) throws XDocletException
121     {
122         if (!sortDirection().equals("")) {
123             generate(template);
124         }
125     }
126
127     /**
128      * Describe what the method does
129      *
130      * @param template Describe what the parameter does
131      * @exception XDocletException Describe the exception
132      */

133     public void forEachRoleMapping(String JavaDoc template) throws XDocletException
134     {
135         String JavaDoc aliases = getKeyAliases();
136
137         if (aliases == null) {
138             return;
139         }
140
141         String JavaDoc leftRole = null;
142         String JavaDoc rightRole = null;
143         int idx = aliases.indexOf(';');
144
145         if (idx == -1) {
146             leftRole = aliases;
147         }
148         else {
149             leftRole = aliases.substring(0, idx);
150             rightRole = aliases.substring(idx + 1);
151         }
152         doForExtendedRole(leftRole, template);
153         if (rightRole != null) {
154             doForExtendedRole(rightRole, template);
155         }
156     }
157
158     /**
159      * Describe what the method does
160      *
161      * @param template Describe what the parameter does
162      * @exception XDocletException Describe the exception
163      */

164     public void forSingleRoleMapping(String JavaDoc template) throws XDocletException
165     {
166         String JavaDoc aliases = getKeyAliases();
167
168         if (aliases == null) {
169             return;
170         }
171         currentAliases = new StringTokenizer(aliases, ",", false);
172         generate(template);
173     }
174
175     /**
176      * Describe what the method does
177      *
178      * @param template Describe what the parameter does
179      * @exception XDocletException Describe the exception
180      */

181     public void forAllAliases(String JavaDoc template) throws XDocletException
182     {
183         while (currentAliases.hasMoreTokens()) {
184             String JavaDoc token = currentAliases.nextToken();
185             int idx = token.indexOf('=');
186
187             if (idx == -1) {
188                 throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.ALIAS_FORM));
189             }
190             currentFieldName = token.substring(0, idx).trim();
191             currentColName = token.substring(idx + 1).trim();
192             generate(template);
193         }
194     }
195
196     /**
197      * Describe what the method does
198      *
199      * @return Describe the return value
200      * @exception XDocletException Describe the exception
201      */

202     public String JavaDoc roleName() throws XDocletException
203     {
204         return currentRoleName;
205     }
206
207     /**
208      * Describe what the method does
209      *
210      * @return Describe the return value
211      * @exception XDocletException Describe the exception
212      */

213     public String JavaDoc keyFieldName() throws XDocletException
214     {
215         return currentFieldName;
216     }
217
218     /**
219      * Describe what the method does
220      *
221      * @return Describe the return value
222      * @exception XDocletException Describe the exception
223      */

224     public String JavaDoc colName() throws XDocletException
225     {
226         return currentColName;
227     }
228
229     // 1.1 features
230

231     public void forNestedFaultGroups(String JavaDoc template) throws XDocletException
232     {
233         Properties prop = new Properties();
234
235         prop.setProperty("tagName", "mvcsoft:fault-group");
236         prop.setProperty("paramName", "nested");
237         nested = classTagValue(prop);
238         //"mvcsoft:fault-group", "nested", 0, null, null, true, false );
239
generate(template);
240     }
241
242     public String JavaDoc nestedFaultGroups() throws XDocletException
243     {
244         if ((nested == null) || nested.equals(""))
245             return "";
246         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
247         Map map = parseNested(nested);
248         Iterator iterator = map.keySet().iterator();
249
250         while (iterator.hasNext()) {
251             String JavaDoc key = (String JavaDoc) iterator.next();
252
253             if (key.equals(CMP_FIELDS))
254                 continue;
255             buffer.append(getFaultGroupRelationshipLinks(key, (Map) map.get(key)));
256         }
257         return buffer.toString();
258     }
259
260     /**
261      * Gets the KeyAliases attribute of the MVCSoftTagsHandler object
262      *
263      * @return The KeyAliases value
264      */

265     private String JavaDoc getKeyAliases()
266     {
267         return getCurrentMethod().getDoc().getTagAttributeValue("mvcsoft:relation", "key-aliases", false);
268     }
269
270     private Map getSubMap(Map mapNested, String JavaDoc cmrPath)
271     {
272         StringTokenizer pathTokenizer = new StringTokenizer(cmrPath, ".", false);
273
274         while (pathTokenizer.hasMoreTokens()) {
275             String JavaDoc token = pathTokenizer.nextToken();
276             Map mapNew = (Map) mapNested.get(token);
277
278             if (mapNew == null) {
279                 mapNew = new HashMap();
280                 mapNested.put(token, mapNew);
281             }
282             mapNested = mapNew;
283         }
284         return mapNested;
285     }
286
287     private String JavaDoc getFaultGroupRelationshipLinks(String JavaDoc cmrField, Map mapNested)
288     {
289         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("<fault-group-relationship-link>");
290
291         buffer.append("<cmr-field>");
292         buffer.append(cmrField);
293         buffer.append("</cmr-field>");
294         buffer.append("<loading-strategy>SeparateQuery</loading-strategy>");
295
296         List cmpFields = (List) mapNested.get(CMP_FIELDS);
297
298         if (cmpFields != null) {
299             for (Iterator iterCmp = cmpFields.iterator(); iterCmp.hasNext(); ) {
300                 String JavaDoc cmp = (String JavaDoc) iterCmp.next();
301
302                 buffer.append("<field-name>");
303                 buffer.append(cmp);
304                 buffer.append("</field-name>");
305             }
306         }
307
308         Iterator iterator = mapNested.keySet().iterator();
309
310         while (iterator.hasNext()) {
311             String JavaDoc key = (String JavaDoc) iterator.next();
312
313             if (key.equals(CMP_FIELDS))
314                 continue;
315             buffer.append(getFaultGroupRelationshipLinks(key, (Map) mapNested.get(key)));
316         }
317         buffer.append("</fault-group-relationship-link>");
318         return buffer.toString();
319     }
320
321     /**
322      * Describe what the method does
323      *
324      * @param token Describe what the parameter does
325      * @param template Describe what the parameter does
326      * @exception XDocletException Describe the exception
327      */

328     private void doForExtendedRole(String JavaDoc token, String JavaDoc template) throws XDocletException
329     {
330         int idx = token.indexOf(':');
331
332         if (idx == -1) {
333             throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.KEY_ALIAS_FORM));
334         }
335
336         currentRoleName = token.substring(0, idx);
337         currentAliases = new StringTokenizer(token.substring(idx + 1), ",", false);
338         generate(template);
339     }
340
341     private Map parseNested(String JavaDoc nestedFaultGroups)
342          throws XDocletException
343     {
344         HashMap mapNested = new HashMap();
345         StringTokenizer tokenizer = new StringTokenizer(nestedFaultGroups, ";", false);
346
347         while (tokenizer.hasMoreTokens()) {
348             String JavaDoc token = tokenizer.nextToken();
349             int idx = token.indexOf(":");
350
351             if (idx == -1) {
352                 throw new XDocletException(Translator.getString(XDocletModulesMvcsoftEjbMessages.class, XDocletModulesMvcsoftEjbMessages.NESTED_FAULT_GROUP_FORM));
353             }
354
355             String JavaDoc cmrPath = token.substring(0, idx);
356             StringTokenizer cmpFields = new StringTokenizer(token.substring(idx + 1), ",", false);
357             Map subMap = getSubMap(mapNested, cmrPath);
358             List fields = new LinkedList();
359
360             while (cmpFields.hasMoreTokens()) {
361                 fields.add(cmpFields.nextToken());
362             }
363             subMap.put(CMP_FIELDS, fields);
364         }
365         return mapNested;
366     }
367 }
368
Popular Tags