-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXML.java
More file actions
1249 lines (1183 loc) · 34.9 KB
/
XML.java
File metadata and controls
1249 lines (1183 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package javaforce;
import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.tree.*;
import javax.swing.event.*;
/**
* XML is a TreeModel data model that encapsules a complete XML file.<br> Each
* XML tag (element) is treated as a node in the tree. Once read() it can be
* viewed and edited with a JTree. Then you can write() it back to a file. XML
* will monitor changes made and update nodes as needed. The read() functions
* include a callback interface so you can further tweak the layout of the XML
* tree.<br> Typical XML Tag:<br> <name [attributes...]> content |
* children </name><br> Singleton XML Tag: (no children)<br> <name
* [attributes...] /><br> Caveats:<br> Only leaf nodes can contain actual
* data (content) (in other words @XmlMixed is not supported).<br>
* Mixed tags are read, but when written the
* content is lost.<br> There must be only one root tag.<br> Support for the
* standard XML header is provided (see header).<br> readClass() and
* writeClass() support : int, short, byte, float, double, boolean, String,
* Color and Custom Classes.<br> Arrays of any of these.<br> All classes and
* fields MUST be public. static and transient members are skipped. No special
* annotations are required.
*/
public class XML implements TreeModelListener {
private DefaultTreeModel treemodel;
private boolean useContentForNameGlobal = false;
private boolean fireEvents = true;
private boolean ignoreEvents = false;
private class XMLTagPart {
private String content;
private String attrs;
public XMLTagPart() {
content = "";
attrs = "";
}
}
/**
* XMLEvent is an interface for a callback handler used during XML loading.
*/
public interface XMLEvent {
public void XMLTagAdded(XMLTag tag);
public void XMLTagRenamed(XMLTag tag);
};
/**
* XMLAttr is one attribute that is listed in each XML tag.
*/
public class XMLAttr {
public String name, value;
public XMLAttr() {
name = "";
value = "";
}
};
/**
* XMLTag is one node in the tree that represents one XML element or 'tag'.
*
* @param name the XML tag name
* @param attrs an ArrayList of XMLAttr
* @param uname the unique name of the tag. Usually equals name unless another
* child with the same parent has the same name. JTree uses uname to display
* the tags.
* @param content the data within the tags head/tail
* @param isLeaf set to force JTree to view node as a leaf
* @param isNotLeaf set to force JTree to view node that is expandable (even
* if it has no children)
* @param isReadOnly ignores edits from JTree
* @param useContentForName causes JTree to use content for display
*/
public class XMLTag extends DefaultMutableTreeNode {
public String name = "";
public ArrayList<XMLAttr> attrs;
public String uname = ""; //unique name (usually same as name)
public String content = "";
/**
* Tag is a singleton - no content - ie: <tag attrs.../>
*/
public boolean isSingle = false;
public boolean isNotLeaf = false;
public boolean isLeaf = false;
public boolean isReadOnly = false;
public boolean useContentForName = false;
/**
* Constructs a new XMLTag
*/
public XMLTag() {
attrs = new ArrayList<XMLAttr>();
}
/**
* Returns the unique name of the tag.
*/
public String toString() {
return getName();
}
/**
* Returns the parent of the tag. This method just overrides the default
* method but returns XMLTag.
*/
public XMLTag getParent() {
if (this == treemodel.getRoot()) {
return null; //in case setRoot() moved the root up
}
return (XMLTag) super.getParent();
}
/**
* Returns true if the node is a leaf. This method just overrides the
* default method and allows better leaf control.
*/
public boolean isLeaf() {
if (isNotLeaf) {
return false;
}
if (isLeaf) {
return true;
}
return (getChildCount() == 0);
}
/**
* Returns a unique name for this node.
*/
public String getName() {
if (useContentForName || useContentForNameGlobal) {
return content;
}
String argName = getArg("name");
if (argName != null) {
return argName;
}
return uname;
}
/**
* Returns a real name for this node (may not be unique).
*/
public String getXMLName() {
return name;
}
/**
* Sets the name for this node.
*/
public void setName(String newName) {
//check if name="..." is in attrs, else use name (and update uname)
XMLAttr attr;
boolean ok = false;
for (Iterator i = attrs.iterator(); i.hasNext();) {
attr = (XMLAttr) i.next();
if (attr.name.equals("name")) {
attr.value = newName;
ok = true;
break;
}
}
if (!ok) {
name = newName;
}
setuname(this);
}
/**
* Returns the child tag at index. This method just overrides the default
* method but returns XMLTag.
*/
public XMLTag getChildAt(int index) {
return (XMLTag) super.getChildAt(index);
}
/**
* Returns value of argument.
*/
public String getArg(String name) {
for (int a = 0; a < attrs.size(); a++) {
if (attrs.get(a).name.equals(name)) {
return attrs.get(a).value;
}
}
return null;
}
/**
* Sets argument to value.
*/
public void setArg(String name, String value) {
for (int a = 0; a < attrs.size(); a++) {
if (attrs.get(a).name.equals(name)) {
attrs.get(a).value = value;
return;
}
}
XMLAttr attr = new XMLAttr();
attr.name = name;
attr.value = value;
attrs.add(attr);
}
/**
* Returns the content of this node.
*/
public String getContent() {
return content;
}
/**
* Sets content.
*/
public void setContent(String newContent) {
content = newContent;
}
};
/**
* The header tag.<br> <?xml version="1.0" encoding="UTF-8" ?>
*/
public XMLTag header = new XMLTag();
/**
* The root tag.
*/
public XMLTag root = new XMLTag();
/**
* Constructs a new XML object.
*/
public XML() {
treemodel = new DefaultTreeModel(root);
treemodel.addTreeModelListener(this);
treemodel.setRoot(root);
}
/**
* Returns the TreeModel that can be passed to JTree constructor.
*/
public TreeModel getTreeModel() {
return treemodel;
}
public DefaultTreeModel getDefaultTreeModel() {
return treemodel;
}
private final int XML_OPEN = 1;
private final int XML_DATA = 2;
private final int XML_CLOSE = 3;
private final int XML_SINGLE = 4;
private int type, nexttype;
private XMLTagPart readtag(Reader rdr) {
boolean quote = false, isAttrs = false;
int ich;
char ch;
XMLTagPart tag = new XMLTagPart();
type = XML_DATA;
if (nexttype != -1) {
type = nexttype;
nexttype = -1;
}
while (true) {
try {
ich = rdr.read();
} catch (Exception e) {
break;
}
if (ich == -1) {
break;
}
ch = (char) ich;
switch (type) {
case XML_OPEN:
case XML_CLOSE:
case XML_SINGLE:
if (ch == '\"') {
quote = !quote;
}
if (!quote) {
if (ch == '/') {
if (tag.content.length() == 0) {
type = XML_CLOSE;
} else {
type = XML_SINGLE;
}
continue;
}
if (ch == '>') {
break;
}
}
if ((ch == ' ') && (!isAttrs)) {
isAttrs = true;
}
if (isAttrs) {
tag.attrs += ch;
} else {
tag.content += ch;
}
continue;
case XML_DATA:
if ((ch == '<') && (!quote)) {
if (tag.content.length() > 0) {
nexttype = XML_OPEN;
break;
}
type = XML_OPEN;
continue;
}
if (ch == '\"') {
if (quote) {
quote = false;
} else {
quote = true;
}
}
tag.content += ch;
continue;
}
break;
}
if (tag.content.length() == 0) {
return null; //EOF
}
if (type == XML_DATA) {
tag.content = decodeSafe(tag.content);
}
return tag;
}
private void string2attrs(XMLTag tag, String attrs) {
//search for name="value"
char ca[] = attrs.toCharArray();
XMLAttr attr;
String name, value;
int length = attrs.length();
int ep;
tag.attrs.clear();
for (int a = 0; a < length; a++) {
if (ca[a] == ' ') {
continue; //skip spaces
}
ep = attrs.indexOf('=', a);
if (ep == -1) {
return;
}
name = "";
for (int b = a; b < ep; b++) {
name += ca[b];
}
a = ep + 1;
value = "";
if (ca[a] == '\"') {
a++;
ep = attrs.indexOf('\"', a);
if (ep == -1) {
return;
}
for (int b = a; b < ep; b++) {
value += ca[b];
}
a = ep + 1;
} else if (ca[a] == '\'') {
a++;
ep = attrs.indexOf('\'', a);
if (ep == -1) {
return;
}
for (int b = a; b < ep; b++) {
value += ca[b];
}
a = ep + 1;
} else {
ep = attrs.indexOf(' ', a);
if (ep == -1) {
ep = length - 1;
}
if (ep <= a) {
return;
}
for (int b = a; b < ep; b++) {
value += ca[b];
}
a = ep + 1;
}
attr = new XMLAttr();
attr.name = name;
attr.value = value;
tag.attrs.add(attr);
}
}
private void setuname(XMLTag tag) {
XMLTag parent = tag.getParent();
String uname = tag.name;
XMLAttr attr;
for (Iterator i = tag.attrs.iterator(); i.hasNext();) {
attr = (XMLAttr) i.next();
if (attr.name.equalsIgnoreCase("name")) {
uname = attr.value;
break;
}
}
String orguname = uname;
if (parent == null) {
tag.uname = uname;
changedTag(tag);
return;
}
boolean ok;
int idx = 1;
int size = parent.getChildCount();
while (true) {
ok = true;
for (int a = 0; a < size; a++) {
XMLTag child = (XMLTag) parent.getChildAt(a);
if (child == tag) {
continue;
}
if (child.getName().equalsIgnoreCase(uname)) {
ok = false;
break;
}
}
if (ok) {
break;
}
uname = orguname + idx;
idx++;
}
tag.uname = uname;
changedTag(tag);
}
/**
* Reads the entire tree from a XML file from filename. No call handler is
* used.
*
* @param filename name of file to load XML data from
*/
public boolean read(String filename) {
return read(filename, null);
}
/**
* Reads the entire tree from a XML file from filename.
*
* @param filename name of file to load XML data from
* @param event callback handler to process each loaded XML tag
*/
public boolean read(String filename, XMLEvent event) {
FileInputStream fis;
boolean ret;
try {
fis = new FileInputStream(filename);
ret = read(fis, event);
fis.close();
} catch (Exception e) {
JFLog.log(e);
return false;
}
return ret;
}
/**
* Reads the entire tree from a XML file from the InputStream. No callback
* handler is used.
*
* @param in InputStream to load XML data from
*/
public boolean read(InputStream in) {
return read(in, null);
}
/**
* Reads the entire tree from a XML file from the InputStream.
*
* @param in InputStream to load XML data from
* @param event callback handler to process each loaded XML tag
*/
public boolean read(InputStream is, XMLEvent event) {
BufferedReader bris;
try {
bris = new BufferedReader(new InputStreamReader(is, "UTF-8"));
} catch (Exception e) {
JFLog.log(e);
return false;
}
this.event = event;
deleteAll();
type = XML_DATA;
nexttype = -1;
XMLTagPart tagpart;
XMLTag tag = null, newtag;
boolean bRoot = false;
boolean bHeader = false;
while (true) {
tagpart = readtag(bris);
if (tagpart == null) {
break;
}
switch (type) {
case XML_OPEN:
if (tagpart.content.startsWith("?xml")) {
if (bHeader) {
JFLog.log("XML:Multiple headers found");
return false;
} //already read the XML header
header.name = tagpart.content;
header.uname = header.name;
string2attrs(header, tagpart.attrs);
if (event != null) {
event.XMLTagAdded(header);
}
break;
}
//no break
case XML_SINGLE:
if (tag == null) {
//root tag
if (bRoot) {
JFLog.log("XML:Multiple root tags found");
return false;
} //already found a root tag
bRoot = true;
root.name = tagpart.content;
root.uname = root.name;
string2attrs(root, tagpart.attrs);
if (event != null) {
event.XMLTagAdded(root);
}
changedTag(root);
tag = root;
} else {
newtag = new XMLTag();
newtag.name = tagpart.content;
string2attrs(newtag, tagpart.attrs);
addTag(tag, newtag);
if (type == XML_SINGLE) {
newtag.isSingle = true;
} else {
tag = newtag;
}
}
break;
case XML_CLOSE:
if (tag == null) {
JFLog.log("XML:tag closed but never opened");
return false;
} //bad xml file
if (!tagpart.content.equalsIgnoreCase(tag.name)) {
JFLog.log("XML:tag closed doesn't match open");
return false;
} //unmatched closing tag
tag = (XMLTag) tag.getParent();
break;
case XML_DATA:
if (tag == null) {
continue; //could happen after header and before root tag
}
tag.content += tagpart.content;
break;
}
}
if (tag != null) {
JFLog.log("XML:tag left open");
return false;
} //tag left open
return true;
}
private String attrs2string(XMLTag tag) {
XMLAttr attr;
int size = tag.attrs.size();
String str = "", tmp;
for (int a = 0; a < size; a++) {
attr = tag.attrs.get(a);
tmp = " " + attr.name + "=\"" + attr.value + "\"";
str += tmp;
}
return str;
}
private void writestr(OutputStream out, String str) {
try {
out.write(str.getBytes("UTF-8"));
} catch (Exception e) {
}
}
private int indent;
private void writetag(OutputStream out, XMLTag tag) {
String tmp;
tmp = "";
for (int a = 0; a < indent; a++) {
tmp += ' ';
}
writestr(out, tmp);
int size = tag.getChildCount();
String attrs;
if (size > 0) {
//write open tag w/ attrs + content
attrs = attrs2string(tag);
tmp = "<" + tag.name + attrs + ">\n";
writestr(out, tmp);
indent += 2;
//write children
for (int a = 0; a < size; a++) {
writetag(out, (XMLTag) tag.getChildAt(a));
}
//write close tag
indent -= 2;
tmp = "";
for (int a = 0; a < indent; a++) {
tmp += ' ';
}
writestr(out, tmp);
tmp = "</" + tag.name + ">\n";
writestr(out, tmp);
} else {
attrs = attrs2string(tag);
if (tag.isSingle) {
tmp = "<" + tag.name + attrs + "/>\n";
} else {
tmp = "<" + tag.name + attrs + ">" + encodeSafe(tag.content) + "</" + tag.name + ">\n";
}
writestr(out, tmp);
}
}
/**
* Writes the entire tree as a XML file to the filename.
*/
public boolean write(String filename) {
FileOutputStream fos;
boolean ret;
try {
fos = new FileOutputStream(filename);
ret = write(fos);
fos.close();
} catch (Exception e) {
JFLog.log(e);
return false;
}
return ret;
}
/**
* Writes the entire tree as a XML file to the OutputStream.
*/
public boolean write(OutputStream os) {
BufferedOutputStream bos = new BufferedOutputStream(os);
String tmp, attrs;
if (root.name.length() == 0) {
return false;
}
if (header.name.length() > 0) {
attrs = attrs2string(header);
tmp = "<" + header.name + attrs + ">\n";
writestr(bos, tmp);
}
//write root header
attrs = attrs2string(root);
tmp = "<" + root.name + attrs + ">\n";
writestr(bos, tmp);
int size = root.getChildCount();
indent = 2;
for (int a = 0; a < size; a++) {
writetag(bos, (XMLTag) root.getChildAt(a));
}
//write root tail
tmp = "</" + root.name + ">\n";
writestr(bos, tmp);
try {
bos.flush();
} catch (Exception e) {
} //must flush or it's lost
return true;
}
private void clearTag(XMLTag tag) {
tag.name = "";
tag.attrs = new ArrayList<XMLAttr>();
tag.uname = "";
tag.content = "";
}
/**
* Deletes the entire tree and resets the root and header tags.
*/
public void deleteAll() {
deleteTag(root);
clearTag(header);
changedTag(header);
clearTag(root);
changedTag(root);
}
/**
* Deletes a node from the parent. Also deletes all it's children.
*/
public boolean deleteTag(XMLTag tag) {
//remove children first
while (tag.getChildCount() > 0) {
deleteTag((XMLTag) tag.getChildAt(0));
}
//now remove itself from parent
if (tag.getParent() == null) {
return true; //can not delete root/header tag itself
}
treemodel.removeNodeFromParent(tag);
return true;
}
private XMLEvent event = null;
/**
* Creates an empty node that can be inserted into the tree using addTag().
*/
public XMLTag createTag() {
return new XMLTag(); //must call addTag() to add to the tree
}
/**
* Adds the node to the targetParent.
*/
public XMLTag addTag(XMLTag targetParent, XMLTag tag) {
treemodel.insertNodeInto(tag, targetParent, targetParent.getChildCount());
setuname(tag);
if (event != null) {
event.XMLTagAdded(tag);
}
return tag;
}
/**
* Adds node with the name, attrs and content specified. If another node
* already exists with the same name the new node's unique name will differ.
*/
public XMLTag addTag(XMLTag targetParent, String name, String attrs, String content) {
XMLTag newtag = new XMLTag();
newtag.name = name;
newtag.content = content;
string2attrs(newtag, attrs);
return addTag(targetParent, newtag);
}
/**
* Adds (a non-existing) or sets (an existing) node with the name, attrs and
* content specified.
*/
public XMLTag addSetTag(XMLTag targetParent, String name, String attrs, String content) {
XMLTag child;
int len = targetParent.getChildCount();
for (int a = 0; a < len; a++) {
child = targetParent.getChildAt(a);
if (child.name.equals(name)) {
setTag(child, name, attrs, content);
return child;
}
}
return addTag(targetParent, name, attrs, content);
}
/**
* Notify the treemodel that you changed a node.
*/
public void changedTag(XMLTag tag) {
if (!fireEvents) return;
ignoreEvents = true;
treemodel.nodeChanged(tag);
ignoreEvents = false;
}
/**
* Sets the name, attrs and contents of the true root node.
*/
public void setRoot(String name, String attrs, String content) {
root.name = name;
root.uname = name;
string2attrs(root, attrs);
root.content = content;
changedTag(root);
}
/**
* Returns the unique name of a node.
*/
public String getName(XMLTag tag) {
return tag.getName();
}
/**
* Sets the name of a node. It's unique name may differ when shown in a tree.
*/
public void setName(XMLTag tag, String newName) {
tag.setName(newName);
}
/**
* Returns a node based on the TreePath path;
*/
public XMLTag getTag(TreePath path) {
return getTag(path.getPath());
}
/**
* Returns a node based on the objs[] path. Relative to virtual root tag. (see
* setRoot())
*/
public XMLTag getTag(Object objs[]) {
XMLTag tag = (XMLTag) treemodel.getRoot(), child;
String name;
if (objs == null || objs.length == 0) {
return null;
}
name = tag.getName();
if (!name.equals(objs[0].toString())) {
return null;
}
int idx = 1;
boolean ok;
int cnt;
while (idx < objs.length) {
ok = false;
cnt = tag.getChildCount();
for (int i = 0; i < cnt; i++) {
child = (XMLTag) tag.getChildAt(i);
name = child.getName();
if (name.equals(objs[idx].toString())) {
ok = true;
idx++;
tag = child;
break;
}
}
if (!ok) {
return null; //next path element not found
}
}
return tag;
}
/**
* Sets the name, attrs and content for an existing node.
*/
public void setTag(XMLTag tag, String name, String attrs, String content) {
tag.name = name;
string2attrs(tag, attrs);
tag.content = content;
if (tag.getParent() != null) {
setuname(tag);
} else {
tag.uname = name;
changedTag(tag);
}
if (event != null) {
event.XMLTagAdded(tag);
}
}
/**
* Sets the root node for the tree. This doesn't effect the true root node.
* This is usefull in hiding parts of a XML file from view when viewed in a
* JTree.
*/
public void setRoot(XMLTag newRoot) {
treemodel.setRoot(newRoot);
}
/**
* Forces getName() to return the tags content instead of the actual name.
* This causes JTree to display the content instead of the tag name.
*
* @param state
*/
public void setUseContentForName(boolean state) {
useContentForNameGlobal = true;
}
/**
* Forces getName() to return the tags content instead of the actual name.
* This causes JTree to display the content instead of the tag name.
*/
public boolean getUseContentForName() {
return useContentForNameGlobal;
}
/**
* Writes all children of tag to object.
*/
public void writeClass(XMLTag tag, Object obj) {
Class<?> c = obj.getClass();
Class<?> fc, fcc;
Field f;
int childCnt = tag.getChildCount();
XMLTag child;
Object i, array[], newArray[];
String name, typeString;
for (int a = 0; a < childCnt; a++) {
try {
child = tag.getChildAt(a);
int childChildcnt = child.getChildCount();
name = child.getXMLName();
f = c.getField(name);
if (f == null) {
JFLog.log("XML:field not found:" + name);
continue;
}
if (childChildcnt > 0) {
fc = f.getType();
if (fc.isArray()) {
fcc = fc.getComponentType();
i = fcc.newInstance();
writeClass(child, i);
array = (Object[]) f.get(obj);
if ((array == null) || (array.length == 0)) {
newArray = (Object[]) Array.newInstance(fcc, 1);
newArray[0] = i;
} else {
newArray = Arrays.copyOf(array, array.length + 1);
newArray[array.length] = i;
}
f.set(obj, newArray);
} else {
Object childObject = fc.newInstance();
writeClass(child, childObject);
f.set(obj, childObject);
}
} else {
typeString = f.toGenericString();
if (typeString.indexOf(" int ") != -1) {
f.setInt(obj, Integer.valueOf(child.content));
} else if (typeString.indexOf(" short ") != -1) {
f.setShort(obj, (short) JF.atoi(child.content));
} else if (typeString.indexOf(" byte ") != -1) {
f.setByte(obj, (byte) JF.atoi(child.content));
} else if (typeString.indexOf(" float ") != -1) {
f.setFloat(obj, Float.valueOf(child.content));
} else if (typeString.indexOf(" double ") != -1) {
f.setDouble(obj, Double.valueOf(child.content));
} else if (typeString.indexOf(" boolean ") != -1) {
f.setBoolean(obj, child.content.equalsIgnoreCase("true"));
} else if (typeString.indexOf(" java.lang.String ") != -1) {
f.set(obj, child.content);
} else if (typeString.indexOf(" java.awt.Color ") != -1) {
f.set(obj, new Color(JF.atox(child.content)));
} else if (typeString.indexOf(" int[] ") != -1) {
int array2[] = (int[]) f.get(obj);
int idx;
if (array2 == null) {
idx = 0;
array2 = new int[1];
} else {
idx = array2.length;
array2 = Arrays.copyOf(array2, array2.length + 1);
}
array2[idx] = Integer.valueOf(child.content);
f.set(obj, array2);
} else if (typeString.indexOf(" short[] ") != -1) {
short array2[] = (short[]) f.get(obj);
int idx;
if (array2 == null) {
idx = 0;
array2 = new short[1];
} else {
idx = array2.length;
array2 = Arrays.copyOf(array2, array2.length + 1);
}
array2[idx] = Short.valueOf(child.content);
f.set(obj, array2);
} else if (typeString.indexOf(" byte[] ") != -1) {
byte array2[] = (byte[]) f.get(obj);
int idx;
if (array2 == null) {
idx = 0;
array2 = new byte[1];
} else {
idx = array2.length;
array2 = Arrays.copyOf(array2, array2.length + 1);
}
array2[idx] = Byte.valueOf(child.content);
f.set(obj, array2);
} else if (typeString.indexOf(" float[] ") != -1) {
float array2[] = (float[]) f.get(obj);
int idx;
if (array2 == null) {
idx = 0;
array2 = new float[1];
} else {
idx = array2.length;
array2 = Arrays.copyOf(array2, array2.length + 1);
}