Skip to content

Commit d4f85e1

Browse files
committed
API cleanups, fix processing#1660 and processing#1680 for JSON, plus processing#1734 for Table
1 parent dd1bafa commit d4f85e1

File tree

6 files changed

+167
-148
lines changed

6 files changed

+167
-148
lines changed

core/src/processing/core/PApplet.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6009,24 +6009,25 @@ header[17] image descriptor (packed bits)
60096009

60106010
// DATA I/O
60116011

6012-
/**
6013-
* @webref input:files
6014-
* @brief Creates a new XML object
6015-
* @param name the name to be given to the root element of the new XML object
6016-
* @return an XML object, or null
6017-
* @see XML
6018-
* @see PApplet#loadXML(String)
6019-
* @see PApplet#parseXML(String)
6020-
* @see PApplet#saveXML(XML, String)
6021-
*/
6022-
public XML createXML(String name) {
6023-
try {
6024-
return new XML(name);
6025-
} catch (Exception e) {
6026-
e.printStackTrace();
6027-
return null;
6028-
}
6029-
}
6012+
6013+
// /**
6014+
// * @webref input:files
6015+
// * @brief Creates a new XML object
6016+
// * @param name the name to be given to the root element of the new XML object
6017+
// * @return an XML object, or null
6018+
// * @see XML
6019+
// * @see PApplet#loadXML(String)
6020+
// * @see PApplet#parseXML(String)
6021+
// * @see PApplet#saveXML(XML, String)
6022+
// */
6023+
// public XML createXML(String name) {
6024+
// try {
6025+
// return new XML(name);
6026+
// } catch (Exception e) {
6027+
// e.printStackTrace();
6028+
// return null;
6029+
// }
6030+
// }
60306031

60316032

60326033
/**
@@ -6103,6 +6104,11 @@ public boolean saveXML(XML xml, String filename, String options) {
61036104
}
61046105

61056106

6107+
public JSONObject parseJSONObject(String input) {
6108+
return new JSONObject(new StringReader(input));
6109+
}
6110+
6111+
61066112
public JSONObject loadJSONObject(String filename) {
61076113
//JSONTokener tokener = new JSONTokener(createReader(filename));
61086114
//return new JSONObject(tokener);
@@ -6120,6 +6126,11 @@ public boolean saveJSONObject(JSONObject json, String filename, String options)
61206126
}
61216127

61226128

6129+
public JSONArray parseJSONArray(String input) {
6130+
return new JSONArray(new StringReader(input));
6131+
}
6132+
6133+
61236134
public JSONArray loadJSONArray(String filename) {
61246135
// JSONTokener tokener = new JSONTokener(createReader(filename));
61256136
// return new JSONArray(tokener);
@@ -6138,15 +6149,15 @@ public boolean saveJSONArray(JSONArray json, String filename, String options) {
61386149

61396150

61406151

6141-
/**
6142-
* @webref input:files
6143-
* @see Table
6144-
* @see PApplet#loadTable(String)
6145-
* @see PApplet#saveTable(Table, String)
6146-
*/
6147-
public Table createTable() {
6148-
return new Table();
6149-
}
6152+
// /**
6153+
// * @webref input:files
6154+
// * @see Table
6155+
// * @see PApplet#loadTable(String)
6156+
// * @see PApplet#saveTable(Table, String)
6157+
// */
6158+
// public Table createTable() {
6159+
// return new Table();
6160+
// }
61506161

61516162

61526163
/**

core/src/processing/data/JSONObject.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public class JSONObject {
129129
* undefined.
130130
*/
131131
private static final class Null {
132-
133132
/**
134133
* There is only intended to be a single instance of the NULL object,
135134
* so the clone method returns itself.

core/src/processing/data/Table.java

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ protected String checkOptions(File file, String options) throws IOException {
200200
if (dotIndex != -1) {
201201
extension = filename.substring(dotIndex + 1).toLowerCase();
202202
if (!extension.equals("csv") &&
203-
!extension.equals("tsv")) {
203+
!extension.equals("tsv") &&
204+
!extension.equals("html") &&
205+
!extension.equals("bin")) {
204206
// ignore extension
205207
extension = null;
206208
}
@@ -628,29 +630,37 @@ public boolean save(File file, String options) throws IOException {
628630

629631
public boolean save(OutputStream output, String options) {
630632
PrintWriter writer = PApplet.createWriter(output);
633+
String opt = null;
631634
if (options != null) {
632635
String[] opts = PApplet.splitTokens(options, ", ");
633-
for (String opt : opts) {
634-
if (opt.equals("csv")) {
635-
writeCSV(writer);
636-
} else if (opt.equals("tsv")) {
637-
writeTSV(writer);
638-
} else if (opt.equals("html")) {
639-
writeHTML(writer);
640-
} else if (opt.equals("bin")) {
641-
try {
642-
saveBinary(output);
643-
} catch (IOException e) {
644-
e.printStackTrace();
645-
return false;
646-
}
647-
} else {
648-
throw new IllegalArgumentException("'" + opt + "' not understood. " +
649-
"Only csv, tsv, bin, and html are " +
650-
"accepted as save parameters");
651-
}
636+
opt = opts[opts.length - 1];
637+
if (!opt.equals("csv") &&
638+
!opt.equals("tsv") &&
639+
!opt.equals("html") &&
640+
!opt.equals("bin")) {
641+
throw new IllegalArgumentException("'" + opt + "' not understood. " +
642+
"Only csv, tsv, bin, and html are " +
643+
"accepted as save parameters");
644+
}
645+
} else {
646+
opt = "tsv"; // fall back to saving as TSV
647+
}
648+
649+
if (opt.equals("csv")) {
650+
writeCSV(writer);
651+
} else if (opt.equals("tsv")) {
652+
writeTSV(writer);
653+
} else if (opt.equals("html")) {
654+
writeHTML(writer);
655+
} else if (opt.equals("bin")) {
656+
try {
657+
saveBinary(output);
658+
} catch (IOException e) {
659+
e.printStackTrace();
660+
return false;
652661
}
653662
}
663+
writer.flush();
654664
writer.close();
655665
return true;
656666
}

core/src/processing/data/XML.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,18 @@ public XML(Reader reader, String options) throws IOException, ParserConfiguratio
167167
/**
168168
* @param name description TBD
169169
*/
170-
// TODO is there a more efficient way of doing this? wow.
171-
public XML(String name) throws ParserConfigurationException {
172-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
173-
DocumentBuilder builder = factory.newDocumentBuilder();
174-
Document document = builder.newDocument();
175-
node = document.createElement(name);
176-
// this.name = name;
177-
this.parent = null;
170+
public XML(String name) {
171+
try {
172+
// TODO is there a more efficient way of doing this? wow.
173+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
174+
DocumentBuilder builder = factory.newDocumentBuilder();
175+
Document document = builder.newDocument();
176+
node = document.createElement(name);
177+
this.parent = null;
178+
179+
} catch (ParserConfigurationException pce) {
180+
throw new RuntimeException(pce);
181+
}
178182
}
179183

180184

0 commit comments

Comments
 (0)