KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * Helper class to build valid XML as a base for all xmlbuilder
23  *
24  * @author Jens Kapitza
25  * @version $Revision: 1.2 $ $Date: 2007/07/22 16:58:28 $ $Author: jenskapitza $
26  * @param <V>
27  * Listtype (Item, TreeItem)
28  */

29 abstract class BaseXmlBuilder<V> {
30
31     private List JavaDoc<V> liste = new ArrayList JavaDoc<V>();
32
33     protected void setListe(List JavaDoc<V> liste) {
34         this.liste = liste;
35     }
36
37     /**
38      * default encoding is utf-8
39      */

40     private String JavaDoc encoding = "UTF-8";
41
42     /**
43      * @return the xml encoding
44      */

45     public String JavaDoc getEncoding() {
46         return this.encoding;
47     }
48
49     /**
50      * set the xml encoding
51      *
52      * @param encoding
53      */

54     public void setEncoding(String JavaDoc encoding) {
55         this.encoding = encoding;
56     }
57
58     protected List JavaDoc<V> getListe() {
59         return this.liste;
60     }
61
62     /**
63      *
64      * @return the item list
65      */

66     protected List JavaDoc<V> getItems() {
67         return getListe();
68     }
69
70     /**
71      *
72      * @return the xml body, xml encoding is added by {@link #toString()}
73      */

74     protected abstract String JavaDoc getXMLString();
75
76     /**
77      * return the full XML ducument
78      */

79     @Override JavaDoc
80     public String JavaDoc toString() {
81         StringBuffer JavaDoc xml = new StringBuffer JavaDoc().append("<?xml version=\"1.0\"");
82         if (getEncoding() != null) {
83             xml.append(" encoding=\"");
84             xml.append(getEncoding());
85             xml.append("\"");
86         }
87         xml.append(" ?>");
88         xml.append(getXMLString());
89
90         return xml.toString();
91     }
92
93     /**
94      * add item to list
95      *
96      * @param o
97      * the item to add
98      * @return BaseXmlBuilder
99      * @see ArrayList#add(Object)
100      */

101     public BaseXmlBuilder<V> add(V o) {
102         this.liste.add(o);
103         return this;
104     }
105
106     /**
107      * delete all items
108      */

109     public void clear() {
110         this.liste.clear();
111     }
112
113     /**
114      * return the item at index
115      *
116      * @param index
117      * the index
118      * @return the item at index
119      */

120     public V get(int index) {
121         return this.liste.get(index);
122     }
123
124     /**
125      * check if itemlist is empty
126      *
127      * @return true if it is empty else false
128      */

129     public boolean isEmpty() {
130         return this.liste.isEmpty();
131     }
132
133     /**
134      *
135      * @return the item count
136      */

137     public int size() {
138         return this.liste.size();
139     }
140
141 }
142
Popular Tags