1 | /* |
---|
2 | * License |
---|
3 | * |
---|
4 | * The contents of this file are subject to the Jabber Open Source License |
---|
5 | * Version 1.0 (the "License"). You may not copy or use this file, in either |
---|
6 | * source code or executable form, except in compliance with the License. You |
---|
7 | * may obtain a copy of the License at http://www.jabber.com/license/ or at |
---|
8 | * http://www.opensource.org/. |
---|
9 | * |
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
12 | * for the specific language governing rights and limitations under the |
---|
13 | * License. |
---|
14 | * |
---|
15 | * Copyrights |
---|
16 | * |
---|
17 | * Portions created by or assigned to Jabber.com, Inc. are |
---|
18 | * Copyright (c) 2000 Jabber.com, Inc. All Rights Reserved. Contact |
---|
19 | * information for Jabber.com, Inc. is available at http://www.jabber.com/. |
---|
20 | * |
---|
21 | * Portions Copyright (c) 1999-2000 David Waite |
---|
22 | * |
---|
23 | * Acknowledgements |
---|
24 | * |
---|
25 | * Special thanks to the Jabber Open Source Contributors for their |
---|
26 | * suggestions and support of Jabber. |
---|
27 | * |
---|
28 | * Changes |
---|
29 | * renamed/added methods |
---|
30 | */ |
---|
31 | package nu.fw.jeti.backend; |
---|
32 | |
---|
33 | import java.util.Map; |
---|
34 | import java.util.Iterator; |
---|
35 | |
---|
36 | /** |
---|
37 | * an XMLData object is the root class of a hierarchy of objects, all which |
---|
38 | * serialize themselves to XML data. |
---|
39 | * |
---|
40 | * @author David Waite <a href="mailto:dwaite@jabber.com"> |
---|
41 | * <i><dwaite@jabber.com></i></a> |
---|
42 | * @author Author: mass |
---|
43 | * @author Author: E.S. de Boer |
---|
44 | * @version Revision: 2 |
---|
45 | */ |
---|
46 | public abstract class XMLData |
---|
47 | { |
---|
48 | /** |
---|
49 | * <code>toString</code> is a serializer for the data contained in the |
---|
50 | * object to an equivalent snippet of XML stream. |
---|
51 | * |
---|
52 | * @return a <code>String</code> value which contains the XML |
---|
53 | * representation of this object |
---|
54 | */ |
---|
55 | public String toString() |
---|
56 | { |
---|
57 | StringBuffer retval=new StringBuffer(); |
---|
58 | appendToXML(retval); |
---|
59 | return new String(retval); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * <code>appendToXML</code> appends the XML representation of the |
---|
64 | * current packet data to the specified <code>StringBuffer</code>. |
---|
65 | * |
---|
66 | * @param xml The <code>StringBuffer</code> to append to |
---|
67 | */ |
---|
68 | public abstract void appendToXML(StringBuffer xml); |
---|
69 | |
---|
70 | /** |
---|
71 | * <code>appendElement</code> adds the XML for a child 'element' to a |
---|
72 | * StringBuffer, as a helper to appendItem. |
---|
73 | * |
---|
74 | * @param xml a <code>StringBuffer</code> value |
---|
75 | * @param name a <code>String</code> value |
---|
76 | * @param value a <code>String</code> value |
---|
77 | * |
---|
78 | * @return a <code>bool</code> which is true if the element was generated. |
---|
79 | * This is used for any shortcuts in the outputted XML |
---|
80 | */ |
---|
81 | protected static final boolean appendElement(StringBuffer xml,String name,String value) |
---|
82 | { |
---|
83 | if (value==null) return false; |
---|
84 | xml.append( '<' ); |
---|
85 | xml.append(name); |
---|
86 | if(value.equals("")) |
---|
87 | {//shortcut |
---|
88 | xml.append("/>" ); |
---|
89 | return true; |
---|
90 | } |
---|
91 | xml.append( '>' ); |
---|
92 | escapeString(xml, value); |
---|
93 | xml.append( "</" ); |
---|
94 | xml.append(name); |
---|
95 | xml.append( '>' ); |
---|
96 | return true; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * <code>appendElement</code> adds the XML for a child 'element' to a |
---|
102 | * StringBuffer, as a helper to appendItem. |
---|
103 | * |
---|
104 | * @param xml a <code>StringBuffer</code> value |
---|
105 | * @param name a <code>String</code> value |
---|
106 | * @param value a <code>String</code> value |
---|
107 | * |
---|
108 | * @return a <code>bool</code> which is true if the element was generated. |
---|
109 | * This is used for any shortcuts in the outputted XML |
---|
110 | */ |
---|
111 | protected static final boolean appendElement(StringBuffer xml,String name,Object value) |
---|
112 | { |
---|
113 | if (value==null) return false; |
---|
114 | return appendElement(xml,name,value.toString()); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * <code>appendElement</code> outputs an element if needed. If value=false |
---|
119 | * it does not output the tag. |
---|
120 | * |
---|
121 | * @param xml a <code>StringBuffer</code> value |
---|
122 | * @param name a <code>String</code> value |
---|
123 | * @param value a <code>boolean</code> value |
---|
124 | * |
---|
125 | * @return a <code>bool</code> which is true if the element was generated. |
---|
126 | * This is used for any shortcuts in the outputted XML |
---|
127 | */ |
---|
128 | protected static final boolean appendElement(StringBuffer xml,String name,boolean value) |
---|
129 | { |
---|
130 | if (value==false)return false; |
---|
131 | xml.append( '<' ); |
---|
132 | xml.append(name); |
---|
133 | xml.append( "/>" ); |
---|
134 | return true; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * <code>appendElement</code> adds a the entry's of a map to the stingbuffer |
---|
139 | * |
---|
140 | * @param xml a <code>StringBuffer</code> value |
---|
141 | * @param map a <code>Map</code> value |
---|
142 | * |
---|
143 | * @return a <code>bool</code> which is true if the element was generated. |
---|
144 | * This is used for any shortcuts in the outputted XML |
---|
145 | */ |
---|
146 | protected static final boolean appendElement(StringBuffer xml,Map map) |
---|
147 | { |
---|
148 | if (map == null) return false; |
---|
149 | for (Iterator i=map.entrySet().iterator(); i.hasNext(); ) |
---|
150 | { |
---|
151 | Map.Entry e = (Map.Entry) i.next(); |
---|
152 | String name = (String)e.getKey(); |
---|
153 | String value =(String)e.getValue(); |
---|
154 | |
---|
155 | if (value==null) continue; |
---|
156 | xml.append( '<' ); |
---|
157 | xml.append(name); |
---|
158 | if(value.equals("")) |
---|
159 | {//shortcut |
---|
160 | xml.append("/>" ); |
---|
161 | continue; |
---|
162 | } |
---|
163 | xml.append( '>' ); |
---|
164 | escapeString(xml, value); |
---|
165 | xml.append( "</" ); |
---|
166 | xml.append(name); |
---|
167 | xml.append( '>' ); |
---|
168 | } |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * <code>appendAttribute</code> outputs an attribute if needed. If value=null |
---|
174 | * it will not output the attribute. Note that it outputs in the format:<p> |
---|
175 | * &qout; <i>attrib</i>=<i>value</i>&qout;, or it puts a proceeding |
---|
176 | * space in front of the attribute, and no trailing space. This is for |
---|
177 | * optimizing the XML generation. |
---|
178 | * |
---|
179 | * @param xml a <code>StringBuffer</code> value |
---|
180 | * @param name a <code>String</code> value |
---|
181 | * @param value a <code>boolean</code> value |
---|
182 | * |
---|
183 | * @return a <code>bool</code> which is true if the attribute was |
---|
184 | * generated. This is used for any shortcuts in the outputted XML |
---|
185 | */ |
---|
186 | protected static final boolean appendAttribute(StringBuffer xml,String name,String value) |
---|
187 | { |
---|
188 | if (value==null || value.length()<1) return false; |
---|
189 | xml.append( ' ' ); |
---|
190 | xml.append(name ); |
---|
191 | xml.append("=\""); |
---|
192 | escapeString(xml,value); |
---|
193 | xml.append("\""); |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * <code>appendAttribute</code> outputs an attribute if needed. If value=null |
---|
199 | * it will not output the attribute. Note that it outputs in the format:<p> |
---|
200 | * &qout; <i>attrib</i>=<i>value</i>&qout;, or it puts a proceeding |
---|
201 | * space in front of the attribute, and no trailing space. This is for |
---|
202 | * optimizing the XML generation. |
---|
203 | * |
---|
204 | * @param xml a <code>StringBuffer</code> value |
---|
205 | * @param name a <code>String</code> value |
---|
206 | * @param value a <code>Object</code> value, which supports toString() |
---|
207 | * |
---|
208 | * @return a <code>bool</code> which is true if the attribute was |
---|
209 | * generated. This is used for any shortcuts in the outputted XML |
---|
210 | */ |
---|
211 | protected static final boolean appendAttribute(StringBuffer xml,String name,Object value) |
---|
212 | { |
---|
213 | if (value==null) return false; |
---|
214 | return appendAttribute(xml,name,value.toString()); |
---|
215 | } |
---|
216 | |
---|
217 | public static final void escapeString(StringBuffer xml, String data) |
---|
218 | { |
---|
219 | char c; |
---|
220 | for (int i=0;i<data.length();i++) |
---|
221 | { |
---|
222 | int d = data.charAt(i); |
---|
223 | if(d>=32 || d==9 ||d==10 || d==13) |
---|
224 | { |
---|
225 | switch(c=data.charAt(i)) |
---|
226 | { |
---|
227 | case '&': |
---|
228 | xml.append("&"); |
---|
229 | break; |
---|
230 | case '<': |
---|
231 | xml.append("<"); |
---|
232 | break; |
---|
233 | case '>': |
---|
234 | xml.append(">"); |
---|
235 | break; |
---|
236 | case '\'': |
---|
237 | xml.append("'"); |
---|
238 | break; |
---|
239 | case '\"': |
---|
240 | xml.append("""); |
---|
241 | break; |
---|
242 | default: |
---|
243 | xml.append(c); |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | } |
---|
249 | /* |
---|
250 | * Overrides for emacs |
---|
251 | * Local variables: |
---|
252 | * tab-width: 4 |
---|
253 | * End: |
---|
254 | */ |
---|