Package org.htmlcleaner
Class TagNode
java.lang.Object
org.htmlcleaner.BaseTokenImpl
org.htmlcleaner.BaseHtmlNode
org.htmlcleaner.TagToken
org.htmlcleaner.TagNode
XML node tag - basic node of the cleaned HTML tree. At the same time, it represents start tag token after HTML parsing phase and before cleaning phase. After cleaning process, tree structure remains containing tag nodes (TagNode class), content (text nodes - ContentNode), comments (CommentNode) and optionally doctype node (DoctypeToken).
-
Field Summary
Fields inherited from class org.htmlcleaner.BaseHtmlNode
parent
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttribute
(String attName, String attValue) Adds specified attribute to this tag or overrides existing one.void
void
addChildren
(List newChildren) Add all elements from specified list to this node.void
addNamespaceDeclaration
(String nsPrefix, String nsURI) Adds namespace declaration to the nodeObject[]
evaluateXPath
(String xPathExpression) Evaluates XPath expression on give node.findElementByAttValue
(String attName, String attValue, boolean isRecursive, boolean isCaseSensitive) findElementByName
(String findName, boolean isRecursive) findElementHavingAttribute
(String attName, boolean isRecursive) TagNode[]
getAllElements
(boolean isRecursive) getAllElementsList
(boolean isRecursive) getAttributeByName
(String attName) Returns the attributes of the tagnode.Returns the attributes of the tagnode in lower case.int
getChildIndex
(HtmlNode child) Deprecated.TagNode[]
getElementList
(ITagNodeCondition condition, boolean isRecursive) Get all elements in the tree that satisfy specified condition.getElementListByAttValue
(String attName, String attValue, boolean isRecursive, boolean isCaseSensitive) getElementListByName
(String findName, boolean isRecursive) getElementListHavingAttribute
(String attName, boolean isRecursive) TagNode[]
getElementsByAttValue
(String attName, String attValue, boolean isRecursive, boolean isCaseSensitive) TagNode[]
getElementsByName
(String findName, boolean isRecursive) TagNode[]
getElementsHavingAttribute
(String attName, boolean isRecursive) getName()
getText()
boolean
hasAttribute
(String attName) Checks existence of specified attribute.boolean
void
insertChild
(int index, HtmlNode childToAdd) Inserts specified node at specified position in array of childrenvoid
insertChildAfter
(HtmlNode node, HtmlNode nodeToInsert) Inserts specified node in the list of children after specified childvoid
insertChildBefore
(HtmlNode node, HtmlNode nodeToInsert) Inserts specified node in the list of children before specified childboolean
boolean
isCopy()
boolean
isEmpty()
boolean
boolean
isPruned()
boolean
makeCopy()
void
Removes all children (subelements and text content).void
removeAttribute
(String attName) Removes specified attribute from this tag.boolean
removeChild
(Object child) Remove specified child element from this node.boolean
Remove this node from the tree.void
serialize
(Serializer serializer, Writer writer) void
setAttributes
(Map<String, String> attributes) Replace the current set of attributes with a new set.void
setAutoGenerated
(boolean autoGenerated) void
setChildren
(List<? extends BaseToken> children) void
setDocType
(DoctypeToken docType) void
setForeignMarkup
(boolean isForeignMarkup) void
setPruned
(boolean pruned) void
setTrimAttributeValues
(boolean isTrimAttributeValues) void
traverse
(TagNodeVisitor visitor) Traverses the tree and performs visitor's action on each node.Methods inherited from class org.htmlcleaner.BaseHtmlNode
getParent, getSiblings, setParent
Methods inherited from class org.htmlcleaner.BaseTokenImpl
getCol, getRow, setCol, setRow
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.htmlcleaner.HtmlNode
getParent, getSiblings, setParent
-
Constructor Details
-
TagNode
-
-
Method Details
-
getName
-
getAttributeByName
- Parameters:
attName
-- Returns:
- Value of the specified attribute, or null if it this tag doesn't contain it.
-
getAttributes
Returns the attributes of the tagnode.- Returns:
- Map instance containing all attribute name/value pairs.
-
getAttributesInLowerCase
Returns the attributes of the tagnode in lower case.- Returns:
- Map instance containing all attribute name/value pairs, with attribute names transformed to lower case
-
setAttributes
Replace the current set of attributes with a new set.- Parameters:
attributes
-
-
hasAttribute
Checks existence of specified attribute.- Parameters:
attName
-- Returns:
- true if TagNode has attribute
-
addAttribute
Adds specified attribute to this tag or overrides existing one.- Parameters:
attName
-attValue
-
-
removeAttribute
Removes specified attribute from this tag.- Parameters:
attName
-
-
getChildren
Deprecated.usegetChildTagList()
, will be refactored and possibly removed in future versions. TODO This method should be refactored because is does not properly match the commonly used Java's getter/setter strategy.- Returns:
- List of child TagNode objects.
-
setChildren
-
getAllChildren
-
getChildTagList
- Returns:
- List of child TagNode objects.
-
hasChildren
public boolean hasChildren()- Returns:
- Whether this node has child elements or not.
-
getChildTags
- Returns:
- An array of child TagNode instances.
-
getText
- Returns:
- Text content of this node and it's subelements.
-
getChildIndex
- Parameters:
child
- Child to find index of- Returns:
- Index of the specified child node inside this node's children, -1 if node is not the child
-
insertChild
Inserts specified node at specified position in array of children- Parameters:
index
-childToAdd
-
-
insertChildBefore
Inserts specified node in the list of children before specified child- Parameters:
node
- Child before which to insert new nodenodeToInsert
- Node to be inserted at specified position
-
insertChildAfter
Inserts specified node in the list of children after specified child- Parameters:
node
- Child after which to insert new nodenodeToInsert
- Node to be inserted at specified position
-
getDocType
-
setDocType
-
addChild
-
addChildren
Add all elements from specified list to this node.- Parameters:
newChildren
-
-
getElementList
Get all elements in the tree that satisfy specified condition.- Parameters:
condition
-isRecursive
-- Returns:
- List of TagNode instances with specified name.
-
getAllElementsList
-
getAllElements
-
findElementByName
-
getElementListByName
-
getElementsByName
-
findElementHavingAttribute
-
getElementListHavingAttribute
-
getElementsHavingAttribute
-
findElementByAttValue
-
getElementListByAttValue
-
getElementsByAttValue
-
evaluateXPath
Evaluates XPath expression on give node.
This is not fully supported XPath parser and evaluator. Examples below show supported elements:- //div//a
- //div//a[@id][@class]
- /body/*[1]/@type
- //div[3]//a[@id][@href='r/n4']
- //div[last() >= 4]//./div[position() = last()])[position() > 22]//li[2]//a
- //div[2]/@*[2]
- data(//div//a[@id][@class])
- //p/last()
- //body//div[3][@class]//span[12.2invalid input: '<'position()]/@id
- data(//a['v' invalid input: '<' @id])
- Parameters:
xPathExpression
-- Returns:
- result of XPather evaluation.
- Throws:
XPatherException
-
removeFromTree
public boolean removeFromTree()Remove this node from the tree.- Returns:
- True if element is removed (if it is not root node).
-
removeChild
Remove specified child element from this node.- Parameters:
child
-- Returns:
- True if child object existed in the children list.
-
removeAllChildren
public void removeAllChildren()Removes all children (subelements and text content). -
setAutoGenerated
public void setAutoGenerated(boolean autoGenerated) - Parameters:
autoGenerated
- the autoGenerated to set
-
isAutoGenerated
public boolean isAutoGenerated()- Returns:
- the autoGenerated
-
isPruned
public boolean isPruned()- Returns:
- true, if node was marked to be pruned.
-
setPruned
public void setPruned(boolean pruned) -
isEmpty
public boolean isEmpty() -
addNamespaceDeclaration
Adds namespace declaration to the node- Parameters:
nsPrefix
- Namespace prefixnsURI
- Namespace URI
-
getNamespaceDeclarations
- Returns:
- Map of namespace declarations for this node
-
serialize
- Specified by:
serialize
in interfaceBaseToken
- Overrides:
serialize
in classBaseHtmlNode
- Throws:
IOException
-
makeCopy
-
isCopy
public boolean isCopy() -
traverse
Traverses the tree and performs visitor's action on each node. It stops when it finishes all the tree or when visitor returns false.- Parameters:
visitor
- TagNodeVisitor implementation
-
isForeignMarkup
public boolean isForeignMarkup()- Returns:
- the isForeignMarkup
-
setForeignMarkup
public void setForeignMarkup(boolean isForeignMarkup) - Parameters:
isForeignMarkup
- the isForeignMarkup to set
-
isTrimAttributeValues
public boolean isTrimAttributeValues()- Returns:
- the isTrimAttributeValues
-
setTrimAttributeValues
public void setTrimAttributeValues(boolean isTrimAttributeValues) - Parameters:
isTrimAttributeValues
- the isTrimAttributeValues to set
-
getChildTagList()
, will be refactored and possibly removed in future versions.