KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import org.apache.commons.beanutils.BeanUtils;
23
24 /**
25  * Helper class to build valid XML typically returned in a response to the
26  * client.
27  *
28  * @author Darren Spurgeon
29  * @author Jens Kapitza
30  * @version $Revision: 1.1 $ $Date: 2007/07/22 16:29:16 $ $Author: jenskapitza $
31  */

32 public final class AjaxXmlBuilder extends AjaxValueListXmlBuilder {
33
34     /**
35      * Add item to XML.
36      *
37      * @param key
38      * The name of the item
39      * @param value
40      * The value of the item
41      * @return the xmlbuilder
42      */

43     public AjaxXmlBuilder addItem(String JavaDoc key, String JavaDoc value) {
44         return addItem(key, value, false);
45     }
46
47     /**
48      * Add item wrapped with inside a CDATA element.
49      *
50      * @param key
51      * The name of the item
52      * @param value
53      * The value of the item
54      * @return the xmlbuilder
55      */

56     public AjaxXmlBuilder addItemAsCData(String JavaDoc key, String JavaDoc value) {
57         return addItem(key, value, true);
58     }
59
60     /**
61      * Add items from a collection.
62      *
63      * @param collection
64      * @param nameProperty
65      * @param valueProperty
66      * @return the xmlbuilder
67      * @throws NoSuchMethodException
68      * @throws InvocationTargetException
69      * @throws IllegalAccessException
70      */

71     public AjaxXmlBuilder addItems(Collection JavaDoc<?> collection,
72             String JavaDoc nameProperty, String JavaDoc valueProperty)
73             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
74             NoSuchMethodException JavaDoc {
75         return addItems(collection, nameProperty, valueProperty, false);
76     }
77
78     /**
79      * Add item to XML.
80      *
81      * @param name
82      * The name of the item
83      * @param value
84      * The value of the item
85      * @param asCData
86      * add as CData
87      * @return the xmlbuilder
88      */

89     public AjaxXmlBuilder addItem(String JavaDoc name, String JavaDoc value, boolean asCData) {
90         super.addItem(name, asCData, value);
91         return this;
92     }
93
94     /**
95      * Add items from a collection.
96      *
97      * @param collection
98      * @param nameProperty
99      * @param valueProperty
100      * @param asCData
101      * @return the xmlbuilder
102      * @throws NoSuchMethodException
103      * @throws InvocationTargetException
104      * @throws IllegalAccessException
105      */

106     public AjaxXmlBuilder addItems(Collection JavaDoc<?> collection,
107             String JavaDoc nameProperty, String JavaDoc valueProperty, boolean asCData)
108             throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc,
109             NoSuchMethodException JavaDoc {
110         for (Object JavaDoc element : collection) {
111             String JavaDoc name = BeanUtils.getProperty(element, nameProperty);
112             String JavaDoc value = BeanUtils.getProperty(element, valueProperty);
113             addItem(name, value, asCData);
114         }
115         return this;
116     }
117
118 }
119
Popular Tags