FileDocCategorySizeDatePackage
Node.javaAPI DocAndroid 1.5 API11742Wed May 06 22:41:06 BST 2009org.kxml2.kdom

Node

public class Node extends Object
A common base class for Document and Element, also used for storing XML fragments.

Fields Summary
public static final int
DOCUMENT
public static final int
ELEMENT
public static final int
TEXT
public static final int
CDSECT
public static final int
ENTITY_REF
public static final int
IGNORABLE_WHITESPACE
public static final int
PROCESSING_INSTRUCTION
public static final int
COMMENT
public static final int
DOCDECL
protected Vector
children
protected StringBuffer
types
Constructors Summary
Methods Summary
public voidaddChild(int index, int type, java.lang.Object child)
inserts the given child object of the given type at the given index.


                     

            

        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);
    
public voidaddChild(int type, java.lang.Object child)
convenience method for addChild (getChildCount (), child)

        addChild(getChildCount(), type, child);
    
public org.kxml2.kdom.ElementcreateElement(java.lang.String namespace, java.lang.String name)
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.


        Element e = new Element();
        e.namespace = namespace == null ? "" : namespace;
        e.name = name;
        return e;
    
public java.lang.ObjectgetChild(int index)
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.

        return children.elementAt(index);
    
public intgetChildCount()
Returns the number of child objects

        return children == null ? 0 : children.size();
    
public org.kxml2.kdom.ElementgetElement(int index)
returns the element at the given index. If the node at the given index is a text node, null is returned

        Object child = getChild(index);
        return (child instanceof Element) ? (Element) child : null;
    
public org.kxml2.kdom.ElementgetElement(java.lang.String namespace, java.lang.String name)
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.


        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);
    
public java.lang.StringgetText(int index)
Returns the text node with the given index or null if the node with the given index is not a text node.

        return (isText(index)) ? (String) getChild(index) : null;
    
public intgetType(int index)
Returns the type of the child at the given index. Possible types are ELEMENT, TEXT, COMMENT, and PROCESSING_INSTRUCTION

        return types.charAt(index);
    
public intindexOf(java.lang.String namespace, java.lang.String name, int 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.


        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 booleanisText(int i)

        int t = getType(i);
        return t == TEXT || t == IGNORABLE_WHITESPACE || t == CDSECT;
    
public voidparse(org.xmlpull.v1.XmlPullParser parser)
Recursively builds the child elements from the given parser until an end tag or end document is found. The end tag is not consumed.


        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);
    
public voidremoveChild(int idx)
Removes the child object at the given index

        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   ***/
    
public voidwrite(org.xmlpull.v1.XmlSerializer writer)
Writes this node to the given XmlWriter. For node and document, this method is identical to writeChildren, except that the stream is flushed automatically.

        writeChildren(writer);
        writer.flush();
    
public voidwriteChildren(org.xmlpull.v1.XmlSerializer writer)
Writes the children of this node to the given XmlWriter.

        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);
            }
        }