KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > workflow > taglib > Element


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.workflow.taglib;
24
25 import java.text.MessageFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Utility class for creating html elements.
34  * Most functions returns the element itself to allow chained operations.
35  */

36 class Element
37 {
38     /**
39      * Pattern for an attribute.
40      */

41     private static final String JavaDoc ATTRIBUTE = "{0}=\"{1}\"";
42     
43     /**
44      * Pattern for an element with both attributes and children.
45      */

46     private static final String JavaDoc CHILDREN_AND_ATTRIBUTES = "<{0} {1}>{2}</{0}>";
47     
48     /**
49      * Pattern for an element with children but no attributes.
50      */

51     private static final String JavaDoc CHILDREN_AND_NO_ATTRIBUTES = "<{0}>{1}</{0}>";
52     
53     /**
54      * Pattern for an element with neither children or attributes.
55      */

56     private static final String JavaDoc NO_CHILDREN_NO_ATTRIBUTES = "<{0}></{0}>";
57     
58     /**
59      * Pattern for an element with attributes but no children.
60      */

61     private static final String JavaDoc NO_CHILDREN_AND_ATTRIBUTES = "<{0} {1}></{0}>";
62     
63     /**
64      * The name of the element.
65      */

66     private final String JavaDoc name;
67     
68     /**
69      * The parent of the element.
70      */

71     private Element parent;
72     
73     /**
74      * The attributes of the element.
75      */

76     private final Map JavaDoc attributes = new HashMap JavaDoc();
77     
78     /**
79      * The children of the element.
80      */

81     private final List JavaDoc children = new ArrayList JavaDoc();
82     
83     /**
84      * Constructs an element with the specified name and with no parent element.
85      *
86      * @param name the name of the element.
87      */

88     public Element(final String JavaDoc name)
89     {
90         this(null, name);
91     }
92     
93     /**
94      * Constructs an element with the specified name and parent element.
95      *
96      * @param parent the parent of the element (null is permitted).
97      * @param name the name of the element.
98      */

99     public Element(final Element parent, final String JavaDoc name)
100     {
101         super();
102         this.parent = parent;
103         this.name = name;
104     }
105     
106     /**
107      * Adds an attribute with the specified name and value if the value isn't null.
108      * If an attribute with the specified name exists, it will be overwritten.
109      *
110      * @param name the name of the attribute.
111      * @param value the value of the attribute.
112      * @return the current element.
113      */

114     public Element addAttribute(final String JavaDoc name, final String JavaDoc value)
115     {
116         return addAttribute(name, value, true);
117     }
118     
119     /**
120      * Adds an attribute with the specified name and value if the specified condition
121      * is true and the value isn't null.
122      * If an attribute with the specified name exists, it will be overwritten.
123      *
124      * @param name the name of the attribute.
125      * @param value the value of the attribute.
126      * @param condition the condition to check.
127      * @return the current element.
128      */

129     public Element addAttribute(final String JavaDoc name, String JavaDoc value, final boolean condition)
130     {
131         if(condition && value != null)
132         {
133             Object JavaDoc o = (Object JavaDoc)attributes.get(name);
134             if(o != null && o instanceof String JavaDoc)
135                 value = o + " " + value;
136             
137             attributes.put(name, value);
138         }
139         return this;
140     }
141     
142     /**
143      * Creates and adds a child with the specified name to the end of the children list.
144      * The parent of the child will be the current element.
145      *
146      * @param name the name of the child element.
147      * @return the child element.
148      */

149     public Element addChild(final String JavaDoc name)
150     {
151         final Element child = new Element(this, name);
152         children.add(child);
153         return child;
154     }
155
156     /**
157      * Creates and adds a child with the specified name to the start of the children list.
158      * The parent of the child will be the current element.
159      *
160      * @param name the name of the child element.
161      * @return the child element.
162      */

163     public Element addChildFirst(final String JavaDoc name)
164     {
165         final Element child = new Element(this, name);
166         children.add(0, child);
167         return child;
168     }
169     
170     /**
171      * Adds the specified child to the end of the children list if non-null.
172      * The parent of the child will be set to the current element.
173      *
174      * @param child the child element.
175      * @return the current element.
176      */

177     public Element addChild(final Element child)
178     {
179         if(child != null)
180         {
181             child.parent = this;
182             children.add(child);
183         }
184         return this;
185     }
186     
187     /**
188      * Creates and adds a text-child to the end of the children list if non-empty.
189      *
190      * @param text the text of the child.
191      * @return the current element.
192      */

193     public Element addText(final String JavaDoc text)
194     {
195         if(text != null && text.length() > 0)
196         {
197             children.add(text);
198         }
199         return this;
200     }
201     
202     /**
203      * Returns true if the element has an attribute with the specified name; false otherwise.
204      *
205      * @return true if the element has an attribute with the specified name; false otherwise.
206      */

207     public boolean hasAttribute(final String JavaDoc name)
208     {
209         return name != null && attributes.containsKey(name);
210     }
211     
212     /**
213      * Returns true if the element has any attributes; false otherwise.
214      *
215      * @return true if the element has any attributes; false otherwise.
216      */

217     public boolean hasAttributes()
218     {
219         return !attributes.isEmpty();
220     }
221
222     /**
223      * Returns true if the element has any children; false otherwise.
224      *
225      * @return true if the element has any children; false otherwise.
226      */

227     public boolean hasChildren()
228     {
229         return !children.isEmpty();
230     }
231     
232     /**
233      * Returns the parent of the element.
234      *
235      * @return the parent of the element.
236      */

237     public Element pop()
238     {
239         return parent;
240     }
241     
242     /**
243      * Returns the string representation of the element.
244      *
245      * @return the string representation of the element.
246      */

247     public String JavaDoc toString()
248     {
249         if(hasAttributes() && hasChildren())
250         {
251             return MessageFormat.format(CHILDREN_AND_ATTRIBUTES, new Object JavaDoc[] { name, attributesString(), childrenString()});
252         }
253         if(hasAttributes() && !hasChildren())
254         {
255             return MessageFormat.format(NO_CHILDREN_AND_ATTRIBUTES, new Object JavaDoc[] { name, attributesString() });
256         }
257         if(!hasAttributes() && hasChildren())
258         {
259             return MessageFormat.format(CHILDREN_AND_NO_ATTRIBUTES, new Object JavaDoc[] { name, childrenString() });
260         }
261         return MessageFormat.format(NO_CHILDREN_NO_ATTRIBUTES, new Object JavaDoc[] { name });
262     }
263     
264     /**
265      * Returns the string representation of the attributes of the element.
266      *
267      * @return the string representation of the attributes of the element.
268      */

269     private String JavaDoc attributesString()
270     {
271         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
272         for(final Iterator JavaDoc i = attributes.keySet().iterator(); i.hasNext(); )
273         {
274             final Object JavaDoc key = i.next();
275             final Object JavaDoc value = attributes.get(key);
276             sb.append((sb.length() > 0 ? " " : "") + MessageFormat.format(ATTRIBUTE, new Object JavaDoc[] { key, value }));
277         }
278         return sb.toString();
279     }
280
281     /**
282      * Returns the string representation of the children of the element.
283      *
284      * @return the string representation of the children of the element.
285      */

286     private String JavaDoc childrenString()
287     {
288         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
289         for(final Iterator JavaDoc i = children.iterator(); i.hasNext(); )
290         {
291             sb.append(i.next());
292         }
293         return sb.toString();
294     }
295 }
Popular Tags