Skip to content

Commit 7c7c17c

Browse files
committed
add get/setContent() for XML
1 parent df77ff0 commit 7c7c17c

File tree

4 files changed

+166
-8
lines changed

4 files changed

+166
-8
lines changed

android/core/src/processing/data/XML.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*
4444
* @webref data:composite
4545
* @see PApplet#loadXML(String)
46+
* @see PApplet#parseXML(String)
47+
* @see PApplet#saveXML(XML, String)
4648
*/
4749
public class XML implements Serializable {
4850

@@ -75,16 +77,26 @@ protected XML() { }
7577
// this(parent.createReader(filename));
7678
// }
7779

80+
81+
/**
82+
* @param file description TBD
83+
*/
7884
public XML(File file) throws IOException, ParserConfigurationException, SAXException {
7985
this(file, null);
8086
}
8187

8288

89+
/**
90+
* @param options description TBD
91+
*/
8392
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
8493
this(PApplet.createReader(file), options);
8594
}
8695

8796

97+
/**
98+
* @param input description TBD
99+
*/
88100
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
89101
this(input, null);
90102
}
@@ -138,6 +150,9 @@ protected XML(Reader reader, String options) throws IOException, ParserConfigura
138150
}
139151

140152

153+
/**
154+
* @param name description TBD
155+
*/
141156
// TODO is there a more efficient way of doing this? wow.
142157
public XML(String name) throws ParserConfigurationException {
143158
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -749,6 +764,58 @@ public String getContent() {
749764
}
750765

751766

767+
public int getIntContent() {
768+
return getIntContent(0);
769+
}
770+
771+
772+
public int getIntContent(int defaultValue) {
773+
return PApplet.parseInt(node.getTextContent(), defaultValue);
774+
}
775+
776+
777+
public float getFloatContent() {
778+
return getFloatContent(0);
779+
}
780+
781+
782+
public float getFloatContent(float defaultValue) {
783+
return PApplet.parseFloat(node.getTextContent(), defaultValue);
784+
}
785+
786+
787+
public long getLongContent() {
788+
return getLongContent(0);
789+
}
790+
791+
792+
public long getLongContent(long defaultValue) {
793+
String c = node.getTextContent();
794+
if (c != null) {
795+
try {
796+
return Long.parseLong(c);
797+
} catch (NumberFormatException nfe) { }
798+
}
799+
return defaultValue;
800+
}
801+
802+
803+
public double getDoubleContent() {
804+
return getDoubleContent(0);
805+
}
806+
807+
808+
public double getDoubleContent(double defaultValue) {
809+
String c = node.getTextContent();
810+
if (c != null) {
811+
try {
812+
return Double.parseDouble(c);
813+
} catch (NumberFormatException nfe) { }
814+
}
815+
return defaultValue;
816+
}
817+
818+
752819
/**
753820
* @webref xml:method
754821
* @brief Sets the content of an element
@@ -758,6 +825,26 @@ public void setContent(String text) {
758825
}
759826

760827

828+
public void setIntContent(int value) {
829+
setContent(String.valueOf(value));
830+
}
831+
832+
833+
public void setFloatContent(float value) {
834+
setContent(String.valueOf(value));
835+
}
836+
837+
838+
public void setLongContent(long value) {
839+
setContent(String.valueOf(value));
840+
}
841+
842+
843+
public void setDoubleContent(double value) {
844+
setContent(String.valueOf(value));
845+
}
846+
847+
761848
/**
762849
* Format this XML data as a String.
763850
*

core/src/processing/data/XML.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,58 @@ public String getContent() {
765765
}
766766

767767

768+
public int getIntContent() {
769+
return getIntContent(0);
770+
}
771+
772+
773+
public int getIntContent(int defaultValue) {
774+
return PApplet.parseInt(node.getTextContent(), defaultValue);
775+
}
776+
777+
778+
public float getFloatContent() {
779+
return getFloatContent(0);
780+
}
781+
782+
783+
public float getFloatContent(float defaultValue) {
784+
return PApplet.parseFloat(node.getTextContent(), defaultValue);
785+
}
786+
787+
788+
public long getLongContent() {
789+
return getLongContent(0);
790+
}
791+
792+
793+
public long getLongContent(long defaultValue) {
794+
String c = node.getTextContent();
795+
if (c != null) {
796+
try {
797+
return Long.parseLong(c);
798+
} catch (NumberFormatException nfe) { }
799+
}
800+
return defaultValue;
801+
}
802+
803+
804+
public double getDoubleContent() {
805+
return getDoubleContent(0);
806+
}
807+
808+
809+
public double getDoubleContent(double defaultValue) {
810+
String c = node.getTextContent();
811+
if (c != null) {
812+
try {
813+
return Double.parseDouble(c);
814+
} catch (NumberFormatException nfe) { }
815+
}
816+
return defaultValue;
817+
}
818+
819+
768820
/**
769821
* @webref xml:method
770822
* @brief Sets the content of an element
@@ -774,6 +826,26 @@ public void setContent(String text) {
774826
}
775827

776828

829+
public void setIntContent(int value) {
830+
setContent(String.valueOf(value));
831+
}
832+
833+
834+
public void setFloatContent(float value) {
835+
setContent(String.valueOf(value));
836+
}
837+
838+
839+
public void setLongContent(long value) {
840+
setContent(String.valueOf(value));
841+
}
842+
843+
844+
public void setDoubleContent(double value) {
845+
setContent(String.valueOf(value));
846+
}
847+
848+
777849
/**
778850
* Format this XML data as a String.
779851
*

core/todo.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ X https://github.com/processing/processing/issues/1682
66
A image caches not being properly disposed (weak references broken?)
77
X http://code.google.com/p/processing/issues/detail?id=1353
88
A https://github.com/processing/processing/issues/1391
9+
X implement content specifiers
10+
X getIntContent()
11+
X getFloatContent()
12+
X getContent() or getStringContent()?
913

1014
_ tint() with JAVA2D does not automatically refresh (with possible fix)
1115
_ https://github.com/processing/processing/issues/1730
1216

13-
_ implement content specifiers
14-
getIntContent()
15-
getFloatContent()
16-
getContent() or getStringContent()?
17+
_ table writing twice when .csv is added
18+
_ https://github.com/processing/processing/issues/1734
1719

1820
finish json support
1921
_ https://github.com/processing/processing/issues/1660

todo.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ X incorporate JDI changes from Manindra
88
X fix autoformat to indent like the p5 book/examples
99
X http://code.google.com/p/processing/issues/detail?id=325
1010
X https://github.com/processing/processing/issues/364
11-
X fix problem with retina checking on Java 7
12-
13-
_ table writing twice when .csv is added
14-
_ https://github.com/processing/processing/issues/1734
11+
X fix NPE problem with retina checking on Java 7
1512

1613
_ / breaks syntax highlighting
1714
_ https://github.com/processing/processing/issues/1681

0 commit comments

Comments
 (0)