propane 3.9.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +2 -2
  4. data/README.md +3 -3
  5. data/Rakefile +6 -6
  6. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  7. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  8. data/lib/java/monkstone/ColorUtil.java +127 -0
  9. data/lib/java/monkstone/MathToolModule.java +287 -0
  10. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  11. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  12. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  13. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  14. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  15. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  16. data/{src/main → lib}/java/monkstone/noise/FastTerrain.java +0 -0
  17. data/{src/main → lib}/java/monkstone/noise/Noise.java +0 -0
  18. data/{src/main → lib}/java/monkstone/noise/NoiseGenerator.java +0 -0
  19. data/{src/main → lib}/java/monkstone/noise/NoiseMode.java +0 -0
  20. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  22. data/{src/main → lib}/java/monkstone/noise/SmoothTerrain.java +0 -0
  23. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  24. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  25. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  26. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  27. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  28. data/lib/java/monkstone/slider/Slider.java +61 -0
  29. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  30. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  31. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  32. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  33. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  34. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  35. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  36. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  37. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  38. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  39. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  40. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  41. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  42. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  43. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  44. data/lib/java/processing/awt/PImageAWT.java +377 -0
  45. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  46. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  47. data/lib/java/processing/awt/ShimAWT.java +581 -0
  48. data/lib/java/processing/core/PApplet.java +15156 -0
  49. data/lib/java/processing/core/PConstants.java +523 -0
  50. data/lib/java/processing/core/PFont.java +1126 -0
  51. data/lib/java/processing/core/PGraphics.java +8600 -0
  52. data/lib/java/processing/core/PImage.java +3377 -0
  53. data/lib/java/processing/core/PMatrix.java +208 -0
  54. data/lib/java/processing/core/PMatrix2D.java +562 -0
  55. data/lib/java/processing/core/PMatrix3D.java +890 -0
  56. data/lib/java/processing/core/PShape.java +3561 -0
  57. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  58. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  59. data/lib/java/processing/core/PStyle.java +63 -0
  60. data/lib/java/processing/core/PSurface.java +198 -0
  61. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  62. data/lib/java/processing/core/PVector.java +1066 -0
  63. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  64. data/lib/java/processing/data/DoubleDict.java +850 -0
  65. data/lib/java/processing/data/DoubleList.java +928 -0
  66. data/lib/java/processing/data/FloatDict.java +847 -0
  67. data/lib/java/processing/data/FloatList.java +936 -0
  68. data/lib/java/processing/data/IntDict.java +807 -0
  69. data/lib/java/processing/data/IntList.java +936 -0
  70. data/lib/java/processing/data/JSONArray.java +1260 -0
  71. data/lib/java/processing/data/JSONObject.java +2282 -0
  72. data/lib/java/processing/data/JSONTokener.java +435 -0
  73. data/lib/java/processing/data/LongDict.java +802 -0
  74. data/lib/java/processing/data/LongList.java +937 -0
  75. data/lib/java/processing/data/Sort.java +46 -0
  76. data/lib/java/processing/data/StringDict.java +613 -0
  77. data/lib/java/processing/data/StringList.java +800 -0
  78. data/lib/java/processing/data/Table.java +4936 -0
  79. data/lib/java/processing/data/TableRow.java +198 -0
  80. data/lib/java/processing/data/XML.java +1156 -0
  81. data/lib/java/processing/dxf/RawDXF.java +404 -0
  82. data/lib/java/processing/event/Event.java +125 -0
  83. data/lib/java/processing/event/KeyEvent.java +70 -0
  84. data/lib/java/processing/event/MouseEvent.java +114 -0
  85. data/lib/java/processing/event/TouchEvent.java +57 -0
  86. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  87. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  88. data/lib/java/processing/net/Client.java +744 -0
  89. data/lib/java/processing/net/Server.java +388 -0
  90. data/lib/java/processing/opengl/FontTexture.java +378 -0
  91. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  92. data/lib/java/processing/opengl/LinePath.java +627 -0
  93. data/lib/java/processing/opengl/LineStroker.java +681 -0
  94. data/lib/java/processing/opengl/PGL.java +3483 -0
  95. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  96. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  97. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  98. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  99. data/lib/java/processing/opengl/PShader.java +1484 -0
  100. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  101. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  102. data/lib/java/processing/opengl/Texture.java +1696 -0
  103. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  104. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  105. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  106. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  107. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  108. data/lib/java/processing/opengl/cursors/move.png +0 -0
  109. data/lib/java/processing/opengl/cursors/text.png +0 -0
  110. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  111. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  112. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  113. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  114. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  115. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  116. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  117. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  118. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  119. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  120. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  121. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  123. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  124. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  125. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  126. data/lib/propane/app.rb +8 -13
  127. data/lib/propane/version.rb +1 -1
  128. data/mvnw +3 -3
  129. data/mvnw.cmd +2 -2
  130. data/pom.rb +7 -2
  131. data/pom.xml +14 -2
  132. data/propane.gemspec +2 -2
  133. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  134. data/src/main/java/monkstone/MathToolModule.java +30 -30
  135. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  136. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  137. data/src/main/java/monkstone/fastmath/DegLutTables.java +15 -15
  138. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  139. data/src/main/java/monkstone/noise/OpenSimplex2F.java +752 -820
  140. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -1106
  141. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  142. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  143. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  144. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  145. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  146. data/src/main/java/processing/core/PApplet.java +89 -89
  147. data/src/main/java/processing/core/PConstants.java +155 -163
  148. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  149. data/vendors/Rakefile +1 -1
  150. metadata +136 -19
@@ -0,0 +1,198 @@
1
+ package processing.data;
2
+
3
+ import java.io.PrintWriter;
4
+
5
+ /**
6
+ * @webref data:composite
7
+ * @see Table
8
+ * @see Table#addRow()
9
+ * @see Table#removeRow(int)
10
+ * @see Table#clearRows()
11
+ * @see Table#getRow(int)
12
+ * @see Table#rows()
13
+ */
14
+ public interface TableRow {
15
+
16
+ /**
17
+ * @webref tablerow:method
18
+ * @brief Get an String value from the specified column
19
+ * @param column ID number of the column to reference
20
+ * @see TableRow#getInt(int)
21
+ * @see TableRow#getFloat(int)
22
+ */
23
+ public String getString(int column);
24
+
25
+ /**
26
+ * @param columnName title of the column to reference
27
+ */
28
+ public String getString(String columnName);
29
+
30
+ /**
31
+ * @webref tablerow:method
32
+ * @brief Get an integer value from the specified column
33
+ * @param column ID number of the column to reference
34
+ * @see TableRow#getFloat(int)
35
+ * @see TableRow#getString(int)
36
+ */
37
+ public int getInt(int column);
38
+
39
+ /**
40
+ * @param columnName title of the column to reference
41
+ */
42
+ public int getInt(String columnName);
43
+
44
+ /**
45
+ * @brief Get a long value from the specified column
46
+ * @param column ID number of the column to reference
47
+ * @see TableRow#getFloat(int)
48
+ * @see TableRow#getString(int)
49
+ */
50
+
51
+ public long getLong(int column);
52
+
53
+ /**
54
+ * @param columnName title of the column to reference
55
+ */
56
+ public long getLong(String columnName);
57
+
58
+ /**
59
+ * @webref tablerow:method
60
+ * @brief Get a float value from the specified column
61
+ * @param column ID number of the column to reference
62
+ * @see TableRow#getInt(int)
63
+ * @see TableRow#getString(int)
64
+ */
65
+ public float getFloat(int column);
66
+
67
+ /**
68
+ * @param columnName title of the column to reference
69
+ */
70
+ public float getFloat(String columnName);
71
+
72
+ /**
73
+ * @brief Get a double value from the specified column
74
+ * @param column ID number of the column to reference
75
+ * @see TableRow#getInt(int)
76
+ * @see TableRow#getString(int)
77
+ */
78
+ public double getDouble(int column);
79
+
80
+ /**
81
+ * @param columnName title of the column to reference
82
+ */
83
+ public double getDouble(String columnName);
84
+
85
+ /**
86
+ * @webref tablerow:method
87
+ * @brief Store a String value in the specified column
88
+ * @param column ID number of the target column
89
+ * @param value value to assign
90
+ * @see TableRow#setInt(int, int)
91
+ * @see TableRow#setFloat(int, float)
92
+ */
93
+ public void setString(int column, String value);
94
+ /**
95
+ * @param columnName title of the target column
96
+ */
97
+ public void setString(String columnName, String value);
98
+
99
+ /**
100
+ * @webref tablerow:method
101
+ * @brief Store an integer value in the specified column
102
+ * @param column ID number of the target column
103
+ * @param value value to assign
104
+ * @see TableRow#setFloat(int, float)
105
+ * @see TableRow#setString(int, String)
106
+ */
107
+ public void setInt(int column, int value);
108
+
109
+ /**
110
+ * @param columnName title of the target column
111
+ */
112
+ public void setInt(String columnName, int value);
113
+
114
+ /**
115
+ * @brief Store a long value in the specified column
116
+ * @param column ID number of the target column
117
+ * @param value value to assign
118
+ * @see TableRow#setFloat(int, float)
119
+ * @see TableRow#setString(int, String)
120
+ */
121
+ public void setLong(int column, long value);
122
+
123
+ /**
124
+ * @param columnName title of the target column
125
+ */
126
+ public void setLong(String columnName, long value);
127
+
128
+ /**
129
+ * @webref tablerow:method
130
+ * @brief Store a float value in the specified column
131
+ * @param column ID number of the target column
132
+ * @param value value to assign
133
+ * @see TableRow#setInt(int, int)
134
+ * @see TableRow#setString(int, String)
135
+ */
136
+ public void setFloat(int column, float value);
137
+
138
+ /**
139
+ * @param columnName title of the target column
140
+ */
141
+ public void setFloat(String columnName, float value);
142
+
143
+ /**
144
+ * @brief Store a double value in the specified column
145
+ * @param column ID number of the target column
146
+ * @param value value to assign
147
+ * @see TableRow#setFloat(int, float)
148
+ * @see TableRow#setString(int, String)
149
+ */
150
+ public void setDouble(int column, double value);
151
+
152
+ /**
153
+ * @param columnName title of the target column
154
+ */
155
+ public void setDouble(String columnName, double value);
156
+
157
+ /**
158
+ * @webref tablerow:method
159
+ * @brief Get the column count.
160
+ * @return count of all columns
161
+ */
162
+ public int getColumnCount();
163
+
164
+ /**
165
+ * @brief Get the column type.
166
+ * @param columnName title of the target column
167
+ * @return type of the column
168
+ */
169
+ public int getColumnType(String columnName);
170
+
171
+ /**
172
+ * @param column ID number of the target column
173
+ */
174
+ public int getColumnType(int column);
175
+
176
+ /**
177
+ * @brief Get the all column types
178
+ * @return list of all column types
179
+ */
180
+ public int[] getColumnTypes();
181
+
182
+ /**
183
+ * @webref tablerow:method
184
+ * @brief Get the column title.
185
+ * @param column ID number of the target column
186
+ * @return title of the column
187
+ */
188
+ public String getColumnTitle(int column);
189
+
190
+ /**
191
+ * @brief Get the all column titles
192
+ * @return list of all column titles
193
+ */
194
+ public String[] getColumnTitles();
195
+
196
+ public void write(PrintWriter writer);
197
+ public void print();
198
+ }
@@ -0,0 +1,1156 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+ /*
4
+ Part of the Processing project - http://processing.org
5
+
6
+ Copyright (c) 2012 The Processing Foundation
7
+ Copyright (c) 2009-12 Ben Fry and Casey Reas
8
+
9
+ This library is free software; you can redistribute it and/or
10
+ modify it under the terms of the GNU Lesser General Public
11
+ License as published by the Free Software Foundation, version 2.
12
+
13
+ This library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty
15
+ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
+ See the GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General
19
+ Public License along with this library; if not, write to the
20
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21
+ Boston, MA 02111-1307 USA
22
+ */
23
+
24
+ package processing.data;
25
+
26
+ import java.io.*;
27
+
28
+ import javax.xml.parsers.*;
29
+
30
+ import org.w3c.dom.*;
31
+ import org.xml.sax.*;
32
+
33
+ import javax.xml.transform.*;
34
+ import javax.xml.transform.dom.*;
35
+ import javax.xml.transform.stream.*;
36
+ import javax.xml.xpath.XPathConstants;
37
+ import javax.xml.xpath.XPathExpression;
38
+ import javax.xml.xpath.XPathFactory;
39
+
40
+ import processing.core.PApplet;
41
+
42
+
43
+ /**
44
+ * This is the base class used for the Processing XML library,
45
+ * representing a single node of an XML tree.
46
+ *
47
+ * @webref data:composite
48
+ * @see PApplet#loadXML(String)
49
+ * @see PApplet#parseXML(String)
50
+ * @see PApplet#saveXML(XML, String)
51
+ */
52
+ public class XML implements Serializable {
53
+
54
+ /** The internal representation, a DOM node. */
55
+ protected Node node;
56
+
57
+ // /** Cached locally because it's used often. */
58
+ // protected String name;
59
+
60
+ /** The parent element. */
61
+ protected XML parent;
62
+
63
+ /** Child elements, once loaded. */
64
+ protected XML[] children;
65
+
66
+ /**
67
+ * @nowebref
68
+ */
69
+ protected XML() { }
70
+
71
+
72
+ // /**
73
+ // * Begin parsing XML data passed in from a PApplet. This code
74
+ // * wraps exception handling, for more advanced exception handling,
75
+ // * use the constructor that takes a Reader or InputStream.
76
+ // *
77
+ // * @throws SAXException
78
+ // * @throws ParserConfigurationException
79
+ // * @throws IOException
80
+ // */
81
+ // public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException {
82
+ // this(parent.createReader(filename));
83
+ // }
84
+
85
+
86
+ /**
87
+ * Advanced users only; use loadXML() in PApplet. This is not a supported
88
+ * function and is subject to change. It is available simply for users that
89
+ * would like to handle the exceptions in a particular way.
90
+ *
91
+ * @nowebref
92
+ */
93
+ public XML(File file) throws IOException, ParserConfigurationException, SAXException {
94
+ this(file, null);
95
+ }
96
+
97
+
98
+ /**
99
+ * Advanced users only; use loadXML() in PApplet.
100
+ *
101
+ * @nowebref
102
+ */
103
+ public XML(File file, String options) throws IOException, ParserConfigurationException, SAXException {
104
+ this(PApplet.createReader(file), options);
105
+ }
106
+
107
+ /**
108
+ * @nowebref
109
+ */
110
+ public XML(InputStream input) throws IOException, ParserConfigurationException, SAXException {
111
+ this(input, null);
112
+ }
113
+
114
+
115
+ /**
116
+ * Unlike the loadXML() method in PApplet, this version works with files
117
+ * that are not in UTF-8 format.
118
+ *
119
+ * @nowebref
120
+ */
121
+ public XML(InputStream input, String options) throws IOException, ParserConfigurationException, SAXException {
122
+ //this(PApplet.createReader(input), options); // won't handle non-UTF8
123
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
124
+
125
+ try {
126
+ // Prevent 503 errors from www.w3.org
127
+ factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
128
+ } catch (IllegalArgumentException e) {
129
+ // ignore this; Android doesn't like it
130
+ }
131
+
132
+ factory.setExpandEntityReferences(false);
133
+ DocumentBuilder builder = factory.newDocumentBuilder();
134
+ Document document = builder.parse(new InputSource(input));
135
+ node = document.getDocumentElement();
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);
172
+
173
+ factory.setExpandEntityReferences(false);
174
+ // factory.setExpandEntityReferences(true);
175
+
176
+ // factory.setCoalescing(true);
177
+ // builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
178
+ DocumentBuilder builder = factory.newDocumentBuilder();
179
+ // builder.setEntityResolver()
180
+
181
+ // SAXParserFactory spf = SAXParserFactory.newInstance();
182
+ // spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
183
+ // SAXParser p = spf.newSAXParser();
184
+
185
+ // builder = DocumentBuilderFactory.newDocumentBuilder();
186
+ // builder = new SAXBuilder();
187
+ // builder.setValidation(validating);
188
+
189
+ Document document = builder.parse(new InputSource(new Reader() {
190
+ @Override
191
+ public int read(char[] cbuf, int off, int len) throws IOException {
192
+ int count = reader.read(cbuf, off, len);
193
+ for (int i = 0; i < count; i++) {
194
+ if (cbuf[off+i] == '\u2028') {
195
+ cbuf[off+i] = '\n';
196
+ }
197
+ }
198
+ return count;
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));
243
+ }
244
+ }
245
+ }
246
+ }
247
+
248
+
249
+ /**
250
+ * @webref xml:method
251
+ * @brief Converts String content to an XML object
252
+ * @param data the content to be parsed as XML
253
+ * @return an XML object, or null
254
+ * @throws SAXException
255
+ * @throws ParserConfigurationException
256
+ * @throws IOException
257
+ * @nowebref
258
+ */
259
+ static public XML parse(String data) throws IOException, ParserConfigurationException, SAXException {
260
+ return XML.parse(data, null);
261
+ }
262
+
263
+ /**
264
+ * @nowebref
265
+ */
266
+ static public XML parse(String data, String options) throws IOException, ParserConfigurationException, SAXException {
267
+ return new XML(new StringReader(data), null);
268
+ }
269
+
270
+
271
+ // protected boolean save(OutputStream output) {
272
+ // return write(PApplet.createWriter(output));
273
+ // }
274
+
275
+
276
+ public boolean save(File file) {
277
+ return save(file, null);
278
+ }
279
+
280
+
281
+ public boolean save(File file, String options) {
282
+ PrintWriter writer = PApplet.createWriter(file);
283
+ boolean result = write(writer);
284
+ writer.flush();
285
+ writer.close();
286
+ return result;
287
+ }
288
+
289
+
290
+ // Sends this object and its kids to a Writer with an indent of 2 spaces,
291
+ // including the declaration at the top so that the output will be valid XML.
292
+ public boolean write(PrintWriter output) {
293
+ output.print(format(2));
294
+ output.flush();
295
+ return true;
296
+ }
297
+
298
+
299
+ /**
300
+ * Returns the parent element. This method returns null for the root
301
+ * element.
302
+ *
303
+ * @webref xml:method
304
+ * @brief Gets a copy of the element's parent
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() {
327
+ // return name;
328
+ return node.getNodeName();
329
+ }
330
+
331
+ /**
332
+ * @webref xml:method
333
+ * @brief Sets the element's name
334
+ */
335
+ public void setName(String newName) {
336
+ Document document = node.getOwnerDocument();
337
+ node = document.renameNode(node, null, newName);
338
+ // name = node.getNodeName();
339
+ }
340
+
341
+
342
+ /**
343
+ * Returns the name of the element (without namespace prefix).
344
+ *
345
+ * Internal function; not included in reference.
346
+ */
347
+ public String getLocalName() {
348
+ return node.getLocalName();
349
+ }
350
+
351
+
352
+ /**
353
+ * Honey, can you just check on the kids? Thanks.
354
+ *
355
+ * Internal function; not included in reference.
356
+ */
357
+ protected void checkChildren() {
358
+ if (children == null) {
359
+ NodeList kids = node.getChildNodes();
360
+ int childCount = kids.getLength();
361
+ children = new XML[childCount];
362
+ for (int i = 0; i < childCount; i++) {
363
+ children[i] = new XML(this, kids.item(i));
364
+ }
365
+ }
366
+ }
367
+
368
+
369
+ /**
370
+ * Returns the number of children.
371
+ *
372
+ * @webref xml:method
373
+ * @brief Returns the element's number of children
374
+ * @return the count.
375
+ */
376
+ public int getChildCount() {
377
+ checkChildren();
378
+ return children.length;
379
+ }
380
+
381
+
382
+ /**
383
+ * Returns a boolean of whether or not there are children.
384
+ *
385
+ * @webref xml:method
386
+ * @brief Checks whether or not an element has any children
387
+ */
388
+ public boolean hasChildren() {
389
+ checkChildren();
390
+ return children.length > 0;
391
+ }
392
+
393
+
394
+ /**
395
+ * Put the names of all children into an array. Same as looping through
396
+ * each child and calling getName() on each XMLElement.
397
+ *
398
+ * @webref xml:method
399
+ * @brief Returns the names of all children as an array
400
+ */
401
+ public String[] listChildren() {
402
+ // NodeList children = node.getChildNodes();
403
+ // int childCount = children.getLength();
404
+ // String[] outgoing = new String[childCount];
405
+ // for (int i = 0; i < childCount; i++) {
406
+ // Node kid = children.item(i);
407
+ // if (kid.getNodeType() == Node.ELEMENT_NODE) {
408
+ // outgoing[i] = kid.getNodeName();
409
+ // } // otherwise just leave him null
410
+ // }
411
+ checkChildren();
412
+ String[] outgoing = new String[children.length];
413
+ for (int i = 0; i < children.length; i++) {
414
+ outgoing[i] = children[i].getName();
415
+ }
416
+ return outgoing;
417
+ }
418
+
419
+
420
+ /**
421
+ * Returns an array containing all the child elements.
422
+ *
423
+ * @webref xml:method
424
+ * @brief Returns an array containing all child elements
425
+ */
426
+ public XML[] getChildren() {
427
+ // NodeList children = node.getChildNodes();
428
+ // int childCount = children.getLength();
429
+ // XMLElement[] kids = new XMLElement[childCount];
430
+ // for (int i = 0; i < childCount; i++) {
431
+ // Node kid = children.item(i);
432
+ // kids[i] = new XMLElement(this, kid);
433
+ // }
434
+ // return kids;
435
+ checkChildren();
436
+ return children;
437
+ }
438
+
439
+
440
+ /**
441
+ * Quick accessor for an element at a particular index.
442
+ *
443
+ * @webref xml:method
444
+ * @brief Returns the child element with the specified index value or path
445
+ */
446
+ public XML getChild(int index) {
447
+ checkChildren();
448
+ return children[index];
449
+ }
450
+
451
+
452
+ /**
453
+ * Get a child by its name or path.
454
+ *
455
+ * @param name element name or path/to/element
456
+ * @return the first matching element or null if no match
457
+ */
458
+ public XML getChild(String name) {
459
+ if (name.length() > 0 && name.charAt(0) == '/') {
460
+ throw new IllegalArgumentException("getChild() should not begin with a slash");
461
+ }
462
+ if (name.indexOf('/') != -1) {
463
+ return getChildRecursive(PApplet.split(name, '/'), 0);
464
+ }
465
+ int childCount = getChildCount();
466
+ for (int i = 0; i < childCount; i++) {
467
+ XML kid = getChild(i);
468
+ String kidName = kid.getName();
469
+ if (kidName != null && kidName.equals(name)) {
470
+ return kid;
471
+ }
472
+ }
473
+ return null;
474
+ }
475
+
476
+
477
+ /**
478
+ * Internal helper function for getChild(String).
479
+ *
480
+ * @param items result of splitting the query on slashes
481
+ * @param offset where in the items[] array we're currently looking
482
+ * @return matching element or null if no match
483
+ * @author processing.org
484
+ */
485
+ protected XML getChildRecursive(String[] items, int offset) {
486
+ // if it's a number, do an index instead
487
+ if (Character.isDigit(items[offset].charAt(0))) {
488
+ XML kid = getChild(Integer.parseInt(items[offset]));
489
+ if (offset == items.length-1) {
490
+ return kid;
491
+ } else {
492
+ return kid.getChildRecursive(items, offset+1);
493
+ }
494
+ }
495
+ int childCount = getChildCount();
496
+ for (int i = 0; i < childCount; i++) {
497
+ XML kid = getChild(i);
498
+ String kidName = kid.getName();
499
+ if (kidName != null && kidName.equals(items[offset])) {
500
+ if (offset == items.length-1) {
501
+ return kid;
502
+ } else {
503
+ return kid.getChildRecursive(items, offset+1);
504
+ }
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
+ }
622
+
623
+
624
+ // /** Remove whitespace nodes. */
625
+ // public void trim() {
626
+ ////// public static boolean isWhitespace(XML xml) {
627
+ ////// if (xml.node.getNodeType() != Node.TEXT_NODE)
628
+ ////// return false;
629
+ ////// Matcher m = whitespace.matcher(xml.node.getNodeValue());
630
+ ////// return m.matches();
631
+ ////// }
632
+ //// trim(this);
633
+ //// }
634
+ //
635
+ // checkChildren();
636
+ // int index = 0;
637
+ // for (int i = 0; i < children.length; i++) {
638
+ // if (i != index) {
639
+ // children[index] = children[i];
640
+ // }
641
+ // Node childNode = (Node) children[i].getNative();
642
+ // if (childNode.getNodeType() != Node.TEXT_NODE ||
643
+ // children[i].getContent().trim().length() > 0) {
644
+ // children[i].trim();
645
+ // index++;
646
+ // }
647
+ // }
648
+ // if (index != children.length) {
649
+ // children = (XML[]) PApplet.subset(children, 0, index);
650
+ // }
651
+ //
652
+ // // possibility, but would have to re-parse the object
653
+ //// helpdesk.objects.com.au/java/how-do-i-remove-whitespace-from-an-xml-document
654
+ //// TransformerFactory factory = TransformerFactory.newInstance();
655
+ //// Transformer transformer = factory.newTransformer(new StreamSource("strip-space.xsl"));
656
+ //// DOMSource source = new DOMSource(document);
657
+ //// StreamResult result = new StreamResult(System.out);
658
+ //// transformer.transform(source, result);
659
+ //
660
+ //// <xsl:stylesheet version="1.0"
661
+ //// xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
662
+ //// <xsl:output method="xml" omit-xml-declaration="yes"/>
663
+ //// <xsl:strip-space elements="*"/>
664
+ //// <xsl:template match="@*|node()">
665
+ //// <xsl:copy>
666
+ //// <xsl:apply-templates select="@*|node()"/>
667
+ //// </xsl:copy>
668
+ //// </xsl:template>
669
+ //// </xsl:stylesheet>
670
+ // }
671
+
672
+
673
+ /**
674
+ * Returns the number of attributes.
675
+ *
676
+ * @webref xml:method
677
+ * @brief Counts the specified element's number of attributes
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
+ */
716
+ // public String getAttribute(String name) {
717
+ // return this.getAttribute(name, null);
718
+ // }
719
+
720
+
721
+ /**
722
+ * Returns the value of an attribute.
723
+ *
724
+ * @param name the non-null full name of the attribute.
725
+ * @param defaultValue the default value of the attribute.
726
+ * @return the value, or defaultValue if the attribute does not exist.
727
+ */
728
+ // public String getAttribute(String name, String defaultValue) {
729
+ // Node attr = node.getAttributes().getNamedItem(name);
730
+ // return (attr == null) ? defaultValue : attr.getNodeValue();
731
+ // }
732
+
733
+
734
+ /**
735
+ * @webref xml:method
736
+ * @brief Gets the content of an attribute as a String
737
+ */
738
+ public String getString(String name) {
739
+ return getString(name, null);
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
+ }
980
+
981
+
982
+ public void setFloatContent(float value) {
983
+ setContent(String.valueOf(value));
984
+ }
985
+
986
+
987
+ public void setLongContent(long value) {
988
+ setContent(String.valueOf(value));
989
+ }
990
+
991
+
992
+ public void setDoubleContent(double value) {
993
+ setContent(String.valueOf(value));
994
+ }
995
+
996
+
997
+ /**
998
+ * Format this XML data as a String.
999
+ *
1000
+ * @webref xml:method
1001
+ * @brief Formats XML data as a String
1002
+ * @param indent -1 for a single line (and no declaration), >= 0 for indents and newlines
1003
+ * @return the content
1004
+ * @see XML#toString()
1005
+ */
1006
+ public String format(int indent) {
1007
+ try {
1008
+ // entities = doctype.getEntities()
1009
+ boolean useIndentAmount = false;
1010
+ TransformerFactory factory = TransformerFactory.newInstance();
1011
+ if (indent != -1) {
1012
+ try {
1013
+ factory.setAttribute("indent-number", indent);
1014
+ } catch (IllegalArgumentException e) {
1015
+ useIndentAmount = true;
1016
+ }
1017
+ }
1018
+ Transformer transformer = factory.newTransformer();
1019
+
1020
+ // Add the XML declaration at the top if this node is the root and we're
1021
+ // not writing to a single line (indent = -1 means single line).
1022
+ if (indent == -1 || parent == null) {
1023
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
1024
+ } else {
1025
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
1026
+ }
1027
+
1028
+ // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
1029
+
1030
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
1031
+
1032
+ // transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); // huh?
1033
+
1034
+ // transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
1035
+ // "-//W3C//DTD XHTML 1.0 Transitional//EN");
1036
+ // transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
1037
+ // "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
1038
+
1039
+ // For Android, because (at least 2.3.3) doesn't like indent-number
1040
+ if (useIndentAmount) {
1041
+ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
1042
+ }
1043
+
1044
+ // transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
1045
+ // transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
1046
+ transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
1047
+ // transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
1048
+
1049
+ // Always indent, otherwise the XML declaration will just be jammed
1050
+ // onto the first line with the XML code as well.
1051
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1052
+
1053
+ // Properties p = transformer.getOutputProperties();
1054
+ // for (Object key : p.keySet()) {
1055
+ // System.out.println(key + " -> " + p.get(key));
1056
+ // }
1057
+
1058
+ // If you smell something, that's because this code stinks. No matter
1059
+ // the settings of the Transformer object, if the XML document already
1060
+ // has whitespace elements, it won't bother re-indenting/re-formatting.
1061
+ // So instead, transform the data once into a single line string.
1062
+ // If indent is -1, then we're done. Otherwise re-run and the settings
1063
+ // of the factory will kick in. If you know a better way to do this,
1064
+ // please contribute. I've wasted too much of my Sunday on it. But at
1065
+ // least the Giants are getting blown out by the Falcons.
1066
+
1067
+ final String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
1068
+ final String sep = System.getProperty("line.separator");
1069
+
1070
+ StringWriter tempWriter = new StringWriter();
1071
+ StreamResult tempResult = new StreamResult(tempWriter);
1072
+ transformer.transform(new DOMSource(node), tempResult);
1073
+ String[] tempLines = PApplet.split(tempWriter.toString(), sep);
1074
+ // PApplet.println(tempLines);
1075
+ if (tempLines[0].startsWith("<?xml")) {
1076
+ // Remove XML declaration from the top before slamming into one line
1077
+ int declEnd = tempLines[0].indexOf("?>") + 2;
1078
+ //if (tempLines[0].length() == decl.length()) {
1079
+ if (tempLines[0].length() == declEnd) {
1080
+ // If it's all the XML declaration, remove it
1081
+ // PApplet.println("removing first line");
1082
+ tempLines = PApplet.subset(tempLines, 1);
1083
+ } else {
1084
+ // PApplet.println("removing part of first line");
1085
+ // If the first node has been moved to this line, be more careful
1086
+ //tempLines[0] = tempLines[0].substring(decl.length());
1087
+ tempLines[0] = tempLines[0].substring(declEnd);
1088
+ }
1089
+ }
1090
+ String singleLine = PApplet.join(PApplet.trim(tempLines), "");
1091
+ if (indent == -1) {
1092
+ return singleLine;
1093
+ }
1094
+
1095
+ // Might just be whitespace, which won't be valid XML for parsing below.
1096
+ // https://github.com/processing/processing/issues/1796
1097
+ // Since indent is not -1, that means they want valid XML,
1098
+ // so we'll give them the single line plus the decl... Lame? sure.
1099
+ if (singleLine.trim().length() == 0) {
1100
+ // You want whitespace? I've got your whitespace right here.
1101
+ return decl + sep + singleLine;
1102
+ }
1103
+
1104
+ // Since the indent is not -1, bring back the XML declaration
1105
+ //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
1106
+
1107
+ StringWriter stringWriter = new StringWriter();
1108
+ StreamResult xmlOutput = new StreamResult(stringWriter);
1109
+ // DOMSource source = new DOMSource(node);
1110
+ Source source = new StreamSource(new StringReader(singleLine));
1111
+ transformer.transform(source, xmlOutput);
1112
+ String outgoing = stringWriter.toString();
1113
+
1114
+ // Add the XML declaration to the top if it's not there already
1115
+ if (outgoing.startsWith(decl)) {
1116
+ int declen = decl.length();
1117
+ int seplen = sep.length();
1118
+ if (outgoing.length() > declen + seplen &&
1119
+ !outgoing.substring(declen, declen + seplen).equals(sep)) {
1120
+ // make sure there's a line break between the XML decl and the code
1121
+ return outgoing.substring(0, decl.length()) +
1122
+ sep + outgoing.substring(decl.length());
1123
+ }
1124
+ return outgoing;
1125
+ } else {
1126
+ return decl + sep + outgoing;
1127
+ }
1128
+
1129
+ } catch (Exception e) {
1130
+ e.printStackTrace();
1131
+ }
1132
+ return null;
1133
+ }
1134
+
1135
+
1136
+ public void print() {
1137
+ PApplet.println(format(2));
1138
+ }
1139
+
1140
+
1141
+ /**
1142
+ * Return the XML document formatted with two spaces for indents.
1143
+ * Chosen to do this since it's the most common case (e.g. with println()).
1144
+ * Same as format(2). Use the format() function for more options.
1145
+ *
1146
+ * @webref xml:method
1147
+ * @brief Gets XML data as a String using default formatting
1148
+ * @return the content
1149
+ * @see XML#format(int)
1150
+ */
1151
+ @Override
1152
+ public String toString() {
1153
+ //return format(2);
1154
+ return format(-1);
1155
+ }
1156
+ }