📄 document.java
字号:
}
// public Content getChild(Filter filter) {
// int i = indexOf(0, filter);
// return (i < 0) ? null : getContent(i);
// }
/**
* This will return all content for the <code>Document</code>.
* The returned list is "live" in document order and changes to it
* affect the document's actual content.
*
* <p>
* Sequential traversal through the List is best done with a Iterator
* since the underlying implement of List.size() may require walking the
* entire list.
* </p>
*
* @return <code>List</code> - all Document content
* @throws IllegalStateException if the root element hasn't been set
*/
public List getContent() {
if (!hasRootElement())
throw new IllegalStateException("Root element not set");
return content;
}
/**
* Return a filtered view of this <code>Document</code>'s content.
*
* <p>
* Sequential traversal through the List is best done with a Iterator
* since the underlying implement of List.size() may require walking the
* entire list.
* </p>
*
* @param filter <code>Filter</code> to apply
* @return <code>List</code> - filtered Document content
* @throws IllegalStateException if the root element hasn't been set
*/
public List getContent(Filter filter) {
if (!hasRootElement())
throw new IllegalStateException("Root element not set");
return content.getView(filter);
}
/**
* Removes all child content from this parent.
*
* @return list of the old children detached from this parent
*/
public List removeContent() {
List old = new ArrayList(content);
content.clear();
return old;
}
/**
* Remove all child content from this parent matching the supplied filter.
*
* @param filter filter to select which content to remove
* @return list of the old children detached from this parent
*/
public List removeContent(Filter filter) {
List old = new ArrayList();
Iterator itr = content.getView(filter).iterator();
while (itr.hasNext()) {
Content child = (Content) itr.next();
old.add(child);
itr.remove();
}
return old;
}
/**
* This sets the content of the <code>Document</code>. The supplied
* List should contain only objects of type <code>Element</code>,
* <code>Comment</code>, and <code>ProcessingInstruction</code>.
*
* <p>
* When all objects in the supplied List are legal and before the new
* content is added, all objects in the old content will have their
* parentage set to null (no parent) and the old content list will be
* cleared. This has the effect that any active list (previously obtained
* with a call to {@link #getContent}) will also
* change to reflect the new content. In addition, all objects in the
* supplied List will have their parentage set to this document, but the
* List itself will not be "live" and further removals and additions will
* have no effect on this document content. If the user wants to continue
* working with a "live" list, then a call to setContent should be
* followed by a call to {@link #getContent} to
* obtain a "live" version of the content.
* </p>
*
* <p>
* Passing a null or empty List clears the existing content.
* </p>
*
* <p>
* In event of an exception the original content will be unchanged and
* the objects in the supplied content will be unaltered.
* </p>
*
* @param newContent <code>List</code> of content to set
* @return this document modified
* @throws IllegalAddException if the List contains objects of
* illegal types or with existing parentage.
*/
public Document setContent(Collection newContent) {
content.clearAndSet(newContent);
return this;
}
/**
*
* <p>
* Sets the effective URI from which this document was loaded,
* and against which relative URLs in this document will be resolved.
* </p>
*
* @param uri the base URI of this document
*/
public final void setBaseURI(String uri) {
this.baseURI = uri; // XXX We don't check the URI
}
/**
* <p>
* Returns the URI from which this document was loaded,
* or null if this is not known.
* </p>
*
* @return the base URI of this document
*/
public final String getBaseURI() {
return baseURI;
}
/*
* Replace the current child the given index with the supplied child.
* <p>
* In event of an exception the original content will be unchanged and
* the supplied child will be unaltered.
* </p>
*
* @param index - index of child to replace.
* @param child - child to add.
* @throws IllegalAddException if the supplied child is already attached
* or not legal content for this parent.
* @throws IndexOutOfBoundsException if index is negative or greater
* than the current number of children.
*/
public Document setContent(int index, Content child) {
content.set(index, child);
return this;
}
/**
* Replace the child at the given index whith the supplied
* collection.
* <p>
* In event of an exception the original content will be unchanged and
* the content in the supplied collection will be unaltered.
* </p>
*
* @param index - index of child to replace.
* @param collection - collection of content to add.
* @return object on which the method was invoked
* @throws IllegalAddException if the collection contains objects of
* illegal types.
* @throws IndexOutOfBoundsException if index is negative or greater
* than the current number of children.
*/
public Document setContent(int index, Collection collection) {
content.remove(index);
content.addAll(index, collection);
return this;
}
public boolean removeContent(Content child) {
return content.remove(child);
}
public Content removeContent(int index) {
return (Content) content.remove(index);
}
/**
* Set this document's content to be the supplied child.
* <p>
* If the supplied child is legal content for a Document and before
* it is added, all content in the current content list will
* be cleared and all current children will have their parentage set to
* null.
* <p>
* This has the effect that any active list (previously obtained with
* a call to one of the {@link #getContent} methods will also change
* to reflect the new content. In addition, all content in the supplied
* collection will have their parentage set to this Document. If the user
* wants to continue working with a <b>"live"</b> list of this Document's
* child, then a call to setContent should be followed by a call to one
* of the {@link #getContent} methods to obtain a <b>"live"</b>
* version of the children.
* <p>
* Passing a null child clears the existing content.
* <p>
* In event of an exception the original content will be unchanged and
* the supplied child will be unaltered.
*
* @param child new content to replace existing content
* @return the parent on which the method was called
* @throws IllegalAddException if the supplied child is already attached
* or not legal content for this parent
*/
public Document setContent(Content child) {
content.clear();
content.add(child);
return this;
}
/**
* This returns a <code>String</code> representation of the
* <code>Document</code>, suitable for debugging. If the XML
* representation of the <code>Document</code> is desired,
* {@link org.jdom.output.XMLOutputter#outputString(Document)}
* should be used.
*
* @return <code>String</code> - information about the
* <code>Document</code>
*/
public String toString() {
StringBuffer stringForm = new StringBuffer()
.append("[Document: ");
DocType docType = getDocType();
if (docType != null) {
stringForm.append(docType.toString())
.append(", ");
} else {
stringForm.append(" No DOCTYPE declaration, ");
}
Element rootElement = getRootElement();
if (rootElement != null) {
stringForm.append("Root is ")
.append(rootElement.toString());
} else {
stringForm.append(" No root element"); // shouldn't happen
}
stringForm.append("]");
return stringForm.toString();
}
/**
* This tests for equality of this <code>Document</code> to the supplied
* <code>Object</code>.
*
* @param ob <code>Object</code> to compare to
* @return <code>boolean</code> whether the <code>Document</code> is
* equal to the supplied <code>Object</code>
*/
public final boolean equals(Object ob) {
return (ob == this);
}
/**
* This returns the hash code for this <code>Document</code>.
*
* @return <code>int</code> hash code
*/
public final int hashCode() {
return super.hashCode();
}
/**
* This will return a deep clone of this <code>Document</code>.
*
* @return <code>Object</code> clone of this <code>Document</code>
*/
public Object clone() {
Document doc = null;
try {
doc = (Document) super.clone();
} catch (CloneNotSupportedException ce) {
// Can't happen
}
// The clone has a reference to this object's content list, so
// owerwrite with a empty list
doc.content = new ContentList(doc);
// Add the cloned content to clone
for (int i = 0; i < content.size(); i++) {
Object obj = content.get(i);
if (obj instanceof Element) {
Element element = (Element)((Element)obj).clone();
doc.content.add(element);
}
else if (obj instanceof Comment) {
Comment comment = (Comment)((Comment)obj).clone();
doc.content.add(comment);
}
else if (obj instanceof ProcessingInstruction) {
ProcessingInstruction pi = (ProcessingInstruction)
((ProcessingInstruction)obj).clone();
doc.content.add(pi);
}
else if (obj instanceof DocType) {
DocType dt = (DocType) ((DocType)obj).clone();
doc.content.add(dt);
}
}
return doc;
}
/**
* Returns an iterator that walks over all descendants in document order.
*
* @return an iterator to walk descendants
*/
public Iterator getDescendants() {
return new DescendantIterator(this);
}
/**
* Returns an iterator that walks over all descendants in document order
* applying the Filter to return only elements that match the filter rule.
* With filters you can match only Elements, only Comments, Elements or
* Comments, only Elements with a given name and/or prefix, and so on.
*
* @param filter filter to select which descendants to see
* @return an iterator to walk descendants within a filter
*/
public Iterator getDescendants(Filter filter) {
return new FilterIterator(new DescendantIterator(this), filter);
}
public Parent getParent() {
return null; // documents never have parents
}
/**
* @see org.jdom.Parent#getDocument()
*/
public Document getDocument() {
return this;
}
/**
* Assigns an arbitrary object to be associated with this document under
* the given "id" string. Null values are permitted. Strings beginning
* with "http://www.jdom.org/ are reserved for JDOM use.
*
* @param id the id of the stored object
* @param value the object to store
*/
public void setProperty(String id, Object value) {
if (propertyMap == null) {
propertyMap = new HashMap();
}
propertyMap.put(id, value);
}
/**
* Returns the object associated with this document under the given "id"
* string, or null if there is no binding or if the binding explicitly
* stored a null value.
*
* @param id the id of the stored object to return
* @return the object associated with the given id
*/
public Object getProperty(String id) {
if (propertyMap == null) return null;
return propertyMap.get(id);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -