📄 abstractelement.java
字号:
} public void add(Namespace namespace) { addNode(namespace); } public void add(ProcessingInstruction pi) { addNode(pi); } public void add(Text text) { addNode(text); } public boolean remove(CDATA cdata) { return removeNode(cdata); } public boolean remove(Comment comment) { return removeNode(comment); } public boolean remove(Element element) { return removeNode(element); } public boolean remove(Entity entity) { return removeNode(entity); } public boolean remove(Namespace namespace) { return removeNode(namespace); } public boolean remove(ProcessingInstruction pi) { return removeNode(pi); } public boolean remove(Text text) { return removeNode(text); } // Helper methods //------------------------------------------------------------------------- public boolean hasMixedContent() { List content = contentList(); if (content == null || content.isEmpty() || content.size() < 2) { return false; } Class prevClass = null; for (Iterator iter = content.iterator(); iter.hasNext();) { Object object = iter.next(); Class newClass = object.getClass(); if (newClass != prevClass) { if (prevClass != null) { return true; } prevClass = newClass; } } return false; } public boolean isTextOnly() { List content = contentList(); if (content == null || content.isEmpty()) { return true; } for (Iterator iter = content.iterator(); iter.hasNext();) { Object object = iter.next(); if (!(object instanceof CharacterData) && !(object instanceof String)) { return false; } } return true; } public void setText(String text) { /* remove all text nodes */ List allContent = contentList(); if (allContent != null) { Iterator it = allContent.iterator(); while (it.hasNext()) { Node node = (Node) it.next(); switch (node.getNodeType()) { case CDATA_SECTION_NODE: //case ENTITY_NODE: case ENTITY_REFERENCE_NODE: case TEXT_NODE: it.remove(); } } } addText(text); } public String getStringValue() { List list = contentList(); 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(); } } return ""; } /** * Puts all <code>Text</code> nodes in the full depth of the sub-tree * underneath this <code>Node</code>, including attribute nodes, into a * "normal" form where only structure (e.g., elements, comments, * processing instructions, CDATA sections, and entity references) * separates <code>Text</code> nodes, i.e., there are neither adjacent * <code>Text</code> nodes nor empty <code>Text</code> nodes. This can * be used to ensure that the DOM view of a document is the same as if * it were saved and re-loaded, and is useful when operations (such as * XPointer lookups) that depend on a particular document tree * structure are to be used.In cases where the document contains * <code>CDATASections</code>, the normalize operation alone may not be * sufficient, since XPointers do not differentiate between * <code>Text</code> nodes and <code>CDATASection</code> nodes. * @version DOM Level 2 */ public void normalize() { List content = contentList(); Text previousText = null; int i = 0; while (i < content.size()) { Node node = (Node) content.get(i); if (node instanceof Text) { Text text = (Text) node; if (previousText != null) { previousText.appendText(text.getText()); remove(text); } else { String value = text.getText(); // only remove empty Text nodes, not whitespace nodes //if ( value == null || value.trim().length() <= 0 ) { if (value == null || value.length() <= 0) { remove(text); } else { previousText = text; i++; } } } else { if (node instanceof Element) { Element element = (Element) node; element.normalize(); } previousText = null; i++; } } } public String elementText(String name) { Element element = element(name); return (element != null) ? element.getText() : null; } public String elementText(QName qName) { Element element = element(qName); return (element != null) ? element.getText() : null; } public String elementTextTrim(String name) { Element element = element(name); return (element != null) ? element.getTextTrim() : null; } public String elementTextTrim(QName qName) { Element element = element(qName); return (element != null) ? element.getTextTrim() : null; } // add to me content from another element // analagous to the addAll(collection) methods in Java 2 collections public void appendAttributes(Element element) { for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); if (attribute.supportsParent()) { addAttribute(attribute.getQName(), attribute.getValue()); } else { add(attribute); } } } /** <p>This returns a deep clone of this element. * The new element is detached from its parent, and getParent() on the * clone will return null.</p> * * @return the clone of this element */ /* 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 getNodeFactory().createQName(localName, namespace); } else { return getNodeFactory().createQName(localName); } } public Namespace getNamespaceForPrefix(String prefix) { if (prefix == null) { prefix = ""; } if (prefix.equals(getNamespacePrefix())) { return getNamespace(); } else if (prefix.equals("xml")) { return getNodeFactory().getXmlNameSpace(); //return AbstractNamespace.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 getNodeFactory().getNoNamespace(); //return AbstractNamespace.NO_NAMESPACE; } return null; } public Namespace getNamespaceForURI(String uri) { if (uri == null || uri.length() <= 0) { return getNodeFactory().getNoNamespace(); //return AbstractNamespace.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 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; 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 */ 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 getNodeFactory().createElement(name); } protected Element createElement(QName qName) { return getNodeFactory().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 */ 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 */ 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); } } /** @return the internal List used to store attributes or * creates one if one is not available */ protected abstract List attributeList(); /** @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 NodeFactory getNodeFactory() { QName qName = getQName(); // QName might be null as we might not have been constructed yet if (qName != null) { NodeFactory factory = qName.getNodeFactory(); if (factory != null) { return factory; } } return super.getNodeFactory(); } /** A Factory Method pattern which creates * a List implementation used to store attributes */ protected List createAttributeList() { return createAttributeList(DEFAULT_CONTENT_LIST_SIZE); } /** A Factory Method pattern which creates * a List implementation used to store attributes */ 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://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 (C) MetaStuff, Ltd. All Rights Reserved. * * $Id: AbstractElement.java,v 1.1 2003/11/02 18:10:03 per_nyfelt Exp $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -