1 package org.catacomb.serial.quickxml;
2
3
4 import org.catacomb.report.E;
5
6
7 public class Element {
8
9 String name;
10 String value;
11 Element[] elements;
12 boolean free;
13
14 public Element(String s) {
15 name = s;
16 elements = new Element[0];
17 free = false;
18 }
19
20
21 public Element(String s, String sv) {
22 name = s;
23 elements = new Element[0];
24 setValue(sv);
25 free = false;
26 }
27
28
29
30
31
32
33 public void setFree() {
34 free = true;
35 }
36
37
38 public boolean isFree() {
39 return free;
40 }
41
42
43 public void setValue(String txtin) {
44 String txt = txtin;
45 clear();
46 value = "";
47 if (txt != null) {
48 txt = txt.trim();
49
50 if (txt.startsWith("<")) {
51 addElementsFrom(txt);
52 } else {
53 value = txt;
54 }
55 }
56 }
57
58
59 public void addElementsFrom(String txtin) {
60 String txt = txtin;
61 txt = txt.trim();
62 if (txt.length() > 0) {
63 if (txt.startsWith("<")) {
64 txt = txt.substring(1);
65 int ic = txt.indexOf(">");
66 if (ic <= 0) {
67 E.error(" parsing element (no closing bracket) " + txt);
68
69 } else {
70 String enm = txt.substring(0, ic);
71 txt = txt.substring(ic + 1);
72
73 String sclo = "</" + enm + ">";
74 int iclo = txt.indexOf(sclo);
75 if (iclo >= 0) {
76 String ev = txt.substring(0, iclo);
77
78 addElement(enm, ev);
79 txt = txt.substring(iclo + sclo.length());
80 addElementsFrom(txt);
81 }
82 }
83 } else {
84 E.error(" - Element adding sublets needs < but got " + txt);
85 }
86 }
87 }
88
89
90
91 public void set(String eltname, String val) {
92 Element elt = getElement(eltname);
93 if (elt == null) {
94 E.error(" - no element " + eltname + "in " + this);
95
96 } else {
97 elt.setValue(val);
98 }
99 }
100
101
102
103 public void set(String e1name, String e2name, String val) {
104 getElement(e1name).set(e2name, val);
105 }
106
107
108 public void setSubelements(String enm, String[] vs) {
109 clear();
110 elements = new Element[vs.length];
111 for (int i = 0; i < vs.length; i++) {
112 elements[i] = new Element(enm);
113 elements[i].setValue(vs[i]);
114 }
115 }
116
117
118 public void clear() {
119 elements = new Element[0];
120 }
121
122
123 public String get() {
124 return value;
125 }
126
127
128 public String getValue() {
129 return value;
130 }
131
132
133 public String getName() {
134 return name;
135 }
136
137
138 public String get(String s) {
139 String ret = null;
140 Element elt = getElement(s);
141 if (elt != null) {
142 ret = elt.get();
143 }
144 return ret;
145 }
146
147
148 public String get(String s1, String s2) {
149 Element elt1 = getElement(s1);
150 Element elt2 = elt1.getElement(s2);
151 return elt2.get();
152 }
153
154
155 public String[] getAll(String s1, String s2) {
156 Element elt1 = getElement(s1);
157 return elt1.getAll(s2);
158 }
159
160
161 public String[] getValues(String s) {
162 return getAll(s);
163 }
164
165
166 public String[] getAll(String s) {
167 int nr = 0;
168 String[] retp = new String[elements.length];
169 for (int i = 0; i < elements.length; i++) {
170 if (elements[i].getName().equals(s)) {
171 retp[nr++] = elements[i].get();
172 }
173 }
174 String[] ret = new String[nr];
175 for (int i = 0; i < nr; i++) {
176 ret[i] = retp[i];
177 }
178 return ret;
179 }
180
181
182
183 public boolean hasElement(String s, String v) {
184 boolean ret = false;
185 for (int i = 0; i < elements.length; i++) {
186 if (elements[i].getName().equals(s)) {
187 if (elements[i].get().equals(v)) {
188 ret = true;
189 break;
190 }
191 }
192 }
193 return ret;
194 }
195
196
197 public boolean hasElement(String eltname) {
198 boolean ret = false;
199 for (int i = 0; i < elements.length; i++) {
200 if (elements[i].getName().equals(eltname)) {
201 ret = true;
202 break;
203 }
204 }
205 return ret;
206 }
207
208
209 public String getValue(String lname) {
210 return get(lname);
211 }
212
213
214
215 public Element[] getElements() {
216 return elements;
217 }
218
219
220 public void limitNumber(String eltname, int nmax) {
221 String[] sv = getAll(eltname);
222 int nel = sv.length;
223 int ntg = nel - nmax;
224 if (ntg > 0) {
225 Element[] ael = getElements();
226 int nlst = ael.length;
227 while (ntg > 0) {
228 nlst = nlst - 1;
229 if (ael[nlst].getName().equals(eltname)) {
230 ntg = ntg - 1;
231 removeElement(nlst);
232 }
233 }
234
235 }
236 }
237
238
239
240 public void removeAll(String eltname) {
241 Element[] ael = getElements();
242 for (int i = ael.length - 1; i >= 0; i--) {
243 if (ael[i].getName().equals(eltname)) {
244 removeElement(i);
245 }
246 }
247 }
248
249
250
251 public void add(Element elt) {
252 addElement(elt);
253 }
254
255
256 public void add(String eltname) {
257 addElement(new Element(eltname));
258 }
259
260
261 public void addElement(String eltname, String ev) {
262 addElement(new Element(eltname, ev));
263 }
264
265
266 public void addElement(String en1, String en2, String ev) {
267 getElement(en1).addElement(new Element(en2, ev));
268 }
269
270
271
272 public void prependElementUnique(String eltname, String ev) {
273 if (ev == null || ev.length() == 0) {
274 return;
275 }
276 String[] sv = getValues(eltname);
277 boolean got = false;
278 int itg = -1;
279 if (sv != null) {
280 for (int i = 0; i < sv.length; i++) {
281 if (ev.equals(sv[i])) {
282 got = true;
283 itg = i;
284 }
285 }
286 }
287 if (got) {
288 removeElement(itg);
289 }
290 prependElement(eltname, ev);
291 }
292
293
294
295 public void prependElement(String eltname, String ev) {
296 prependElement(new Element(eltname, ev));
297 }
298
299
300 public void addElement(Element elt) {
301 int n = elements.length;
302 Element[] en = new Element[n + 1];
303 for (int i = 0; i < n; i++) {
304 en[i] = elements[i];
305 }
306 en[n] = elt;
307 elements = en;
308 }
309
310
311 public void removeElement(int itg) {
312 int n = elements.length;
313 Element[] en = new Element[n - 1];
314 for (int i = 0; i < itg; i++) {
315 en[i] = elements[i];
316 }
317 for (int i = itg; i < n - 1; i++) {
318 en[i] = elements[i + 1];
319 }
320 elements = en;
321 }
322
323
324 public void setElement(String name, String value) {
325 removeAll(name);
326 addElement(name, value);
327
328 }
329
330
331
332 public void prependElement(Element elt) {
333 int n = elements.length;
334 Element[] en = new Element[n + 1];
335 for (int i = 0; i < n; i++) {
336 en[i + 1] = elements[i];
337 }
338 en[0] = elt;
339 elements = en;
340 }
341
342
343 public void randomFill() {
344 if (elements.length > 0) {
345 for (int i = 0; i < elements.length; i++) {
346 elements[i].randomFill();
347 }
348 } else {
349 value = "" + Math.random();
350 }
351 }
352
353
354 public Element getElement(String sn) {
355 Element ret = null;
356 for (int i = 0; i < elements.length; i++) {
357 if (elements[i].getName().equals(sn)) {
358 ret = elements[i];
359 }
360 }
361 return ret;
362 }
363
364
365 public void populateFrom(String srcin) {
366 String src = srcin;
367 Element elt = this;
368
369 String enm = elt.getName();
370 String otag = "<" + enm + ">";
371 String ctag = "</" + enm + ">";
372
373 int io = src.indexOf(otag);
374 if (io >= 0) {
375 src = src.substring(io + otag.length(), src.length());
376 int ic = src.indexOf(ctag);
377 if (ic > 0) {
378 src = src.substring(0, ic);
379
380 Element[] ea = elt.getElements();
381 if (!elt.isFree() && ea != null && ea.length > 0) {
382 for (int i = 0; i < ea.length; i++) {
383 ea[i].populateFrom(src);
384 }
385 } else {
386 elt.setValue(src);
387 }
388 }
389 }
390 }
391
392
393
394 public String dump() {
395 StringBuffer sb = new StringBuffer();
396
397 appendTo(sb, "");
398
399 return sb.toString();
400 }
401
402
403 private void appendTo(StringBuffer sb, String indent) {
404 sb.append(indent + "<" + name + ">");
405
406 int nel = elements.length;
407 if (nel > 0) {
408 sb.append("\n");
409 for (int i = 0; i < nel; i++) {
410 elements[i].appendTo(sb, indent + " ");
411 }
412 sb.append(indent);
413
414 } else {
415 String sv = value;
416 if (sv == null) {
417 sv = "";
418 }
419 sv = sv.trim();
420 sb.append(sv);
421 }
422 sb.append("</" + name + ">\n");
423 if (nel > 0) {
424 sb.append("\n");
425 }
426 }
427
428 }