📄 defaultelement.java
字号:
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: DefaultElement.java,v 1.5 2003/11/02 18:04:59 per_nyfelt Exp $ */package org.dom4j.tree;import org.dom4j.*;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** <p><code>DefaultElement</code> is the default DOM4J default implementation * of an XML element.</p> * * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @version $Revision: 1.5 $ */public class DefaultElement extends AbstractElement { /** The <code>NodeFactory</code> instance used by default */ private static final NodeFactory NODE_FACTORY = DocumentFactory.getInstance(); /** The <code>QName</code> for this element */ private QName qname; /** Stores the parent branch of this node which is either a Document * if this element is the root element in a document, or another Element * if it is a child of the root document, or null if it has not been added * to a document yet. */ private Branch parentBranch; /** Stores null for no content, a Node for a single content node * or a List for multiple content nodes. * The List will be lazily constructed when required. */ private Object content; /** Lazily constructes list of attributes */ private Object attributes; public DefaultElement(String name) { this(NODE_FACTORY.createQName(name)); } public DefaultElement(QName qname) { this.qname = qname; } public DefaultElement(QName qname, int attributeCount) { this.qname = qname; if (attributeCount > 1) { this.attributes = new ArrayList(attributeCount); } } public DefaultElement(String name, Namespace namespace) { this(NODE_FACTORY.createQName(name, namespace)); } public Element getParent() { return (parentBranch instanceof Element) ? (Element) parentBranch : null; } public void setParent(Element parent) { if (parentBranch instanceof Element || parent != null) { parentBranch = parent; } } public Document getDocument() { if (parentBranch instanceof Document) { return (Document) parentBranch; } else if (parentBranch instanceof Element) { Element parent = (Element) parentBranch; return parent.getDocument(); } return null; } public void setDocument(Document document) { if (parentBranch instanceof Document || document != null) { parentBranch = document; } } public boolean supportsParent() { return true; } public QName getQName() { return qname; } public void setQName(QName qname) { this.qname = qname; } public String getText() { if (content instanceof List) { return super.getText(); } else { if (content != null) { return getContentAsText(content); } else { return ""; } } } public String getStringValue() { if (content instanceof List) { List list = (List) content; int size = list.size(); if (size > 0) { if (size == 1) { // optimised to avoid StringBuffer creation return getContentAsStringValue(list.get(0)); } else { StringBuffer buffer = new StringBuffer(); for (int i = 0; i < size; i++) { Object node = list.get(i); String string = getContentAsStringValue(node); if (string.length() > 0) { if (USE_STRINGVALUE_SEPARATOR) { if (buffer.length() > 0) { buffer.append(' '); } } buffer.append(string); } } return buffer.toString(); } } } else { if (content != null) { return getContentAsStringValue(content); } } return ""; } public Object clone() { DefaultElement answer = (DefaultElement) super.clone(); if (answer != this) { answer.content = null; answer.attributes = null; answer.appendAttributes(this); answer.appendContent(this); } return answer; } public Namespace getNamespaceForPrefix(String prefix) { if (prefix == null) { prefix = ""; } if (prefix.equals(getNamespacePrefix())) { return getNamespace(); } else if (prefix.equals("xml")) { return AbstractNamespace.XML_NAMESPACE; } else { if (content instanceof List) { List list = (List) content; 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; } } } } else if (content instanceof Namespace) { Namespace namespace = (Namespace) content; 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 AbstractNamespace.NO_NAMESPACE; } return null; } public Namespace getNamespaceForURI(String uri) { if (uri == null || uri.length() <= 0) { return AbstractNamespace.NO_NAMESPACE; } else if (uri.equals(getNamespaceURI())) { return getNamespace(); } else { if (content instanceof List) { List list = (List) content; 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; } } } } else if (content instanceof Namespace) { Namespace namespace = (Namespace) content; if (uri.equals(namespace.getURI())) { return namespace; } } Element parent = getParent(); if (parent != null) { return parent.getNamespaceForURI(uri); } return null; } } public List declaredNamespaces() { BackedList answer = createResultList(); if (getNamespaceURI().length() > 0) { answer.addLocal(getNamespace()); } if (content instanceof List) { List list = (List) content; int size = list.size(); for (int i = 0; i < size; i++) { Object object = list.get(i); if (object instanceof Namespace) { answer.addLocal(object); } } } else { if (content instanceof Namespace) { answer.addLocal(content); } } return answer; } public List additionalNamespaces() { if (content instanceof List) { List list = (List) content; 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; answer.addLocal(namespace); } } return answer; } else { if (content instanceof Namespace) { Namespace namespace = (Namespace) content; return createSingleResultList(namespace); } else { return createEmptyList(); } } } public List additionalNamespaces(String defaultNamespaceURI) { if (content instanceof List) { List list = (List) content; 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; } else { if (content instanceof Namespace) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -