1
2 package org.textensor.xml;
3
4
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Modifier;
7 import java.util.List;
8
9
10 public class XMLWriter {
11
12 boolean conciseTags;
13 boolean quoteStrings;
14
15
16 public XMLWriter() {
17 conciseTags = false;
18 quoteStrings = true;
19 }
20
21
22 public void setConciseTags(boolean b) {
23 conciseTags = b;
24 }
25
26
27 public void setQuoteStrings(boolean b) {
28 quoteStrings = b;
29 }
30
31
32 public static void err(String s) {
33 System.out.println(s);
34 }
35
36
37 public static XMLWriter newInstance() {
38 return new XMLWriter();
39 }
40
41
42 public static String serialize(Object ob) {
43 return getSerialization(ob);
44 }
45
46
47 public static String getSerialization(Object ob) {
48 return newInstance().writeObject(ob);
49 }
50
51
52
53 public String writeObject(Object obj) {
54 StringBuffer sb = new StringBuffer();
55 appendObject(sb, "", null, obj, true);
56 return sb.toString();
57 }
58
59
60 public void appendObject(StringBuffer sbv, String psk, String knownAs, Object ob,
61 boolean writeClass) {
62 boolean write = true;
63
64 if (ob instanceof String) {
65 sbv.append(psk);
66 sbv.append("<String>");
67 sbv.append(ob);
68 sbv.append("</String>\n");
69 return;
70 }
71
72
73 if (!write) {
74
75 return;
76 }
77
78
79
80 String tag = "error";
81 if (knownAs != null) {
82 tag = knownAs;
83 } else {
84 tag = ob.getClass().getName();
85
86 if (conciseTags) {
87 int ilast = tag.lastIndexOf(".");
88 if (ilast >= 0) {
89 tag = tag.substring(ilast + 1, tag.length());
90 }
91 }
92 }
93
94 sbv.append(psk);
95 sbv.append("<" + tag + ">\n");
96
97
98
99
100
101
102
103
104 String sk = psk + " ";
105
106 Field[] flds = ob.getClass().getFields();
107
108 for (int i = 0; i < flds.length; i++) {
109 String fieldName = flds[i].getName();
110 Object ret = null;
111 try {
112 ret = flds[i].get(ob);
113 } catch (Exception e) {
114 err("WARNING - failed to get field " + fieldName + " in " + ob);
115 }
116 if (Modifier.isFinal(flds[i].getModifiers()))
117 ret = null;
118
119 if (ret instanceof Double) {
120 appendNV(sbv, sk, fieldName, ((Double)ret).doubleValue());
121
122 } else if (ret instanceof Integer) {
123 appendNV(sbv, sk, fieldName, ((Integer)ret).intValue());
124
125 } else if (ret instanceof Boolean) {
126 appendNV(sbv, sk, fieldName, ((Boolean)ret).booleanValue());
127
128 } else if (ret instanceof String) {
129 appendNV(sbv, sk, fieldName, (String)ret);
130
131 } else if (ret instanceof double[]) {
132 appendNV(sbv, sk, fieldName, (double[])ret);
133
134 } else if (ret instanceof int[]) {
135 appendNV(sbv, sk, fieldName, (int[])ret);
136
137 } else if (ret instanceof boolean[]) {
138 appendNV(sbv, sk, fieldName, (boolean[])ret);
139
140 } else if (ret instanceof String[]) {
141 appendNV(sbv, sk, fieldName, (String[])ret);
142
143 } else if (ret instanceof double[][]) {
144 appendNV(sbv, sk, fieldName, (double[][])ret);
145
146 } else if (ret instanceof int[][]) {
147 appendNV(sbv, sk, fieldName, (int[][])ret);
148
149 } else if (ret != null) {
150 appendObject(sbv, sk, fieldName, ret, true);
151
152 }
153 }
154
155
156 if (ob instanceof List) {
157 for (Object listobj : (List)ob) {
158 appendObject(sbv, psk + " ", null, listobj, true);
159 }
160 }
161
162
163 sbv.append(psk);
164 sbv.append("</" + tag + ">\n");
165 }
166
167
168
169 private void appendNV(StringBuffer sbv, String sk, String name, String value) {
170 sbv.append(sk + "<" + name + ">");
171 appendString(sbv, value);
172 sbv.append("</" + name + ">\n");
173 }
174
175
176 private void appendNV(StringBuffer sbv, String sk, String name, boolean value) {
177 sbv.append(sk + "<" + name + ">");
178 sbv.append(value ? "1" : "0");
179 sbv.append("</" + name + ">\n");
180 }
181
182
183
184 private void appendNV(StringBuffer sbv, String sk, String name, int value) {
185 sbv.append(sk + "<" + name + ">");
186 sbv.append("" + value);
187 sbv.append("</" + name + ">\n");
188 }
189
190
191 private void appendNV(StringBuffer sbv, String sk, String name, double value) {
192 sbv.append(sk + "<" + name + ">");
193 sbv.append("" + value);
194 sbv.append("</" + name + ">\n");
195 }
196
197
198
199 private void appendNV(StringBuffer sbv, String sk, String name, String[] value) {
200 sbv.append(sk + "<" + name + ">\n");
201
202 for (int i = 0; i < value.length; i++) {
203 sbv.append(sk);
204 sbv.append(" ");
205 appendString(sbv, (value[i] != null ? value[i] : ""));
206 sbv.append("\n");
207 }
208 sbv.append(sk + "</" + name + ">\n");
209 }
210
211
212 private void appendNV(StringBuffer sbv, String sk, String name, int[] value) {
213 sbv.append(sk + "<" + name + ">");
214 for (int i = 0; i < value.length; i++) {
215 if (i % 16 == 0)
216 sbv.append("\n" + sk + " ");
217 sbv.append(" " + value[i] + " ");
218 }
219 sbv.append("\n");
220 sbv.append(sk + "</" + name + ">\n");
221 }
222
223
224 private void appendNV(StringBuffer sbv, String sk, String name, boolean[] value) {
225 sbv.append(sk + "<" + name + ">\n" + sk + " ");
226 for (int i = 0; i < value.length; i++) {
227 sbv.append(" " + (value[i] ? 1 : 0) + " ");
228 }
229 sbv.append("\n");
230 sbv.append(sk + "</" + name + ">\n");
231 }
232
233
234 private void appendNV(StringBuffer sbv, String sk, String name, double[] value) {
235 sbv.append(sk + "<" + name + ">");
236 for (int i = 0; i < value.length; i++) {
237 if (i % 4 == 0)
238 sbv.append("\n" + sk + " ");
239 sbv.append(" " + value[i] + " ");
240 }
241 sbv.append("\n");
242 sbv.append(sk + "</" + name + ">\n");
243 }
244
245
246
247 private void appendNV(StringBuffer sbv, String sk, String name, int[][] value) {
248 sbv.append(sk + "<" + name + ">\n");
249 for (int i = 0; i < value.length; i++) {
250 sbv.append(sk);
251 sbv.append(" <row>");
252 int[] ii = value[i];
253 for (int k = 0; k < ii.length; k++) {
254 if (k % 16 == 0)
255 sbv.append("\n " + sk + " ");
256 sbv.append(" " + ii[k] + " ");
257 }
258 sbv.append("\n");
259 sbv.append(sk);
260 sbv.append(" ");
261 sbv.append("</row>\n");
262 }
263 sbv.append(sk + "</" + name + ">\n");
264 }
265
266
267
268 private void appendNV(StringBuffer sbv, String sk, String name, double[][] value) {
269 sbv.append(sk + "<" + name + ">\n");
270 for (int i = 0; i < value.length; i++) {
271 sbv.append(sk);
272 sbv.append(" <row>");
273 double[] ii = value[i];
274 for (int k = 0; k < ii.length; k++) {
275 if (k % 4 == 0)
276 sbv.append("\n " + sk + " ");
277 sbv.append(" " + ii[k] + " ");
278 }
279 sbv.append("\n");
280 sbv.append(sk);
281 sbv.append(" ");
282 sbv.append("</row>\n");
283 }
284 sbv.append(sk + "</" + name + ">\n");
285
286 }
287
288
289
290 private void appendString(StringBuffer sbv, String sss) {
291 if (sss == null) {
292 if (quoteStrings) {
293 sbv.append("\"null\"");
294 } else {
295 sbv.append("null");
296 }
297
298 } else {
299 sss = StringEncoder.xmlEscape(sss);
300
301 if (!quoteStrings) {
302 sbv.append(sss);
303 } else {
304 sbv.append("\"");
305 sbv.append(sss);
306 sbv.append("\"");
307 }
308 }
309 }
310
311
312 }