propane 3.11.0-java → 4.0.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +1 -1
- data/CHANGELOG.md +2 -0
- data/README.md +5 -5
- data/Rakefile +1 -1
- data/lib/propane/app.rb +2 -2
- data/lib/propane/version.rb +1 -1
- data/lib/propane-4.0.0.jar +0 -0
- data/library/slider/slider.rb +1 -1
- data/pom.rb +8 -8
- data/pom.xml +8 -8
- data/propane.gemspec +3 -3
- data/src/main/java/monkstone/ColorUtil.java +1 -1
- data/src/main/java/monkstone/MathToolModule.java +1 -1
- data/src/main/java/monkstone/PropaneLibrary.java +1 -1
- data/src/main/java/monkstone/fastmath/DegLutTables.java +10 -11
- data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
- data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
- data/src/main/java/monkstone/noise/LICENSE +121 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SliderBar.java +1 -1
- data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
- data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
- data/src/main/java/monkstone/vecmath/package-info.java +1 -1
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +92 -68
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
- data/src/main/java/monkstone/videoevent/CaptureEvent.java +1 -1
- data/src/main/java/monkstone/videoevent/MovieEvent.java +1 -1
- data/src/main/java/monkstone/videoevent/package-info.java +1 -1
- data/src/main/java/processing/awt/PGraphicsJava2D.java +0 -1
- data/src/main/java/processing/awt/PImageAWT.java +2 -4
- data/src/main/java/processing/core/PApplet.java +4 -4
- data/src/main/java/processing/core/PImage.java +3025 -3047
- data/src/main/java/processing/core/PMatrix.java +5 -2
- data/src/main/java/processing/data/DoubleDict.java +72 -43
- data/src/main/java/processing/data/DoubleList.java +6 -2
- data/src/main/java/processing/data/FloatDict.java +744 -756
- data/src/main/java/processing/data/FloatList.java +68 -26
- data/src/main/java/processing/data/IntDict.java +72 -45
- data/src/main/java/processing/data/IntList.java +63 -26
- data/src/main/java/processing/data/JSONArray.java +892 -931
- data/src/main/java/processing/data/JSONObject.java +1169 -1262
- data/src/main/java/processing/data/JSONTokener.java +30 -49
- data/src/main/java/processing/data/LongDict.java +699 -712
- data/src/main/java/processing/data/LongList.java +676 -700
- data/src/main/java/processing/data/Sort.java +1 -0
- data/src/main/java/processing/data/Table.java +4040 -3661
- data/src/main/java/processing/data/TableRow.java +16 -0
- data/src/main/java/processing/data/XML.java +1041 -956
- data/src/main/java/processing/event/TouchEvent.java +1 -1
- data/src/main/java/processing/opengl/FontTexture.java +2 -2
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +15 -18
- data/src/main/java/processing/opengl/PJOGL.java +2 -2
- data/src/main/java/processing/opengl/PShapeOpenGL.java +23 -24
- data/test/vecmath_spec_test.rb +14 -3
- data/vendors/Rakefile +1 -1
- metadata +9 -8
- data/lib/propane-3.11.0.jar +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
2
|
|
3
|
-
/*
|
3
|
+
/*
|
4
4
|
Part of the Processing project - http://processing.org
|
5
5
|
|
6
6
|
Copyright (c) 2012 The Processing Foundation
|
@@ -19,8 +19,7 @@
|
|
19
19
|
Public License along with this library; if not, write to the
|
20
20
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
21
21
|
Boston, MA 02111-1307 USA
|
22
|
-
*/
|
23
|
-
|
22
|
+
*/
|
24
23
|
package processing.data;
|
25
24
|
|
26
25
|
import java.io.*;
|
@@ -35,14 +34,14 @@ import javax.xml.transform.dom.*;
|
|
35
34
|
import javax.xml.transform.stream.*;
|
36
35
|
import javax.xml.xpath.XPathConstants;
|
37
36
|
import javax.xml.xpath.XPathExpression;
|
37
|
+
import javax.xml.xpath.XPathExpressionException;
|
38
38
|
import javax.xml.xpath.XPathFactory;
|
39
39
|
|
40
40
|
import processing.core.PApplet;
|
41
41
|
|
42
|
-
|
43
42
|
/**
|
44
|
-
* This is the base class used for the Processing XML library,
|
45
|
-
*
|
43
|
+
* This is the base class used for the Processing XML library, representing a
|
44
|
+
* single node of an XML tree.
|
46
45
|
*
|
47
46
|
* @webref data:composite
|
48
47
|
* @see PApplet#loadXML(String)
|
@@ -51,23 +50,28 @@ import processing.core.PApplet;
|
|
51
50
|
*/
|
52
51
|
public class XML implements Serializable {
|
53
52
|
|
54
|
-
|
55
|
-
|
53
|
+
/**
|
54
|
+
* The internal representation, a DOM node.
|
55
|
+
*/
|
56
|
+
protected Node node;
|
56
57
|
|
57
58
|
// /** Cached locally because it's used often. */
|
58
59
|
// protected String name;
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
60
|
+
/**
|
61
|
+
* The parent element.
|
62
|
+
*/
|
63
|
+
protected XML parent;
|
64
|
+
|
65
|
+
/**
|
66
|
+
* Child elements, once loaded.
|
67
|
+
*/
|
68
|
+
protected XML[] children;
|
69
|
+
|
70
|
+
/**
|
71
|
+
* @nowebref
|
72
|
+
*/
|
73
|
+
protected XML() {
|
74
|
+
}
|
71
75
|
|
72
76
|
// /**
|
73
77
|
// * Begin parsing XML data passed in from a PApplet. This code
|
@@ -81,324 +85,359 @@ public class XML implements Serializable {
|
|
81
85
|
// public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException {
|
82
86
|
// this(parent.createReader(filename));
|
83
87
|
// }
|
88
|
+
/**
|
89
|
+
* Advanced users only; use loadXML() in PApplet.This is not a supported
|
90
|
+
* function and is subject to change.It is available simply for users that
|
91
|
+
* would like to handle the exceptions in a particular way.
|
92
|
+
*
|
93
|
+
* @param file
|
94
|
+
* @throws java.io.IOException
|
95
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
96
|
+
* @throws org.xml.sax.SAXException
|
97
|
+
* @nowebref
|
98
|
+
*/
|
99
|
+
public XML(File file) throws IOException, ParserConfigurationException, SAXException {
|
100
|
+
this(file, null);
|
101
|
+
}
|
84
102
|
|
103
|
+
/**
|
104
|
+
* Advanced users only; use loadXML() in PApplet.
|
105
|
+
*
|
106
|
+
* @param file
|
107
|
+
* @param options
|
108
|
+
* @throws java.io.IOException
|
109
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
110
|
+
* @throws org.xml.sax.SAXException
|
111
|
+
* @nowebref
|
112
|
+
*/
|
113
|
+
public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
|
114
|
+
this(PApplet.createReader(file), options);
|
115
|
+
}
|
85
116
|
|
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
|
-
* Advanced users only; use loadXML() in PApplet.
|
141
|
-
*
|
142
|
-
* @nowebref
|
143
|
-
*/
|
144
|
-
public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException {
|
145
|
-
this(reader, null);
|
146
|
-
}
|
147
|
-
|
148
|
-
|
149
|
-
/**
|
150
|
-
* Advanced users only; use loadXML() in PApplet.
|
151
|
-
*
|
152
|
-
* Added extra code to handle \u2028 (Unicode NLF), which is sometimes
|
153
|
-
* inserted by web browsers (Safari?) and not distinguishable from a "real"
|
154
|
-
* LF (or CRLF) in some text editors (i.e. TextEdit on OS X). Only doing
|
155
|
-
* this for XML (and not all Reader objects) because LFs are essential.
|
156
|
-
* https://github.com/processing/processing/issues/2100
|
157
|
-
*
|
158
|
-
* @nowebref
|
159
|
-
*/
|
160
|
-
public XML(final Reader reader, String options) throws IOException, ParserConfigurationException, SAXException {
|
161
|
-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
162
|
-
|
163
|
-
// Prevent 503 errors from www.w3.org
|
164
|
-
try {
|
165
|
-
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
166
|
-
} catch (IllegalArgumentException e) {
|
167
|
-
// ignore this; Android doesn't like it
|
168
|
-
}
|
169
|
-
|
170
|
-
// without a validating DTD, this doesn't do anything since it doesn't know what is ignorable
|
171
|
-
// factory.setIgnoringElementContentWhitespace(true);
|
117
|
+
/**
|
118
|
+
* @param input
|
119
|
+
* @throws java.io.IOException
|
120
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
121
|
+
* @throws org.xml.sax.SAXException
|
122
|
+
* @nowebref
|
123
|
+
*/
|
124
|
+
public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
|
125
|
+
this(input, null);
|
126
|
+
}
|
127
|
+
|
128
|
+
/**
|
129
|
+
* Unlike the loadXML() method in PApplet, this version works with files
|
130
|
+
* that are not in UTF-8 format.
|
131
|
+
*
|
132
|
+
* @param input
|
133
|
+
* @param options
|
134
|
+
* @throws java.io.IOException
|
135
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
136
|
+
* @throws org.xml.sax.SAXException
|
137
|
+
* @nowebref
|
138
|
+
*/
|
139
|
+
public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException {
|
140
|
+
//this(PApplet.createReader(input), options); // won't handle non-UTF8
|
141
|
+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
142
|
+
|
143
|
+
try {
|
144
|
+
// Prevent 503 errors from www.w3.org
|
145
|
+
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
146
|
+
} catch (IllegalArgumentException e) {
|
147
|
+
// ignore this; Android doesn't like it
|
148
|
+
}
|
149
|
+
|
150
|
+
factory.setExpandEntityReferences(false);
|
151
|
+
DocumentBuilder builder = factory.newDocumentBuilder();
|
152
|
+
Document document = builder.parse(new InputSource(input));
|
153
|
+
node = document.getDocumentElement();
|
154
|
+
}
|
155
|
+
|
156
|
+
/**
|
157
|
+
* Advanced users only; use loadXML() in PApplet.
|
158
|
+
*
|
159
|
+
* @param reader
|
160
|
+
* @throws java.io.IOException
|
161
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
162
|
+
* @throws org.xml.sax.SAXException
|
163
|
+
* @nowebref
|
164
|
+
*/
|
165
|
+
public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException {
|
166
|
+
this(reader, null);
|
167
|
+
}
|
172
168
|
|
173
|
-
|
169
|
+
/**
|
170
|
+
* Advanced users only; use loadXML() in PApplet.Added extra code to handle
|
171
|
+
* (Unicode NLF), which is sometimes inserted by web browsers (Safari?) and
|
172
|
+
* not distinguishable from a "real" LF (or CRLF) in some text editors
|
173
|
+
* (i.e.TextEdit on OS X).Only doing this for XML (and not all Reader
|
174
|
+
* objects) because LFs are
|
175
|
+
* essential.https://github.com/processing/processing/issues/2100
|
176
|
+
*
|
177
|
+
*
|
178
|
+
* @param reader
|
179
|
+
* @param options
|
180
|
+
* @throws java.io.IOException
|
181
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
182
|
+
* @throws org.xml.sax.SAXException
|
183
|
+
* @nowebref
|
184
|
+
*/
|
185
|
+
public XML(final Reader reader, String options) throws IOException, ParserConfigurationException, SAXException {
|
186
|
+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
187
|
+
|
188
|
+
// Prevent 503 errors from www.w3.org
|
189
|
+
try {
|
190
|
+
factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
191
|
+
} catch (IllegalArgumentException e) {
|
192
|
+
// ignore this; Android doesn't like it
|
193
|
+
}
|
194
|
+
|
195
|
+
// without a validating DTD, this doesn't do anything since it doesn't know what is ignorable
|
196
|
+
// factory.setIgnoringElementContentWhitespace(true);
|
197
|
+
factory.setExpandEntityReferences(false);
|
174
198
|
// factory.setExpandEntityReferences(true);
|
175
199
|
|
176
200
|
// factory.setCoalescing(true);
|
177
201
|
// builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
178
|
-
|
202
|
+
DocumentBuilder builder = factory.newDocumentBuilder();
|
179
203
|
// builder.setEntityResolver()
|
180
204
|
|
181
205
|
// SAXParserFactory spf = SAXParserFactory.newInstance();
|
182
206
|
// spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
183
207
|
// SAXParser p = spf.newSAXParser();
|
208
|
+
// builder = DocumentBuilderFactory.newDocumentBuilder();
|
209
|
+
// builder = new SAXBuilder();
|
210
|
+
// builder.setValidation(validating);
|
211
|
+
Document document = builder.parse(new InputSource(new Reader() {
|
212
|
+
@Override
|
213
|
+
public int read(char[] cbuf, int off, int len) throws IOException {
|
214
|
+
int count = reader.read(cbuf, off, len);
|
215
|
+
for (int i = 0; i < count; i++) {
|
216
|
+
if (cbuf[off + i] == '\u2028') {
|
217
|
+
cbuf[off + i] = '\n';
|
218
|
+
}
|
219
|
+
}
|
220
|
+
return count;
|
221
|
+
}
|
222
|
+
|
223
|
+
@Override
|
224
|
+
public void close() throws IOException {
|
225
|
+
reader.close();
|
226
|
+
}
|
227
|
+
}));
|
228
|
+
node = document.getDocumentElement();
|
229
|
+
}
|
184
230
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
@Override
|
202
|
-
public void close() throws IOException {
|
203
|
-
reader.close();
|
204
|
-
}
|
205
|
-
}));
|
206
|
-
node = document.getDocumentElement();
|
207
|
-
}
|
208
|
-
|
209
|
-
|
210
|
-
/**
|
211
|
-
* @param name creates a node with this name
|
212
|
-
*
|
213
|
-
*/
|
214
|
-
public XML(String name) {
|
215
|
-
try {
|
216
|
-
// TODO is there a more efficient way of doing this? wow.
|
217
|
-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
218
|
-
DocumentBuilder builder = factory.newDocumentBuilder();
|
219
|
-
Document document = builder.newDocument();
|
220
|
-
node = document.createElement(name);
|
221
|
-
this.parent = null;
|
222
|
-
|
223
|
-
} catch (ParserConfigurationException pce) {
|
224
|
-
throw new RuntimeException(pce);
|
225
|
-
}
|
226
|
-
}
|
227
|
-
|
228
|
-
/**
|
229
|
-
* @nowebref
|
230
|
-
*/
|
231
|
-
protected XML(XML parent, Node node) {
|
232
|
-
this.node = node;
|
233
|
-
this.parent = parent;
|
234
|
-
|
235
|
-
for (String attr : parent.listAttributes()) {
|
236
|
-
if (attr.startsWith("xmlns")) {
|
237
|
-
// Copy namespace attributes to the kids, otherwise this XML
|
238
|
-
// can no longer be printed (or manipulated in most ways).
|
239
|
-
// Only do this when it's an Element, otherwise it's trying to set
|
240
|
-
// attributes on text notes (interstitial content).
|
241
|
-
if (node instanceof Element) {
|
242
|
-
setString(attr, parent.getString(attr));
|
231
|
+
/**
|
232
|
+
* @param name creates a node with this name
|
233
|
+
*
|
234
|
+
*/
|
235
|
+
public XML(String name) {
|
236
|
+
try {
|
237
|
+
// TODO is there a more efficient way of doing this? wow.
|
238
|
+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
239
|
+
DocumentBuilder builder = factory.newDocumentBuilder();
|
240
|
+
Document document = builder.newDocument();
|
241
|
+
node = document.createElement(name);
|
242
|
+
this.parent = null;
|
243
|
+
|
244
|
+
} catch (ParserConfigurationException pce) {
|
245
|
+
throw new RuntimeException(pce);
|
243
246
|
}
|
244
|
-
}
|
245
247
|
}
|
246
|
-
}
|
247
|
-
|
248
248
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
249
|
+
/**
|
250
|
+
* @param parent
|
251
|
+
* @param node
|
252
|
+
* @nowebref
|
253
|
+
*/
|
254
|
+
protected XML(XML parent, Node node) {
|
255
|
+
this.node = node;
|
256
|
+
this.parent = parent;
|
257
|
+
|
258
|
+
for (String attr : parent.listAttributes()) {
|
259
|
+
if (attr.startsWith("xmlns")) {
|
260
|
+
// Copy namespace attributes to the kids, otherwise this XML
|
261
|
+
// can no longer be printed (or manipulated in most ways).
|
262
|
+
// Only do this when it's an Element, otherwise it's trying to set
|
263
|
+
// attributes on text notes (interstitial content).
|
264
|
+
if (node instanceof Element) {
|
265
|
+
setString(attr, parent.getString(attr));
|
266
|
+
}
|
267
|
+
}
|
268
|
+
}
|
269
|
+
}
|
262
270
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
271
|
+
/**
|
272
|
+
* @webref xml:method
|
273
|
+
* @brief Converts String content to an XML object
|
274
|
+
* @param data the content to be parsed as XML
|
275
|
+
* @return an XML object, or null
|
276
|
+
* @throws SAXException
|
277
|
+
* @throws ParserConfigurationException
|
278
|
+
* @throws IOException
|
279
|
+
* @nowebref
|
280
|
+
*/
|
281
|
+
static public XML parse(String data) throws IOException, ParserConfigurationException, SAXException {
|
282
|
+
return XML.parse(data, null);
|
283
|
+
}
|
269
284
|
|
285
|
+
/**
|
286
|
+
* @param data
|
287
|
+
* @param options
|
288
|
+
* @return
|
289
|
+
* @throws java.io.IOException
|
290
|
+
* @throws javax.xml.parsers.ParserConfigurationException
|
291
|
+
* @throws org.xml.sax.SAXException
|
292
|
+
* @nowebref
|
293
|
+
*/
|
294
|
+
static public XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException {
|
295
|
+
return new XML(new StringReader(data), null);
|
296
|
+
}
|
270
297
|
|
271
298
|
// protected boolean save(OutputStream output) {
|
272
299
|
// return write(PApplet.createWriter(output));
|
273
300
|
// }
|
301
|
+
/**
|
302
|
+
*
|
303
|
+
* @param file
|
304
|
+
* @return
|
305
|
+
*/
|
306
|
+
public boolean save(File file) {
|
307
|
+
return save(file, null);
|
308
|
+
}
|
309
|
+
|
310
|
+
/**
|
311
|
+
*
|
312
|
+
* @param file
|
313
|
+
* @param options
|
314
|
+
* @return
|
315
|
+
*/
|
316
|
+
public boolean save(File file, String options) {
|
317
|
+
boolean result;
|
318
|
+
try (PrintWriter writer = PApplet.createWriter(file)) {
|
319
|
+
result = write(writer);
|
320
|
+
writer.flush();
|
321
|
+
}
|
322
|
+
return result;
|
323
|
+
}
|
274
324
|
|
325
|
+
// Sends this object and its kids to a Writer with an indent of 2 spaces,
|
326
|
+
// including the declaration at the top so that the output will be valid XML.
|
327
|
+
/**
|
328
|
+
*
|
329
|
+
* @param output
|
330
|
+
* @return
|
331
|
+
*/
|
332
|
+
public boolean write(PrintWriter output) {
|
333
|
+
output.print(format(2));
|
334
|
+
output.flush();
|
335
|
+
return true;
|
336
|
+
}
|
275
337
|
|
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
|
-
public XML getParent() {
|
307
|
-
return this.parent;
|
308
|
-
}
|
309
|
-
|
310
|
-
/**
|
311
|
-
* Internal function; not included in reference.
|
312
|
-
*/
|
313
|
-
protected Object getNative() {
|
314
|
-
return node;
|
315
|
-
}
|
316
|
-
|
317
|
-
|
318
|
-
/**
|
319
|
-
* Returns the full name (i.e. the name including an eventual namespace
|
320
|
-
* prefix) of the element.
|
321
|
-
*
|
322
|
-
* @webref xml:method
|
323
|
-
* @brief Gets the element's full name
|
324
|
-
* @return the name, or null if the element only contains #PCDATA.
|
325
|
-
*/
|
326
|
-
public String getName() {
|
338
|
+
/**
|
339
|
+
* Returns the parent element.This method returns null for the root element.
|
340
|
+
*
|
341
|
+
* @return
|
342
|
+
* @webref xml:method
|
343
|
+
* @brief Gets a copy of the element's parent
|
344
|
+
*/
|
345
|
+
public XML getParent() {
|
346
|
+
return this.parent;
|
347
|
+
}
|
348
|
+
|
349
|
+
/**
|
350
|
+
* Internal function; not included in reference.
|
351
|
+
*
|
352
|
+
* @return
|
353
|
+
*/
|
354
|
+
protected Object getNative() {
|
355
|
+
return node;
|
356
|
+
}
|
357
|
+
|
358
|
+
/**
|
359
|
+
* Returns the full name (i.e. the name including an eventual namespace
|
360
|
+
* prefix) of the element.
|
361
|
+
*
|
362
|
+
* @webref xml:method
|
363
|
+
* @brief Gets the element's full name
|
364
|
+
* @return the name, or null if the element only contains #PCDATA.
|
365
|
+
*/
|
366
|
+
public String getName() {
|
327
367
|
// return name;
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
368
|
+
return node.getNodeName();
|
369
|
+
}
|
370
|
+
|
371
|
+
/**
|
372
|
+
* @param newName
|
373
|
+
* @webref xml:method
|
374
|
+
* @brief Sets the element's name
|
375
|
+
*/
|
376
|
+
public void setName(String newName) {
|
377
|
+
Document document = node.getOwnerDocument();
|
378
|
+
node = document.renameNode(node, null, newName);
|
338
379
|
// name = node.getNodeName();
|
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
|
-
public String[] listChildren() {
|
380
|
+
}
|
381
|
+
|
382
|
+
/**
|
383
|
+
* Returns the name of the element (without namespace prefix).Internal
|
384
|
+
* function; not included in reference.
|
385
|
+
*
|
386
|
+
* @return
|
387
|
+
*/
|
388
|
+
public String getLocalName() {
|
389
|
+
return node.getLocalName();
|
390
|
+
}
|
391
|
+
|
392
|
+
/**
|
393
|
+
* Honey, can you just check on the kids? Thanks.
|
394
|
+
*
|
395
|
+
* Internal function; not included in reference.
|
396
|
+
*/
|
397
|
+
protected void checkChildren() {
|
398
|
+
if (children == null) {
|
399
|
+
NodeList kids = node.getChildNodes();
|
400
|
+
int childCount = kids.getLength();
|
401
|
+
children = new XML[childCount];
|
402
|
+
for (int i = 0; i < childCount; i++) {
|
403
|
+
children[i] = new XML(this, kids.item(i));
|
404
|
+
}
|
405
|
+
}
|
406
|
+
}
|
407
|
+
|
408
|
+
/**
|
409
|
+
* Returns the number of children.
|
410
|
+
*
|
411
|
+
* @webref xml:method
|
412
|
+
* @brief Returns the element's number of children
|
413
|
+
* @return the count.
|
414
|
+
*/
|
415
|
+
public int getChildCount() {
|
416
|
+
checkChildren();
|
417
|
+
return children.length;
|
418
|
+
}
|
419
|
+
|
420
|
+
/**
|
421
|
+
* Returns a boolean of whether or not there are children.
|
422
|
+
*
|
423
|
+
* @return
|
424
|
+
* @webref xml:method
|
425
|
+
* @brief Checks whether or not an element has any children
|
426
|
+
*/
|
427
|
+
public boolean hasChildren() {
|
428
|
+
checkChildren();
|
429
|
+
return children.length > 0;
|
430
|
+
}
|
431
|
+
|
432
|
+
/**
|
433
|
+
* Put the names of all children into an array.Same as looping through each
|
434
|
+
* child and calling getName() on each XMLElement.
|
435
|
+
*
|
436
|
+
* @return
|
437
|
+
* @webref xml:method
|
438
|
+
* @brief Returns the names of all children as an array
|
439
|
+
*/
|
440
|
+
public String[] listChildren() {
|
402
441
|
// NodeList children = node.getChildNodes();
|
403
442
|
// int childCount = children.getLength();
|
404
443
|
// String[] outgoing = new String[childCount];
|
@@ -408,22 +447,22 @@ public class XML implements Serializable {
|
|
408
447
|
// outgoing[i] = kid.getNodeName();
|
409
448
|
// } // otherwise just leave him null
|
410
449
|
// }
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
450
|
+
checkChildren();
|
451
|
+
String[] outgoing = new String[children.length];
|
452
|
+
for (int i = 0; i < children.length; i++) {
|
453
|
+
outgoing[i] = children[i].getName();
|
454
|
+
}
|
455
|
+
return outgoing;
|
456
|
+
}
|
457
|
+
|
458
|
+
/**
|
459
|
+
* Returns an array containing all the child elements.
|
460
|
+
*
|
461
|
+
* @return
|
462
|
+
* @webref xml:method
|
463
|
+
* @brief Returns an array containing all child elements
|
464
|
+
*/
|
465
|
+
public XML[] getChildren() {
|
427
466
|
// NodeList children = node.getChildNodes();
|
428
467
|
// int childCount = children.getLength();
|
429
468
|
// XMLElement[] kids = new XMLElement[childCount];
|
@@ -432,194 +471,204 @@ public class XML implements Serializable {
|
|
432
471
|
// kids[i] = new XMLElement(this, kid);
|
433
472
|
// }
|
434
473
|
// return kids;
|
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
|
-
|
474
|
+
checkChildren();
|
475
|
+
return children;
|
476
|
+
}
|
477
|
+
|
478
|
+
/**
|
479
|
+
* Quick accessor for an element at a particular index.
|
480
|
+
*
|
481
|
+
* @param index
|
482
|
+
* @return
|
483
|
+
* @webref xml:method
|
484
|
+
* @brief Returns the child element with the specified index value or path
|
485
|
+
*/
|
486
|
+
public XML getChild(int index) {
|
487
|
+
checkChildren();
|
488
|
+
return children[index];
|
489
|
+
}
|
490
|
+
|
491
|
+
/**
|
492
|
+
* Get a child by its name or path.
|
493
|
+
*
|
494
|
+
* @param name element name or path/to/element
|
495
|
+
* @return the first matching element or null if no match
|
496
|
+
*/
|
497
|
+
public XML getChild(String name) {
|
498
|
+
if (name.length() > 0 && name.charAt(0) == '/') {
|
499
|
+
throw new IllegalArgumentException("getChild() should not begin with a slash");
|
500
|
+
}
|
501
|
+
if (name.indexOf('/') != -1) {
|
502
|
+
return getChildRecursive(PApplet.split(name, '/'), 0);
|
503
|
+
}
|
504
|
+
int childCount = getChildCount();
|
505
|
+
for (int i = 0; i < childCount; i++) {
|
506
|
+
XML kid = getChild(i);
|
507
|
+
String kidName = kid.getName();
|
508
|
+
if (kidName != null && kidName.equals(name)) {
|
509
|
+
return kid;
|
510
|
+
}
|
511
|
+
}
|
512
|
+
return null;
|
513
|
+
}
|
514
|
+
|
515
|
+
/**
|
516
|
+
* Internal helper function for getChild(String).
|
517
|
+
*
|
518
|
+
* @param items result of splitting the query on slashes
|
519
|
+
* @param offset where in the items[] array we're currently looking
|
520
|
+
* @return matching element or null if no match
|
521
|
+
* @author processing.org
|
522
|
+
*/
|
523
|
+
protected XML getChildRecursive(String[] items, int offset) {
|
524
|
+
// if it's a number, do an index instead
|
525
|
+
if (Character.isDigit(items[offset].charAt(0))) {
|
526
|
+
XML kid = getChild(Integer.parseInt(items[offset]));
|
527
|
+
if (offset == items.length - 1) {
|
528
|
+
return kid;
|
529
|
+
} else {
|
530
|
+
return kid.getChildRecursive(items, offset + 1);
|
531
|
+
}
|
532
|
+
}
|
533
|
+
int childCount = getChildCount();
|
534
|
+
for (int i = 0; i < childCount; i++) {
|
535
|
+
XML kid = getChild(i);
|
536
|
+
String kidName = kid.getName();
|
537
|
+
if (kidName != null && kidName.equals(items[offset])) {
|
538
|
+
if (offset == items.length - 1) {
|
539
|
+
return kid;
|
540
|
+
} else {
|
541
|
+
return kid.getChildRecursive(items, offset + 1);
|
542
|
+
}
|
543
|
+
}
|
544
|
+
}
|
545
|
+
return null;
|
546
|
+
}
|
547
|
+
|
548
|
+
/**
|
549
|
+
* Get any children that match this name or path. Similar to getChild(), but
|
550
|
+
* will grab multiple matches rather than only the first.
|
551
|
+
*
|
552
|
+
* @param name element name or path/to/element
|
553
|
+
* @return array of child elements that match
|
554
|
+
* @author processing.org
|
555
|
+
*/
|
556
|
+
public XML[] getChildren(String name) {
|
557
|
+
if (name.length() > 0 && name.charAt(0) == '/') {
|
558
|
+
throw new IllegalArgumentException("getChildren() should not begin with a slash");
|
559
|
+
}
|
560
|
+
if (name.indexOf('/') != -1) {
|
561
|
+
return getChildrenRecursive(PApplet.split(name, '/'), 0);
|
562
|
+
}
|
563
|
+
// if it's a number, do an index instead
|
564
|
+
// (returns a single element array, since this will be a single match
|
565
|
+
if (Character.isDigit(name.charAt(0))) {
|
566
|
+
return new XML[]{getChild(Integer.parseInt(name))};
|
567
|
+
}
|
568
|
+
int childCount = getChildCount();
|
569
|
+
XML[] matches = new XML[childCount];
|
570
|
+
int matchCount = 0;
|
571
|
+
for (int i = 0; i < childCount; i++) {
|
572
|
+
XML kid = getChild(i);
|
573
|
+
String kidName = kid.getName();
|
574
|
+
if (kidName != null && kidName.equals(name)) {
|
575
|
+
matches[matchCount++] = kid;
|
576
|
+
}
|
577
|
+
}
|
578
|
+
return (XML[]) PApplet.subset(matches, 0, matchCount);
|
579
|
+
}
|
580
|
+
|
581
|
+
/**
|
582
|
+
*
|
583
|
+
* @param items
|
584
|
+
* @param offset
|
585
|
+
* @return
|
586
|
+
*/
|
587
|
+
protected XML[] getChildrenRecursive(String[] items, int offset) {
|
588
|
+
if (offset == items.length - 1) {
|
589
|
+
return getChildren(items[offset]);
|
590
|
+
}
|
591
|
+
XML[] matches = getChildren(items[offset]);
|
592
|
+
XML[] outgoing = new XML[0];
|
593
|
+
for (XML matche : matches) {
|
594
|
+
XML[] kidMatches = matche.getChildrenRecursive(items, offset + 1);
|
595
|
+
outgoing = (XML[]) PApplet.concat(outgoing, kidMatches);
|
596
|
+
}
|
597
|
+
return outgoing;
|
598
|
+
}
|
599
|
+
|
600
|
+
/**
|
601
|
+
* @param tag
|
602
|
+
* @return
|
603
|
+
* @webref xml:method
|
604
|
+
* @brief Appends a new child to the element
|
605
|
+
*/
|
606
|
+
public XML addChild(String tag) {
|
607
|
+
Document document = node.getOwnerDocument();
|
608
|
+
Node newChild = document.createElement(tag);
|
609
|
+
return appendChild(newChild);
|
610
|
+
}
|
611
|
+
|
612
|
+
/**
|
613
|
+
*
|
614
|
+
* @param child
|
615
|
+
* @return
|
616
|
+
*/
|
617
|
+
public XML addChild(XML child) {
|
618
|
+
Document document = node.getOwnerDocument();
|
619
|
+
Node newChild = document.importNode((Node) child.getNative(), true);
|
620
|
+
return appendChild(newChild);
|
621
|
+
}
|
622
|
+
|
623
|
+
/**
|
624
|
+
* Internal handler to add the node structure.
|
625
|
+
*
|
626
|
+
* @param newNode
|
627
|
+
* @return
|
628
|
+
*/
|
629
|
+
protected XML appendChild(Node newNode) {
|
630
|
+
node.appendChild(newNode);
|
631
|
+
XML newbie = new XML(this, newNode);
|
632
|
+
if (children != null) {
|
633
|
+
children = (XML[]) PApplet.concat(children, new XML[]{newbie});
|
504
634
|
}
|
505
|
-
|
506
|
-
}
|
507
|
-
return null;
|
508
|
-
}
|
509
|
-
|
510
|
-
|
511
|
-
/**
|
512
|
-
* Get any children that match this name or path. Similar to getChild(),
|
513
|
-
* but will grab multiple matches rather than only the first.
|
514
|
-
*
|
515
|
-
* @param name element name or path/to/element
|
516
|
-
* @return array of child elements that match
|
517
|
-
* @author processing.org
|
518
|
-
*/
|
519
|
-
public XML[] getChildren(String name) {
|
520
|
-
if (name.length() > 0 && name.charAt(0) == '/') {
|
521
|
-
throw new IllegalArgumentException("getChildren() should not begin with a slash");
|
522
|
-
}
|
523
|
-
if (name.indexOf('/') != -1) {
|
524
|
-
return getChildrenRecursive(PApplet.split(name, '/'), 0);
|
525
|
-
}
|
526
|
-
// if it's a number, do an index instead
|
527
|
-
// (returns a single element array, since this will be a single match
|
528
|
-
if (Character.isDigit(name.charAt(0))) {
|
529
|
-
return new XML[] { getChild(Integer.parseInt(name)) };
|
530
|
-
}
|
531
|
-
int childCount = getChildCount();
|
532
|
-
XML[] matches = new XML[childCount];
|
533
|
-
int matchCount = 0;
|
534
|
-
for (int i = 0; i < childCount; i++) {
|
535
|
-
XML kid = getChild(i);
|
536
|
-
String kidName = kid.getName();
|
537
|
-
if (kidName != null && kidName.equals(name)) {
|
538
|
-
matches[matchCount++] = kid;
|
539
|
-
}
|
540
|
-
}
|
541
|
-
return (XML[]) PApplet.subset(matches, 0, matchCount);
|
542
|
-
}
|
543
|
-
|
544
|
-
|
545
|
-
protected XML[] getChildrenRecursive(String[] items, int offset) {
|
546
|
-
if (offset == items.length-1) {
|
547
|
-
return getChildren(items[offset]);
|
548
|
-
}
|
549
|
-
XML[] matches = getChildren(items[offset]);
|
550
|
-
XML[] outgoing = new XML[0];
|
551
|
-
for (int i = 0; i < matches.length; i++) {
|
552
|
-
XML[] kidMatches = matches[i].getChildrenRecursive(items, offset+1);
|
553
|
-
outgoing = (XML[]) PApplet.concat(outgoing, kidMatches);
|
554
|
-
}
|
555
|
-
return outgoing;
|
556
|
-
}
|
557
|
-
|
558
|
-
|
559
|
-
/**
|
560
|
-
* @webref xml:method
|
561
|
-
* @brief Appends a new child to the element
|
562
|
-
*/
|
563
|
-
public XML addChild(String tag) {
|
564
|
-
Document document = node.getOwnerDocument();
|
565
|
-
Node newChild = document.createElement(tag);
|
566
|
-
return appendChild(newChild);
|
567
|
-
}
|
568
|
-
|
569
|
-
|
570
|
-
public XML addChild(XML child) {
|
571
|
-
Document document = node.getOwnerDocument();
|
572
|
-
Node newChild = document.importNode((Node) child.getNative(), true);
|
573
|
-
return appendChild(newChild);
|
574
|
-
}
|
575
|
-
|
576
|
-
|
577
|
-
/** Internal handler to add the node structure. */
|
578
|
-
protected XML appendChild(Node newNode) {
|
579
|
-
node.appendChild(newNode);
|
580
|
-
XML newbie = new XML(this, newNode);
|
581
|
-
if (children != null) {
|
582
|
-
children = (XML[]) PApplet.concat(children, new XML[] { newbie });
|
583
|
-
}
|
584
|
-
return newbie;
|
585
|
-
}
|
586
|
-
|
587
|
-
|
588
|
-
/**
|
589
|
-
* @webref xml:method
|
590
|
-
* @brief Removes the specified child
|
591
|
-
*/
|
592
|
-
public void removeChild(XML kid) {
|
593
|
-
node.removeChild(kid.node);
|
594
|
-
children = null; // TODO not efficient
|
595
|
-
}
|
596
|
-
|
597
|
-
/**
|
598
|
-
* Removes whitespace nodes.
|
599
|
-
* Those whitespace nodes are required to reconstruct the original XML's spacing and indentation.
|
600
|
-
* If you call this and use saveXML() your original spacing will be gone.
|
601
|
-
*
|
602
|
-
* @nowebref
|
603
|
-
* @brief Removes whitespace nodes
|
604
|
-
*/
|
605
|
-
public void trim() {
|
606
|
-
try {
|
607
|
-
XPathFactory xpathFactory = XPathFactory.newInstance();
|
608
|
-
XPathExpression xpathExp =
|
609
|
-
xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
|
610
|
-
NodeList emptyTextNodes = (NodeList)
|
611
|
-
xpathExp.evaluate(node, XPathConstants.NODESET);
|
612
|
-
|
613
|
-
// Remove each empty text node from document.
|
614
|
-
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
|
615
|
-
Node emptyTextNode = emptyTextNodes.item(i);
|
616
|
-
emptyTextNode.getParentNode().removeChild(emptyTextNode);
|
617
|
-
}
|
618
|
-
} catch (Exception e) {
|
619
|
-
throw new RuntimeException(e);
|
620
|
-
}
|
621
|
-
}
|
635
|
+
return newbie;
|
636
|
+
}
|
622
637
|
|
638
|
+
/**
|
639
|
+
* @param kid
|
640
|
+
* @webref xml:method
|
641
|
+
* @brief Removes the specified child
|
642
|
+
*/
|
643
|
+
public void removeChild(XML kid) {
|
644
|
+
node.removeChild(kid.node);
|
645
|
+
children = null; // TODO not efficient
|
646
|
+
}
|
647
|
+
|
648
|
+
/**
|
649
|
+
* Removes whitespace nodes. Those whitespace nodes are required to
|
650
|
+
* reconstruct the original XML's spacing and indentation. If you call this
|
651
|
+
* and use saveXML() your original spacing will be gone.
|
652
|
+
*
|
653
|
+
* @nowebref
|
654
|
+
* @brief Removes whitespace nodes
|
655
|
+
*/
|
656
|
+
public void trim() {
|
657
|
+
try {
|
658
|
+
XPathFactory xpathFactory = XPathFactory.newInstance();
|
659
|
+
XPathExpression xpathExp
|
660
|
+
= xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
|
661
|
+
NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(node, XPathConstants.NODESET);
|
662
|
+
|
663
|
+
// Remove each empty text node from document.
|
664
|
+
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
|
665
|
+
Node emptyTextNode = emptyTextNodes.item(i);
|
666
|
+
emptyTextNode.getParentNode().removeChild(emptyTextNode);
|
667
|
+
}
|
668
|
+
} catch (XPathExpressionException | DOMException e) {
|
669
|
+
throw new RuntimeException(e);
|
670
|
+
}
|
671
|
+
}
|
623
672
|
|
624
673
|
// /** Remove whitespace nodes. */
|
625
674
|
// public void trim() {
|
@@ -668,489 +717,525 @@ public class XML implements Serializable {
|
|
668
717
|
//// </xsl:template>
|
669
718
|
//// </xsl:stylesheet>
|
670
719
|
// }
|
720
|
+
/**
|
721
|
+
* Returns the number of attributes.
|
722
|
+
*
|
723
|
+
* @return
|
724
|
+
* @webref xml:method
|
725
|
+
* @brief Counts the specified element's number of attributes
|
726
|
+
*/
|
727
|
+
public int getAttributeCount() {
|
728
|
+
return node.getAttributes().getLength();
|
729
|
+
}
|
730
|
+
|
731
|
+
/**
|
732
|
+
* Get a list of the names for all of the attributes for this node.
|
733
|
+
*
|
734
|
+
* @return
|
735
|
+
* @webref xml:method
|
736
|
+
* @brief Returns a list of names of all attributes as an array
|
737
|
+
*/
|
738
|
+
public String[] listAttributes() {
|
739
|
+
NamedNodeMap nnm = node.getAttributes();
|
740
|
+
String[] outgoing = new String[nnm.getLength()];
|
741
|
+
for (int i = 0; i < outgoing.length; i++) {
|
742
|
+
outgoing[i] = nnm.item(i).getNodeName();
|
743
|
+
}
|
744
|
+
return outgoing;
|
745
|
+
}
|
671
746
|
|
747
|
+
/**
|
748
|
+
* Returns whether an attribute exists.
|
749
|
+
*
|
750
|
+
* @param name
|
751
|
+
* @return
|
752
|
+
* @webref xml:method
|
753
|
+
* @brief Checks whether or not an element has the specified attribute
|
754
|
+
*/
|
755
|
+
public boolean hasAttribute(String name) {
|
756
|
+
return (node.getAttributes().getNamedItem(name) != null);
|
757
|
+
}
|
672
758
|
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
public int getAttributeCount() {
|
680
|
-
return node.getAttributes().getLength();
|
681
|
-
}
|
682
|
-
|
683
|
-
|
684
|
-
/**
|
685
|
-
* Get a list of the names for all of the attributes for this node.
|
686
|
-
*
|
687
|
-
* @webref xml:method
|
688
|
-
* @brief Returns a list of names of all attributes as an array
|
689
|
-
*/
|
690
|
-
public String[] listAttributes() {
|
691
|
-
NamedNodeMap nnm = node.getAttributes();
|
692
|
-
String[] outgoing = new String[nnm.getLength()];
|
693
|
-
for (int i = 0; i < outgoing.length; i++) {
|
694
|
-
outgoing[i] = nnm.item(i).getNodeName();
|
695
|
-
}
|
696
|
-
return outgoing;
|
697
|
-
}
|
698
|
-
|
699
|
-
/**
|
700
|
-
* Returns whether an attribute exists.
|
701
|
-
*
|
702
|
-
* @webref xml:method
|
703
|
-
* @brief Checks whether or not an element has the specified attribute
|
704
|
-
*/
|
705
|
-
public boolean hasAttribute(String name) {
|
706
|
-
return (node.getAttributes().getNamedItem(name) != null);
|
707
|
-
}
|
708
|
-
|
709
|
-
|
710
|
-
/**
|
711
|
-
* Returns the value of an attribute.
|
712
|
-
*
|
713
|
-
* @param name the non-null name of the attribute.
|
714
|
-
* @return the value, or null if the attribute does not exist.
|
715
|
-
*/
|
759
|
+
/**
|
760
|
+
* Returns the value of an attribute.
|
761
|
+
*
|
762
|
+
* @param name the non-null name of the attribute.
|
763
|
+
* @return the value, or null if the attribute does not exist.
|
764
|
+
*/
|
716
765
|
// public String getAttribute(String name) {
|
717
766
|
// return this.getAttribute(name, null);
|
718
767
|
// }
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
* @return the value, or defaultValue if the attribute does not exist.
|
727
|
-
*/
|
768
|
+
/**
|
769
|
+
* Returns the value of an attribute.
|
770
|
+
*
|
771
|
+
* @param name the non-null full name of the attribute.
|
772
|
+
* @param defaultValue the default value of the attribute.
|
773
|
+
* @return the value, or defaultValue if the attribute does not exist.
|
774
|
+
*/
|
728
775
|
// public String getAttribute(String name, String defaultValue) {
|
729
776
|
// Node attr = node.getAttributes().getNamedItem(name);
|
730
777
|
// return (attr == null) ? defaultValue : attr.getNodeValue();
|
731
778
|
// }
|
779
|
+
/**
|
780
|
+
* @param name
|
781
|
+
* @return
|
782
|
+
* @webref xml:method
|
783
|
+
* @brief Gets the content of an attribute as a String
|
784
|
+
*/
|
785
|
+
public String getString(String name) {
|
786
|
+
return getString(name, null);
|
787
|
+
}
|
788
|
+
|
789
|
+
/**
|
790
|
+
*
|
791
|
+
* @param name
|
792
|
+
* @param defaultValue
|
793
|
+
* @return
|
794
|
+
*/
|
795
|
+
public String getString(String name, String defaultValue) {
|
796
|
+
NamedNodeMap attrs = node.getAttributes();
|
797
|
+
if (attrs != null) {
|
798
|
+
Node attr = attrs.getNamedItem(name);
|
799
|
+
if (attr != null) {
|
800
|
+
return attr.getNodeValue();
|
801
|
+
}
|
802
|
+
}
|
803
|
+
return defaultValue;
|
804
|
+
}
|
732
805
|
|
806
|
+
/**
|
807
|
+
* @param name
|
808
|
+
* @param value
|
809
|
+
* @webref xml:method
|
810
|
+
* @brief Sets the content of an attribute as a String
|
811
|
+
*/
|
812
|
+
public void setString(String name, String value) {
|
813
|
+
((Element) node).setAttribute(name, value);
|
814
|
+
}
|
733
815
|
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
public String getString(String name, String defaultValue) {
|
744
|
-
NamedNodeMap attrs = node.getAttributes();
|
745
|
-
if (attrs != null) {
|
746
|
-
Node attr = attrs.getNamedItem(name);
|
747
|
-
if (attr != null) {
|
748
|
-
return attr.getNodeValue();
|
749
|
-
}
|
750
|
-
}
|
751
|
-
return defaultValue;
|
752
|
-
}
|
753
|
-
|
754
|
-
|
755
|
-
/**
|
756
|
-
* @webref xml:method
|
757
|
-
* @brief Sets the content of an attribute as a String
|
758
|
-
*/
|
759
|
-
public void setString(String name, String value) {
|
760
|
-
((Element) node).setAttribute(name, value);
|
761
|
-
}
|
762
|
-
|
763
|
-
|
764
|
-
/**
|
765
|
-
* @webref xml:method
|
766
|
-
* @brief Gets the content of an attribute as an int
|
767
|
-
*/
|
768
|
-
public int getInt(String name) {
|
769
|
-
return getInt(name, 0);
|
770
|
-
}
|
771
|
-
|
772
|
-
|
773
|
-
/**
|
774
|
-
* @webref xml:method
|
775
|
-
* @brief Sets the content of an attribute as an int
|
776
|
-
*/
|
777
|
-
public void setInt(String name, int value) {
|
778
|
-
setString(name, String.valueOf(value));
|
779
|
-
}
|
780
|
-
|
781
|
-
|
782
|
-
/**
|
783
|
-
* Returns the value of an attribute.
|
784
|
-
*
|
785
|
-
* @param name the non-null full name of the attribute
|
786
|
-
* @param defaultValue the default value of the attribute
|
787
|
-
* @return the value, or defaultValue if the attribute does not exist
|
788
|
-
*/
|
789
|
-
public int getInt(String name, int defaultValue) {
|
790
|
-
String value = getString(name);
|
791
|
-
return (value == null) ? defaultValue : Integer.parseInt(value);
|
792
|
-
}
|
793
|
-
|
794
|
-
|
795
|
-
/**
|
796
|
-
* @webref xml:method
|
797
|
-
* @brief Sets the content of an element as an int
|
798
|
-
*/
|
799
|
-
public void setLong(String name, long value) {
|
800
|
-
setString(name, String.valueOf(value));
|
801
|
-
}
|
802
|
-
|
803
|
-
|
804
|
-
/**
|
805
|
-
* Returns the value of an attribute.
|
806
|
-
*
|
807
|
-
* @param name the non-null full name of the attribute.
|
808
|
-
* @param defaultValue the default value of the attribute.
|
809
|
-
* @return the value, or defaultValue if the attribute does not exist.
|
810
|
-
*/
|
811
|
-
public long getLong(String name, long defaultValue) {
|
812
|
-
String value = getString(name);
|
813
|
-
return (value == null) ? defaultValue : Long.parseLong(value);
|
814
|
-
}
|
815
|
-
|
816
|
-
|
817
|
-
/**
|
818
|
-
* Returns the value of an attribute, or zero if not present.
|
819
|
-
*
|
820
|
-
* @webref xml:method
|
821
|
-
* @brief Gets the content of an attribute as a float
|
822
|
-
*/
|
823
|
-
public float getFloat(String name) {
|
824
|
-
return getFloat(name, 0);
|
825
|
-
}
|
826
|
-
|
827
|
-
|
828
|
-
/**
|
829
|
-
* Returns the value of an attribute.
|
830
|
-
*
|
831
|
-
* @param name the non-null full name of the attribute.
|
832
|
-
* @param defaultValue the default value of the attribute.
|
833
|
-
* @return the value, or defaultValue if the attribute does not exist.
|
834
|
-
*/
|
835
|
-
public float getFloat(String name, float defaultValue) {
|
836
|
-
String value = getString(name);
|
837
|
-
return (value == null) ? defaultValue : Float.parseFloat(value);
|
838
|
-
}
|
839
|
-
|
840
|
-
|
841
|
-
/**
|
842
|
-
* @webref xml:method
|
843
|
-
* @brief Sets the content of an attribute as a float
|
844
|
-
*/
|
845
|
-
public void setFloat(String name, float value) {
|
846
|
-
setString(name, String.valueOf(value));
|
847
|
-
}
|
848
|
-
|
849
|
-
|
850
|
-
public double getDouble(String name) {
|
851
|
-
return getDouble(name, 0);
|
852
|
-
}
|
853
|
-
|
854
|
-
|
855
|
-
/**
|
856
|
-
* Returns the value of an attribute.
|
857
|
-
*
|
858
|
-
* @param name the non-null full name of the attribute
|
859
|
-
* @param defaultValue the default value of the attribute
|
860
|
-
* @return the value, or defaultValue if the attribute does not exist
|
861
|
-
*/
|
862
|
-
public double getDouble(String name, double defaultValue) {
|
863
|
-
String value = getString(name);
|
864
|
-
return (value == null) ? defaultValue : Double.parseDouble(value);
|
865
|
-
}
|
866
|
-
|
867
|
-
|
868
|
-
public void setDouble(String name, double value) {
|
869
|
-
setString(name, String.valueOf(value));
|
870
|
-
}
|
871
|
-
|
872
|
-
|
873
|
-
/**
|
874
|
-
* Return the #PCDATA content of the element. If the element has a
|
875
|
-
* combination of #PCDATA content and child elements, the #PCDATA
|
876
|
-
* sections can be retrieved as unnamed child objects. In this case,
|
877
|
-
* this method returns null.
|
878
|
-
*
|
879
|
-
* @webref xml:method
|
880
|
-
* @brief Gets the content of an element
|
881
|
-
* @return the content.
|
882
|
-
* @see XML#getIntContent()
|
883
|
-
* @see XML#getFloatContent()
|
884
|
-
*/
|
885
|
-
public String getContent() {
|
886
|
-
return node.getTextContent();
|
887
|
-
}
|
888
|
-
|
889
|
-
|
890
|
-
public String getContent(String defaultValue) {
|
891
|
-
String s = node.getTextContent();
|
892
|
-
return (s != null) ? s : defaultValue;
|
893
|
-
}
|
894
|
-
|
895
|
-
|
896
|
-
/**
|
897
|
-
* @webref xml:method
|
898
|
-
* @brief Gets the content of an element as an int
|
899
|
-
* @return the content.
|
900
|
-
* @see XML#getContent()
|
901
|
-
* @see XML#getFloatContent()
|
902
|
-
*/
|
903
|
-
public int getIntContent() {
|
904
|
-
return getIntContent(0);
|
905
|
-
}
|
906
|
-
|
907
|
-
|
908
|
-
/**
|
909
|
-
* @param defaultValue the default value of the attribute
|
910
|
-
*/
|
911
|
-
public int getIntContent(int defaultValue) {
|
912
|
-
return PApplet.parseInt(node.getTextContent(), defaultValue);
|
913
|
-
}
|
914
|
-
|
915
|
-
|
916
|
-
/**
|
917
|
-
* @webref xml:method
|
918
|
-
* @brief Gets the content of an element as a float
|
919
|
-
* @return the content.
|
920
|
-
* @see XML#getContent()
|
921
|
-
* @see XML#getIntContent()
|
922
|
-
*/
|
923
|
-
public float getFloatContent() {
|
924
|
-
return getFloatContent(0);
|
925
|
-
}
|
926
|
-
|
927
|
-
|
928
|
-
/**
|
929
|
-
* @param defaultValue the default value of the attribute
|
930
|
-
*/
|
931
|
-
public float getFloatContent(float defaultValue) {
|
932
|
-
return PApplet.parseFloat(node.getTextContent(), defaultValue);
|
933
|
-
}
|
934
|
-
|
935
|
-
|
936
|
-
public long getLongContent() {
|
937
|
-
return getLongContent(0);
|
938
|
-
}
|
939
|
-
|
940
|
-
|
941
|
-
public long getLongContent(long defaultValue) {
|
942
|
-
String c = node.getTextContent();
|
943
|
-
if (c != null) {
|
944
|
-
try {
|
945
|
-
return Long.parseLong(c);
|
946
|
-
} catch (NumberFormatException nfe) { }
|
947
|
-
}
|
948
|
-
return defaultValue;
|
949
|
-
}
|
950
|
-
|
951
|
-
|
952
|
-
public double getDoubleContent() {
|
953
|
-
return getDoubleContent(0);
|
954
|
-
}
|
955
|
-
|
956
|
-
|
957
|
-
public double getDoubleContent(double defaultValue) {
|
958
|
-
String c = node.getTextContent();
|
959
|
-
if (c != null) {
|
960
|
-
try {
|
961
|
-
return Double.parseDouble(c);
|
962
|
-
} catch (NumberFormatException nfe) { }
|
963
|
-
}
|
964
|
-
return defaultValue;
|
965
|
-
}
|
966
|
-
|
967
|
-
|
968
|
-
/**
|
969
|
-
* @webref xml:method
|
970
|
-
* @brief Sets the content of an element
|
971
|
-
*/
|
972
|
-
public void setContent(String text) {
|
973
|
-
node.setTextContent(text);
|
974
|
-
}
|
975
|
-
|
976
|
-
|
977
|
-
public void setIntContent(int value) {
|
978
|
-
setContent(String.valueOf(value));
|
979
|
-
}
|
816
|
+
/**
|
817
|
+
* @param name
|
818
|
+
* @return
|
819
|
+
* @webref xml:method
|
820
|
+
* @brief Gets the content of an attribute as an int
|
821
|
+
*/
|
822
|
+
public int getInt(String name) {
|
823
|
+
return getInt(name, 0);
|
824
|
+
}
|
980
825
|
|
826
|
+
/**
|
827
|
+
* @param name
|
828
|
+
* @param value
|
829
|
+
* @webref xml:method
|
830
|
+
* @brief Sets the content of an attribute as an int
|
831
|
+
*/
|
832
|
+
public void setInt(String name, int value) {
|
833
|
+
setString(name, String.valueOf(value));
|
834
|
+
}
|
981
835
|
|
982
|
-
|
983
|
-
|
984
|
-
|
836
|
+
/**
|
837
|
+
* Returns the value of an attribute.
|
838
|
+
*
|
839
|
+
* @param name the non-null full name of the attribute
|
840
|
+
* @param defaultValue the default value of the attribute
|
841
|
+
* @return the value, or defaultValue if the attribute does not exist
|
842
|
+
*/
|
843
|
+
public int getInt(String name, int defaultValue) {
|
844
|
+
String value = getString(name);
|
845
|
+
return (value == null) ? defaultValue : Integer.parseInt(value);
|
846
|
+
}
|
985
847
|
|
848
|
+
/**
|
849
|
+
* @param name
|
850
|
+
* @param value
|
851
|
+
* @webref xml:method
|
852
|
+
* @brief Sets the content of an element as an int
|
853
|
+
*/
|
854
|
+
public void setLong(String name, long value) {
|
855
|
+
setString(name, String.valueOf(value));
|
856
|
+
}
|
986
857
|
|
987
|
-
|
988
|
-
|
989
|
-
|
858
|
+
/**
|
859
|
+
* Returns the value of an attribute.
|
860
|
+
*
|
861
|
+
* @param name the non-null full name of the attribute.
|
862
|
+
* @param defaultValue the default value of the attribute.
|
863
|
+
* @return the value, or defaultValue if the attribute does not exist.
|
864
|
+
*/
|
865
|
+
public long getLong(String name, long defaultValue) {
|
866
|
+
String value = getString(name);
|
867
|
+
return (value == null) ? defaultValue : Long.parseLong(value);
|
868
|
+
}
|
990
869
|
|
870
|
+
/**
|
871
|
+
* Returns the value of an attribute, or zero if not present.
|
872
|
+
*
|
873
|
+
* @param name
|
874
|
+
* @return
|
875
|
+
* @webref xml:method
|
876
|
+
* @brief Gets the content of an attribute as a float
|
877
|
+
*/
|
878
|
+
public float getFloat(String name) {
|
879
|
+
return getFloat(name, 0);
|
880
|
+
}
|
991
881
|
|
992
|
-
|
993
|
-
|
994
|
-
|
882
|
+
/**
|
883
|
+
* Returns the value of an attribute.
|
884
|
+
*
|
885
|
+
* @param name the non-null full name of the attribute.
|
886
|
+
* @param defaultValue the default value of the attribute.
|
887
|
+
* @return the value, or defaultValue if the attribute does not exist.
|
888
|
+
*/
|
889
|
+
public float getFloat(String name, float defaultValue) {
|
890
|
+
String value = getString(name);
|
891
|
+
return (value == null) ? defaultValue : Float.parseFloat(value);
|
892
|
+
}
|
995
893
|
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1004
|
-
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
|
1010
|
-
|
1011
|
-
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
894
|
+
/**
|
895
|
+
* @param name
|
896
|
+
* @param value
|
897
|
+
* @webref xml:method
|
898
|
+
* @brief Sets the content of an attribute as a float
|
899
|
+
*/
|
900
|
+
public void setFloat(String name, float value) {
|
901
|
+
setString(name, String.valueOf(value));
|
902
|
+
}
|
903
|
+
|
904
|
+
/**
|
905
|
+
*
|
906
|
+
* @param name
|
907
|
+
* @return
|
908
|
+
*/
|
909
|
+
public double getDouble(String name) {
|
910
|
+
return getDouble(name, 0);
|
911
|
+
}
|
912
|
+
|
913
|
+
/**
|
914
|
+
* Returns the value of an attribute.
|
915
|
+
*
|
916
|
+
* @param name the non-null full name of the attribute
|
917
|
+
* @param defaultValue the default value of the attribute
|
918
|
+
* @return the value, or defaultValue if the attribute does not exist
|
919
|
+
*/
|
920
|
+
public double getDouble(String name, double defaultValue) {
|
921
|
+
String value = getString(name);
|
922
|
+
return (value == null) ? defaultValue : Double.parseDouble(value);
|
923
|
+
}
|
924
|
+
|
925
|
+
/**
|
926
|
+
*
|
927
|
+
* @param name
|
928
|
+
* @param value
|
929
|
+
*/
|
930
|
+
public void setDouble(String name, double value) {
|
931
|
+
setString(name, String.valueOf(value));
|
932
|
+
}
|
933
|
+
|
934
|
+
/**
|
935
|
+
* Return the #PCDATA content of the element. If the element has a
|
936
|
+
* combination of #PCDATA content and child elements, the #PCDATA sections
|
937
|
+
* can be retrieved as unnamed child objects. In this case, this method
|
938
|
+
* returns null.
|
939
|
+
*
|
940
|
+
* @webref xml:method
|
941
|
+
* @brief Gets the content of an element
|
942
|
+
* @return the content.
|
943
|
+
* @see XML#getIntContent()
|
944
|
+
* @see XML#getFloatContent()
|
945
|
+
*/
|
946
|
+
public String getContent() {
|
947
|
+
return node.getTextContent();
|
948
|
+
}
|
949
|
+
|
950
|
+
/**
|
951
|
+
*
|
952
|
+
* @param defaultValue
|
953
|
+
* @return
|
954
|
+
*/
|
955
|
+
public String getContent(String defaultValue) {
|
956
|
+
String s = node.getTextContent();
|
957
|
+
return (s != null) ? s : defaultValue;
|
958
|
+
}
|
959
|
+
|
960
|
+
/**
|
961
|
+
* @webref xml:method
|
962
|
+
* @brief Gets the content of an element as an int
|
963
|
+
* @return the content.
|
964
|
+
* @see XML#getContent()
|
965
|
+
* @see XML#getFloatContent()
|
966
|
+
*/
|
967
|
+
public int getIntContent() {
|
968
|
+
return getIntContent(0);
|
969
|
+
}
|
970
|
+
|
971
|
+
/**
|
972
|
+
* @param defaultValue the default value of the attribute
|
973
|
+
* @return
|
974
|
+
*/
|
975
|
+
public int getIntContent(int defaultValue) {
|
976
|
+
return PApplet.parseInt(node.getTextContent(), defaultValue);
|
977
|
+
}
|
978
|
+
|
979
|
+
/**
|
980
|
+
* @webref xml:method
|
981
|
+
* @brief Gets the content of an element as a float
|
982
|
+
* @return the content.
|
983
|
+
* @see XML#getContent()
|
984
|
+
* @see XML#getIntContent()
|
985
|
+
*/
|
986
|
+
public float getFloatContent() {
|
987
|
+
return getFloatContent(0);
|
988
|
+
}
|
989
|
+
|
990
|
+
/**
|
991
|
+
* @param defaultValue the default value of the attribute
|
992
|
+
* @return
|
993
|
+
*/
|
994
|
+
public float getFloatContent(float defaultValue) {
|
995
|
+
return PApplet.parseFloat(node.getTextContent(), defaultValue);
|
996
|
+
}
|
997
|
+
|
998
|
+
/**
|
999
|
+
*
|
1000
|
+
* @return
|
1001
|
+
*/
|
1002
|
+
public long getLongContent() {
|
1003
|
+
return getLongContent(0);
|
1004
|
+
}
|
1005
|
+
|
1006
|
+
/**
|
1007
|
+
*
|
1008
|
+
* @param defaultValue
|
1009
|
+
* @return
|
1010
|
+
*/
|
1011
|
+
public long getLongContent(long defaultValue) {
|
1012
|
+
String c = node.getTextContent();
|
1013
|
+
if (c != null) {
|
1014
|
+
try {
|
1015
|
+
return Long.parseLong(c);
|
1016
|
+
} catch (NumberFormatException nfe) {
|
1017
|
+
}
|
1016
1018
|
}
|
1017
|
-
|
1018
|
-
|
1019
|
+
return defaultValue;
|
1020
|
+
}
|
1019
1021
|
|
1020
|
-
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1022
|
+
/**
|
1023
|
+
*
|
1024
|
+
* @return
|
1025
|
+
*/
|
1026
|
+
public double getDoubleContent() {
|
1027
|
+
return getDoubleContent(0);
|
1028
|
+
}
|
1027
1029
|
|
1028
|
-
|
1030
|
+
/**
|
1031
|
+
*
|
1032
|
+
* @param defaultValue
|
1033
|
+
* @return
|
1034
|
+
*/
|
1035
|
+
public double getDoubleContent(double defaultValue) {
|
1036
|
+
String c = node.getTextContent();
|
1037
|
+
if (c != null) {
|
1038
|
+
try {
|
1039
|
+
return Double.parseDouble(c);
|
1040
|
+
} catch (NumberFormatException nfe) {
|
1041
|
+
}
|
1042
|
+
}
|
1043
|
+
return defaultValue;
|
1044
|
+
}
|
1029
1045
|
|
1030
|
-
|
1046
|
+
/**
|
1047
|
+
* @param text
|
1048
|
+
* @webref xml:method
|
1049
|
+
* @brief Sets the content of an element
|
1050
|
+
*/
|
1051
|
+
public void setContent(String text) {
|
1052
|
+
node.setTextContent(text);
|
1053
|
+
}
|
1031
1054
|
|
1032
|
-
|
1055
|
+
/**
|
1056
|
+
*
|
1057
|
+
* @param value
|
1058
|
+
*/
|
1059
|
+
public void setIntContent(int value) {
|
1060
|
+
setContent(String.valueOf(value));
|
1061
|
+
}
|
1033
1062
|
|
1063
|
+
/**
|
1064
|
+
*
|
1065
|
+
* @param value
|
1066
|
+
*/
|
1067
|
+
public void setFloatContent(float value) {
|
1068
|
+
setContent(String.valueOf(value));
|
1069
|
+
}
|
1070
|
+
|
1071
|
+
/**
|
1072
|
+
*
|
1073
|
+
* @param value
|
1074
|
+
*/
|
1075
|
+
public void setLongContent(long value) {
|
1076
|
+
setContent(String.valueOf(value));
|
1077
|
+
}
|
1078
|
+
|
1079
|
+
/**
|
1080
|
+
*
|
1081
|
+
* @param value
|
1082
|
+
*/
|
1083
|
+
public void setDoubleContent(double value) {
|
1084
|
+
setContent(String.valueOf(value));
|
1085
|
+
}
|
1086
|
+
|
1087
|
+
/**
|
1088
|
+
* Format this XML data as a String.
|
1089
|
+
*
|
1090
|
+
* @webref xml:method
|
1091
|
+
* @brief Formats XML data as a String
|
1092
|
+
* @param indent -1 for a single line (and no declaration), >= 0 for indents
|
1093
|
+
* and newlines
|
1094
|
+
* @return the content
|
1095
|
+
* @see XML#toString()
|
1096
|
+
*/
|
1097
|
+
public String format(int indent) {
|
1098
|
+
try {
|
1099
|
+
// entities = doctype.getEntities()
|
1100
|
+
boolean useIndentAmount = false;
|
1101
|
+
TransformerFactory factory = TransformerFactory.newInstance();
|
1102
|
+
if (indent != -1) {
|
1103
|
+
try {
|
1104
|
+
factory.setAttribute("indent-number", indent);
|
1105
|
+
} catch (IllegalArgumentException e) {
|
1106
|
+
useIndentAmount = true;
|
1107
|
+
}
|
1108
|
+
}
|
1109
|
+
Transformer transformer = factory.newTransformer();
|
1110
|
+
|
1111
|
+
// Add the XML declaration at the top if this node is the root and we're
|
1112
|
+
// not writing to a single line (indent = -1 means single line).
|
1113
|
+
if (indent == -1 || parent == null) {
|
1114
|
+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
1115
|
+
} else {
|
1116
|
+
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
|
1120
|
+
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
1121
|
+
|
1122
|
+
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); // huh?
|
1034
1123
|
// transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
|
1035
1124
|
// "-//W3C//DTD XHTML 1.0 Transitional//EN");
|
1036
1125
|
// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
|
1037
1126
|
// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
}
|
1127
|
+
// For Android, because (at least 2.3.3) doesn't like indent-number
|
1128
|
+
if (useIndentAmount) {
|
1129
|
+
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
|
1130
|
+
}
|
1043
1131
|
|
1044
1132
|
// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
|
1045
1133
|
// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
|
1046
|
-
|
1134
|
+
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
1047
1135
|
// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
|
1048
1136
|
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1137
|
+
// Always indent, otherwise the XML declaration will just be jammed
|
1138
|
+
// onto the first line with the XML code as well.
|
1139
|
+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
1052
1140
|
|
1053
1141
|
// Properties p = transformer.getOutputProperties();
|
1054
1142
|
// for (Object key : p.keySet()) {
|
1055
1143
|
// System.out.println(key + " -> " + p.get(key));
|
1056
1144
|
// }
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1067
|
-
|
1068
|
-
|
1069
|
-
|
1070
|
-
|
1071
|
-
|
1072
|
-
transformer.transform(new DOMSource(node), tempResult);
|
1073
|
-
String[] tempLines = PApplet.split(tempWriter.toString(), sep);
|
1145
|
+
// If you smell something, that's because this code stinks. No matter
|
1146
|
+
// the settings of the Transformer object, if the XML document already
|
1147
|
+
// has whitespace elements, it won't bother re-indenting/re-formatting.
|
1148
|
+
// So instead, transform the data once into a single line string.
|
1149
|
+
// If indent is -1, then we're done. Otherwise re-run and the settings
|
1150
|
+
// of the factory will kick in. If you know a better way to do this,
|
1151
|
+
// please contribute. I've wasted too much of my Sunday on it. But at
|
1152
|
+
// least the Giants are getting blown out by the Falcons.
|
1153
|
+
final String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
|
1154
|
+
final String sep = System.getProperty("line.separator");
|
1155
|
+
|
1156
|
+
StringWriter tempWriter = new StringWriter();
|
1157
|
+
StreamResult tempResult = new StreamResult(tempWriter);
|
1158
|
+
transformer.transform(new DOMSource(node), tempResult);
|
1159
|
+
String[] tempLines = PApplet.split(tempWriter.toString(), sep);
|
1074
1160
|
// PApplet.println(tempLines);
|
1075
|
-
|
1076
|
-
|
1077
|
-
|
1078
|
-
|
1079
|
-
|
1080
|
-
|
1161
|
+
if (tempLines[0].startsWith("<?xml")) {
|
1162
|
+
// Remove XML declaration from the top before slamming into one line
|
1163
|
+
int declEnd = tempLines[0].indexOf("?>") + 2;
|
1164
|
+
//if (tempLines[0].length() == decl.length()) {
|
1165
|
+
if (tempLines[0].length() == declEnd) {
|
1166
|
+
// If it's all the XML declaration, remove it
|
1081
1167
|
// PApplet.println("removing first line");
|
1082
|
-
|
1083
|
-
|
1168
|
+
tempLines = PApplet.subset(tempLines, 1);
|
1169
|
+
} else {
|
1084
1170
|
// PApplet.println("removing part of first line");
|
1085
|
-
|
1086
|
-
|
1087
|
-
|
1088
|
-
|
1089
|
-
|
1090
|
-
|
1091
|
-
|
1092
|
-
|
1093
|
-
|
1094
|
-
|
1095
|
-
|
1096
|
-
|
1097
|
-
|
1098
|
-
|
1099
|
-
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
StreamResult xmlOutput = new StreamResult(stringWriter);
|
1171
|
+
// If the first node has been moved to this line, be more careful
|
1172
|
+
//tempLines[0] = tempLines[0].substring(decl.length());
|
1173
|
+
tempLines[0] = tempLines[0].substring(declEnd);
|
1174
|
+
}
|
1175
|
+
}
|
1176
|
+
String singleLine = PApplet.join(PApplet.trim(tempLines), "");
|
1177
|
+
if (indent == -1) {
|
1178
|
+
return singleLine;
|
1179
|
+
}
|
1180
|
+
|
1181
|
+
// Might just be whitespace, which won't be valid XML for parsing below.
|
1182
|
+
// https://github.com/processing/processing/issues/1796
|
1183
|
+
// Since indent is not -1, that means they want valid XML,
|
1184
|
+
// so we'll give them the single line plus the decl... Lame? sure.
|
1185
|
+
if (singleLine.trim().length() == 0) {
|
1186
|
+
// You want whitespace? I've got your whitespace right here.
|
1187
|
+
return decl + sep + singleLine;
|
1188
|
+
}
|
1189
|
+
|
1190
|
+
// Since the indent is not -1, bring back the XML declaration
|
1191
|
+
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
1192
|
+
StringWriter stringWriter = new StringWriter();
|
1193
|
+
StreamResult xmlOutput = new StreamResult(stringWriter);
|
1109
1194
|
// DOMSource source = new DOMSource(node);
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1195
|
+
Source source = new StreamSource(new StringReader(singleLine));
|
1196
|
+
transformer.transform(source, xmlOutput);
|
1197
|
+
String outgoing = stringWriter.toString();
|
1198
|
+
|
1199
|
+
// Add the XML declaration to the top if it's not there already
|
1200
|
+
if (outgoing.startsWith(decl)) {
|
1201
|
+
int declen = decl.length();
|
1202
|
+
int seplen = sep.length();
|
1203
|
+
if (outgoing.length() > declen + seplen
|
1204
|
+
&& !outgoing.substring(declen, declen + seplen).equals(sep)) {
|
1205
|
+
// make sure there's a line break between the XML decl and the code
|
1206
|
+
return outgoing.substring(0, decl.length())
|
1207
|
+
+ sep + outgoing.substring(decl.length());
|
1208
|
+
}
|
1209
|
+
return outgoing;
|
1210
|
+
} else {
|
1211
|
+
return decl + sep + outgoing;
|
1212
|
+
}
|
1213
|
+
|
1214
|
+
} catch (IllegalArgumentException | TransformerException e) {
|
1123
1215
|
}
|
1124
|
-
return
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
* @see XML#format(int)
|
1150
|
-
*/
|
1151
|
-
@Override
|
1152
|
-
public String toString() {
|
1153
|
-
//return format(2);
|
1154
|
-
return format(-1);
|
1155
|
-
}
|
1216
|
+
return null;
|
1217
|
+
}
|
1218
|
+
|
1219
|
+
/**
|
1220
|
+
*
|
1221
|
+
*/
|
1222
|
+
public void print() {
|
1223
|
+
PApplet.println(format(2));
|
1224
|
+
}
|
1225
|
+
|
1226
|
+
/**
|
1227
|
+
* Return the XML document formatted with two spaces for indents. Chosen to
|
1228
|
+
* do this since it's the most common case (e.g. with println()). Same as
|
1229
|
+
* format(2). Use the format() function for more options.
|
1230
|
+
*
|
1231
|
+
* @webref xml:method
|
1232
|
+
* @brief Gets XML data as a String using default formatting
|
1233
|
+
* @return the content
|
1234
|
+
* @see XML#format(int)
|
1235
|
+
*/
|
1236
|
+
@Override
|
1237
|
+
public String toString() {
|
1238
|
+
//return format(2);
|
1239
|
+
return format(-1);
|
1240
|
+
}
|
1156
1241
|
}
|