Class Element
- All Implemented Interfaces:
Serializable,Cloneable,Element,Node,Attributable,Group,Item,Namespaceable
- Author:
- tomyeh
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classprotected classNested classes/interfaces inherited from class org.zkoss.idom.impl.AbstractGroup
AbstractGroup.ChildArray, AbstractGroup.ElementMap -
Field Summary
FieldsModifier and TypeFieldDescriptionAdditional namespaces.The attributes.protected StringThe local name.protected NamespaceThe namespace.Fields inherited from class org.zkoss.idom.impl.AbstractGroup
_childrenFields inherited from interface org.zkoss.idom.Item
FIND_BY_PREFIX, FIND_BY_REGEX, FIND_BY_TAGNAME, FIND_IGNORE_CASE, FIND_RECURSIVEFields inherited from interface org.w3c.dom.Node
ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED, DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, DOCUMENT_POSITION_PRECEDING, DOCUMENT_TYPE_NODE, ELEMENT_NODE, ENTITY_NODE, ENTITY_REFERENCE_NODE, NOTATION_NODE, PROCESSING_INSTRUCTION_NODE, TEXT_NODE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanAdds a namespace to the namespace declaration.clone()Clones this object (a deep cloning not including contents contained in Textual nodes).final StringgetAttribute(String tname) final intgetAttributeIndex(int indexFrom, String tname) Gets the index of the attribute with the giving local name.final intgetAttributeIndex(int indexFrom, String namespace, String name, int mode) Gets the index of the first attribute that matches the specified criteria.final AttributegetAttributeItem(String tname) Gets the attribute with the tag name.final AttributegetAttributeItem(String namespace, String name, int mode) Gets the first attribute that matches the specified criteria.Returns all attributes of this object.final AttrgetAttributeNode(String tname) final AttrgetAttributeNodeNS(String nsURI, String lname) final StringgetAttributeNS(String nsURI, String lname) final NamedNodeMapgetAttributes(String namespace, String name, int mode) Gets a list of attributes of the specified criteria.final StringgetAttributeValue(String tname) Returns the value of the attribute of the specified tag name, or null if not specified.final StringgetAttributeValue(String namespace, String name, int mode) Gets the value of the first attribute that matches the giving criteria, or null if not found.final ObjectGets the content of this element.final ObjectgetContent(String path) Returns the content of the child element with the giving path, or null if the content is null or the child element doesn't exist.final Collection<Namespace>Returns namespace declared on this element.final NodeListgetElementsByTagName(String tname) final NodeListgetElementsByTagNameNS(String nsURI, String lname) final StringGets the local name of this item.final StringgetName()Gets the tag name of the element -- the name with prefix.final NamespaceGets the namespace.final NamespacegetNamespace(String prefix) Returns the Namespace in scope on this element for the given prefix (this involves searching up the tree, so the results depend on the current location of the element), or null if not found.final Stringfinal shortfinal StringAlways null.final Stringfinal StringGets the tag name of this item.final StringgetText()Returns the concatenation ofTextualchildren; never null.final StringgetText(boolean trim) Returns the concatenation ofTextualchildren; never null.final booleanhasAttribute(String tname) final booleanhasAttributeNS(String nsURI, String lname) final booleanfinal booleanhasContent(String path) Tests whether the child element with the giving path exists.final booleanTests whether this element is the root element of the owning document.Creates an empty list of attributes.final voidremoveAttribute(String tname) final AttrremoveAttributeNode(Attr oldAttr) final voidremoveAttributeNS(String nsURI, String lname) final ObjectremoveContent(String path) Removes the content of the child element with the giving path, and the child element itself if no other child.final voidsetAttribute(String tname, String value) final AttributesetAttribute(Attribute attr) Adds the giving attribute.final AttrsetAttributeNode(Attr newAttr) final AttrsetAttributeNodeNS(Attr newAttr) final voidsetAttributeNS(String nsURI, String tname, String value) final AttributesetAttributeValue(String tname, String value) Sets the value of the attribute with the giving tag name.final ObjectsetContent(Object obj) Sets the content of this element.final ObjectsetContent(String path, Object obj) Sets the content of the child element with the giving path.voidsetIdAttribute(String name, boolean isId) voidsetIdAttributeNode(Attr idAttr, boolean isId) voidsetIdAttributeNS(String namespaceURI, String localName, boolean isId) final voidsetLocalName(String lname) Sets the local name of this item.final voidSets the tag name of the element.final voidsetNamespace(String prefix, String nsURI) Sets the namespace.final voidSets the namespace.final voidfinal voidsetTagName(String tname) Sets the tag name of this item.final StringtoString()Methods inherited from class org.zkoss.idom.impl.AbstractGroup
anyElement, appendChild, coalesce, detachChildren, getChildNodes, getChildren, getElement, getElement, getElementIndex, getElementIndex, getElementNames, getElements, getElements, getElements, getElementValue, getElementValue, getFirstChild, getLastChild, hasChildNodes, insertBefore, newChildren, removeChild, replaceChildMethods inherited from class org.zkoss.idom.impl.AbstractItem
cloneNode, compareDocumentPosition, detach, equals, getBaseURI, getDocument, getFeature, getLocator, getNextSibling, getNodeName, getOwnerDocument, getParent, getParentNode, getPreviousSibling, getTextContent, getUserData, hashCode, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, match, normalize, setLocator, setNodeValue, setParent, setText, setTextContent, setUserDataMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.zkoss.idom.Item
detach, getDocument, getLocator, getParent, setLocator, setParent, setTextMethods inherited from interface org.w3c.dom.Node
appendChild, cloneNode, compareDocumentPosition, getBaseURI, getChildNodes, getFeature, getFirstChild, getLastChild, getNextSibling, getNodeName, getOwnerDocument, getParentNode, getPreviousSibling, getTextContent, getUserData, hasChildNodes, insertBefore, isDefaultNamespace, isEqualNode, isSameNode, isSupported, lookupNamespaceURI, lookupPrefix, normalize, removeChild, replaceChild, setNodeValue, setTextContent, setUserData
-
Field Details
-
_ns
The namespace. -
_lname
The local name. -
_attrs
The attributes. May be null. -
_addNamespaces
Additional namespaces. May be null
-
-
Constructor Details
-
Element
Constructor.- Parameters:
nsURI- the namespace URItname- the tag name
-
Element
Constructor.- Parameters:
ns- the namespace; if null, the default namespace is assumed (not necessaryNamespace.NO_NAMESPACE).lname- the local name
-
Element
Constructor without a namespace (i.e.,Namespace.NO_NAMESPACE).- Parameters:
lname- the local name
-
Element
protected Element()Constructor. Unlike other constructors, it doesn't set the modification flag.
-
-
Method Details
-
isRootElement
public final boolean isRootElement()Tests whether this element is the root element of the owning document.Note: false is returned if it doesn't has any parent.
-
getNamespace
Returns the Namespace in scope on this element for the given prefix (this involves searching up the tree, so the results depend on the current location of the element), or null if not found.If prefix is empty, it searches for the "default" namespace in scope. Thus, to search for attribute's namespace, caller have to skip this one and use NO_NAMESPACE. (due XML, an attribute without prefix is NO_NAMESPACE)
- Parameters:
prefix- namespace prefix to look up; null for empty
-
getDeclaredNamespaces
Returns namespace declared on this element.It is not a "live" representation. Also, it is read-only.
Note: Namespace.equals compares namespace's URI. However, the distinction here is the prefix, because it is mainly for getNamespace(prefix).
- Returns:
- the namespace declarations.
-
addDeclaredNamespace
Adds a namespace to the namespace declaration.- Returns:
- true if the namespace is added
- Throws:
DOMException- if the name space with the same prefix already exists but with different URI
-
getContent
Gets the content of this element.The content of an element is the first Binary or Text child of the element. Each element can has zero or one content.
Note:
getText()returns the concatenation of all Text children, not just the first one.- Returns:
- the content of this element; null if no such child
- See Also:
-
setContent
Sets the content of this element.All existent Binary or Text children of this element are removed first. If the object is a String, a Text item is created to hold it. Otherwise, a Binary item is created to hold it.
Non-Binary/Text children are preserved.
If obj is a
Itemor an array/collection ofItem, this method will add them as child vertices rather than being the content. Moreover, if the first item of the array/collection isItem, it is assumed to be all valid component to being has valid vertices. If not, an exception is thrown.Thus, getContent might not return the object being set by setContent.
- Parameters:
obj- the object to set; null is OK- Returns:
- the previous content
- See Also:
-
getContent
Returns the content of the child element with the giving path, or null if the content is null or the child element doesn't exist.Note that there might be more than one child with the same path in an iDOM tree; this method simply picks the first one that matches and returns its content. To access certain one, you might use [n] to [@attr = value] specify which one to access.
To know whether the child element exists or content is null, use
hasContent(java.lang.String).The content of an element is a special feature of iDOM. Like a Map, it is designed to let developers use names (in a path-like format) to access objects. See
setContent(String, Object).Like Unix path, the giving name could use '/' to concatenate a series of child elements.
An empty path denotes this element itself. Leading, ending and consecutive '/' will be ignored.
Example:
Object o = element.getContent("abc/def");
String s = Objects.toString(element.getContent("ab/cd"));
element.setContent("t:ab/cd/f:ef", new Integer(10));TODO: support [n] and [@attr = value]
- Parameters:
path- a path; e.g., "b", "a/b", "t:a/t:b"- See Also:
-
hasContent
Tests whether the child element with the giving path exists. Note that there might be more than one child with the same path in an iDOM tree; this method simply tell you that "yes", at least on such path exist. To get the content, usegetContent(String). -
setContent
Sets the content of the child element with the giving path.Note that there might be more than one child with the same path in an iDOM tree; this method simply pick one that matches and set its content (see
setContent(Object)).The content of an element is a special feature of iDOM. Like a Map, it is designed to let developers use names (in a path-like format) to access objects. See
getContent(String).Like Unix path, the giving name could use '/' to concatenate a series of child elements.
An empty path denotes this element itself. Leading, ending and consecutive '/' will be ignored.
If any element in the path is not found, it will be created automatically.
- Parameters:
path- a path; e.g., "b", "a/b", "t:a/t:b"obj- the object to set; null is acceptable- Returns:
- the previous content
- See Also:
-
removeContent
Removes the content of the child element with the giving path, and the child element itself if no other child.Unlike
setContent(String, Object)with null, the child element identified by path will be detached if it has no other child (but the content). So does its parent excluding this element. Thus, removeContent(path) could undo setContent(path, v).- Returns:
- the previous content
- See Also:
-
setNamespace
Sets the namespace. If ns is null, the default namespace is assumed (not necessaryNamespace.NO_NAMESPACE.According W3C/DOM, unlike element, an attribute doesn't allow a namespace that has an URI but without a prefix.
- Specified by:
setNamespacein interfaceNamespaceable
-
setNamespace
Sets the namespace. -
getNamespace
Description copied from interface:NamespaceableGets the namespace.- Specified by:
getNamespacein interfaceNamespaceable- Returns:
- the namespace; never null
-
getTagName
Description copied from interface:NamespaceableGets the tag name of this item. The tag name is also called the fully qualified name -- the name with the namespace prefix, e.g., prefix:name.To get the local name (the name without prefix), Namespaceable.getLocalName could be used.
- Specified by:
getTagNamein interfaceElement- Specified by:
getTagNamein interfaceNamespaceable
-
setTagName
Description copied from interface:NamespaceableSets the tag name of this item.- Specified by:
setTagNamein interfaceNamespaceable
-
getLocalName
Description copied from interface:NamespaceableGets the local name of this item. The local name is the name without prefix.To get the tag name (the name with prefix), Namespaceable.getTagName could be used.
- Specified by:
getLocalNamein interfaceNamespaceable- Specified by:
getLocalNamein interfaceNode- Overrides:
getLocalNamein classAbstractItem
-
setLocalName
Description copied from interface:NamespaceableSets the local name of this item.- Specified by:
setLocalNamein interfaceNamespaceable
-
getName
Gets the tag name of the element -- the name with prefix. To get the local name, use getLocalName. -
setName
Sets the tag name of the element. It will affect the local name and the namespace's prefix.- Specified by:
setNamein interfaceItem- Overrides:
setNamein classAbstractItem- See Also:
-
getText
Returns the concatenation ofTextualchildren; never null. Note: both <tag/> and <tag></tag> returns an empty string. To tell the difference, check the number of children.- Specified by:
getTextin interfaceItem- Overrides:
getTextin classAbstractItem- See Also:
-
getText
Returns the concatenation ofTextualchildren; never null.- Parameters:
trim- whether to trim before returning- See Also:
-
getAttributeItems
Description copied from interface:AttributableReturns all attributes of this object.The returned list is "live". Any modification to it affects the object that owns the attributes.
If the new added attribute has the same tag name as that of any existent attribute, DOMException is thrown. Thus, it is, sometimes, more convenient to ue setAttribute.
Naming reason: we don't call it getAttributes() to avoid the name conflict with Node.getAttributes().
- Specified by:
getAttributeItemsin interfaceAttributable- Returns:
- an empty list if no attribute at all
-
newAttrArray
Creates an empty list of attributes. -
getAttributeIndex
Description copied from interface:AttributableGets the index of the first attribute that matches the specified criteria.- Specified by:
getAttributeIndexin interfaceAttributable- Parameters:
indexFrom- the index to start searching from; 0 for beginningnamespace- the namespace URI if FIND_BY_PREFIX is not specified; the namespace prefix if FIND_BY_PREFIX specified; null to ignorename- the local name if FIND_BY_TAGNAME is not specified; the tag name if FIND_BY_TAGNAME specified; null to ignoremode- the search mode; zero or any combination of Item.FIND_xxx- Returns:
- the index if found; -1 if not found
-
getAttributeIndex
Description copied from interface:AttributableGets the index of the attribute with the giving local name.- Specified by:
getAttributeIndexin interfaceAttributable- Parameters:
indexFrom- the index to start searching from; 0 for beginningtname- the tag name (i.e.,Attribute.getName()) -- consists of the prefix and the local name- Returns:
- the index if found; -1 if not found
-
getAttributeItem
Description copied from interface:AttributableGets the first attribute that matches the specified criteria.The name is a bit strange because we have to avoid name conflicts with org.w3c.dom.Node.
- Specified by:
getAttributeItemin interfaceAttributable- Parameters:
namespace- the namespace URI if FIND_BY_PREFIX is not specified; the namespace prefix if FIND_BY_PREFIX specified; null to ignorename- the local name if FIND_BY_TAGNAME is not specified; the tag name if FIND_BY_TAGNAME specified; null to ignoremode- the search mode; zero or any combination of Item.FIND_xxx- Returns:
- the index if found; -1 if not found
-
getAttributeItem
Description copied from interface:AttributableGets the attribute with the tag name.The name is a bit strange because we have to avoid name conflicts with org.w3c.dom.Node.
- Specified by:
getAttributeItemin interfaceAttributable- Parameters:
tname- the tag name (i.e.,Attribute.getName()) -- consists of the prefix and the local name- Returns:
- null if not found
-
getAttributes
Description copied from interface:AttributableGets a list of attributes of the specified criteria.- Specified by:
getAttributesin interfaceAttributable- Parameters:
namespace- the namespace URI if FIND_BY_PREFIX is not specified; the namespace prefix if FIND_BY_PREFIX specified; null to ignorename- the local name if FIND_BY_TAGNAME is not specified; the tag name if FIND_BY_TAGNAME specified; null to ignoremode- the search mode; zero or any combination of Item.FIND_xxx- Returns:
- null if not found
-
setAttribute
Description copied from interface:AttributableAdds the giving attribute. If there is any existent one with the same tag name, it will be replaced. If not, the new attribute will be appended.- Specified by:
setAttributein interfaceAttributable- Parameters:
attr- the new attribute to add- Returns:
- the attribute being replaced; null if no one is replaced
-
getAttributeValue
Description copied from interface:AttributableGets the value of the first attribute that matches the giving criteria, or null if not found.According to Section 3.3.3 of XML 1.0 spec, the value is normalized, including trimmed.
- Specified by:
getAttributeValuein interfaceAttributable- Parameters:
namespace- the namespace URI if FIND_BY_PREFIX is not specified; the namespace prefix if FIND_BY_PREFIX specified; null to ignorename- the local name if FIND_BY_TAGNAME is not specified; the tag name if FIND_BY_TAGNAME specified; null to ignore- Returns:
- the value of the attribute; null if not found
-
getAttributeValue
Description copied from interface:AttributableReturns the value of the attribute of the specified tag name, or null if not specified.Note: unlike W3C's getAttribute, which returns empty if not specified, this method returns null if not specified.
- Specified by:
getAttributeValuein interfaceAttributable
-
setAttributeValue
Description copied from interface:AttributableSets the value of the attribute with the giving tag name. If the attribute doesn't exist, a new attribute will be created and added.Note: it looks similar to Attribute(String, String), but this method requires the tag name.
- Specified by:
setAttributeValuein interfaceAttributable- Parameters:
tname- the tag name (i.e., Attribute.getName)value- the new value.- Returns:
- the attribute being replaced; null if no one is replaced
-
clone
Description copied from class:AbstractItemClones this object (a deep cloning not including contents contained in Textual nodes). Note: after cloning, the read-only flag always becomes false, and the parent becomes null (i.e., detached).- Specified by:
clonein interfaceItem- Overrides:
clonein classAbstractGroup
-
getNodeType
public final short getNodeType()- Specified by:
getNodeTypein interfaceNode
-
getNodeValue
Always null. Unlike other nodes, it is not the same as getText.- Specified by:
getNodeValuein interfaceNode- Overrides:
getNodeValuein classAbstractItem
-
getAttributes
- Specified by:
getAttributesin interfaceNode- Overrides:
getAttributesin classAbstractItem
-
hasAttributes
public final boolean hasAttributes()- Specified by:
hasAttributesin interfaceNode- Overrides:
hasAttributesin classAbstractItem
-
getNamespaceURI
- Specified by:
getNamespaceURIin interfaceNode- Overrides:
getNamespaceURIin classAbstractItem
-
getPrefix
- Specified by:
getPrefixin interfaceNode- Overrides:
getPrefixin classAbstractItem
-
setPrefix
- Specified by:
setPrefixin interfaceNode- Overrides:
setPrefixin classAbstractItem
-
getElementsByTagName
- Specified by:
getElementsByTagNamein interfaceElement
-
getElementsByTagNameNS
- Specified by:
getElementsByTagNameNSin interfaceElement
-
getAttributeNode
- Specified by:
getAttributeNodein interfaceElement
-
getAttributeNodeNS
- Specified by:
getAttributeNodeNSin interfaceElement
-
getAttribute
- Specified by:
getAttributein interfaceElement
-
getAttributeNS
- Specified by:
getAttributeNSin interfaceElement
-
setAttribute
- Specified by:
setAttributein interfaceElement
-
setAttributeNS
- Specified by:
setAttributeNSin interfaceElement
-
setAttributeNode
- Specified by:
setAttributeNodein interfaceElement
-
setAttributeNodeNS
- Specified by:
setAttributeNodeNSin interfaceElement
-
removeAttribute
- Specified by:
removeAttributein interfaceElement
-
removeAttributeNS
- Specified by:
removeAttributeNSin interfaceElement
-
removeAttributeNode
- Specified by:
removeAttributeNodein interfaceElement
-
hasAttribute
- Specified by:
hasAttributein interfaceElement
-
hasAttributeNS
- Specified by:
hasAttributeNSin interfaceElement
-
toString
-
getSchemaTypeInfo
- Specified by:
getSchemaTypeInfoin interfaceElement
-
setIdAttribute
- Specified by:
setIdAttributein interfaceElement- Throws:
DOMException
-
setIdAttributeNS
public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException - Specified by:
setIdAttributeNSin interfaceElement- Throws:
DOMException
-
setIdAttributeNode
- Specified by:
setIdAttributeNodein interfaceElement- Throws:
DOMException
-