diff options
author | Jesse Wilson <jessewilson@google.com> | 2011-12-09 11:38:53 -0500 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-08-01 16:59:02 -0700 |
commit | 10be789f42863b6d3cb2991d0439dd6528288bac (patch) | |
tree | 3b8cbb7a81f9decfab0f57a333b786c836adec34 /xml/src/main/java/org/kxml2 | |
parent | 4917d74f04a24ee3ee9bd85a3ed2a0c2ded164e2 (diff) | |
download | libcore-10be789f42863b6d3cb2991d0439dd6528288bac.zip libcore-10be789f42863b6d3cb2991d0439dd6528288bac.tar.gz libcore-10be789f42863b6d3cb2991d0439dd6528288bac.tar.bz2 |
Add a test for KxmlSerializer attributes with whitespace chars.
Delete some unused Kxml classes while I'm in the area, and
combine our two classes named KXmlSerializerTest.
(cherry-pick of 22a12704190060f74e308a5b5aa32d0b7f715183.)
Change-Id: Id8b3f72edc58547e49ad86c51a171dce3669be3f
Diffstat (limited to 'xml/src/main/java/org/kxml2')
-rw-r--r-- | xml/src/main/java/org/kxml2/kdom/Document.java | 129 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/kdom/Element.java | 335 | ||||
-rw-r--r-- | xml/src/main/java/org/kxml2/kdom/Node.java | 366 |
3 files changed, 0 insertions, 830 deletions
diff --git a/xml/src/main/java/org/kxml2/kdom/Document.java b/xml/src/main/java/org/kxml2/kdom/Document.java deleted file mode 100644 index 9123e62..0000000 --- a/xml/src/main/java/org/kxml2/kdom/Document.java +++ /dev/null @@ -1,129 +0,0 @@ -/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. */ - - -package org.kxml2.kdom; - -import java.io.*; - -import org.xmlpull.v1.*; -/** The document consists of some legacy events and a single root - element. This class basically adds some consistency checks to - Node. */ - -public class Document extends Node { - - protected int rootIndex = -1; - String encoding; - Boolean standalone; - - /** returns "#document" */ - - public String getEncoding () { - return encoding; - } - - public void setEncoding(String enc) { - this.encoding = enc; - } - - public void setStandalone (Boolean standalone) { - this.standalone = standalone; - } - - public Boolean getStandalone() { - return standalone; - } - - - public String getName() { - return "#document"; - } - - /** Adds a child at the given index position. Throws - an exception when a second root element is added */ - - public void addChild(int index, int type, Object child) { - if (type == ELEMENT) { - // if (rootIndex != -1) - // throw new RuntimeException("Only one document root element allowed"); - - rootIndex = index; - } - else if (rootIndex >= index) - rootIndex++; - - super.addChild(index, type, child); - } - - /** reads the document and checks if the last event - is END_DOCUMENT. If not, an exception is thrown. - The end event is consumed. For parsing partial - XML structures, consider using Node.parse (). */ - - public void parse(XmlPullParser parser) - throws IOException, XmlPullParserException { - - parser.require(XmlPullParser.START_DOCUMENT, null, null); - parser.nextToken (); - - encoding = parser.getInputEncoding(); - standalone = (Boolean)parser.getProperty ("http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone"); - - super.parse(parser); - - if (parser.getEventType() != XmlPullParser.END_DOCUMENT) - throw new RuntimeException("Document end expected!"); - - } - - public void removeChild(int index) { - if (index == rootIndex) - rootIndex = -1; - else if (index < rootIndex) - rootIndex--; - - super.removeChild(index); - } - - /** returns the root element of this document. */ - - public Element getRootElement() { - if (rootIndex == -1) - throw new RuntimeException("Document has no root element!"); - - return (Element) getChild(rootIndex); - } - - - /** Writes this node to the given XmlWriter. For node and document, - this method is identical to writeChildren, except that the - stream is flushed automatically. */ - - public void write(XmlSerializer writer) - throws IOException { - - writer.startDocument(encoding, standalone); - writeChildren(writer); - writer.endDocument(); - } - - -}
\ No newline at end of file diff --git a/xml/src/main/java/org/kxml2/kdom/Element.java b/xml/src/main/java/org/kxml2/kdom/Element.java deleted file mode 100644 index a9cb426..0000000 --- a/xml/src/main/java/org/kxml2/kdom/Element.java +++ /dev/null @@ -1,335 +0,0 @@ -/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. */ - -package org.kxml2.kdom; - -import java.io.*; -import java.util.*; - -import org.xmlpull.v1.*; - -/** - * In order to create an element, please use the createElement method - * instead of invoking the constructor directly. The right place to - * add user defined initialization code is the init method. */ - -public class Element extends Node { - - protected String namespace; - protected String name; - protected Vector attributes; - protected Node parent; - protected Vector prefixes; - - public Element() { - } - - /** - * called when all properties are set, but before children - * are parsed. Please do not use setParent for initialization - * code any longer. */ - - public void init() { - } - - - - - /** - * removes all children and attributes */ - - public void clear() { - attributes = null; - children = null; - } - - /** - * Forwards creation request to parent if any, otherwise - * calls super.createElement. */ - - public Element createElement( - String namespace, - String name) { - - return (this.parent == null) - ? super.createElement(namespace, name) - : this.parent.createElement(namespace, name); - } - - /** - * Returns the number of attributes of this element. */ - - public int getAttributeCount() { - return attributes == null ? 0 : attributes.size(); - } - - public String getAttributeNamespace (int index) { - return ((String []) attributes.elementAt (index)) [0]; - } - -/* public String getAttributePrefix (int index) { - return ((String []) attributes.elementAt (index)) [1]; - }*/ - - public String getAttributeName (int index) { - return ((String []) attributes.elementAt (index)) [1]; - } - - - public String getAttributeValue (int index) { - return ((String []) attributes.elementAt (index)) [2]; - } - - - public String getAttributeValue (String namespace, String name) { - for (int i = 0; i < getAttributeCount (); i++) { - if (name.equals (getAttributeName (i)) - && (namespace == null || namespace.equals (getAttributeNamespace(i)))) { - return getAttributeValue (i); - } - } - return null; - } - - /** - * Returns the root node, determined by ascending to the - * all parents un of the root element. */ - - public Node getRoot() { - - Element current = this; - - while (current.parent != null) { - if (!(current.parent instanceof Element)) return current.parent; - current = (Element) current.parent; - } - - return current; - } - - /** - * returns the (local) name of the element */ - - public String getName() { - return name; - } - - /** - * returns the namespace of the element */ - - public String getNamespace() { - return namespace; - } - - - /** - * returns the namespace for the given prefix */ - - public String getNamespaceUri (String prefix) { - int cnt = getNamespaceCount (); - for (int i = 0; i < cnt; i++) { - if (prefix == getNamespacePrefix (i) || - (prefix != null && prefix.equals (getNamespacePrefix (i)))) - return getNamespaceUri (i); - } - return parent instanceof Element ? ((Element) parent).getNamespaceUri (prefix) : null; - } - - - /** - * returns the number of declared namespaces, NOT including - * parent elements */ - - public int getNamespaceCount () { - return (prefixes == null ? 0 : prefixes.size ()); - } - - - public String getNamespacePrefix (int i) { - return ((String []) prefixes.elementAt (i)) [0]; - } - - public String getNamespaceUri (int i) { - return ((String []) prefixes.elementAt (i)) [1]; - } - - - /** - * Returns the parent node of this element */ - - public Node getParent() { - return parent; - } - - /* - * Returns the parent element if available, null otherwise - - public Element getParentElement() { - return (parent instanceof Element) - ? ((Element) parent) - : null; - } -*/ - - /** - * Builds the child elements from the given Parser. By overwriting - * parse, an element can take complete control over parsing its - * subtree. */ - - public void parse(XmlPullParser parser) - throws IOException, XmlPullParserException { - - for (int i = parser.getNamespaceCount (parser.getDepth () - 1); - i < parser.getNamespaceCount (parser.getDepth ()); i++) { - setPrefix (parser.getNamespacePrefix (i), parser.getNamespaceUri(i)); - } - - - for (int i = 0; i < parser.getAttributeCount (); i++) - setAttribute (parser.getAttributeNamespace (i), -// parser.getAttributePrefix (i), - parser.getAttributeName (i), - parser.getAttributeValue (i)); - - - // if (prefixMap == null) throw new RuntimeException ("!!"); - - init(); - - - if (parser.isEmptyElementTag()) - parser.nextToken (); - else { - parser.nextToken (); - super.parse(parser); - - if (getChildCount() == 0) - addChild(IGNORABLE_WHITESPACE, ""); - } - - parser.require( - XmlPullParser.END_TAG, - getNamespace(), - getName()); - - parser.nextToken (); - } - - - /** - * Sets the given attribute; a value of null removes the attribute */ - - public void setAttribute (String namespace, String name, String value) { - if (attributes == null) - attributes = new Vector (); - - if (namespace == null) - namespace = ""; - - for (int i = attributes.size()-1; i >=0; i--){ - String[] attribut = (String[]) attributes.elementAt(i); - if (attribut[0].equals(namespace) && - attribut[1].equals(name)){ - - if (value == null) { - attributes.removeElementAt(i); - } - else { - attribut[2] = value; - } - return; - } - } - - attributes.addElement - (new String [] {namespace, name, value}); - } - - - /** - * Sets the given prefix; a namespace value of null removess the - * prefix */ - - public void setPrefix (String prefix, String namespace) { - if (prefixes == null) prefixes = new Vector (); - prefixes.addElement (new String [] {prefix, namespace}); - } - - - /** - * sets the name of the element */ - - public void setName(String name) { - this.name = name; - } - - /** - * sets the namespace of the element. Please note: For no - * namespace, please use Xml.NO_NAMESPACE, null is not a legal - * value. Currently, null is converted to Xml.NO_NAMESPACE, but - * future versions may throw an exception. */ - - public void setNamespace(String namespace) { - if (namespace == null) - throw new NullPointerException ("Use \"\" for empty namespace"); - this.namespace = namespace; - } - - /** - * Sets the Parent of this element. Automatically called from the - * add method. Please use with care, you can simply - * create inconsitencies in the document tree structure using - * this method! */ - - protected void setParent(Node parent) { - this.parent = parent; - } - - - /** - * Writes this element and all children to the given XmlWriter. */ - - public void write(XmlSerializer writer) - throws IOException { - - if (prefixes != null) { - for (int i = 0; i < prefixes.size(); i++) { - writer.setPrefix (getNamespacePrefix (i), getNamespaceUri (i)); - } - } - - writer.startTag( - getNamespace(), - getName()); - - int len = getAttributeCount(); - - for (int i = 0; i < len; i++) { - writer.attribute( - getAttributeNamespace(i), - getAttributeName(i), - getAttributeValue(i)); - } - - writeChildren(writer); - - writer.endTag(getNamespace (), getName ()); - } -} diff --git a/xml/src/main/java/org/kxml2/kdom/Node.java b/xml/src/main/java/org/kxml2/kdom/Node.java deleted file mode 100644 index 820390c..0000000 --- a/xml/src/main/java/org/kxml2/kdom/Node.java +++ /dev/null @@ -1,366 +0,0 @@ -/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS - * IN THE SOFTWARE. */ - -package org.kxml2.kdom; - -import java.util.*; -import java.io.*; -import org.xmlpull.v1.*; -/** A common base class for Document and Element, also used for - storing XML fragments. */ - -public class Node { //implements XmlIO{ - - public static final int DOCUMENT = 0; - public static final int ELEMENT = 2; - public static final int TEXT = 4; - public static final int CDSECT = 5; - public static final int ENTITY_REF = 6; - public static final int IGNORABLE_WHITESPACE = 7; - public static final int PROCESSING_INSTRUCTION = 8; - public static final int COMMENT = 9; - public static final int DOCDECL = 10; - - protected Vector children; - protected StringBuffer types; - - /** inserts the given child object of the given type at the - given index. */ - - public void addChild(int index, int type, Object child) { - - if (child == null) - throw new NullPointerException(); - - if (children == null) { - children = new Vector(); - types = new StringBuffer(); - } - - if (type == ELEMENT) { - if (!(child instanceof Element)) - throw new RuntimeException("Element obj expected)"); - - ((Element) child).setParent(this); - } - else if (!(child instanceof String)) - throw new RuntimeException("String expected"); - - children.insertElementAt(child, index); - types.insert(index, (char) type); - } - - /** convenience method for addChild (getChildCount (), child) */ - - public void addChild(int type, Object child) { - addChild(getChildCount(), type, child); - } - - /** Builds a default element with the given properties. Elements - should always be created using this method instead of the - constructor in order to enable construction of specialized - subclasses by deriving custom Document classes. Please note: - For no namespace, please use Xml.NO_NAMESPACE, null is not a - legal value. Currently, null is converted to Xml.NO_NAMESPACE, - but future versions may throw an exception. */ - - public Element createElement(String namespace, String name) { - - Element e = new Element(); - e.namespace = namespace == null ? "" : namespace; - e.name = name; - return e; - } - - /** Returns the child object at the given index. For child - elements, an Element object is returned. For all other child - types, a String is returned. */ - - public Object getChild(int index) { - return children.elementAt(index); - } - - /** Returns the number of child objects */ - - public int getChildCount() { - return children == null ? 0 : children.size(); - } - - /** returns the element at the given index. If the node at the - given index is a text node, null is returned */ - - public Element getElement(int index) { - Object child = getChild(index); - return (child instanceof Element) ? (Element) child : null; - } - - /** Returns the element with the given namespace and name. If the - element is not found, or more than one matching elements are - found, an exception is thrown. */ - - public Element getElement(String namespace, String name) { - - int i = indexOf(namespace, name, 0); - int j = indexOf(namespace, name, i + 1); - - if (i == -1 || j != -1) - throw new RuntimeException( - "Element {" - + namespace - + "}" - + name - + (i == -1 ? " not found in " : " more than once in ") - + this); - - return getElement(i); - } - - /* returns "#document-fragment". For elements, the element name is returned - - public String getName() { - return "#document-fragment"; - } - - /** Returns the namespace of the current element. For Node - and Document, Xml.NO_NAMESPACE is returned. - - public String getNamespace() { - return ""; - } - - public int getNamespaceCount () { - return 0; - } - - /** returns the text content if the element has text-only - content. Throws an exception for mixed content - - public String getText() { - - StringBuffer buf = new StringBuffer(); - int len = getChildCount(); - - for (int i = 0; i < len; i++) { - if (isText(i)) - buf.append(getText(i)); - else if (getType(i) == ELEMENT) - throw new RuntimeException("not text-only content!"); - } - - return buf.toString(); - } - */ - - /** Returns the text node with the given index or null if the node - with the given index is not a text node. */ - - public String getText(int index) { - return (isText(index)) ? (String) getChild(index) : null; - } - - /** Returns the type of the child at the given index. Possible - types are ELEMENT, TEXT, COMMENT, and PROCESSING_INSTRUCTION */ - - public int getType(int index) { - return types.charAt(index); - } - - /** Convenience method for indexOf (getNamespace (), name, - startIndex). - - public int indexOf(String name, int startIndex) { - return indexOf(getNamespace(), name, startIndex); - } - */ - - /** Performs search for an element with the given namespace and - name, starting at the given start index. A null namespace - matches any namespace, please use Xml.NO_NAMESPACE for no - namespace). returns -1 if no matching element was found. */ - - public int indexOf(String namespace, String name, int startIndex) { - - int len = getChildCount(); - - for (int i = startIndex; i < len; i++) { - - Element child = getElement(i); - - if (child != null - && name.equals(child.getName()) - && (namespace == null || namespace.equals(child.getNamespace()))) - return i; - } - return -1; - } - - public boolean isText(int i) { - int t = getType(i); - return t == TEXT || t == IGNORABLE_WHITESPACE || t == CDSECT; - } - - /** Recursively builds the child elements from the given parser - until an end tag or end document is found. - The end tag is not consumed. */ - - public void parse(XmlPullParser parser) - throws IOException, XmlPullParserException { - - boolean leave = false; - - do { - int type = parser.getEventType(); - - // System.out.println(parser.getPositionDescription()); - - switch (type) { - - case XmlPullParser.START_TAG : - { - Element child = - createElement( - parser.getNamespace(), - parser.getName()); - // child.setAttributes (event.getAttributes ()); - addChild(ELEMENT, child); - - // order is important here since - // setparent may perform some init code! - - child.parse(parser); - break; - } - - case XmlPullParser.END_DOCUMENT : - case XmlPullParser.END_TAG : - leave = true; - break; - - default : - if (parser.getText() != null) - addChild( - type == XmlPullParser.ENTITY_REF ? TEXT : type, - parser.getText()); - else if ( - type == XmlPullParser.ENTITY_REF - && parser.getName() != null) { - addChild(ENTITY_REF, parser.getName()); - } - parser.nextToken(); - } - } - while (!leave); - } - - /** Removes the child object at the given index */ - - public void removeChild(int idx) { - children.removeElementAt(idx); - - /*** Modification by HHS - start ***/ - // types.deleteCharAt (index); - /***/ - int n = types.length() - 1; - - for (int i = idx; i < n; i++) - types.setCharAt(i, types.charAt(i + 1)); - - types.setLength(n); - - /*** Modification by HHS - end ***/ - } - - /* returns a valid XML representation of this Element including - attributes and children. - public String toString() { - try { - ByteArrayOutputStream bos = - new ByteArrayOutputStream(); - XmlWriter xw = - new XmlWriter(new OutputStreamWriter(bos)); - write(xw); - xw.close(); - return new String(bos.toByteArray()); - } - catch (IOException e) { - throw new RuntimeException(e.toString()); - } - } - */ - - /** Writes this node to the given XmlWriter. For node and document, - this method is identical to writeChildren, except that the - stream is flushed automatically. */ - - public void write(XmlSerializer writer) throws IOException { - writeChildren(writer); - writer.flush(); - } - - /** Writes the children of this node to the given XmlWriter. */ - - public void writeChildren(XmlSerializer writer) throws IOException { - if (children == null) - return; - - int len = children.size(); - - for (int i = 0; i < len; i++) { - int type = getType(i); - Object child = children.elementAt(i); - switch (type) { - case ELEMENT : - ((Element) child).write(writer); - break; - - case TEXT : - writer.text((String) child); - break; - - case IGNORABLE_WHITESPACE : - writer.ignorableWhitespace((String) child); - break; - - case CDSECT : - writer.cdsect((String) child); - break; - - case COMMENT : - writer.comment((String) child); - break; - - case ENTITY_REF : - writer.entityRef((String) child); - break; - - case PROCESSING_INSTRUCTION : - writer.processingInstruction((String) child); - break; - - case DOCDECL : - writer.docdecl((String) child); - break; - - default : - throw new RuntimeException("Illegal type: " + type); - } - } - } -} |