⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlelement.java

📁 Nano的XML解析器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* XMLElement.java                                                 NanoXML/Java * * $Revision: 1.5 $ * $Date: 2002/02/06 18:50:12 $ * $Name: RELEASE_2_2_1 $ * * This file is part of NanoXML 2 for Java. * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the * use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * *  1. The origin of this software must not be misrepresented; you must not *     claim that you wrote the original software. If you use this software in *     a product, an acknowledgment in the product documentation would be *     appreciated but is not required. * *  2. Altered source versions must be plainly marked as such, and must not be *     misrepresented as being the original software. * *  3. This notice may not be removed or altered from any source distribution. */package net.n3.nanoxml;import java.io.Serializable;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.Vector;/** * XMLElement is an XML element. The standard NanoXML builder generates a * tree of such elements. * * @see net.n3.nanoxml.StdXMLBuilder * * @author Marc De Scheemaecker * @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $ */public class XMLElement implements IXMLElement, Serializable {    /**     * Necessary for serialization.     */    static final long serialVersionUID = -2383376380548624920L;    /**     * No line number defined.     */    public static final int NO_LINE = -1;    /**     * The parent element.     */    private IXMLElement parent;    /**     * The attributes of the element.     */    private Vector attributes;    /**     * The child elements.     */    private Vector children;    /**     * The name of the element.     */    private String name;    /**     * The full name of the element.     */    private String fullName;    /**     * The namespace URI.     */    private String namespace;    /**     * The content of the element.     */    private String content;    /**     * The system ID of the source data where this element is located.     */    private String systemID;    /**     * The line in the source data where this element starts.     */    private int lineNr;    /**     * Creates an empty element to be used for #PCDATA content.     */    public XMLElement() {        this(null, null, null, NO_LINE);    }    /**     * Creates an empty element.     *     * @param fullName the name of the element.     */    public XMLElement(String fullName) {        this(fullName, null, null, NO_LINE);    }    /**     * Creates an empty element.     *     * @param fullName the name of the element.     * @param systemID the system ID of the XML data where the element starts.     * @param lineNr   the line in the XML data where the element starts.     */    public XMLElement(String fullName,                      String systemID,                      int    lineNr) {        this(fullName, null, systemID, lineNr);    }    /**     * Creates an empty element.     *     * @param fullName  the full name of the element     * @param namespace the namespace URI.     */    public XMLElement(String fullName,                      String namespace) {        this(fullName, namespace, null, NO_LINE);    }    /**     * Creates an empty element.     *     * @param fullName  the full name of the element     * @param namespace the namespace URI.     * @param systemID  the system ID of the XML data where the element starts.     * @param lineNr    the line in the XML data where the element starts.     */    public XMLElement(String fullName,                      String namespace,                      String systemID,                      int    lineNr) {        this.attributes = new Vector();        this.children = new Vector(8);        this.fullName = fullName;        if (namespace == null) {            this.name = fullName;        } else {            int index = fullName.indexOf(':');            if (index >= 0) {                this.name = fullName.substring(index + 1);            } else {                this.name = fullName;            }        }        this.namespace = namespace;        this.content = null;        this.lineNr = lineNr;        this.systemID = systemID;        this.parent = null;    }    /**     * Creates an element to be used for #PCDATA content.     */    public IXMLElement createPCDataElement() {        return new XMLElement();    }    /**     * Creates an empty element.     *     * @param fullName the name of the element.     */    public IXMLElement createElement(String fullName) {        return new XMLElement(fullName);    }    /**     * Creates an empty element.     *     * @param fullName the name of the element.     * @param systemID the system ID of the XML data where the element starts.     * @param lineNr   the line in the XML data where the element starts.     */    public IXMLElement createElement(String fullName,                                     String systemID,                                     int    lineNr) {        return new XMLElement(fullName, systemID, lineNr);    }    /**     * Creates an empty element.     *     * @param fullName  the full name of the element     * @param namespace the namespace URI.     */    public IXMLElement createElement(String fullName,                                     String namespace) {        return new XMLElement(fullName, namespace);    }    /**     * Creates an empty element.     *     * @param fullName  the full name of the element     * @param namespace the namespace URI.     * @param systemID  the system ID of the XML data where the element starts.     * @param lineNr    the line in the XML data where the element starts.     */    public IXMLElement createElement(String fullName,                                     String namespace,                                     String systemID,                                     int    lineNr) {        return new XMLElement(fullName, namespace, systemID, lineNr);    }    /**     * Cleans up the object when it's destroyed.     */    protected void finalize() throws Throwable {        this.attributes.clear();        this.attributes = null;        this.children = null;        this.fullName = null;        this.name = null;        this.namespace = null;        this.content = null;        this.systemID = null;        this.parent = null;        super.finalize();    }    /**     * Returns the parent element. This method returns null for the root     * element.     */    public IXMLElement getParent() {        return this.parent;    }    /**     * Returns the full name (i.e. the name including an eventual namespace     * prefix) of the element.     *     * @return the name, or null if the element only contains #PCDATA.     */    public String getFullName() {        return this.fullName;    }    /**     * Returns the name of the element.     *     * @return the name, or null if the element only contains #PCDATA.     */    public String getName() {        return this.name;    }    /**     * Returns the namespace of the element.     *     * @return the namespace, or null if no namespace is associated with the     *         element.     */    public String getNamespace() {        return this.namespace;    }    /**     * Sets the full name. This method also sets the short name and clears the     * namespace URI.     *     * @param name the non-null name.     */    public void setName(String name) {        this.name = name;        this.fullName = name;        this.namespace = null;    }    /**     * Sets the name.     *     * @param fullName  the non-null full name.     * @param namespace the namespace URI, which may be null.     */    public void setName(String fullName,                        String namespace) {        int index = fullName.indexOf(':');        if ((namespace == null) || (index < 0)) {            this.name = fullName;        } else {            this.name = fullName.substring(index + 1);        }        this.fullName = fullName;        this.namespace = namespace;    }    /**     * Adds a child element.     *     * @param child the non-null child to add.     */    public void addChild(IXMLElement child) {        if (child == null) {            throw new IllegalArgumentException("child must not be null");        }        if ((child.getName() == null) && (! this.children.isEmpty())) {            IXMLElement lastChild = (IXMLElement) this.children.lastElement();            if (lastChild.getName() == null) {                lastChild.setContent(lastChild.getContent()                                     + child.getContent());                return;            }        }        ((XMLElement)child).parent = this;        this.children.addElement(child);    }    /**     * Inserts a child element.     *     * @param child the non-null child to add.     * @param index where to put the child.     */    public void insertChild(IXMLElement child,                            int         index) {        if (child == null) {            throw new IllegalArgumentException("child must not be null");        }        if ((child.getName() == null) && (! this.children.isEmpty())) {            IXMLElement lastChild = (IXMLElement) this.children.lastElement();            if (lastChild.getName() == null) {                lastChild.setContent(lastChild.getContent()                                     + child.getContent());                return;            }        }        ((XMLElement) child).parent = this;        this.children.insertElementAt(child, index);    }    /**     * Removes a child element.     *     * @param child the non-null child to remove.     */    public void removeChild(IXMLElement child) {        if (child == null) {            throw new IllegalArgumentException("child must not be null");        }        this.children.removeElement(child);    }    /**     * Removes the child located at a certain index.     *     * @param index the index of the child, where the first child has index 0.     */    public void removeChildAtIndex(int index) {        this.children.removeElementAt(index);    }    /**     * Returns an enumeration of all child elements.     *     * @return the non-null enumeration     */    public Enumeration enumerateChildren() {        return this.children.elements();    }    /**     * Returns whether the element is a leaf element.     *     * @return true if the element has no children.     */    public boolean isLeaf() {        return this.children.isEmpty();    }    /**     * Returns whether the element has children.     *     * @return true if the element has children.     */    public boolean hasChildren() {        return (! this.children.isEmpty());    }    /**     * Returns the number of children.     *     * @return the count.     */    public int getChildrenCount() {        return this.children.size();    }    /**     * Returns a vector containing all the child elements.     *     * @return the vector.     */    public Vector getChildren() {        return this.children;    }    /**     * Returns the child at a specific index.     *     * @param index the index of the child     *     * @return the non-null child     *     * @throws java.lang.ArrayIndexOutOfBoundsException     *		if the index is out of bounds.     */    public IXMLElement getChildAtIndex(int index)    throws ArrayIndexOutOfBoundsException {        return (IXMLElement) this.children.elementAt(index);    }        /**     * Searches a child element.     *     * @param name the full name of the child to search for.     *     * @return the child element, or null if no such child was found.     */    public IXMLElement getFirstChildNamed(String name) {        Enumeration enum = this.children.elements();        while (enum.hasMoreElements()) {            IXMLElement child = (IXMLElement) enum.nextElement();            String childName = child.getFullName();            if ((childName != null) && childName.equals(name)) {                return child;            }        }        return null;    }    /**     * Searches a child element.     *     * @param name      the name of the child to search for.     * @param namespace the namespace, which may be null.     *     * @return the child element, or null if no such child was found.     */    public IXMLElement getFirstChildNamed(String name,                                          String namespace) {        Enumeration enum = this.children.elements();        while (enum.hasMoreElements()) {            IXMLElement child = (IXMLElement) enum.nextElement();            String str = child.getName();            boolean found = (str != null) && (str.equals(name));            str = child.getNamespace();            if (str == null) {                found &= (name == null);            } else {                found &= str.equals(namespace);            }            if (found) {                return child;            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -