incorporating edits from the desktop version--new xml api

This commit is contained in:
benfry
2010-08-07 20:10:18 +00:00
parent fec4ae5c60
commit d9e37ab6b5
7 changed files with 1452 additions and 1321 deletions

View File

@@ -173,7 +173,7 @@ public class PShapeSVG extends PShape {
// not proper parsing of the viewBox, but will cover us for cases where
// the width and height of the object is not specified
String viewBoxStr = svg.getStringAttribute("viewBox");
String viewBoxStr = svg.getString("viewBox");
if (viewBoxStr != null) {
int[] viewBox = PApplet.parseInt(PApplet.splitTokens(viewBoxStr));
width = viewBox[2];
@@ -183,8 +183,8 @@ public class PShapeSVG extends PShape {
// TODO if viewbox is not same as width/height, then use it to scale
// the original objects. for now, viewbox only used when width/height
// are empty values (which by the spec means w/h of "100%"
String unitWidth = svg.getStringAttribute("width");
String unitHeight = svg.getStringAttribute("height");
String unitWidth = svg.getString("width");
String unitHeight = svg.getString("height");
if (unitWidth != null) {
width = parseUnitSize(unitWidth);
height = parseUnitSize(unitHeight);
@@ -258,7 +258,7 @@ public class PShapeSVG extends PShape {
}
element = properties;
name = properties.getStringAttribute("id");
name = properties.getString("id");
// @#$(* adobe illustrator mangles names of objects when re-saving
if (name != null) {
while (true) {
@@ -269,10 +269,10 @@ public class PShapeSVG extends PShape {
}
}
String displayStr = properties.getStringAttribute("display", "inline");
String displayStr = properties.getString("display", "inline");
visible = !displayStr.equals("none");
String transformStr = properties.getStringAttribute("transform");
String transformStr = properties.getString("transform");
if (transformStr != null) {
matrix = parseMatrix(transformStr);
}
@@ -290,6 +290,9 @@ public class PShapeSVG extends PShape {
for (XMLElement elem : elements) {
PShape kid = parseChild(elem);
if (kid != null) {
// if (kid.name != null) {
// System.out.println("adding child " + kid.name);
// }
addChild(kid);
}
}
@@ -442,7 +445,7 @@ public class PShapeSVG extends PShape {
family = PATH;
this.close = close;
String pointsAttr = element.getStringAttribute("points");
String pointsAttr = element.getString("points");
if (pointsAttr != null) {
String[] pointsBuffer = PApplet.splitTokens(pointsAttr);
vertexCount = pointsBuffer.length;
@@ -460,7 +463,7 @@ public class PShapeSVG extends PShape {
family = PATH;
primitive = 0;
String pathData = element.getStringAttribute("d");
String pathData = element.getString("d");
if (pathData == null) return;
char[] pathDataChars = pathData.toCharArray();
@@ -864,50 +867,50 @@ public class PShapeSVG extends PShape {
protected void parseColors(XMLElement properties) {
if (properties.hasAttribute("opacity")) {
String opacityText = properties.getStringAttribute("opacity");
String opacityText = properties.getString("opacity");
setOpacity(opacityText);
}
if (properties.hasAttribute("stroke")) {
String strokeText = properties.getStringAttribute("stroke");
String strokeText = properties.getString("stroke");
setColor(strokeText, false);
}
if (properties.hasAttribute("stroke-opacity")) {
String strokeOpacityText = properties.getStringAttribute("stroke-opacity");
String strokeOpacityText = properties.getString("stroke-opacity");
setStrokeOpacity(strokeOpacityText);
}
if (properties.hasAttribute("stroke-width")) {
// if NaN (i.e. if it's 'inherit') then default back to the inherit setting
String lineweight = properties.getStringAttribute("stroke-width");
String lineweight = properties.getString("stroke-width");
setStrokeWeight(lineweight);
}
if (properties.hasAttribute("stroke-linejoin")) {
String linejoin = properties.getStringAttribute("stroke-linejoin");
String linejoin = properties.getString("stroke-linejoin");
setStrokeJoin(linejoin);
}
if (properties.hasAttribute("stroke-linecap")) {
String linecap = properties.getStringAttribute("stroke-linecap");
String linecap = properties.getString("stroke-linecap");
setStrokeCap(linecap);
}
// fill defaults to black (though stroke defaults to "none")
// http://www.w3.org/TR/SVG/painting.html#FillProperties
if (properties.hasAttribute("fill")) {
String fillText = properties.getStringAttribute("fill");
String fillText = properties.getString("fill");
setColor(fillText, true);
}
if (properties.hasAttribute("fill-opacity")) {
String fillOpacityText = properties.getStringAttribute("fill-opacity");
String fillOpacityText = properties.getString("fill-opacity");
setFillOpacity(fillOpacityText);
}
if (properties.hasAttribute("style")) {
String styleText = properties.getStringAttribute("style");
String styleText = properties.getString("style");
String[] styleTokens = PApplet.splitTokens(styleText, ";");
//PApplet.println(styleTokens);
@@ -1086,7 +1089,7 @@ public class PShapeSVG extends PShape {
* @return unit-parsed version of the data
*/
static protected float getFloatWithUnit(XMLElement element, String attribute) {
String val = element.getStringAttribute(attribute);
String val = element.getString(attribute);
return (val == null) ? 0 : parseUnitSize(val);
}
@@ -1146,14 +1149,14 @@ public class PShapeSVG extends PShape {
XMLElement elem = elements[i];
String name = elem.getName();
if (name.equals("stop")) {
String offsetAttr = elem.getStringAttribute("offset");
String offsetAttr = elem.getString("offset");
float div = 1.0f;
if (offsetAttr.endsWith("%")) {
div = 100.0f;
offsetAttr = offsetAttr.substring(0, offsetAttr.length() - 1);
}
offset[count] = PApplet.parseFloat(offsetAttr) / div;
String style = elem.getStringAttribute("style");
String style = elem.getString("style");
HashMap<String, String> styles = parseStyleAttributes(style);
String colorStr = styles.get("stop-color");
@@ -1184,7 +1187,7 @@ public class PShapeSVG extends PShape {
this.y2 = getFloatWithUnit(properties, "y2");
String transformStr =
properties.getStringAttribute("gradientTransform");
properties.getString("gradientTransform");
if (transformStr != null) {
float t[] = parseMatrix(transformStr).get(null);
@@ -1227,7 +1230,7 @@ public class PShapeSVG extends PShape {
this.r = getFloatWithUnit(properties, "r");
String transformStr =
properties.getStringAttribute("gradientTransform");
properties.getString("gradientTransform");
if (transformStr != null) {
float t[] = parseMatrix(transformStr).get(null);

View File

@@ -28,24 +28,19 @@
package processing.xml;
import java.io.IOException;
import java.io.Reader;
import java.util.Stack;
/**
* StdXMLBuilder is a concrete implementation of IXMLBuilder which creates a
* tree of IXMLElement from an XML data source.
* StdXMLBuilder creates a tree of XML elements from a data source.
*
* @see processing.xml.XMLElement
*
* @author Marc De Scheemaecker
* @version $Name: RELEASE_2_2_1 $, $Revision: 1.3 $
*/
public class StdXMLBuilder
{
public class StdXMLBuilder {
/**
* This stack contains the current element and its parents.
*/
@@ -59,42 +54,21 @@ public class StdXMLBuilder
private XMLElement parent;
/**
* Prototype element for creating the tree.
*/
//private XMLElement prototype;
/**
* Creates the builder.
*/
public StdXMLBuilder()
{
public StdXMLBuilder() {
this(new XMLElement());
this.stack = null;
this.root = null;
//this(new XMLElement());
}
public StdXMLBuilder(XMLElement parent)
{
public StdXMLBuilder(XMLElement parent) {
this.parent = parent;
}
/**
* Creates the builder.
*
* @param prototype the prototype to use when building the tree.
*/
// public StdXMLBuilder(XMLElement prototype)
// {
// this.stack = null;
// this.root = null;
// this.prototype = prototype;
// }
/**
* Cleans up the object when it's destroyed.
*/
@@ -178,7 +152,7 @@ public class StdXMLBuilder
if (this.stack.empty()) {
//System.out.println("setting root");
parent.set(fullName, nsURI, systemID, lineNr);
parent.init(fullName, nsURI, systemID, lineNr);
stack.push(parent);
root = parent;
} else {
@@ -232,11 +206,11 @@ public class StdXMLBuilder
XMLElement elt = (XMLElement) this.stack.pop();
if (elt.getChildCount() == 1) {
XMLElement child = elt.getChildAtIndex(0);
XMLElement child = elt.getChild(0);
if (child.getLocalName() == null) {
elt.setContent(child.getContent());
elt.removeChildAtIndex(0);
elt.removeChild(0);
}
}
}
@@ -276,15 +250,15 @@ public class StdXMLBuilder
if (top.hasAttribute(fullName)) {
throw new XMLParseException(top.getSystemID(),
top.getLineNr(),
top.getLine(),
"Duplicate attribute: " + key);
}
if (nsPrefix != null) {
top.setAttribute(fullName, nsURI, value);
} else {
top.setAttribute(fullName, value);
}
// if (nsPrefix != null) {
// top.setAttribute(fullName, nsURI, value);
// } else {
top.setString(fullName, value);
// }
}

View File

@@ -497,17 +497,18 @@ public class StdXMLParser {
attrTypes.addElement("CDATA");
}
for (int i = 0; i < attrNames.size(); i++) {
String key = (String) attrNames.elementAt(i);
String value = (String) attrValues.elementAt(i);
//String type = (String) attrTypes.elementAt(i);
// post 1.2.1, just treat namespaces like any other attribute
// for (int i = 0; i < attrNames.size(); i++) {
// String key = (String) attrNames.elementAt(i);
// String value = (String) attrValues.elementAt(i);
// //String type = (String) attrTypes.elementAt(i);
if (key.equals("xmlns")) {
defaultNamespace = value;
} else if (key.startsWith("xmlns:")) {
namespaces.put(key.substring(6), value);
}
}
// if (key.equals("xmlns")) {
// defaultNamespace = value;
// } else if (key.startsWith("xmlns:")) {
// namespaces.put(key.substring(6), value);
// }
// }
if (prefix == null) {
this.builder.startElement(name, prefix, defaultNamespace,
@@ -523,9 +524,9 @@ public class StdXMLParser {
for (int i = 0; i < attrNames.size(); i++) {
String key = (String) attrNames.elementAt(i);
if (key.startsWith("xmlns")) {
continue;
}
// if (key.startsWith("xmlns")) {
// continue;
// }
String value = (String) attrValues.elementAt(i);
String type = (String) attrTypes.elementAt(i);

View File

@@ -43,13 +43,13 @@ class XMLAttribute
/**
* The full name of the attribute.
*/
private String fullName;
private String name;
/**
* The short name of the attribute.
*/
private String name;
private String localName;
/**
@@ -85,8 +85,8 @@ class XMLAttribute
String value,
String type)
{
this.fullName = fullName;
this.name = name;
this.name = fullName;
this.localName = name;
this.namespace = namespace;
this.value = value;
this.type = type;
@@ -96,18 +96,18 @@ class XMLAttribute
/**
* Returns the full name of the attribute.
*/
String getFullName()
String getName()
{
return this.fullName;
return this.name;
}
/**
* Returns the short name of the attribute.
*/
String getName()
String getLocalName()
{
return this.name;
return this.localName;
}

File diff suppressed because it is too large Load Diff

View File

@@ -33,8 +33,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Vector;
//import java.util.Enumeration;
/**
@@ -42,8 +41,9 @@ import java.util.Vector;
*
* @author Marc De Scheemaecker
*/
public class XMLWriter
{
public class XMLWriter {
static final int INDENT = 2;
static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
/**
* Where to write the output to.
@@ -96,7 +96,7 @@ public class XMLWriter
public void write(XMLElement xml)
throws IOException
{
this.write(xml, false, 0, true);
this.write(xml, false, 0, INDENT, true);
}
@@ -107,11 +107,8 @@ public class XMLWriter
* @param prettyPrint if spaces need to be inserted to make the output more
* readable
*/
public void write(XMLElement xml,
boolean prettyPrint)
throws IOException
{
this.write(xml, prettyPrint, 0, true);
public void write(XMLElement xml, boolean prettyPrint) throws IOException {
this.write(xml, prettyPrint, 0, INDENT, true);
}
@@ -123,12 +120,9 @@ public class XMLWriter
* readable
* @param indent how many spaces to indent the element.
*/
public void write(XMLElement xml,
boolean prettyPrint,
int indent)
throws IOException
{
this.write(xml, prettyPrint, indent, true);
public void write(XMLElement xml, boolean prettyPrint, int initialIndent)
throws IOException {
this.write(xml, prettyPrint, initialIndent, INDENT, true);
}
@@ -138,16 +132,15 @@ public class XMLWriter
* @param xml the non-null XML element to write.
* @param prettyPrint if spaces need to be inserted to make the output more
* readable
* @param indent how many spaces to indent the element.
* @param initialIndent how many spaces to indent the first element.
*/
public void write(XMLElement xml,
boolean prettyPrint,
int indent,
boolean collapseEmptyElements)
throws IOException
{
boolean prettyPrint,
int initialIndent,
int eachIndent,
boolean collapseEmptyElements) throws IOException {
if (prettyPrint) {
for (int i = 0; i < indent; i++) {
for (int i = 0; i < initialIndent; i++) {
this.writer.print(' ');
}
}
@@ -164,45 +157,50 @@ public class XMLWriter
} else {
this.writer.print('<');
this.writer.print(xml.getName());
Vector<String> nsprefixes = new Vector<String>();
// Vector<String> nsprefixes = new Vector<String>();
if (xml.getNamespace() != null) {
if (xml.getLocalName().equals(xml.getName())) {
this.writer.print(" xmlns=\"" + xml.getNamespace() + '"');
} else {
String prefix = xml.getName();
prefix = prefix.substring(0, prefix.indexOf(':'));
nsprefixes.addElement(prefix);
this.writer.print(" xmlns:" + prefix);
this.writer.print("=\"" + xml.getNamespace() + "\"");
}
}
// namespace was spewing all sorts of garbage into the xml doc.
// disabling this and looking for a better solution
// if (xml.getNamespace() != null) {
//// System.out.println("namespace is " + xml.getNamespace());
//// System.out.println(" names are " + xml.getLocalName() + " " + xml.getName());
// if (xml.getLocalName().equals(xml.getName())) {
// this.writer.print(" xmlns=\"" + xml.getNamespace() + '"');
// } else {
// String prefix = xml.getName();
// prefix = prefix.substring(0, prefix.indexOf(':'));
// nsprefixes.addElement(prefix);
// this.writer.print(" xmlns:" + prefix);
// this.writer.print("=\"" + xml.getNamespace() + "\"");
// }
// }
Enumeration<?> en = xml.enumerateAttributeNames();
// Enumeration<?> en = xml.enumerateAttributeNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
int index = key.indexOf(':');
// while (en.hasMoreElements()) {
// String key = (String) en.nextElement();
// int index = key.indexOf(':');
//
// if (index >= 0) {
// String namespace = xml.getAttributeNamespace(key);
//
// if (namespace != null) {
// String prefix = key.substring(0, index);
//
// if (!nsprefixes.contains(prefix)) {
// this.writer.print(" xmlns:" + prefix);
// this.writer.print("=\"" + namespace + '"');
// nsprefixes.addElement(prefix);
// }
// }
// }
// }
if (index >= 0) {
String namespace = xml.getAttributeNamespace(key);
// en = xml.enumerateAttributeNames();
if (namespace != null) {
String prefix = key.substring(0, index);
if (! nsprefixes.contains(prefix)) {
this.writer.print(" xmlns:" + prefix);
this.writer.print("=\"" + namespace + '"');
nsprefixes.addElement(prefix);
}
}
}
}
en = xml.enumerateAttributeNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
// while (en.hasMoreElements()) {
// String key = (String) en.nextElement();
for (String key : xml.listAttributes()) {
String value = xml.getAttribute(key, null);
this.writer.print(" " + key + "=\"");
this.writeEncoded(value);
@@ -225,16 +223,16 @@ public class XMLWriter
writer.println();
}
en = xml.enumerateChildren();
while (en.hasMoreElements()) {
XMLElement child = (XMLElement) en.nextElement();
this.write(child, prettyPrint, indent + 4,
collapseEmptyElements);
int count = xml.getChildCount();
for (int i = 0; i < count; i++) {
XMLElement child = xml.getChild(i);
this.write(child, prettyPrint,
initialIndent + eachIndent, eachIndent,
collapseEmptyElements);
}
if (prettyPrint) {
for (int i = 0; i < indent; i++) {
for (int i = 0; i < initialIndent; i++) {
this.writer.print(' ');
}
}
@@ -303,5 +301,4 @@ public class XMLWriter
}
}
}
}

View File

@@ -4,7 +4,7 @@ X right now would cause NumberFormatException
X add notes to the wiki about the size() method
X make sure sketchRenderer()/sketchWidth()/sketchHeight() are working on desktop
o see about getting them documented in the reference
_ do a writeup of the size() method in the wiki
X do a writeup of the size() method in the wiki
X size() command is currently ignored in Android
X http://dev.processing.org/bugs/show_bug.cgi?id=1397
X http://code.google.com/p/processing/issues/detail?id=211
@@ -29,11 +29,7 @@ X http://code.google.com/p/processing/issues/detail?id=221
X change skewX/Y to shearX/Y
_ need updated reference for this
not included
KEYCODE_VOLUME_DOWN
KEYCODE_VOLUME_UP
KEYCODE_CAMERA
KEYCODE_HOME
_ throw an error if a file in the 'data' dir ends with .gz
create new keystore
location: [ ] (browse)
@@ -271,8 +267,6 @@ _ probably same as memory error above
// jdf maybedone
_ if hitting 'run' in p5, need to kill any sketch that's currently running
_ throw an error if a file in the 'data' dir ends with .gz
_ need to make data folder copy more efficient than just copying everything
_ right now, first copies to src inside Build.java (which then copies to bin)