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

📄 abstractelement.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * public Object clone() { Element clone = createElement(getQName());
     * clone.appendAttributes(this); clone.appendContent(this); return clone; }
     */
    public Element createCopy() {
        Element clone = createElement(getQName());

        clone.appendAttributes(this);

        clone.appendContent(this);

        return clone;
    }

    public Element createCopy(String name) {
        Element clone = createElement(name);

        clone.appendAttributes(this);

        clone.appendContent(this);

        return clone;
    }

    public Element createCopy(QName qName) {
        Element clone = createElement(qName);

        clone.appendAttributes(this);

        clone.appendContent(this);

        return clone;
    }

    public QName getQName(String qualifiedName) {
        String prefix = "";

        String localName = qualifiedName;

        int index = qualifiedName.indexOf(":");

        if (index > 0) {
            prefix = qualifiedName.substring(0, index);

            localName = qualifiedName.substring(index + 1);
        }

        Namespace namespace = getNamespaceForPrefix(prefix);

        if (namespace != null) {
            return getDocumentFactory().createQName(localName, namespace);
        } else {
            return getDocumentFactory().createQName(localName);
        }
    }

    public Namespace getNamespaceForPrefix(String prefix) {
        if (prefix == null) {
            prefix = "";
        }

        if (prefix.equals(getNamespacePrefix())) {
            return getNamespace();
        } else if (prefix.equals("xml")) {
            return Namespace.XML_NAMESPACE;
        } else {
            List list = contentList();

            int size = list.size();

            for (int i = 0; i < size; i++) {
                Object object = list.get(i);

                if (object instanceof Namespace) {
                    Namespace namespace = (Namespace) object;

                    if (prefix.equals(namespace.getPrefix())) {
                        return namespace;
                    }
                }
            }
        }

        Element parent = getParent();

        if (parent != null) {
            Namespace answer = parent.getNamespaceForPrefix(prefix);

            if (answer != null) {
                return answer;
            }
        }

        if ((prefix == null) || (prefix.length() <= 0)) {
            return Namespace.NO_NAMESPACE;
        }

        return null;
    }

    public Namespace getNamespaceForURI(String uri) {
        if ((uri == null) || (uri.length() <= 0)) {
            return Namespace.NO_NAMESPACE;
        } else if (uri.equals(getNamespaceURI())) {
            return getNamespace();
        } else {
            List list = contentList();

            int size = list.size();

            for (int i = 0; i < size; i++) {
                Object object = list.get(i);

                if (object instanceof Namespace) {
                    Namespace namespace = (Namespace) object;

                    if (uri.equals(namespace.getURI())) {
                        return namespace;
                    }
                }
            }

            return null;
        }
    }

    public List getNamespacesForURI(String uri) {
        BackedList answer = createResultList();

        // if (getNamespaceURI().equals(uri)) {
        //
        // answer.addLocal(getNamespace());
        //
        // }
        List list = contentList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if ((object instanceof Namespace)
                    && ((Namespace) object).getURI().equals(uri)) {
                answer.addLocal(object);
            }
        }

        return answer;
    }

    public List declaredNamespaces() {
        BackedList answer = createResultList();

        // if (getNamespaceURI().length() > 0) {
        //
        // answer.addLocal(getNamespace());
        //
        // }
        //
        List list = contentList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof Namespace) {
                answer.addLocal(object);
            }
        }

        return answer;
    }

    public List additionalNamespaces() {
        List list = contentList();

        int size = list.size();

        BackedList answer = createResultList();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof Namespace) {
                Namespace namespace = (Namespace) object;

                if (!namespace.equals(getNamespace())) {
                    answer.addLocal(namespace);
                }
            }
        }

        return answer;
    }

    public List additionalNamespaces(String defaultNamespaceURI) {
        List list = contentList();

        BackedList answer = createResultList();

        int size = list.size();

        for (int i = 0; i < size; i++) {
            Object object = list.get(i);

            if (object instanceof Namespace) {
                Namespace namespace = (Namespace) object;

                if (!defaultNamespaceURI.equals(namespace.getURI())) {
                    answer.addLocal(namespace);
                }
            }
        }

        return answer;
    }

    // Implementation helper methods
    // -------------------------------------------------------------------------

    /**
     * Ensures that the list of attributes has the given size
     * 
     * @param minCapacity
     *            DOCUMENT ME!
     */
    public void ensureAttributesCapacity(int minCapacity) {
        if (minCapacity > 1) {
            List list = attributeList();

            if (list instanceof ArrayList) {
                ArrayList arrayList = (ArrayList) list;

                arrayList.ensureCapacity(minCapacity);
            }
        }
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected Element createElement(String name) {
        return getDocumentFactory().createElement(name);
    }

    protected Element createElement(QName qName) {
        return getDocumentFactory().createElement(qName);
    }

    protected void addNode(Node node) {
        if (node.getParent() != null) {
            // XXX: could clone here
            String message = "The Node already has an existing parent of \""
                    + node.getParent().getQualifiedName() + "\"";

            throw new IllegalAddException(this, node, message);
        }

        addNewNode(node);
    }

    protected void addNode(int index, Node node) {
        if (node.getParent() != null) {
            // XXX: could clone here
            String message = "The Node already has an existing parent of \""
                    + node.getParent().getQualifiedName() + "\"";

            throw new IllegalAddException(this, node, message);
        }

        addNewNode(index, node);
    }

    /**
     * Like addNode() but does not require a parent check
     * 
     * @param node
     *            DOCUMENT ME!
     */
    protected void addNewNode(Node node) {
        contentList().add(node);

        childAdded(node);
    }

    protected void addNewNode(int index, Node node) {
        contentList().add(index, node);

        childAdded(node);
    }

    protected boolean removeNode(Node node) {
        boolean answer = contentList().remove(node);

        if (answer) {
            childRemoved(node);
        }

        return answer;
    }

    /**
     * Called when a new child node is added to create any parent relationships
     * 
     * @param node
     *            DOCUMENT ME!
     */
    protected void childAdded(Node node) {
        if (node != null) {
            node.setParent(this);
        }
    }

    protected void childRemoved(Node node) {
        if (node != null) {
            node.setParent(null);

            node.setDocument(null);
        }
    }

    /**
     * DOCUMENT ME!
     * 
     * @return the internal List used to store attributes or creates one if one
     *         is not available
     */
    protected abstract List attributeList();

    /**
     * DOCUMENT ME!
     * 
     * @param attributeCount
     *            DOCUMENT ME!
     * 
     * @return the internal List used to store attributes or creates one with
     *         the specified size if one is not available
     */
    protected abstract List attributeList(int attributeCount);

    protected DocumentFactory getDocumentFactory() {
        QName qName = getQName();

        // QName might be null as we might not have been constructed yet
        if (qName != null) {
            DocumentFactory factory = qName.getDocumentFactory();

            if (factory != null) {
                return factory;
            }
        }

        return DOCUMENT_FACTORY;
    }

    /**
     * A Factory Method pattern which creates a List implementation used to
     * store attributes
     * 
     * @return DOCUMENT ME!
     */
    protected List createAttributeList() {
        return createAttributeList(DEFAULT_CONTENT_LIST_SIZE);
    }

    /**
     * A Factory Method pattern which creates a List implementation used to
     * store attributes
     * 
     * @param size
     *            DOCUMENT ME!
     * 
     * @return DOCUMENT ME!
     */
    protected List createAttributeList(int size) {
        return new ArrayList(size);
    }

    protected Iterator createSingleIterator(Object result) {
        return new SingleIterator(result);
    }
}

/*
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 * 1. Redistributions of source code must retain copyright statements and
 * notices. Redistributions must also contain a copy of this document.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name "DOM4J" must not be used to endorse or promote products derived
 * from this Software without prior written permission of MetaStuff, Ltd. For
 * written permission, please contact dom4j-info@metastuff.com.
 * 
 * 4. Products derived from this Software may not be called "DOM4J" nor may
 * "DOM4J" appear in their names without prior written permission of MetaStuff,
 * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
 * 
 * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
 * 
 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 */

⌨️ 快捷键说明

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