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

📄 abstractelement.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: AbstractElement.java,v 1.4 2003/11/02 18:04:59 per_nyfelt Exp $ */package org.dom4j.tree;import org.dom4j.*;import org.dom4j.CharacterData;import org.dom4j.io.XMLWriter;import org.xml.sax.Attributes;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;import java.util.*;/** <p><code>AbstractElement</code> is an abstract base class for  * tree implementors to use for implementation inheritence.</p>  *  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>  * @version $Revision: 1.4 $  */public abstract class AbstractElement    extends AbstractBranch    implements Element {    protected static final List EMPTY_LIST = Collections.EMPTY_LIST;    protected static final Iterator EMPTY_ITERATOR = EMPTY_LIST.iterator();    protected static final boolean VERBOSE_TOSTRING = false;    protected static final boolean USE_STRINGVALUE_SEPARATOR = false;    public AbstractElement() {    }    public short getNodeType() {        return ELEMENT_NODE;    }    public boolean isRootElement() {        Document document = getDocument();        if (document != null) {            Element root = document.getRootElement();            if (root == this) {                return true;            }        }        return false;    }    public void setName(String name) {        setQName(getNodeFactory().createQName(name));    }    public void setNamespace(Namespace namespace) {        setQName(getNodeFactory().createQName(getName(), namespace));    }    /** Returns the XPath expression to match this Elements name     * which is getQualifiedName() if there is a namespace prefix defined or     * if no namespace is present then it is getName() or if a namespace is defined     * with no prefix then the expression is *[name()='X'] where X = getName().     */    public String getXPathNameStep() {        String uri = getNamespaceURI();        if (uri == null || uri.length() == 0) {            return getName();        }        String prefix = getNamespacePrefix();        if (prefix == null || prefix.length() == 0) {            return "*[name()='" + getName() + "']";        }        return getQualifiedName();    }    public String getPath(Element context) {        Element parent = getParent();        if (parent == null) {            return "/" + getXPathNameStep();        }        else if (parent == context) {            return getXPathNameStep();        }        return parent.getPath(context) + "/" + getXPathNameStep();    }    public String getUniquePath(Element context) {        Element parent = getParent();        if (parent == null) {            return "/" + getXPathNameStep();        }        StringBuffer buffer = new StringBuffer();        if (parent != context) {            buffer.append(parent.getUniquePath(context));            buffer.append("/");        }        buffer.append(getXPathNameStep());        List mySiblings = parent.elements(getQName());        if (mySiblings.size() > 1) {            int idx = mySiblings.indexOf(this);            if (idx >= 0) {                buffer.append("[");                buffer.append(Integer.toString(++idx));                buffer.append("]");            }        }        return buffer.toString();    }    public String asXML() {        try {            StringWriter out = new StringWriter();            XMLWriter writer = new XMLWriter(out, outputFormat);            writer.write(this);            return out.toString();        }        catch (IOException e) {            throw new RuntimeException(                "Wierd IOException while generating textual representation: " + e.getMessage());        }    }    public void write(Writer out) throws IOException {        XMLWriter writer = new XMLWriter(out, outputFormat);        writer.write(this);    }    /** <p><code>accept</code> method is the <code>Visitor Pattern</code> method.      * </p>      *      * @param visitor <code>Visitor</code> is the visitor.      */    public void accept(Visitor visitor) {        visitor.visit(this);        // visit attributes        for (int i = 0, size = attributeCount(); i < size; i++) {            Attribute attribute = attribute(i);            visitor.visit(attribute);        }        // visit content        for (int i = 0, size = nodeCount(); i < size; i++) {            Node node = node(i);            node.accept(visitor);        }    }    public String toString() {        String uri = getNamespaceURI();        if (uri != null && uri.length() > 0) {            if (VERBOSE_TOSTRING) {                return super.toString()                    + " [Element: <"                    + getQualifiedName()                    + " uri: "                    + uri                    + " attributes: "                    + attributeList()                    + " content: "                    + contentList()                    + " />]";            }            else {                return super.toString()                    + " [Element: <"                    + getQualifiedName()                    + " uri: "                    + uri                    + " attributes: "                    + attributeList()                    + "/>]";            }        }        else {            if (VERBOSE_TOSTRING) {                return super.toString()                    + " [Element: <"                    + getQualifiedName()                    + " attributes: "                    + attributeList()                    + " content: "                    + contentList()                    + " />]";            }            else {                return super.toString()                    + " [Element: <"                    + getQualifiedName()                    + " attributes: "                    + attributeList()                    + "/>]";            }        }    }    // QName methods    //-------------------------------------------------------------------------    public Namespace getNamespace() {        return getQName().getNamespace();    }    public String getName() {        return getQName().getName();    }    public String getNamespacePrefix() {        return getQName().getNamespacePrefix();    }    public String getNamespaceURI() {        return getQName().getNamespaceURI();    }    public String getQualifiedName() {        return getQName().getQualifiedName();    }    public Object getData() {        return getText();    }    public void setData(Object data) {        // ignore this method    }    // Node methods    //-------------------------------------------------------------------------    public Node node(int index) {        if (index >= 0) {            List list = contentList();            if (index >= list.size()) {                return null;            }            Object node = list.get(index);            if (node != null) {                if (node instanceof Node) {                    return (Node) node;                }                else {                    return getNodeFactory().createText(node.toString());                }            }        }        return null;    }    public int indexOf(Node node) {        return contentList().indexOf(node);    }    public int nodeCount() {        return contentList().size();    }    public Iterator nodeIterator() {        return contentList().iterator();    }    // Element methods    //-------------------------------------------------------------------------    public Element element(String name) {        List list = contentList();        int size = list.size();        for (int i = 0; i < size; i++) {            Object object = list.get(i);            if (object instanceof Element) {                Element element = (Element) object;                if (name.equals(element.getName())) {                    return element;                }            }        }        return null;    }    public Element element(QName qName) {        List list = contentList();        int size = list.size();        for (int i = 0; i < size; i++) {            Object object = list.get(i);            if (object instanceof Element) {                Element element = (Element) object;                if (qName.equals(element.getQName())) {                    return element;                }            }        }        return null;    }    public Element element(String name, Namespace namespace) {        return element(getNodeFactory().createQName(name, namespace));    }    public List elements() {        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 Element) {                answer.addLocal(object);            }        }        return answer;    }    public List elements(String name) {        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 Element) {                Element element = (Element) object;                if (name.equals(element.getName())) {                    answer.addLocal(element);                }            }        }        return answer;    }    public List elements(QName qName) {        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 Element) {                Element element = (Element) object;                if (qName.equals(element.getQName())) {                    answer.addLocal(element);                }            }        }        return answer;    }    public List elements(String name, Namespace namespace) {        return elements(getNodeFactory().createQName(name, namespace));    }    public Iterator elementIterator() {        List list = contentList();        return new ElementIterator(list.iterator());    }    public Iterator elementIterator(String name) {        List list = contentList();        return new ElementNameIterator(list.iterator(), name);    }    public Iterator elementIterator(QName qName) {        List list = contentList();        return new ElementQNameIterator(list.iterator(), qName);    }    public Iterator elementIterator(String name, Namespace namespace) {        return elementIterator(getNodeFactory().createQName(name, namespace));    }    // Attribute methods    //-------------------------------------------------------------------------    public List attributes() {        return new ContentListFacade(this, attributeList());    }    public Iterator attributeIterator() {        return attributeList().iterator();    }    public Attribute attribute(int index) {        return (Attribute) attributeList().get(index);    }    public int attributeCount() {        return attributeList().size();    }    public Attribute attribute(String name) {        List list = attributeList();        int size = list.size();        for (int i = 0; i < size; i++) {            Attribute attribute = (Attribute) list.get(i);            if (name.equals(attribute.getName())) {                return attribute;            }        }        return null;    }    public Attribute attribute(QName qName) {        List list = attributeList();        int size = list.size();        for (int i = 0; i < size; i++) {            Attribute attribute = (Attribute) list.get(i);            if (qName.equals(attribute.getQName())) {                return attribute;            }        }        return null;    }    public Attribute attribute(String name, Namespace namespace) {        return attribute(getNodeFactory().createQName(name, namespace));    }    /** This method provides a more optimal way of setting all the attributes     * on an Element particularly for use in {@link org.dom4j.io.SAXReader}.      */    public void setAttributes(        Attributes attributes,        NamespaceStack namespaceStack,        boolean noNamespaceAttributes) {        // now lets add all attribute values        int size = attributes.getLength();        if (size > 0) {            NodeFactory factory = getNodeFactory();            if (size == 1) {                // allow lazy construction of the List of Attributes

⌨️ 快捷键说明

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