KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxtags > xml > AjaxTreeXmlBuilder


1 /**
2  * Copyright 2005 Darren L. Spurgeon
3  * Copyright 2007 Jens Kapitza
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.ajaxtags.xml;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.ajaxtags.helpers.TreeItem;
25 import org.apache.commons.beanutils.BeanUtils;
26 import org.apache.commons.lang.StringUtils;
27
28 /**
29  * Helper class to build valid XML, for the AjaxTreeTag, typically returned in a
30  * response to the client.
31  *
32  * @author Musachy Barroso
33  * @author Jens Kapitza
34  * @version $Revision: 1.3 $ $Date: 2007/07/24 12:21:13 $ $Author: jenskapitza $
35  */

36 public final class AjaxTreeXmlBuilder extends BaseXmlBuilder<TreeItem> {
37
38     private Map JavaDoc<String JavaDoc, String JavaDoc> ensureMap(Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
39         if (attributes == null) {
40             return new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
41         }
42         return attributes;
43     }
44
45     /**
46      * add tree item to xml builder
47      *
48      * @param name
49      * @param value
50      * @return AjaxTreeXmlBuilder xml builder
51      */

52     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value) {
53         return addItem(name, value, null);
54     }
55
56     /**
57      *
58      * add tree item to xml builder
59      *
60      * @param name
61      * @param value
62      * @return AjaxTreeXmlBuilder xml builder
63      */

64     public AjaxTreeXmlBuilder addItemAsCData(String JavaDoc name, String JavaDoc value) {
65         return addItemAsCData(name, value, null);
66     }
67
68     /**
69      *
70      * add tree item to xml builder
71      *
72      * @param name
73      * @param value
74      * @param attributes
75      * @return AjaxTreeXmlBuilder xml builder
76      */

77     public AjaxTreeXmlBuilder addItemAsCData(String JavaDoc name, String JavaDoc value,
78             Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
79         return addItem(name, value, true, attributes);
80     }
81
82     /**
83      *
84      * add tree item to xml builder
85      *
86      * @param name
87      * @param value
88      * @param attributes
89      * @return AjaxTreeXmlBuilder xml builder
90      */

91     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value,
92             Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
93         return addItem(name, value, false, attributes);
94     }
95
96     /**
97      *
98      * add tree item to xml builder
99      *
100      * @param name
101      * @param value
102      * @param asCData
103      * @param attributes
104      * @return AjaxTreeXmlBuilder xml builder
105      */

106     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value,
107             boolean asCData, Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
108         TreeItem treeitem = new TreeItem(name, value, asCData,
109                 ensureMap(attributes));
110         getListe().add(treeitem);
111         return this;
112     }
113
114     /**
115      *
116      * add tree item to xml builder
117      *
118      * @param name
119      * @param value
120      * @param url
121      * @param asCData
122      * @return AjaxTreeXmlBuilder xml builder
123      */

124     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value, String JavaDoc url,
125             boolean asCData) {
126         return addItem(name, value, false, url, asCData);
127     }
128
129     /**
130      * add tree item
131      *
132      * @param name
133      * @param value
134      * @param collapsed
135      * @param url
136      * @return AjaxTreeXmlBuilder xml builder
137      */

138     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value,
139             boolean collapsed, String JavaDoc url) {
140         return addItem(name, value, collapsed, url, false);
141     }
142
143     /**
144      *
145      * add tree item
146      *
147      * @param name
148      * @param value
149      * @param collapsed
150      * @param url
151      * @param asCData
152      * @return AjaxTreeXmlBuilder xml builder
153      */

154     public AjaxTreeXmlBuilder addItem(String JavaDoc name, String JavaDoc value,
155             boolean collapsed, String JavaDoc url, boolean asCData) {
156         Map JavaDoc<String JavaDoc, String JavaDoc> data = ensureMap(null);
157         data.put(TreeItem.URL, url);
158         data.put(TreeItem.COLLAPSED, String.valueOf(collapsed));
159         return addItem(name, value, asCData, data);
160     }
161
162     /**
163      *
164      * add tree item to xml builder
165      *
166      * @param name
167      * @param value
168      * @param collapsed
169      * @param url
170      * @return AjaxTreeXmlBuilder xml builder
171      */

172     public AjaxTreeXmlBuilder addItemAsCData(String JavaDoc name, String JavaDoc value,
173             boolean collapsed, String JavaDoc url) {
174         Map JavaDoc<String JavaDoc, String JavaDoc> data = ensureMap(null);
175         data.put(TreeItem.URL, url);
176         data.put(TreeItem.COLLAPSED, String.valueOf(collapsed));
177         return addItem(name, value, true, data);
178     }
179
180     /**
181      *
182      * add tree items to xml builder
183      *
184      * @param collection
185      * @param nameProperty
186      * @param valueProperty
187      * @param collapsedProperty
188      * @param urlProperty
189      * @return AjaxTreeXmlBuilder xml builder
190      * @throws IllegalAccessException
191      * @throws InvocationTargetException
192      * @throws NoSuchMethodException
193      */

194     public AjaxTreeXmlBuilder addItems(Collection JavaDoc<?> collection,
195             String JavaDoc nameProperty, String JavaDoc valueProperty,
196             String JavaDoc collapsedProperty, String JavaDoc urlProperty)
197             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
198             NoSuchMethodException JavaDoc {
199         return addItems(collection, nameProperty, valueProperty,
200                 collapsedProperty, urlProperty, false);
201     }
202
203     /**
204      *
205      * add tree items to xml builder
206      *
207      * @param collection
208      * @param nameProperty
209      * @param valueProperty
210      * @param collapsedProperty
211      * @param urlProperty
212      * @param asCData
213      * @return AjaxTreeXmlBuilder xml builder
214      * @throws IllegalAccessException
215      * @throws InvocationTargetException
216      * @throws NoSuchMethodException
217      */

218     public AjaxTreeXmlBuilder addItems(Collection JavaDoc<?> collection,
219             String JavaDoc nameProperty, String JavaDoc valueProperty,
220             String JavaDoc collapsedProperty, String JavaDoc urlProperty, boolean asCData)
221             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
222             NoSuchMethodException JavaDoc {
223         return addItems(collection, nameProperty, valueProperty,
224                 collapsedProperty, urlProperty, null, asCData);
225     }
226
227     /**
228      *
229      * add tree items to xml builder
230      *
231      * @param collection
232      * @param nameProperty
233      * @param valueProperty
234      * @param collapsedProperty
235      * @param urlProperty
236      * @param leafProperty
237      * @param asCData
238      * @return AjaxTreeXmlBuilder xml builder
239      * @throws IllegalAccessException
240      * @throws InvocationTargetException
241      * @throws NoSuchMethodException
242      */

243     public AjaxTreeXmlBuilder addItems(Collection JavaDoc<?> collection,
244             String JavaDoc nameProperty, String JavaDoc valueProperty,
245             String JavaDoc collapsedProperty, String JavaDoc urlProperty, String JavaDoc leafProperty,
246             boolean asCData) throws IllegalAccessException JavaDoc,
247             InvocationTargetException JavaDoc, NoSuchMethodException JavaDoc {
248
249         for (Object JavaDoc element : collection) {
250             String JavaDoc name = BeanUtils.getProperty(element, nameProperty);
251             String JavaDoc value = BeanUtils.getProperty(element, valueProperty);
252             Map JavaDoc<String JavaDoc, String JavaDoc> data = ensureMap(null);
253             if (StringUtils.isNotEmpty(urlProperty)) {
254                 data.put(TreeItem.URL, BeanUtils.getProperty(element,
255                         urlProperty));
256             }
257             if (StringUtils.isNotEmpty(collapsedProperty)) {
258                 data.put(TreeItem.COLLAPSED, String.valueOf(BeanUtils
259                         .getProperty(element, collapsedProperty)));
260             }
261             if (StringUtils.isNotEmpty(leafProperty)) {
262                 data.put(TreeItem.LEAF, BeanUtils.getProperty(element,
263                         leafProperty));
264             }
265             addItem(name, value, data);
266         }
267         return this;
268     }
269
270     /**
271      *
272      * add tree items to xml builder
273      *
274      * @param collection
275      * @param nameProperty
276      * @param valueProperty
277      * @param collapsedProperty
278      * @param urlProperty
279      * @return AjaxTreeXmlBuilder xml builder
280      * @throws IllegalAccessException
281      * @throws InvocationTargetException
282      * @throws NoSuchMethodException
283      */

284     public AjaxTreeXmlBuilder addItemsAsCData(Collection JavaDoc<?> collection,
285             String JavaDoc nameProperty, String JavaDoc valueProperty,
286             String JavaDoc collapsedProperty, String JavaDoc urlProperty)
287             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
288             NoSuchMethodException JavaDoc {
289         return addItemsAsCData(collection, nameProperty, valueProperty,
290                 collapsedProperty, urlProperty, null);
291     }
292
293     /**
294      *
295      * add tree items to xml builder
296      *
297      * @param collection
298      * @param nameProperty
299      * @param valueProperty
300      * @param collapsedProperty
301      * @param urlProperty
302      * @param leafProperty
303      * @return AjaxTreeXmlBuilder xml builder
304      * @throws IllegalAccessException
305      * @throws InvocationTargetException
306      * @throws NoSuchMethodException
307      */

308     public AjaxTreeXmlBuilder addItemsAsCData(Collection JavaDoc<?> collection,
309             String JavaDoc nameProperty, String JavaDoc valueProperty,
310             String JavaDoc collapsedProperty, String JavaDoc urlProperty, String JavaDoc leafProperty)
311             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
312             NoSuchMethodException JavaDoc {
313         return addItems(collection, nameProperty, valueProperty,
314                 collapsedProperty, urlProperty, leafProperty, true);
315     }
316
317     /**
318      * build an xml body to describe TreeItem
319      *
320      * @see BaseXmlBuilder#getXMLString()
321      *
322      */

323     @Override JavaDoc
324     protected String JavaDoc getXMLString() {
325         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
326
327         xml.append("<ajax-response>");
328         xml.append("<response>");
329         for (TreeItem item : getItems()) {
330             xml.append("<item>");
331             xml.append("<name>");
332             if (item.isAsCData()) {
333                 xml.append("<![CDATA[");
334             }
335             xml.append(item.getName());
336             if (item.isAsCData()) {
337                 xml.append("]]>");
338             }
339             xml.append("</name>");
340             xml.append("<value>");
341             if (item.isAsCData()) {
342                 xml.append("<![CDATA[");
343             }
344             xml.append(item.getValue());
345             if (item.isAsCData()) {
346                 xml.append("]]>");
347             }
348             xml.append("</value>");
349
350             for (String JavaDoc attr : item.getAttributeKeySet()) {
351
352                 xml.append("<").append(attr).append(">");
353                 xml.append(item.getAttributeValue(attr));
354                 xml.append("</").append(attr).append(">");
355             }
356
357             xml.append("</item>");
358         }
359         xml.append("</response>");
360         xml.append("</ajax-response>");
361
362         return xml.toString();
363
364     }
365
366 }
367
Popular Tags