KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 package org.ajaxtags.xml;
17
18 import org.ajaxtags.helpers.ValueItem;
19
20 /**
21  * Helper class to build valid XML for ajax with more then one value
22  *
23  * @author Jens Kapitza
24  * @version $Revision: 1.1 $ $Date: 2007/07/22 16:29:16 $ $Author: jenskapitza $
25  */

26 class AjaxValueListXmlBuilder extends BaseXmlBuilder<ValueItem> {
27
28     /**
29      * add an Item
30      *
31      * @param name
32      * the name
33      * @param asCdata
34      * true if so else false
35      * @param value
36      * a list of values
37      * @return the xmlbuilder
38      *
39      */

40
41     public AjaxValueListXmlBuilder addItem(String JavaDoc name, boolean asCdata,
42             String JavaDoc... value) {
43         getListe().add(new ValueItem(name, asCdata, value));
44         return this;
45     }
46
47     /**
48      * add an Item with asCdata = false
49      *
50      * @param name
51      * the name
52      * @param value
53      * a list of values
54      * @return the xmlbuilder
55      */

56     public AjaxValueListXmlBuilder addItem(String JavaDoc name, String JavaDoc... value) {
57         return addItem(name, false, value);
58     }
59
60     /**
61      * build the node
62      *
63      * @param item
64      * the item
65      * @return xml string for this item
66      */

67     private static String JavaDoc valueToString(ValueItem item) {
68         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
69         xml.append("<name>");
70         if (item.isAsCData()) {
71             xml.append("<![CDATA[");
72         }
73         xml.append(item.getName());
74         if (item.isAsCData()) {
75             xml.append("]]>");
76         }
77         xml.append("</name>");
78         for (String JavaDoc value : item.getValue()) {
79             xml.append("<value>");
80             if (item.isAsCData()) {
81                 xml.append("<![CDATA[");
82             }
83             xml.append(value);
84             if (item.isAsCData()) {
85                 xml.append("]]>");
86             }
87             xml.append("</value>");
88         }
89
90         return xml.toString();
91     }
92
93     /**
94      * build the xml string
95      *
96      * @see BaseXmlBuilder#getXMLString()
97      */

98     @Override JavaDoc
99     protected String JavaDoc getXMLString() {
100         StringBuffer JavaDoc xml = new StringBuffer JavaDoc();
101         xml.append("<ajax-response>");
102         xml.append("<response>");
103         for (ValueItem item : getItems()) {
104             xml.append("<item>");
105             xml.append(valueToString(item));
106             xml.append("</item>");
107         }
108         xml.append("</response>");
109         xml.append("</ajax-response>");
110
111         return xml.toString();
112     }
113
114 }
115
Popular Tags