API cleanups, fix #1660 and #1680 for JSON, plus #1734 for Table

This commit is contained in:
Ben Fry
2013-04-28 20:43:49 -04:00
parent 3ab855e69a
commit b56459d14f
6 changed files with 166 additions and 147 deletions

View File

@@ -200,7 +200,9 @@ public class Table {
if (dotIndex != -1) {
extension = filename.substring(dotIndex + 1).toLowerCase();
if (!extension.equals("csv") &&
!extension.equals("tsv")) {
!extension.equals("tsv") &&
!extension.equals("html") &&
!extension.equals("bin")) {
// ignore extension
extension = null;
}
@@ -628,29 +630,37 @@ public class Table {
public boolean save(OutputStream output, String options) {
PrintWriter writer = PApplet.createWriter(output);
String opt = null;
if (options != null) {
String[] opts = PApplet.splitTokens(options, ", ");
for (String opt : opts) {
if (opt.equals("csv")) {
writeCSV(writer);
} else if (opt.equals("tsv")) {
writeTSV(writer);
} else if (opt.equals("html")) {
writeHTML(writer);
} else if (opt.equals("bin")) {
try {
saveBinary(output);
} catch (IOException e) {
e.printStackTrace();
return false;
}
} else {
throw new IllegalArgumentException("'" + opt + "' not understood. " +
"Only csv, tsv, bin, and html are " +
"accepted as save parameters");
}
opt = opts[opts.length - 1];
if (!opt.equals("csv") &&
!opt.equals("tsv") &&
!opt.equals("html") &&
!opt.equals("bin")) {
throw new IllegalArgumentException("'" + opt + "' not understood. " +
"Only csv, tsv, bin, and html are " +
"accepted as save parameters");
}
} else {
opt = "tsv"; // fall back to saving as TSV
}
if (opt.equals("csv")) {
writeCSV(writer);
} else if (opt.equals("tsv")) {
writeTSV(writer);
} else if (opt.equals("html")) {
writeHTML(writer);
} else if (opt.equals("bin")) {
try {
saveBinary(output);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
writer.flush();
writer.close();
return true;
}