-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathXML.java
More file actions
1157 lines (970 loc) · 32.9 KB
/
XML.java
File metadata and controls
1157 lines (970 loc) · 32.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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012 The Processing Foundation
Copyright (c) 2009-12 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.data;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import processing.core.PApplet;
/**
* This is the base class used for the Processing XML library,
* representing a single node of an XML tree.
*
* @webref data:composite
* @see PApplet#loadXML(String)
* @see PApplet#parseXML(String)
* @see PApplet#saveXML(XML, String)
*/
public class XML implements Serializable {
/** The internal representation, a DOM node. */
protected Node node;
// /** Cached locally because it's used often. */
// protected String name;
/** The parent element. */
protected XML parent;
/** Child elements, once loaded. */
protected XML[] children;
/**
* @nowebref
*/
protected XML() { }
// /**
// * Begin parsing XML data passed in from a PApplet. This code
// * wraps exception handling, for more advanced exception handling,
// * use the constructor that takes a Reader or InputStream.
// *
// * @throws SAXException
// * @throws ParserConfigurationException
// * @throws IOException
// */
// public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException {
// this(parent.createReader(filename));
// }
/**
* Advanced users only; use loadXML() in PApplet. This is not a supported
* function and is subject to change. It is available simply for users that
* would like to handle the exceptions in a particular way.
*
* @nowebref
*/
public XML(File file) throws IOException, ParserConfigurationException, SAXException {
this(file, null);
}
/**
* Advanced users only; use loadXML() in PApplet.
*
* @nowebref
*/
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
this(PApplet.createReader(file), options);
}
/**
* @nowebref
*/
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
this(input, null);
}
/**
* Unlike the loadXML() method in PApplet, this version works with files
* that are not in UTF-8 format.
*
* @nowebref
*/
public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException {
//this(PApplet.createReader(input), options); // won't handle non-UTF8
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
// Prevent 503 errors from www.w3.org
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (IllegalArgumentException e) {
// ignore this; Android doesn't like it
}
factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(input));
node = document.getDocumentElement();
}
/**
* Advanced users only; use loadXML() in PApplet.
*
* @nowebref
*/
public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException {
this(reader, null);
}
/**
* Advanced users only; use loadXML() in PApplet.
*
* Added extra code to handle \u2028 (Unicode NLF), which is sometimes
* inserted by web browsers (Safari?) and not distinguishable from a "real"
* LF (or CRLF) in some text editors (i.e. TextEdit on OS X). Only doing
* this for XML (and not all Reader objects) because LFs are essential.
* https://github.com/processing/processing/issues/2100
*
* @nowebref
*/
public XML(final Reader reader, String options) throws IOException, ParserConfigurationException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Prevent 503 errors from www.w3.org
try {
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
} catch (IllegalArgumentException e) {
// ignore this; Android doesn't like it
}
// without a validating DTD, this doesn't do anything since it doesn't know what is ignorable
// factory.setIgnoringElementContentWhitespace(true);
factory.setExpandEntityReferences(false);
// factory.setExpandEntityReferences(true);
// factory.setCoalescing(true);
// builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
DocumentBuilder builder = factory.newDocumentBuilder();
// builder.setEntityResolver()
// SAXParserFactory spf = SAXParserFactory.newInstance();
// spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
// SAXParser p = spf.newSAXParser();
// builder = DocumentBuilderFactory.newDocumentBuilder();
// builder = new SAXBuilder();
// builder.setValidation(validating);
Document document = builder.parse(new InputSource(new Reader() {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int count = reader.read(cbuf, off, len);
for (int i = 0; i < count; i++) {
if (cbuf[off+i] == '\u2028') {
cbuf[off+i] = '\n';
}
}
return count;
}
@Override
public void close() throws IOException {
reader.close();
}
}));
node = document.getDocumentElement();
}
/**
* @param name creates a node with this name
*
*/
public XML(String name) {
try {
// TODO is there a more efficient way of doing this? wow.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
node = document.createElement(name);
this.parent = null;
} catch (ParserConfigurationException pce) {
throw new RuntimeException(pce);
}
}
/**
* @nowebref
*/
protected XML(XML parent, Node node) {
this.node = node;
this.parent = parent;
for (String attr : parent.listAttributes()) {
if (attr.startsWith("xmlns")) {
// Copy namespace attributes to the kids, otherwise this XML
// can no longer be printed (or manipulated in most ways).
// Only do this when it's an Element, otherwise it's trying to set
// attributes on text notes (interstitial content).
if (node instanceof Element) {
setString(attr, parent.getString(attr));
}
}
}
}
/**
* @webref xml:method
* @brief Converts String content to an XML object
* @param data the content to be parsed as XML
* @return an XML object, or null
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
* @nowebref
*/
static public XML parse(String data) throws IOException, ParserConfigurationException, SAXException {
return XML.parse(data, null);
}
/**
* @nowebref
*/
static public XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException {
return new XML(new StringReader(data), null);
}
// protected boolean save(OutputStream output) {
// return write(PApplet.createWriter(output));
// }
public boolean save(File file) {
return save(file, null);
}
public boolean save(File file, String options) {
PrintWriter writer = PApplet.createWriter(file);
boolean result = write(writer);
writer.flush();
writer.close();
return result;
}
// Sends this object and its kids to a Writer with an indent of 2 spaces,
// including the declaration at the top so that the output will be valid XML.
public boolean write(PrintWriter output) {
output.print(format(2));
output.flush();
return true;
}
/**
* Returns the parent element. This method returns null for the root
* element.
*
* @webref xml:method
* @brief Gets a copy of the element's parent
*/
public XML getParent() {
return this.parent;
}
/**
* Internal function; not included in reference.
*/
protected Object getNative() {
return node;
}
/**
* Returns the full name (i.e. the name including an eventual namespace
* prefix) of the element.
*
* @webref xml:method
* @brief Gets the element's full name
* @return the name, or null if the element only contains #PCDATA.
*/
public String getName() {
// return name;
return node.getNodeName();
}
/**
* @webref xml:method
* @brief Sets the element's name
*/
public void setName(String newName) {
Document document = node.getOwnerDocument();
node = document.renameNode(node, null, newName);
// name = node.getNodeName();
}
/**
* Returns the name of the element (without namespace prefix).
*
* Internal function; not included in reference.
*/
public String getLocalName() {
return node.getLocalName();
}
/**
* Honey, can you just check on the kids? Thanks.
*
* Internal function; not included in reference.
*/
protected void checkChildren() {
if (children == null) {
NodeList kids = node.getChildNodes();
int childCount = kids.getLength();
children = new XML[childCount];
for (int i = 0; i < childCount; i++) {
children[i] = new XML(this, kids.item(i));
}
}
}
/**
* Returns the number of children.
*
* @webref xml:method
* @brief Returns the element's number of children
* @return the count.
*/
public int getChildCount() {
checkChildren();
return children.length;
}
/**
* Returns a boolean of whether or not there are children.
*
* @webref xml:method
* @brief Checks whether or not an element has any children
*/
public boolean hasChildren() {
checkChildren();
return children.length > 0;
}
/**
* Put the names of all children into an array. Same as looping through
* each child and calling getName() on each XMLElement.
*
* @webref xml:method
* @brief Returns the names of all children as an array
*/
public String[] listChildren() {
// NodeList children = node.getChildNodes();
// int childCount = children.getLength();
// String[] outgoing = new String[childCount];
// for (int i = 0; i < childCount; i++) {
// Node kid = children.item(i);
// if (kid.getNodeType() == Node.ELEMENT_NODE) {
// outgoing[i] = kid.getNodeName();
// } // otherwise just leave him null
// }
checkChildren();
String[] outgoing = new String[children.length];
for (int i = 0; i < children.length; i++) {
outgoing[i] = children[i].getName();
}
return outgoing;
}
/**
* Returns an array containing all the child elements.
*
* @webref xml:method
* @brief Returns an array containing all child elements
*/
public XML[] getChildren() {
// NodeList children = node.getChildNodes();
// int childCount = children.getLength();
// XMLElement[] kids = new XMLElement[childCount];
// for (int i = 0; i < childCount; i++) {
// Node kid = children.item(i);
// kids[i] = new XMLElement(this, kid);
// }
// return kids;
checkChildren();
return children;
}
/**
* Quick accessor for an element at a particular index.
*
* @nowebref
*/
public XML getChild(int index) {
checkChildren();
return children[index];
}
/**
* Get a child by its name or path.
*
* @webref xml:method
* @brief Returns the child element with the specified index value or path
* @param name element name or path/to/element
* @return the first matching element or null if no match
*/
public XML getChild(String name) {
if (name.length() > 0 && name.charAt(0) == '/') {
throw new IllegalArgumentException("getChild() should not begin with a slash");
}
if (name.indexOf('/') != -1) {
return getChildRecursive(PApplet.split(name, '/'), 0);
}
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
XML kid = getChild(i);
String kidName = kid.getName();
if (kidName != null && kidName.equals(name)) {
return kid;
}
}
return null;
}
/**
* Internal helper function for getChild(String).
*
* @param items result of splitting the query on slashes
* @param offset where in the items[] array we're currently looking
* @return matching element or null if no match
* @author processing.org
*/
protected XML getChildRecursive(String[] items, int offset) {
// if it's a number, do an index instead
if (Character.isDigit(items[offset].charAt(0))) {
XML kid = getChild(Integer.parseInt(items[offset]));
if (offset == items.length-1) {
return kid;
} else {
return kid.getChildRecursive(items, offset+1);
}
}
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
XML kid = getChild(i);
String kidName = kid.getName();
if (kidName != null && kidName.equals(items[offset])) {
if (offset == items.length-1) {
return kid;
} else {
return kid.getChildRecursive(items, offset+1);
}
}
}
return null;
}
/**
* Get any children that match this name or path. Similar to getChild(),
* but will grab multiple matches rather than only the first.
*
* @param name element name or path/to/element
* @return array of child elements that match
* @author processing.org
*/
public XML[] getChildren(String name) {
if (name.length() > 0 && name.charAt(0) == '/') {
throw new IllegalArgumentException("getChildren() should not begin with a slash");
}
if (name.indexOf('/') != -1) {
return getChildrenRecursive(PApplet.split(name, '/'), 0);
}
// if it's a number, do an index instead
// (returns a single element array, since this will be a single match
if (Character.isDigit(name.charAt(0))) {
return new XML[] { getChild(Integer.parseInt(name)) };
}
int childCount = getChildCount();
XML[] matches = new XML[childCount];
int matchCount = 0;
for (int i = 0; i < childCount; i++) {
XML kid = getChild(i);
String kidName = kid.getName();
if (kidName != null && kidName.equals(name)) {
matches[matchCount++] = kid;
}
}
return (XML[]) PApplet.subset(matches, 0, matchCount);
}
protected XML[] getChildrenRecursive(String[] items, int offset) {
if (offset == items.length-1) {
return getChildren(items[offset]);
}
XML[] matches = getChildren(items[offset]);
XML[] outgoing = new XML[0];
for (int i = 0; i < matches.length; i++) {
XML[] kidMatches = matches[i].getChildrenRecursive(items, offset+1);
outgoing = (XML[]) PApplet.concat(outgoing, kidMatches);
}
return outgoing;
}
/**
* @webref xml:method
* @brief Appends a new child to the element
*/
public XML addChild(String tag) {
Document document = node.getOwnerDocument();
Node newChild = document.createElement(tag);
return appendChild(newChild);
}
public XML addChild(XML child) {
Document document = node.getOwnerDocument();
Node newChild = document.importNode((Node) child.getNative(), true);
return appendChild(newChild);
}
/** Internal handler to add the node structure. */
protected XML appendChild(Node newNode) {
node.appendChild(newNode);
XML newbie = new XML(this, newNode);
if (children != null) {
children = (XML[]) PApplet.concat(children, new XML[] { newbie });
}
return newbie;
}
/**
* @webref xml:method
* @brief Removes the specified child
*/
public void removeChild(XML kid) {
node.removeChild(kid.node);
children = null; // TODO not efficient
}
/**
* Removes whitespace nodes.
* Those whitespace nodes are required to reconstruct the original XML's spacing and indentation.
* If you call this and use saveXML() your original spacing will be gone.
*
* @nowebref
* @brief Removes whitespace nodes
*/
public void trim() {
try {
XPathFactory xpathFactory = XPathFactory.newInstance();
XPathExpression xpathExp =
xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
NodeList emptyTextNodes = (NodeList)
xpathExp.evaluate(node, XPathConstants.NODESET);
// Remove each empty text node from document.
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
Node emptyTextNode = emptyTextNodes.item(i);
emptyTextNode.getParentNode().removeChild(emptyTextNode);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// /** Remove whitespace nodes. */
// public void trim() {
////// public static boolean isWhitespace(XML xml) {
////// if (xml.node.getNodeType() != Node.TEXT_NODE)
////// return false;
////// Matcher m = whitespace.matcher(xml.node.getNodeValue());
////// return m.matches();
////// }
//// trim(this);
//// }
//
// checkChildren();
// int index = 0;
// for (int i = 0; i < children.length; i++) {
// if (i != index) {
// children[index] = children[i];
// }
// Node childNode = (Node) children[i].getNative();
// if (childNode.getNodeType() != Node.TEXT_NODE ||
// children[i].getContent().trim().length() > 0) {
// children[i].trim();
// index++;
// }
// }
// if (index != children.length) {
// children = (XML[]) PApplet.subset(children, 0, index);
// }
//
// // possibility, but would have to re-parse the object
//// helpdesk.objects.com.au/java/how-do-i-remove-whitespace-from-an-xml-document
//// TransformerFactory factory = TransformerFactory.newInstance();
//// Transformer transformer = factory.newTransformer(new StreamSource("strip-space.xsl"));
//// DOMSource source = new DOMSource(document);
//// StreamResult result = new StreamResult(System.out);
//// transformer.transform(source, result);
//
//// <xsl:stylesheet version="1.0"
//// xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
//// <xsl:output method="xml" omit-xml-declaration="yes"/>
//// <xsl:strip-space elements="*"/>
//// <xsl:template match="@*|node()">
//// <xsl:copy>
//// <xsl:apply-templates select="@*|node()"/>
//// </xsl:copy>
//// </xsl:template>
//// </xsl:stylesheet>
// }
/**
* Returns the number of attributes.
*
* @webref xml:method
* @brief Counts the specified element's number of attributes
*/
public int getAttributeCount() {
return node.getAttributes().getLength();
}
/**
* Get a list of the names for all of the attributes for this node.
*
* @webref xml:method
* @brief Returns a list of names of all attributes as an array
*/
public String[] listAttributes() {
NamedNodeMap nnm = node.getAttributes();
String[] outgoing = new String[nnm.getLength()];
for (int i = 0; i < outgoing.length; i++) {
outgoing[i] = nnm.item(i).getNodeName();
}
return outgoing;
}
/**
* Returns whether an attribute exists.
*
* @webref xml:method
* @brief Checks whether or not an element has the specified attribute
*/
public boolean hasAttribute(String name) {
return (node.getAttributes().getNamedItem(name) != null);
}
/**
* Returns the value of an attribute.
*
* @param name the non-null name of the attribute.
* @return the value, or null if the attribute does not exist.
*/
// public String getAttribute(String name) {
// return this.getAttribute(name, null);
// }
/**
* Returns the value of an attribute.
*
* @param name the non-null full name of the attribute.
* @param defaultValue the default value of the attribute.
* @return the value, or defaultValue if the attribute does not exist.
*/
// public String getAttribute(String name, String defaultValue) {
// Node attr = node.getAttributes().getNamedItem(name);
// return (attr == null) ? defaultValue : attr.getNodeValue();
// }
/**
* @webref xml:method
* @brief Gets the content of an attribute as a String
*/
public String getString(String name) {
return getString(name, null);
}
public String getString(String name, String defaultValue) {
NamedNodeMap attrs = node.getAttributes();
if (attrs != null) {
Node attr = attrs.getNamedItem(name);
if (attr != null) {
return attr.getNodeValue();
}
}
return defaultValue;
}
/**
* @webref xml:method
* @brief Sets the content of an attribute as a String
*/
public void setString(String name, String value) {
((Element) node).setAttribute(name, value);
}
/**
* @webref xml:method
* @brief Gets the content of an attribute as an int
*/
public int getInt(String name) {
return getInt(name, 0);
}
/**
* @webref xml:method
* @brief Sets the content of an attribute as an int
*/
public void setInt(String name, int value) {
setString(name, String.valueOf(value));
}
/**
* Returns the value of an attribute.
*
* @param name the non-null full name of the attribute
* @param defaultValue the default value of the attribute
* @return the value, or defaultValue if the attribute does not exist
*/
public int getInt(String name, int defaultValue) {
String value = getString(name);
return (value == null) ? defaultValue : Integer.parseInt(value);
}
/**
* @webref xml:method
* @brief Sets the content of an element as an int
*/
public void setLong(String name, long value) {
setString(name, String.valueOf(value));
}
/**
* Returns the value of an attribute.
*
* @param name the non-null full name of the attribute.
* @param defaultValue the default value of the attribute.
* @return the value, or defaultValue if the attribute does not exist.
*/
public long getLong(String name, long defaultValue) {
String value = getString(name);
return (value == null) ? defaultValue : Long.parseLong(value);
}
/**
* Returns the value of an attribute, or zero if not present.
*
* @webref xml:method
* @brief Gets the content of an attribute as a float
*/
public float getFloat(String name) {
return getFloat(name, 0);
}
/**
* Returns the value of an attribute.
*
* @param name the non-null full name of the attribute.
* @param defaultValue the default value of the attribute.
* @return the value, or defaultValue if the attribute does not exist.
*/
public float getFloat(String name, float defaultValue) {
String value = getString(name);
return (value == null) ? defaultValue : Float.parseFloat(value);
}
/**
* @webref xml:method
* @brief Sets the content of an attribute as a float
*/
public void setFloat(String name, float value) {
setString(name, String.valueOf(value));
}
public double getDouble(String name) {
return getDouble(name, 0);
}
/**
* Returns the value of an attribute.
*
* @param name the non-null full name of the attribute
* @param defaultValue the default value of the attribute
* @return the value, or defaultValue if the attribute does not exist
*/
public double getDouble(String name, double defaultValue) {
String value = getString(name);
return (value == null) ? defaultValue : Double.parseDouble(value);
}
public void setDouble(String name, double value) {
setString(name, String.valueOf(value));
}
/**
* Return the #PCDATA content of the element. If the element has a
* combination of #PCDATA content and child elements, the #PCDATA
* sections can be retrieved as unnamed child objects. In this case,
* this method returns null.
*
* @webref xml:method
* @brief Gets the content of an element
* @return the content.
* @see XML#getIntContent()
* @see XML#getFloatContent()
*/
public String getContent() {
return node.getTextContent();
}
public String getContent(String defaultValue) {
String s = node.getTextContent();
return (s != null) ? s : defaultValue;
}
/**
* @webref xml:method
* @brief Gets the content of an element as an int
* @return the content.
* @see XML#getContent()
* @see XML#getFloatContent()
*/
public int getIntContent() {
return getIntContent(0);
}
/**
* @param defaultValue the default value of the attribute
*/
public int getIntContent(int defaultValue) {
return PApplet.parseInt(node.getTextContent(), defaultValue);
}
/**
* @webref xml:method
* @brief Gets the content of an element as a float
* @return the content.
* @see XML#getContent()
* @see XML#getIntContent()
*/
public float getFloatContent() {
return getFloatContent(0);
}
/**
* @param defaultValue the default value of the attribute
*/
public float getFloatContent(float defaultValue) {
return PApplet.parseFloat(node.getTextContent(), defaultValue);
}
public long getLongContent() {
return getLongContent(0);
}
public long getLongContent(long defaultValue) {
String c = node.getTextContent();
if (c != null) {
try {
return Long.parseLong(c);
} catch (NumberFormatException nfe) { }
}
return defaultValue;
}
public double getDoubleContent() {
return getDoubleContent(0);
}
public double getDoubleContent(double defaultValue) {
String c = node.getTextContent();
if (c != null) {
try {
return Double.parseDouble(c);
} catch (NumberFormatException nfe) { }
}
return defaultValue;
}
/**
* @webref xml:method
* @brief Sets the content of an element
*/
public void setContent(String text) {
node.setTextContent(text);
}
public void setIntContent(int value) {
setContent(String.valueOf(value));
}
public void setFloatContent(float value) {
setContent(String.valueOf(value));
}
public void setLongContent(long value) {
setContent(String.valueOf(value));
}
public void setDoubleContent(double value) {
setContent(String.valueOf(value));
}
/**
* Format this XML data as a String.
*