📄 contentlist.java
字号:
/*--
$Id: ContentList.java,v 1.42 2007/11/10 05:28:58 jhunter Exp $
Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org).
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``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 THE JDOM AUTHORS OR THE PROJECT
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.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom;
import java.util.*;
import org.jdom.filter.*;
/**
* A non-public list implementation holding only legal JDOM content, including
* content for Document or Element nodes. Users see this class as a simple List
* implementation.
*
* @see CDATA
* @see Comment
* @see Element
* @see EntityRef
* @see ProcessingInstruction
* @see Text
*
* @version $Revision: 1.42 $, $Date: 2007/11/10 05:28:58 $
* @author Alex Rosen
* @author Philippe Riand
* @author Bradley S. Huffman
*/
final class ContentList extends AbstractList implements java.io.Serializable {
private static final String CVS_ID =
"@(#) $RCSfile: ContentList.java,v $ $Revision: 1.42 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1 $";
private static final long serialVersionUID = 1L;
private static final int INITIAL_ARRAY_SIZE = 5;
/** Our backing list */
private Content elementData[];
private int size;
/** Document or Element this list belongs to */
private Parent parent;
/** Force either a Document or Element parent */
ContentList(Parent parent) {
this.parent = parent;
}
/**
* Package internal method to support building from sources that are
* 100% trusted.
*
* @param c content to add without any checks
*/
final void uncheckedAddContent(Content c) {
c.parent = parent;
ensureCapacity(size + 1);
elementData[size++] = c;
modCount++;
}
/**
* Inserts the specified object at the specified position in this list.
* Shifts the object currently at that position (if any) and any
* subsequent objects to the right (adds one to their indices).
*
* @param index The location to set the value to.
* @param obj The object to insert into the list.
* throws IndexOutOfBoundsException if index < 0 || index > size()
*/
public void add(int index, Object obj) {
if (obj == null) {
throw new IllegalAddException("Cannot add null object");
}
if (obj instanceof String) { // String is OK to add as special case
obj = new Text(obj.toString()); // wrap it as a Content
}
if ((obj instanceof Content)) {
add(index, (Content) obj);
} else {
throw new IllegalAddException("Class " +
obj.getClass().getName() +
" is of unrecognized type and cannot be added");
}
}
/**
* @see org.jdom.ContentList#add(int, org.jdom.Content)
*/
private void documentCanContain(int index, Content child) throws IllegalAddException {
if (child instanceof Element) {
if (indexOfFirstElement() >= 0) {
throw new IllegalAddException(
"Cannot add a second root element, only one is allowed");
}
if (indexOfDocType() > index) {
throw new IllegalAddException(
"A root element cannot be added before the DocType");
}
}
if (child instanceof DocType) {
if (indexOfDocType() >= 0) {
throw new IllegalAddException(
"Cannot add a second doctype, only one is allowed");
}
int firstElt = indexOfFirstElement();
if (firstElt != -1 && firstElt < index) {
throw new IllegalAddException(
"A DocType cannot be added after the root element");
}
}
if (child instanceof CDATA) {
throw new IllegalAddException("A CDATA is not allowed at the document root");
}
if (child instanceof Text) {
throw new IllegalAddException("A Text is not allowed at the document root");
}
if (child instanceof EntityRef) {
throw new IllegalAddException("An EntityRef is not allowed at the document root");
}
}
private static void elementCanContain(int index, Content child) throws IllegalAddException {
if (child instanceof DocType) {
throw new IllegalAddException(
"A DocType is not allowed except at the document level");
}
}
/**
* Check and add the <code>Element</code> to this list at
* the given index.
*
* @param index index where to add <code>Element</code>
* @param child <code>Element</code> to add
*/
void add(int index, Content child) {
if (child == null) {
throw new IllegalAddException("Cannot add null object");
}
if (parent instanceof Document) {
documentCanContain(index, child);
}
else {
elementCanContain(index, child);
}
if (child.getParent() != null) {
Parent p = child.getParent();
if (p instanceof Document) {
throw new IllegalAddException((Element)child,
"The Content already has an existing parent document");
}
else {
throw new IllegalAddException(
"The Content already has an existing parent \"" +
((Element)p).getQualifiedName() + "\"");
}
}
if (child == parent) {
throw new IllegalAddException(
"The Element cannot be added to itself");
}
// Detect if we have <a><b><c/></b></a> and c.add(a)
if ((parent instanceof Element && child instanceof Element) &&
((Element) child).isAncestor((Element)parent)) {
throw new IllegalAddException(
"The Element cannot be added as a descendent of itself");
}
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
}
child.setParent(parent);
ensureCapacity(size+1);
if( index==size ) {
elementData[size++] = child;
} else {
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = child;
size++;
}
modCount++;
}
/**
* Add the specified collecton to the end of this list.
*
* @param collection The collection to add to the list.
* @return <code>true</code> if the list was modified as a result of
* the add.
*/
public boolean addAll(Collection collection) {
return addAll(size(), collection);
}
/**
* Inserts the specified collecton at the specified position in this list.
* Shifts the object currently at that position (if any) and any
* subsequent objects to the right (adds one to their indices).
*
* @param index The offset to start adding the data in the collection
* @param collection The collection to insert into the list.
* @return <code>true</code> if the list was modified as a result of
* the add.
* throws IndexOutOfBoundsException if index < 0 || index > size()
*/
public boolean addAll(int index, Collection collection) {
if (index<0 || index>size) {
throw new IndexOutOfBoundsException("Index: " + index +
" Size: " + size());
}
if ((collection == null) || (collection.size() == 0)) {
return false;
}
ensureCapacity(size() + collection.size());
int count = 0;
try {
Iterator i = collection.iterator();
while (i.hasNext()) {
Object obj = i.next();
add(index + count, obj);
count++;
}
}
catch (RuntimeException exception) {
for (int i = 0; i < count; i++) {
remove(index);
}
throw exception;
}
return true;
}
/**
* Clear the current list.
*/
public void clear() {
if (elementData != null) {
for (int i = 0; i < size; i++) {
Content obj = elementData[i];
removeParent(obj);
}
elementData = null;
size = 0;
}
modCount++;
}
/**
* Clear the current list and set it to the contents
* of the <code>Collection</code>.
* object.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -