From 53dff0de63422b37b672fa5762d2d9c0650b3ff1 Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 28 Jul 2012 19:30:43 +0000 Subject: [PATCH] several XML fixes --- android/core/src/processing/core/PApplet.java | 16 +- .../core/src/processing/core/PShapeSVG.java | 2 +- android/core/src/processing/data/XML.java | 30 ++- .../src/processing/opengl/PGraphics2D.java | 13 +- android/todo.txt | 9 +- app/src/processing/mode/android/Manifest.java | 58 ++-- core/src/processing/core/PApplet.java | 14 +- core/src/processing/core/PGraphicsJava2D.java | 3 +- core/src/processing/data/XML.java | 51 ++-- core/src/processing/opengl/PGraphics2D.java | 247 +++++++++--------- core/todo.txt | 3 +- 11 files changed, 245 insertions(+), 201 deletions(-) diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index f50cf40ed..b0e1e3674 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -3753,14 +3753,24 @@ public class PApplet extends Activity implements PConstants, Runnable { public XML loadXML(String filename) { - return new XML(this, filename); + try { + return new XML(this, filename); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } static public XML loadXML(File file) { - return new XML(file); + try { + return new XML(file); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } - + public Table loadTable(String filename) { return new Table(this, filename); diff --git a/android/core/src/processing/core/PShapeSVG.java b/android/core/src/processing/core/PShapeSVG.java index 23960f1e3..d5fab309d 100644 --- a/android/core/src/processing/core/PShapeSVG.java +++ b/android/core/src/processing/core/PShapeSVG.java @@ -154,7 +154,7 @@ public class PShapeSVG extends PShape { /** * Initializes a new SVG Object with the given filename. */ - public PShapeSVG(PApplet parent, String filename) { + public PShapeSVG(PApplet parent, String filename) throws Exception { // this will grab the root document, starting // the xml version and initial comments are ignored this(new XML(parent, filename)); diff --git a/android/core/src/processing/data/XML.java b/android/core/src/processing/data/XML.java index c7adbce21..8e4a78147 100644 --- a/android/core/src/processing/data/XML.java +++ b/android/core/src/processing/data/XML.java @@ -62,23 +62,28 @@ public class XML implements Serializable { * Begin parsing XML data passed in from a PApplet. This code * wraps exception handling, for more advanced exception handling, * use the constructor that takes a Reader or InputStream. + * @throws SAXException + * @throws ParserConfigurationException + * @throws IOException */ - public XML(PApplet parent, String filename) { + public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException { this(parent.createReader(filename)); } - public XML(File file) { + public XML(File file) throws IOException, ParserConfigurationException, SAXException { this(PApplet.createReader(file)); } - public XML(Reader reader) { - try { + public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - // Prevent 503 errors from www.w3.org - factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + try { + factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + } catch (IllegalArgumentException e) { + // ignore this; Android doesn't like it + } // without a validating DTD, this doesn't do anything since it doesn't know what is ignorable // factory.setIgnoringElementContentWhitespace(true); @@ -109,15 +114,7 @@ public class XML implements Serializable { // for (int i = 0; i < nodeList.getLength(); i++) { // } // print(createWriter("data/1_alt_reparse.html"), document.getDocumentElement(), 0); - - } catch (ParserConfigurationException pce) { - pce.printStackTrace(); - } catch (IOException e1) { - e1.printStackTrace(); - } catch (SAXException e2) { - e2.printStackTrace(); } - } // TODO is there a more efficient way of doing this? wow. @@ -154,7 +151,12 @@ public class XML implements Serializable { static public XML parse(String xml) { + try { return new XML(new StringReader(xml)); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } diff --git a/android/core/src/processing/opengl/PGraphics2D.java b/android/core/src/processing/opengl/PGraphics2D.java index 0cd86722d..6482001e6 100644 --- a/android/core/src/processing/opengl/PGraphics2D.java +++ b/android/core/src/processing/opengl/PGraphics2D.java @@ -22,7 +22,6 @@ package processing.opengl; -import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; @@ -220,17 +219,17 @@ public class PGraphics2D extends PGraphicsOpenGL { static protected PShape2D loadShapeImpl(PGraphics pg, String filename, String extension) { PShapeSVG svg = null; - if (extension.equals("svg")) { - svg = new PShapeSVG(pg.parent, filename); + try { + if (extension.equals("svg")) { + svg = new PShapeSVG(pg.parent, filename); - } else if (extension.equals("svgz")) { - try { + } else if (extension.equals("svgz")) { InputStream input = new GZIPInputStream(pg.parent.createInput(filename)); XML xml = new XML(PApplet.createReader(input)); svg = new PShapeSVG(xml); - } catch (IOException e) { - e.printStackTrace(); } + } catch (Exception e) { + e.printStackTrace(); } if (svg != null) { diff --git a/android/todo.txt b/android/todo.txt index c5ac2ec94..14d8003e9 100644 --- a/android/todo.txt +++ b/android/todo.txt @@ -1,7 +1,12 @@ 0206 android X add full PAppletMethods implementation to Android -_ swap Run on Device and Run on Emulator -_ http://code.google.com/p/processing/issues/detail?id=1083 +X swap Run on Device and Run on Emulator +X http://code.google.com/p/processing/issues/detail?id=1083 +X XML crash on loading because of desktop-specific attribute +X error: "http://apache.org/xml/features/nonvalidating/load-external-dtd" +X http://code.google.com/p/processing/issues/detail?id=1128 +X fix problems with XML loading so that PShape works again +X http://code.google.com/p/processing/issues/detail?id=1054 _ Android OPENGL renderer + JAVA2D PGraphics results in PTexture exception _ http://code.google.com/p/processing/issues/detail?id=1019 diff --git a/app/src/processing/mode/android/Manifest.java b/app/src/processing/mode/android/Manifest.java index 1818538b2..fd7b7b648 100644 --- a/app/src/processing/mode/android/Manifest.java +++ b/app/src/processing/mode/android/Manifest.java @@ -27,6 +27,10 @@ import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + import processing.app.*; import processing.core.PApplet; import processing.data.XML; @@ -195,32 +199,38 @@ public class Manifest { save(file); // load the copy from the build location and start messing with it - XML mf = new XML(new FileReader(file)); + XML mf = null; + try { + mf = new XML(new FileReader(file)); - // package name, or default - String p = mf.getString("package").trim(); - if (p.length() == 0) { - mf.setString("package", defaultPackageName()); - } + // package name, or default + String p = mf.getString("package").trim(); + if (p.length() == 0) { + mf.setString("package", defaultPackageName()); + } - // app name and label, or the class name - XML app = mf.getChild("application"); - String label = app.getString("android:label"); - if (label.length() == 0) { - app.setString("android:label", className); - } - app.setString("android:debuggable", debug ? "true" : "false"); + // app name and label, or the class name + XML app = mf.getChild("application"); + String label = app.getString("android:label"); + if (label.length() == 0) { + app.setString("android:label", className); + } + app.setString("android:debuggable", debug ? "true" : "false"); - XML activity = app.getChild("activity"); - // the '.' prefix is just an alias for the full package name - // http://developer.android.com/guide/topics/manifest/activity-element.html#name - activity.setString("android:name", "." + className); // this has to be right + XML activity = app.getChild("activity"); + // the '.' prefix is just an alias for the full package name + // http://developer.android.com/guide/topics/manifest/activity-element.html#name + activity.setString("android:name", "." + className); // this has to be right - PrintWriter writer = PApplet.createWriter(file); - writer.print(mf.toString()); - writer.flush(); + PrintWriter writer = PApplet.createWriter(file); + writer.print(mf.toString()); + writer.flush(); // mf.write(writer); - writer.close(); + writer.close(); + + } catch (Exception e) { + e.printStackTrace(); + } } @@ -255,6 +265,12 @@ public class Manifest { } catch (FileNotFoundException e) { System.err.println("Could not read " + manifestFile.getAbsolutePath()); e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (ParserConfigurationException e) { + e.printStackTrace(); + } catch (SAXException e) { + e.printStackTrace(); } } if (xml == null) { diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 4a339aea6..0f7a552b1 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -5295,12 +5295,22 @@ public class PApplet extends Applet public XML loadXML(String filename) { - return new XML(this, filename); + try { + return new XML(this, filename); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } static public XML loadXML(File file) { - return new XML(file); + try { + return new XML(file); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } diff --git a/core/src/processing/core/PGraphicsJava2D.java b/core/src/processing/core/PGraphicsJava2D.java index 9185fd051..c0a9a2af3 100644 --- a/core/src/processing/core/PGraphicsJava2D.java +++ b/core/src/processing/core/PGraphicsJava2D.java @@ -26,7 +26,6 @@ package processing.core; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; -import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.zip.GZIPInputStream; @@ -1128,7 +1127,7 @@ public class PGraphicsJava2D extends PGraphics /*PGraphics2D*/ { InputStream input = new GZIPInputStream(parent.createInput(filename)); XML xml = new XML(PApplet.createReader(input)); svg = new PShapeSVG(xml); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } else { diff --git a/core/src/processing/data/XML.java b/core/src/processing/data/XML.java index 004874c2c..9f2d681ce 100644 --- a/core/src/processing/data/XML.java +++ b/core/src/processing/data/XML.java @@ -62,61 +62,59 @@ public class XML implements Serializable { * Begin parsing XML data passed in from a PApplet. This code * wraps exception handling, for more advanced exception handling, * use the constructor that takes a Reader or InputStream. + * @throws SAXException + * @throws ParserConfigurationException + * @throws IOException */ - public XML(PApplet parent, String filename) { + public XML(PApplet parent, String filename) throws IOException, ParserConfigurationException, SAXException { this(parent.createReader(filename)); } - public XML(File file) { + public XML(File file) throws IOException, ParserConfigurationException, SAXException { this(PApplet.createReader(file)); } - public XML(Reader reader) { + public XML(Reader reader) throws IOException, ParserConfigurationException, SAXException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + // Prevent 503 errors from www.w3.org try { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - - // Prevent 503 errors from www.w3.org factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + } catch (IllegalArgumentException e) { + // ignore this; Android doesn't like it + } - // without a validating DTD, this doesn't do anything since it doesn't know what is ignorable + // without a validating DTD, this doesn't do anything since it doesn't know what is ignorable // factory.setIgnoringElementContentWhitespace(true); - factory.setExpandEntityReferences(false); + factory.setExpandEntityReferences(false); // factory.setExpandEntityReferences(true); // factory.setCoalescing(true); // builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); - DocumentBuilder builder = factory.newDocumentBuilder(); + DocumentBuilder builder = factory.newDocumentBuilder(); // builder.setEntityResolver() // SAXParserFactory spf = SAXParserFactory.newInstance(); // spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); // SAXParser p = spf.newSAXParser(); - // builder = DocumentBuilderFactory.newDocumentBuilder(); - // builder = new SAXBuilder(); - // builder.setValidation(validating); + // builder = DocumentBuilderFactory.newDocumentBuilder(); + // builder = new SAXBuilder(); + // builder.setValidation(validating); // print(dataPath("1broke.html"), System.out); // Document document = builder.parse(dataPath("1_alt.html")); - Document document = builder.parse(new InputSource(reader)); - node = document.getDocumentElement(); - name = node.getNodeName(); + Document document = builder.parse(new InputSource(reader)); + node = document.getDocumentElement(); + name = node.getNodeName(); // NodeList nodeList = document.getDocumentElement().getChildNodes(); // for (int i = 0; i < nodeList.getLength(); i++) { // } // print(createWriter("data/1_alt_reparse.html"), document.getDocumentElement(), 0); - - } catch (ParserConfigurationException pce) { - pce.printStackTrace(); - } catch (IOException e1) { - e1.printStackTrace(); - } catch (SAXException e2) { - e2.printStackTrace(); - } } @@ -154,7 +152,12 @@ public class XML implements Serializable { static public XML parse(String xml) { - return new XML(new StringReader(xml)); + try { + return new XML(new StringReader(xml)); + } catch (Exception e) { + e.printStackTrace(); + return null; + } } diff --git a/core/src/processing/opengl/PGraphics2D.java b/core/src/processing/opengl/PGraphics2D.java index 0cd86722d..eb05cfbac 100644 --- a/core/src/processing/opengl/PGraphics2D.java +++ b/core/src/processing/opengl/PGraphics2D.java @@ -22,7 +22,6 @@ package processing.opengl; -import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; @@ -34,33 +33,33 @@ import processing.core.PShapeSVG; import processing.data.XML; public class PGraphics2D extends PGraphicsOpenGL { - + public PGraphics2D() { super(); hints[ENABLE_PERSPECTIVE_CORRECTED_LINES] = false; } - + ////////////////////////////////////////////////////////////// // RENDERER SUPPORT QUERIES - - + + public boolean is2D() { return true; } - + public boolean is3D() { return false; - } - - + } + + ////////////////////////////////////////////////////////////// // HINTS - + public void hint(int which) { if (which == ENABLE_PERSPECTIVE_CORRECTED_LINES) { showWarning("2D lines cannot be perspective-corrected."); @@ -69,149 +68,149 @@ public class PGraphics2D extends PGraphicsOpenGL { super.hint(which); } - + ////////////////////////////////////////////////////////////// // PROJECTION - - + + public void ortho() { showMethodWarning("ortho"); } - - + + public void ortho(float left, float right, float bottom, float top) { showMethodWarning("ortho"); } - - + + public void ortho(float left, float right, float bottom, float top, float near, float far) { showMethodWarning("ortho"); } - - + + public void perspective() { showMethodWarning("perspective"); - } - - + } + + public void perspective(float fov, float aspect, float zNear, float zFar) { showMethodWarning("perspective"); } - - + + public void frustum(float left, float right, float bottom, float top, float znear, float zfar) { showMethodWarning("frustum"); } - - - protected void defaultPerspective() { + + + protected void defaultPerspective() { super.ortho(-width/2, +width/2, -height/2, +height/2, -1, +1); } - - + + ////////////////////////////////////////////////////////////// // CAMERA - - + + public void beginCamera() { showMethodWarning("beginCamera"); } - - + + public void endCamera() { showMethodWarning("endCamera"); } - - + + public void camera() { showMethodWarning("camera"); } - - + + public void camera(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { showMethodWarning("camera"); } - - protected void defaultCamera() { + + protected void defaultCamera() { super.camera(width/2, height/2); } - - + + ////////////////////////////////////////////////////////////// // MATRIX MORE! - - + + protected void begin2D() { pushProjection(); defaultPerspective(); pushMatrix(); - defaultCamera(); + defaultCamera(); } - + protected void end2D() { popMatrix(); - popProjection(); - } + popProjection(); + } + - ////////////////////////////////////////////////////////////// // SHAPE - - + + public void shape(PShape shape) { if (shape.is2D()) { super.shape(shape); } else { - showWarning("The shape object is not 2D, cannot be displayed with this renderer"); + showWarning("The shape object is not 2D, cannot be displayed with this renderer"); } } - - + + public void shape(PShape shape, float x, float y) { if (shape.is2D()) { super.shape(shape, x, y); } else { - showWarning("The shape object is not 2D, cannot be displayed with this renderer"); - } + showWarning("The shape object is not 2D, cannot be displayed with this renderer"); + } } - - + + public void shape(PShape shape, float a, float b, float c, float d) { if (shape.is2D()) { super.shape(shape, a, b, c, d); } else { - showWarning("The shape object is not 2D, cannot be displayed with this renderer"); - } + showWarning("The shape object is not 2D, cannot be displayed with this renderer"); + } } - - + + public void shape(PShape shape, float x, float y, float z) { - showDepthWarningXYZ("shape"); + showDepthWarningXYZ("shape"); } - - + + public void shape(PShape shape, float x, float y, float z, float c, float d, float e) { - showDepthWarningXYZ("shape"); + showDepthWarningXYZ("shape"); } - - + + ////////////////////////////////////////////////////////////// // SHAPE I/O - - + + static protected boolean isSupportedExtension(String extension) { return extension.equals("svg") || extension.equals("svgz"); } @@ -219,7 +218,7 @@ public class PGraphics2D extends PGraphicsOpenGL { static protected PShape2D loadShapeImpl(PGraphics pg, String filename, String extension) { PShapeSVG svg = null; - + if (extension.equals("svg")) { svg = new PShapeSVG(pg.parent, filename); @@ -228,30 +227,30 @@ public class PGraphics2D extends PGraphicsOpenGL { InputStream input = new GZIPInputStream(pg.parent.createInput(filename)); XML xml = new XML(PApplet.createReader(input)); svg = new PShapeSVG(xml); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } - + if (svg != null) { - PShape2D p2d = PShape2D.createShape(pg.parent, svg); + PShape2D p2d = PShape2D.createShape(pg.parent, svg); return p2d; } else { return null; } } - + ////////////////////////////////////////////////////////////// // SHAPE CREATION - - + + public PShape createShape(PShape source) { - return PShape2D.createShape(parent, source); + return PShape2D.createShape(parent, source); } - + public PShape createShape() { return createShape(POLYGON); } @@ -260,13 +259,13 @@ public class PGraphics2D extends PGraphicsOpenGL { public PShape createShape(int type) { return createShapeImpl(parent, type); } - - + + public PShape createShape(int kind, float... p) { - return createShapeImpl(parent, kind, p); + return createShapeImpl(parent, kind, p); } - - + + static protected PShape2D createShapeImpl(PApplet parent, int type) { PShape2D shape = null; if (type == PShape.GROUP) { @@ -368,21 +367,21 @@ public class PGraphics2D extends PGraphicsOpenGL { } return shape; - } - - + } + + ////////////////////////////////////////////////////////////// // BEZIER VERTICES - + public void bezierVertex(float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4) { showDepthWarningXYZ("bezierVertex"); } - - + + ////////////////////////////////////////////////////////////// // QUADRATIC BEZIER VERTICES @@ -391,19 +390,19 @@ public class PGraphics2D extends PGraphicsOpenGL { public void quadraticVertex(float x2, float y2, float z2, float x4, float y4, float z4) { showDepthWarningXYZ("quadVertex"); - } - - + } + + ////////////////////////////////////////////////////////////// - // CURVE VERTICES - - + // CURVE VERTICES + + public void curveVertex(float x, float y, float z) { showDepthWarningXYZ("curveVertex"); - } - - + } + + ////////////////////////////////////////////////////////////// // BOX @@ -411,9 +410,9 @@ public class PGraphics2D extends PGraphicsOpenGL { public void box(float w, float h, float d) { showMethodWarning("box"); - } + } + - ////////////////////////////////////////////////////////////// // SPHERE @@ -421,30 +420,30 @@ public class PGraphics2D extends PGraphicsOpenGL { public void sphere(float r) { showMethodWarning("sphere"); - } - - + } + + ////////////////////////////////////////////////////////////// // VERTEX SHAPES - - + + public void vertex(float x, float y, float z) { showDepthWarningXYZ("vertex"); } - + public void vertex(float x, float y, float z, float u, float v) { showDepthWarningXYZ("vertex"); - } - + } + ////////////////////////////////////////////////////////////// - // MATRIX TRANSFORMATIONS - + // MATRIX TRANSFORMATIONS + public void translate(float tx, float ty, float tz) { showDepthWarningXYZ("translate"); } - + public void rotateX(float angle) { showDepthWarning("rotateX"); } @@ -459,27 +458,27 @@ public class PGraphics2D extends PGraphicsOpenGL { public void rotate(float angle, float vx, float vy, float vz) { showVariationWarning("rotate"); - } - + } + public void applyMatrix(PMatrix3D source) { showVariationWarning("applyMatrix"); } - + public void applyMatrix(float n00, float n01, float n02, float n03, float n10, float n11, float n12, float n13, float n20, float n21, float n22, float n23, float n30, float n31, float n32, float n33) { showVariationWarning("applyMatrix"); - } - + } + public void scale(float sx, float sy, float sz) { showDepthWarningXYZ("scale"); } - + ////////////////////////////////////////////////////////////// - // SCREEN AND MODEL COORDS - + // SCREEN AND MODEL COORDS + public float screenX(float x, float y, float z) { showDepthWarningXYZ("screenX"); return 0; @@ -493,18 +492,18 @@ public class PGraphics2D extends PGraphicsOpenGL { public float screenZ(float x, float y, float z) { showDepthWarningXYZ("screenZ"); return 0; - } - + } + public PMatrix3D getMatrix(PMatrix3D target) { showVariationWarning("getMatrix"); return target; } - + public void setMatrix(PMatrix3D source) { showVariationWarning("setMatrix"); } - - ////////////////////////////////////////////////////////////// + + ////////////////////////////////////////////////////////////// // LIGHTS diff --git a/core/todo.txt b/core/todo.txt index 529bd7941..6b587747f 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -6,7 +6,6 @@ X http://code.google.com/p/processing/issues/detail?id=1045 X displayWidth/Height not being set properly before setup() X http://code.google.com/p/processing/issues/detail?id=1120 X can't reproduce.. might be Java update, or multi-display issue? - X selectInput() and selectOutput() freezes X http://code.google.com/p/processing/issues/detail?id=173 o http://code.google.com/p/processing/issues/detail?id=445 @@ -20,6 +19,8 @@ X use callbacks instead X need to decide if you specify the function name, or if it's specific X Set default path for selectXxxxx() functions X http://code.google.com/p/processing/issues/detail?id=233 +X XML now throws exceptions in its constructor, use loadXML() instead +X http://code.google.com/p/processing/issues/detail?id=1138 3D and OpenGL A ortho() causing line and fill to be misaligned