Package jodd.lagarto.dom
Class Node
- java.lang.Object
-
- jodd.lagarto.dom.Node
-
- All Implemented Interfaces:
java.lang.Cloneable
- Direct Known Subclasses:
CData,Comment,Document,DocumentType,Element,Text,XmlDeclaration
public abstract class Node extends java.lang.Object implements java.lang.CloneableDOM node.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNode.NodeTypeNode types.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<Attribute>attributesprotected Element[]childElementNodesprotected intchildElementNodesCountprotected java.util.List<Node>childNodesprotected java.lang.StringnodeNameprotected java.lang.StringnodeRawNameprotected Node.NodeTypenodeTypeprotected java.lang.StringnodeValueprotected DocumentownerDocumentprotected NodeparentNodeprotected intsiblingElementIndexprotected intsiblingIndexprotected intsiblingNameIndex
-
Constructor Summary
Constructors Modifier Constructor Description protectedNode(Document document, Node.NodeType nodeType, java.lang.String nodeName)Creates new node.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddChild(Node node)Appends child node.voidaddChild(Node... nodes)Appends several child nodes at once.voidappendTextContent(java.lang.Appendable appendable)Appends the text content to anAppendable(StringBuilder,CharBuffer...).protected voidchangeOwnerDocument(Node node, Document ownerDocument)Changes owner document for given node and all its children.booleancheck()Checks the health of child nodes.abstract Nodeclone()protected <T extends Node>
TcloneTo(T dest)Copies all non-final values to the empty cloned object.voiddetachFromParent()Removes this node from DOM tree.Node[]filterChildNodes(java.util.function.Predicate<Node> nodePredicate)Filters child nodes.NodefindChildNodeWithName(java.lang.String name)Finds the first child node with given node name.AttributegetAttribute(int index)Returns attribute at given index ornullif index not found.java.lang.StringgetAttribute(java.lang.String name)Returns attribute value.protected AttributegetAttributeInstance(java.lang.String name)intgetAttributesCount()Returns total number of attributes.NodegetChild(int index)Returns a child node at given index ornullif child doesn't exist for that index.NodegetChild(int... indexes)Returns a child node with given hierarchy.ElementgetChildElement(int index)Returns a child element node at given index.Element[]getChildElements()Returns an array of all children elements.intgetChildElementsCount()Returns number of child elements.intgetChildElementsCount(java.lang.String elementName)Returns number of child elements with given name.Node[]getChildNodes()Returns an array of all children nodes.intgetChildNodesCount()Returns number of all child nodes.java.lang.StringgetCssPath()Returns CSS path to this node from document root.NodegetFirstChild()Returns first child ornullif no children exist.ElementgetFirstChildElement()Returns first child element node ornullif no element children exist.ElementgetFirstChildElement(java.lang.String elementName)Returns first child element with given name ornullif no such children exist.java.lang.StringgetHtml()Generates HTML.java.lang.StringgetInnerHtml()Generates inner HTML.NodegetLastChild()Returns last child ornullif no children exist.ElementgetLastChildElement()Returns last child element node ornullif no such child node exist.ElementgetLastChildElement(java.lang.String elementName)Returns last child element with given name ornullif no such child node exist.NodegetNextSibling()Returns this node's next sibling of any type ornullif this is the last sibling.NodegetNextSiblingElement()Returns this node's next element.NodegetNextSiblingName()Returns this node's next element with the same name.java.lang.StringgetNodeName()Returns nodes name ornullif name is not available.java.lang.StringgetNodeRawName()Returns nodes raw name - exactly as it was given in the input.Node.NodeTypegetNodeType()Returnsnode type.java.lang.StringgetNodeValue()Returns node value ornullif value is not available.DocumentgetOwnerDocument()Returns owner document, root node for this DOM tree.NodegetParentNode()Returns parent node ornullif no parent exist.NodegetPreviousSibling()Returns this node's previous sibling of any type ornullif this is the first sibling.NodegetPreviousSiblingElement()Returns this node's previous sibling of element type ornullif this is the first sibling.NodegetPreviousSiblingName()Returns this node's previous sibling element with the same name.intgetSiblingElementIndex()intgetSiblingIndex()Get the list index of this node in its node sibling list.intgetSiblingNameIndex()java.lang.StringgetTextContent()Returns the text content of this node and its descendants.booleanhasAttribute(java.lang.String name)Returnstrueif node contains an attribute.booleanhasAttributes()Returnstrueif node has attributes.booleanhasChildNodes()Returnstrueif node has child nodes.protected intindexOfAttributeInstance(java.lang.String name)protected voidinitAttributes()Initializes attributes when needed.protected voidinitChildElementNodes()Initializes list of child elements.protected voidinitChildNodes(Node newNode)Initializes child nodes list when needed.protected voidinitSiblingNames()Initializes siblings elements of the same name.voidinsertAfter(Node[] newChilds, Node refChild)Inserts several child nodes after referent node.voidinsertAfter(Node newChild, Node refChild)Inserts node after provided node.voidinsertBefore(Node[] newChilds, Node refChild)Inserts several child nodes before provided node.voidinsertBefore(Node newChild, Node refChild)Inserts node before provided node.voidinsertChild(Node[] nodes, int index)Inserts several nodes at ones.voidinsertChild(Node node, int index)Inserts node at given index.booleanisAttributeContaining(java.lang.String name, java.lang.String word)Returnstrueif attribute containing some word.protected voidreindexChildren()Reindex children nodes.protected voidreindexChildrenOnAdd(int addedCount)Optimized variant ofreindexChildren()for addition.voidremoveAllChilds()Removes all child nodes.booleanremoveAttribute(java.lang.String name)NoderemoveChild(int index)Removes child node at given index.voidremoveChild(Node childNode)Removes child node.voidsetAttribute(java.lang.String name)Sets attribute that doesn't need a value.voidsetAttribute(java.lang.String name, java.lang.String value)Sets attribute value.voidsetNodeValue(java.lang.String value)Sets node value.voidvisit(NodeVisitor nodeVisitor)Visits the DOM tree.voidvisitChildren(NodeVisitor nodeVisitor)Visits children nodes.protected abstract voidvisitNode(NodeVisitor nodeVisitor)Visits single node.
-
-
-
Field Detail
-
nodeName
protected final java.lang.String nodeName
-
nodeRawName
protected final java.lang.String nodeRawName
-
nodeType
protected final Node.NodeType nodeType
-
ownerDocument
protected Document ownerDocument
-
nodeValue
protected java.lang.String nodeValue
-
attributes
protected java.util.List<Attribute> attributes
-
parentNode
protected Node parentNode
-
childNodes
protected java.util.List<Node> childNodes
-
childElementNodesCount
protected int childElementNodesCount
-
childElementNodes
protected Element[] childElementNodes
-
siblingIndex
protected int siblingIndex
-
siblingElementIndex
protected int siblingElementIndex
-
siblingNameIndex
protected int siblingNameIndex
-
-
Constructor Detail
-
Node
protected Node(Document document, Node.NodeType nodeType, java.lang.String nodeName)
Creates new node.
-
-
Method Detail
-
cloneTo
protected <T extends Node> T cloneTo(T dest)
Copies all non-final values to the empty cloned object. Cache-related values are not copied.
-
clone
public abstract Node clone()
- Overrides:
clonein classjava.lang.Object
-
getNodeType
public Node.NodeType getNodeType()
Returnsnode type.
-
getNodeName
public java.lang.String getNodeName()
Returns nodes name ornullif name is not available.
-
getNodeRawName
public java.lang.String getNodeRawName()
Returns nodes raw name - exactly as it was given in the input.
-
getNodeValue
public java.lang.String getNodeValue()
Returns node value ornullif value is not available.
-
setNodeValue
public void setNodeValue(java.lang.String value)
Sets node value.
-
getOwnerDocument
public Document getOwnerDocument()
Returns owner document, root node for this DOM tree.
-
detachFromParent
public void detachFromParent()
Removes this node from DOM tree.
-
addChild
public void addChild(Node node)
Appends child node. Don't use this node in the loop, since it might be slow due toreindexChildren().
-
addChild
public void addChild(Node... nodes)
Appends several child nodes at once. Reindex is done only once, after all children are added.
-
insertChild
public void insertChild(Node node, int index)
Inserts node at given index.
-
insertChild
public void insertChild(Node[] nodes, int index)
Inserts several nodes at ones. Reindex is done onl once, after all children are added.
-
insertBefore
public void insertBefore(Node newChild, Node refChild)
Inserts node before provided node.
-
insertBefore
public void insertBefore(Node[] newChilds, Node refChild)
Inserts several child nodes before provided node.
-
insertAfter
public void insertAfter(Node newChild, Node refChild)
Inserts node after provided node.
-
insertAfter
public void insertAfter(Node[] newChilds, Node refChild)
Inserts several child nodes after referent node.
-
removeChild
public Node removeChild(int index)
Removes child node at given index. Returns removed node ornullif index is invalid.
-
removeChild
public void removeChild(Node childNode)
Removes child node. It works only with direct children, i.e. if provided child node is not a child nothing happens.
-
removeAllChilds
public void removeAllChilds()
Removes all child nodes. Each child node will be detached from this parent.
-
getParentNode
public Node getParentNode()
Returns parent node ornullif no parent exist.
-
hasAttributes
public boolean hasAttributes()
Returnstrueif node has attributes.
-
getAttributesCount
public int getAttributesCount()
Returns total number of attributes.
-
getAttribute
public Attribute getAttribute(int index)
Returns attribute at given index ornullif index not found.
-
hasAttribute
public boolean hasAttribute(java.lang.String name)
Returnstrueif node contains an attribute.
-
getAttribute
public java.lang.String getAttribute(java.lang.String name)
Returns attribute value. Returnsnullwhen attribute doesn't exist or when attribute exist but doesn't specify a value.
-
getAttributeInstance
protected Attribute getAttributeInstance(java.lang.String name)
-
indexOfAttributeInstance
protected int indexOfAttributeInstance(java.lang.String name)
-
removeAttribute
public boolean removeAttribute(java.lang.String name)
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.String value)Sets attribute value. Value may benull.
-
setAttribute
public void setAttribute(java.lang.String name)
Sets attribute that doesn't need a value.
-
isAttributeContaining
public boolean isAttributeContaining(java.lang.String name, java.lang.String word)Returnstrueif attribute containing some word.
-
hasChildNodes
public boolean hasChildNodes()
Returnstrueif node has child nodes.
-
getChildNodesCount
public int getChildNodesCount()
Returns number of all child nodes.
-
getChildElementsCount
public int getChildElementsCount()
Returns number of child elements.
-
getChildElementsCount
public int getChildElementsCount(java.lang.String elementName)
Returns number of child elements with given name.
-
getChildNodes
public Node[] getChildNodes()
Returns an array of all children nodes. Returns an empty array if there are no children.
-
findChildNodeWithName
public Node findChildNodeWithName(java.lang.String name)
Finds the first child node with given node name.
-
filterChildNodes
public Node[] filterChildNodes(java.util.function.Predicate<Node> nodePredicate)
Filters child nodes.
-
getChildElements
public Element[] getChildElements()
Returns an array of all children elements.
-
getChild
public Node getChild(int index)
Returns a child node at given index ornullif child doesn't exist for that index.
-
getChild
public Node getChild(int... indexes)
Returns a child node with given hierarchy. Just a shortcut for successive calls ofgetChild(int).
-
getChildElement
public Element getChildElement(int index)
Returns a child element node at given index. If index is out of bounds,nullis returned.
-
getFirstChild
public Node getFirstChild()
Returns first child ornullif no children exist.
-
getFirstChildElement
public Element getFirstChildElement()
Returns first child element node ornullif no element children exist.
-
getFirstChildElement
public Element getFirstChildElement(java.lang.String elementName)
Returns first child element with given name ornullif no such children exist.
-
getLastChild
public Node getLastChild()
Returns last child ornullif no children exist.
-
getLastChildElement
public Element getLastChildElement()
Returns last child element node ornullif no such child node exist.
-
getLastChildElement
public Element getLastChildElement(java.lang.String elementName)
Returns last child element with given name ornullif no such child node exist.
-
check
public boolean check()
Checks the health of child nodes. Useful during complex tree manipulation, to check if everything is OK. Not optimized for speed, should be used just for testing purposes.
-
reindexChildren
protected void reindexChildren()
Reindex children nodes. Must be called on every children addition/removal. IterateschildNodeslist and:- calculates three different sibling indexes,
- calculates total child element node count,
- resets child element nodes array (will be init lazy later by @{#initChildElementNodes}.
-
reindexChildrenOnAdd
protected void reindexChildrenOnAdd(int addedCount)
Optimized variant ofreindexChildren()for addition. Only added children are optimized.
-
initChildElementNodes
protected void initChildElementNodes()
Initializes list of child elements.
-
initSiblingNames
protected void initSiblingNames()
Initializes siblings elements of the same name.
-
initAttributes
protected void initAttributes()
Initializes attributes when needed.
-
initChildNodes
protected void initChildNodes(Node newNode)
Initializes child nodes list when needed. Also fix owner document for new node, if needed.
-
changeOwnerDocument
protected void changeOwnerDocument(Node node, Document ownerDocument)
Changes owner document for given node and all its children.
-
getSiblingIndex
public int getSiblingIndex()
Get the list index of this node in its node sibling list. For example, if this is the first node sibling, returns 0. Index address all nodes, i.e. of all node types.
-
getSiblingElementIndex
public int getSiblingElementIndex()
-
getSiblingNameIndex
public int getSiblingNameIndex()
-
getNextSibling
public Node getNextSibling()
Returns this node's next sibling of any type ornullif this is the last sibling.
-
getNextSiblingElement
public Node getNextSiblingElement()
Returns this node's next element.
-
getNextSiblingName
public Node getNextSiblingName()
Returns this node's next element with the same name.
-
getPreviousSibling
public Node getPreviousSibling()
Returns this node's previous sibling of any type ornullif this is the first sibling.
-
getPreviousSiblingElement
public Node getPreviousSiblingElement()
Returns this node's previous sibling of element type ornullif this is the first sibling.
-
getPreviousSiblingName
public Node getPreviousSiblingName()
Returns this node's previous sibling element with the same name.
-
getTextContent
public java.lang.String getTextContent()
Returns the text content of this node and its descendants.- See Also:
appendTextContent(Appendable)
-
appendTextContent
public void appendTextContent(java.lang.Appendable appendable)
Appends the text content to anAppendable(StringBuilder,CharBuffer...). This way we can reuse theAppendableinstance during the creation of text content and have better performances.
-
getHtml
public java.lang.String getHtml()
Generates HTML.
-
getInnerHtml
public java.lang.String getInnerHtml()
Generates inner HTML.
-
visit
public void visit(NodeVisitor nodeVisitor)
Visits the DOM tree.
-
visitChildren
public void visitChildren(NodeVisitor nodeVisitor)
Visits children nodes.
-
visitNode
protected abstract void visitNode(NodeVisitor nodeVisitor)
Visits single node. Implementations just needs to call the correct visitor callback function.
-
getCssPath
public java.lang.String getCssPath()
Returns CSS path to this node from document root.
-
-