public abstract class Content extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<Content>
Content is the root of a Composite Design Pattern (Gamma et al., 1996).
Content fulfils the component-role, and its subclass Element fulfils the
composite-role, acting as a container for further Content nodes. Each
Content subtype controls what kinds of Content may be added as children.
The standard behaviour of addContent(Content)
either adds the
node, or ignores it, if it is not appropriate. Some leaf-nodes, such as
Text, treat addContent(Text)
as a request to append the
textual content of the other node. It is possible to iterate over the
Content subtree in document order (a pre-order traversal) using a
XMLIterator, which is used implicitly when a foreach
style of iteration is requested. Explicit iteration may also supply
a Filter to restrict which kinds of Content node are visited.
Content nodes may be added to at most one parent Content node. This is to preserve the tree-structure of an XML document. If it is desired to move Content, this must first be detached from its parent. If it is desired to duplicate Content for attachment elsewhere, the Content must first be cloned to produce an unattached copy. The cloned Content is a deep copy of the Content subtree, in which all Content nodes are recursively cloned and re-attached to their cloned parents, but all raw data is shared.
Modifier and Type | Field and Description |
---|---|
static int |
ATTRIBUTE
A constant denoting the Attribute content-type.
|
static int |
COMMENT
A constant denoting the Comment content-type.
|
static int |
DATA
A constant denoting the Data content-type.
|
static int |
DECLARATION
A constant denoting the Declaration content-type.
|
static int |
DOCTYPE
A constant denoting the Doctype content-type.
|
static int |
DOCUMENT
A constant denoting the Document content-type.
|
static int |
ELEMENT
A constant denoting the Element content-type.
|
static int |
INSTRUCTION
A constant denoting the Instruction content-type.
|
private Content |
parent
The owner of this Content node.
|
static int |
TEXT
A constant denoting the Text content-type.
|
static int |
UNDEFINED
A constant denoting the undefined content-type.
|
Modifier | Constructor and Description |
---|---|
protected |
Content()
Creates this Content node.
|
Modifier and Type | Method and Description |
---|---|
Content |
addAttribute(Attribute node)
Adds an Attribute to this Content node.
|
Content |
addContent(Content node)
Adds another Content node to this Content.
|
Content |
addContent(java.lang.String text)
Adds textual content to this Content.
|
Content |
clone()
Clones this Content node and its descendants.
|
java.util.Iterator<Content> |
descendants()
Returns an Iterator which visits the descendants of this Content node.
|
java.util.Iterator<Content> |
descendants(Filter filter)
Returns an Iterator which visits a filtered subset of the descendants
of this Content node.
|
Content |
detach()
Detaches this Content node from its parent node.
|
java.util.List<Attribute> |
getAttributes()
Returns a list of this Content node's Attributes.
|
Comment |
getComment()
Returns the first Comment node associated with this Content.
|
Content |
getContent(Filter filter)
Returns the first child Content node satisfying the matching rule of a
Filter.
|
Content |
getContent(Filter filter,
int index)
Returns the child Content node satisfying a Filter and found at the
given index.
|
Content |
getContent(int index)
Returns the Content node found under this node at the given index.
|
java.util.List<Content> |
getContents()
Returns a list of all the contents under this Content node.
|
java.util.List<Content> |
getContents(Filter filter)
Returns a list of contents under this Content node that satisfy the
matching rule of a Filter.
|
Document |
getDocument()
Returns the Document associated with this Content node.
|
java.lang.String |
getIdentifier()
Returns the identifier for this Content.
|
Content |
getParent()
Returns the parent Content node of this Content node.
|
java.lang.String |
getText()
Returns the textual content of this Content node.
|
int |
getType()
Returns the type of this Content.
|
boolean |
hasContents()
Reports whether this Content node has any contents.
|
java.util.Iterator<Content> |
iterator()
Returns an Iterator which traverses this Content subtree in pre-order.
|
java.util.Iterator<Content> |
iterator(Filter filter)
Returns an Iterator which visits a filtered subset of this Content
subtree.
|
Content |
removeContent(Content node)
Removes a Content node from this Content's immediate children.
|
Content |
removeContent(int index)
Removes the Content node at the given index from this Content's
immediate children.
|
java.util.List<Content> |
removeContents(Filter filter)
Removes all Content nodes immediately under this node that satisfy
the matching rule of a Filter.
|
protected void |
setParent(Content node)
Secret method used to install a parent Content node.
|
java.lang.String |
toString()
Returns a pretty representation of this Content.
|
public static final int UNDEFINED
public static final int ATTRIBUTE
public static final int ELEMENT
public static final int TEXT
public static final int DATA
public static final int DECLARATION
public static final int INSTRUCTION
public static final int DOCTYPE
public static final int COMMENT
public static final int DOCUMENT
private Content parent
protected void setParent(Content node) throws SemanticError
addContent(Content)
method, and checks
that this Content node has at most one parent node. This method is also
called with a null argument by the removeContent(Content)
method to detach this Content node from its parent.node
- the desired parent node, possibly null.SemanticError
public Content clone()
clone
in class java.lang.Object
public Content detach()
public int getType()
public Content getParent()
public java.lang.String getIdentifier()
public Comment getComment()
public Content addContent(Content node)
node
- another Content node.public Content addContent(java.lang.String text)
text
- the textual content.public Content getContent(int index)
index
- the index of the Content.public Content getContent(Filter filter)
filter
- the Filter used to select an acceptable node.public Content getContent(Filter filter, int index)
filter
- the Filter used to select acceptable nodes.index
- the index of the sought node.public Content removeContent(Content node)
node
- another Content node.public Content removeContent(int index)
index
- the index of the Content.public boolean hasContents()
public java.util.List<Content> getContents()
public java.util.List<Content> getContents(Filter filter)
filter
- a Filter to select acceptable nodes.public java.util.List<Content> removeContents(Filter filter)
filter
- a content Filter.public Content addAttribute(Attribute node)
node
- the Attribute to add.public java.util.List<Attribute> getAttributes()
public Document getDocument()
public java.lang.String getText()
public java.util.Iterator<Content> iterator()
iterator
in interface java.lang.Iterable<Content>
XMLIterator
public java.util.Iterator<Content> iterator(Filter filter)
filter
- the Filter to apply.XMLIterator
public java.util.Iterator<Content> descendants()
iterator()
method.XMLIterator
public java.util.Iterator<Content> descendants(Filter filter)
descendants()
method, but only a subset of
nodes are actually visited.filter
- the Filter to apply.XMLIterator
public java.lang.String toString()
toString
in class java.lang.Object