KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > util > CollectionTagsHandler


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

5 package xdoclet.modules.util;
6
7 import java.util.*;
8
9 import xdoclet.*;
10 import xdoclet.util.Translator;
11
12 /**
13  * This taghandler puts together some utility tags useful during template processing. For now, contains only a mechanism
14  * to manage collections of strings
15  *
16  * @author Marcus Brito (pazu@animegaiden.com.br)
17  * @created 25 Jun 2002
18  * @xdoclet.taghandler namespace="Collection"
19  * @version $Revision: 1.2 $
20  */

21 public class CollectionTagsHandler extends XDocletTagSupport
22 {
23     // This will be a map of collections
24
private Map collections = new HashMap();
25
26     /**
27      * Obtains one value contained in the collection. This tag only apply to map valued collections, and an xdoclet
28      * exception will be throw if the specified collection is not a map.
29      *
30      * @param attributes The attributes of the template tag
31      * @return The requested value or null if no such value exists.
32      * @throws XDocletException if something goes wrong
33      * @doc.tag type="content"
34      * @doc.param name="key" optional="false" description="The collection to operate on."
35      * @doc.param name="name" optional="false" description="The key to retrive."
36      */

37     public String JavaDoc get(Properties attributes) throws XDocletException
38     {
39         String JavaDoc name = attributes.getProperty("name");
40         String JavaDoc key = attributes.getProperty("key");
41
42         // Makes sure name is specified
43
if (name == null || name.length() == 0) {
44             throw new XDocletException(Translator.getString(XDocletMessages.class,
45                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
46         }
47
48         // Makes key is specified
49
if (key == null || key.length() == 0) {
50             throw new XDocletException(Translator.getString(XDocletMessages.class,
51                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"key"}));
52         }
53
54         // Makes sure the name exists
55
if (!collections.containsKey(name)) {
56             throw new XDocletException(Translator.getString(CollectionMessages.class,
57                 CollectionMessages.COLLECTION_NOT_DEFINED, new String JavaDoc[]{name}));
58         }
59
60         // Make sure the name is a map
61
if (!(collections.get(name) instanceof Map)) {
62             throw new XDocletException(Translator.getString(CollectionMessages.class,
63                 CollectionMessages.COLLECTION_IS_NOT_MAP, new String JavaDoc[]{name}));
64         }
65
66         return (String JavaDoc) ((Map) collections.get(name)).get(key);
67     }
68
69
70     /**
71      * Creates a new utility collection that will store template data. If a collection with the specified name already
72      * exists, an XDocletException will be thrown.
73      *
74      * @param attributes The attributes of the template tag
75      * @throws XDocletException if something goes wrong
76      * @doc.tag type="content"
77      * @doc.param name="name" optional="false" description="The name for the newly created collection"
78      * @doc.param name="type" optional="true" values="map,set" description="The type of the collection to
79      * create. Default value is set"
80      */

81     public void create(Properties attributes) throws XDocletException
82     {
83         String JavaDoc name = attributes.getProperty("name");
84         String JavaDoc type = attributes.getProperty("type");
85
86         // Make sure name is specified
87
if (name == null || name.length() == 0) {
88             throw new XDocletException(Translator.getString(XDocletMessages.class,
89                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
90         }
91
92         // Prevent collection overwriting
93
if (collections.containsKey(name)) {
94             throw new XDocletException(Translator.getString(CollectionMessages.class,
95                 CollectionMessages.COLLECTION_ALREADY_EXISTS, new String JavaDoc[]{name}));
96         }
97
98         // Creates a new map or set, as requested
99
if ("map".equals(type))
100             collections.put(name, new HashMap());
101         else
102             collections.put(name, new HashSet());
103     }
104
105     /**
106      * Puts a new element into the specified collection. If the collection is a set, only the 'name' and 'value'
107      * attributes should be specified. If the collection is a map, the 'key' value should also be specified. If the
108      * 'key' is specified and the collection is a set, or if 'key' is not specified and the collection is a map, an
109      * XDocletException will be thrown.
110      *
111      * @param attributes The attributes of the template tag
112      * @throws XDocletException if something goes wrong
113      * @doc.tag type="content"
114      * @doc.param name="name" optional="false" description="The name of the collection to operate on. If
115      * the collection does not exists, an execption will be thrown."
116      * @doc.param name="key" optional="true" description="The key to the new value. Should only be
117      * specified if the collection is a map."
118      * @doc.param name="value" optional="false" description="The value to put into the collection."
119      */

120     public void put(Properties attributes) throws XDocletException
121     {
122         String JavaDoc name = attributes.getProperty("name");
123         String JavaDoc key = attributes.getProperty("key");
124         String JavaDoc value = attributes.getProperty("value");
125
126         // Makes sure name is specified
127
if (name == null || name.length() == 0) {
128             throw new XDocletException(Translator.getString(XDocletMessages.class,
129                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
130         }
131
132         // Makes sure value is specified
133
if (value == null || value.length() == 0) {
134             throw new XDocletException(Translator.getString(XDocletMessages.class,
135                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"value"}));
136         }
137
138         // Make sure the collection exists
139
if (!collections.containsKey(name)) {
140             throw new XDocletException(Translator.getString(CollectionMessages.class,
141                 CollectionMessages.COLLECTION_NOT_DEFINED, new String JavaDoc[]{name}));
142         }
143
144         // Puts the value into the collection, as requested
145
if (collections.get(name) instanceof Map) {
146             if (key == null || key.length() == 0) {
147                 throw new XDocletException(Translator.getString(XDocletMessages.class,
148                     XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"key"}));
149             }
150
151             ((Map) collections.get(name)).put(key, value);
152         }
153         else {
154             // Throws an exception if key is specified
155
if (key != null) {
156                 throw new XDocletException(Translator.getString(CollectionMessages.class,
157                     CollectionMessages.COLLECTION_IS_NOT_MAP, new String JavaDoc[]{name}));
158             }
159
160             ((Set) collections.get(name)).add(value);
161         }
162     }
163
164
165     /**
166      * Removes an element from the specified collection. One of 'key' or 'value' attributes should be specified,
167      * depending if the collection is a map or a set.
168      *
169      * @param attributes The attributes of the template tag
170      * @throws XDocletException if something goes wrong
171      * @doc.tag type="content"
172      * @doc.param name="name" optional="false" description="The name of the collection to operate on. If
173      * the collection does not exists, an execption will be thrown."
174      * @doc.param name="key" optional="true" description="The key to remove from the map. Invalid if the
175      * collection is a set."
176      * @doc.param name="value" optional="true" description="The value to remove from the set. Invalid if
177      * the collection is a map."
178      */

179     public void remove(Properties attributes) throws XDocletException
180     {
181         String JavaDoc name = attributes.getProperty("name");
182         String JavaDoc key = attributes.getProperty("key");
183         String JavaDoc value = attributes.getProperty("value");
184
185         // Makes sure name is specified
186
if (name == null || name.length() == 0) {
187             throw new XDocletException(Translator.getString(XDocletMessages.class,
188                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
189         }
190
191         // Make sure name exists
192
if (!collections.containsKey(name)) {
193             throw new XDocletException(Translator.getString(CollectionMessages.class,
194                 CollectionMessages.COLLECTION_NOT_DEFINED, new String JavaDoc[]{name}));
195         }
196
197         // Removes the key from the name, as requested
198
if (collections.get(name) instanceof Map) {
199             // Makes sure key is specified
200
if (key == null || key.length() == 0) {
201                 throw new XDocletException(Translator.getString(XDocletMessages.class,
202                     XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"key"}));
203             }
204
205             ((Map) collections.get(name)).remove(key);
206         }
207         else {
208             // Makes sure value is specified
209
if (value == null || value.length() == 0) {
210                 throw new XDocletException(Translator.getString(XDocletMessages.class,
211                     XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"value"}));
212             }
213
214             ((Set) collections.get(name)).remove(key);
215         }
216     }
217
218     /**
219      * Generates the contained template code if the specified collection contains the key or value passed as attributes.
220      * If the collection is a set, only the 'value' attribute should be specified. If the collection is a map, the 'key'
221      * attribute should be specifed and if the 'value' attribute is also specified, an additional check for equality
222      * will be made.
223      *
224      * @param template The block tag contents
225      * @param attributes The attributes of the tag template
226      * @exception XDocletException
227      * @doc.tag type="block"
228      * @doc.param name="name" optional="false" description="The collection to operate on."
229      * @doc.param name="key" optional="true" description="The key to check, if the collection is a
230      * map."
231      * @doc.param name="value" optional="true" description="The valu to check, if the collection is a
232      * set. If the collection is a map, the value to check for equality."
233      */

234     public void ifContains(String JavaDoc template, Properties attributes) throws XDocletException
235     {
236         if (contains(attributes))
237             generate(template);
238     }
239
240     /**
241      * Generates the contained template code if the specified collection doesn't contain the key or value passed as
242      * attributes. If the collection is a set, only the 'value' attribute should be specified. If the collection is a
243      * map, the 'key' attribute should be specifed and if the 'value' attribute is also specified, an additional check
244      * for equality will be made.
245      *
246      * @param template The block tag contents
247      * @param attributes The attributes of the tag template
248      * @exception XDocletException
249      * @doc.tag type="block"
250      * @doc.param name="name" optional="false" description="The collection to operate on."
251      * @doc.param name="key" optional="true" description="The key to check, if the collection is a
252      * map."
253      * @doc.param name="value" optional="true" description="The valu to check, if the collection is a
254      * set. If the collection is a map, the value to check for equality."
255      */

256     public void ifDoesntContain(String JavaDoc template, Properties attributes) throws XDocletException
257     {
258         if (!contains(attributes))
259             generate(template);
260     }
261
262     /**
263      * Destroys the specified collection. The collection must exists or an exception will be thrown.
264      *
265      * @param attributes The attributes of the tag template
266      * @throws XDocletException if something goes wrong
267      * @doc.tag type="content"
268      * @doc.param name="name" description="The collection to destroy."
269      */

270     public void destroy(Properties attributes) throws XDocletException
271     {
272         String JavaDoc name = attributes.getProperty("name");
273
274         // Makes sure name is specified
275
if (name == null || name.length() == 0) {
276             throw new XDocletException(Translator.getString(XDocletMessages.class,
277                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
278         }
279
280         // Makes sure name exists
281
if (!collections.containsKey(name)) {
282             throw new XDocletException(Translator.getString(CollectionMessages.class,
283                 CollectionMessages.COLLECTION_NOT_DEFINED, new String JavaDoc[]{name}));
284         }
285
286         collections.remove(name);
287     }
288
289
290     /**
291      * Checks if the specified collection contains a certain key/value. If the collection is a set, only the 'value'
292      * attribute should be specified. If the collection is a map, the 'key' value should be specified and if the 'value'
293      * attribute is also specified, an additional check for equality will be made.
294      *
295      * @param attributes The attributes of the template tag
296      * @return
297      * @exception XDocletException
298      */

299     private boolean contains(Properties attributes) throws XDocletException
300     {
301         String JavaDoc name = attributes.getProperty("name");
302         String JavaDoc key = attributes.getProperty("key");
303         String JavaDoc value = attributes.getProperty("value");
304
305         // Makes sure name is specified
306
if (name == null || name.length() == 0) {
307             throw new XDocletException(Translator.getString(XDocletMessages.class,
308                 XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"name"}));
309         }
310
311         // Make sure name exists
312
if (!collections.containsKey(name)) {
313             throw new XDocletException(Translator.getString(CollectionMessages.class,
314                 CollectionMessages.COLLECTION_NOT_DEFINED, new String JavaDoc[]{name}));
315         }
316
317         // Checks if the collection contains the key/value specified
318
if (collections.get(name) instanceof Map) {
319             // Makes sure key is specified
320
if (key == null || key.length() == 0) {
321                 throw new XDocletException(Translator.getString(XDocletMessages.class,
322                     XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"key"}));
323             }
324
325             // If value is specified, checks for equality
326
if (value == null)
327                 return ((Map) collections.get(name)).containsKey(key);
328             else
329                 return value.equals(((Map) collections.get(name)).get(key));
330         }
331         else {
332             // Makes sure value is specified
333
if (value == null || value.length() == 0) {
334                 throw new XDocletException(Translator.getString(XDocletMessages.class,
335                     XDocletMessages.PARAMETER_MISSING_OR_EMPTY, new String JavaDoc[]{"value"}));
336             }
337
338             return ((Set) collections.get(name)).contains(value);
339         }
340     }
341 }
342
Popular Tags