org.jsoup.nodes
Class TextNode

java.lang.Object
  extended by org.jsoup.nodes.Node
      extended by org.jsoup.nodes.TextNode
All Implemented Interfaces:
Cloneable

public class TextNode
extends Node

A text node.

Author:
Jonathan Hedley, [email protected]

Constructor Summary
TextNode(String text, String baseUri)
          Create a new TextNode representing the supplied (unencoded) text).
 
Method Summary
 String absUrl(String attributeKey)
          Get an absolute URL from a URL attribute that may be relative (i.e.
 String attr(String attributeKey)
          Get an attribute's value by its key.
 Node attr(String attributeKey, String attributeValue)
          Set an attribute (key=value).
 Attributes attributes()
          Get all of the element's attributes.
static TextNode createFromEncoded(String encodedText, String baseUri)
          Create a new TextNode from HTML encoded (aka escaped) data.
 String getWholeText()
          Get the (unencoded) text of this text node, including any newlines and spaces present in the original.
 boolean hasAttr(String attributeKey)
          Test if this element has an attribute.
 boolean isBlank()
          Test if this text node is blank -- that is, empty or only whitespace (including newlines).
 String nodeName()
          Get the node name of this node.
 Node removeAttr(String attributeKey)
          Remove an attribute from this element.
 TextNode splitText(int offset)
          Split this text node into two nodes at the specified string offset.
 String text()
          Get the text content of this text node.
 TextNode text(String text)
          Set the text content of this text node.
 String toString()
           
 
Methods inherited from class org.jsoup.nodes.Node
addChildren, addChildren, after, baseUri, before, childNode, childNodes, childNodesAsArray, clone, doClone, equals, hashCode, indent, nextSibling, outerHtml, outerHtml, ownerDocument, parent, previousSibling, remove, removeChild, replaceChild, replaceWith, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes, wrap
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TextNode

public TextNode(String text,
                String baseUri)
Create a new TextNode representing the supplied (unencoded) text).

Parameters:
text - raw text
baseUri - base uri
See Also:
createFromEncoded(String, String)
Method Detail

nodeName

public String nodeName()
Description copied from class: Node
Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).

Specified by:
nodeName in class Node
Returns:
node name

text

public String text()
Get the text content of this text node.

Returns:
Unencoded, normalised text.
See Also:
getWholeText()

text

public TextNode text(String text)
Set the text content of this text node.

Parameters:
text - unencoded text
Returns:
this, for chaining

getWholeText

public String getWholeText()
Get the (unencoded) text of this text node, including any newlines and spaces present in the original.

Returns:
text

isBlank

public boolean isBlank()
Test if this text node is blank -- that is, empty or only whitespace (including newlines).

Returns:
true if this document is empty or only whitespace, false if it contains any text content.

splitText

public TextNode splitText(int offset)
Split this text node into two nodes at the specified string offset. After splitting, this node will contain the original text up to the offset, and will have a new text node sibling containing the text after the offset.

Parameters:
offset - string offset point to split node at.
Returns:
the newly created text node containing the text after the offset.

toString

public String toString()
Overrides:
toString in class Node

createFromEncoded

public static TextNode createFromEncoded(String encodedText,
                                         String baseUri)
Create a new TextNode from HTML encoded (aka escaped) data.

Parameters:
encodedText - Text containing encoded HTML (e.g. <)
Returns:
TextNode containing unencoded data (e.g. <)

attr

public String attr(String attributeKey)
Description copied from class: Node
Get an attribute's value by its key.

To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs, which is a shortcut to the Node.absUrl(java.lang.String) method. E.g.:

String url = a.attr("abs:href");

Overrides:
attr in class Node
Parameters:
attributeKey - The attribute key.
Returns:
The attribute, or empty string if not present (to avoid nulls).
See Also:
Node.attributes(), Node.hasAttr(String), Node.absUrl(String)

attributes

public Attributes attributes()
Description copied from class: Node
Get all of the element's attributes.

Overrides:
attributes in class Node
Returns:
attributes (which implements iterable, in same order as presented in original HTML).

attr

public Node attr(String attributeKey,
                 String attributeValue)
Description copied from class: Node
Set an attribute (key=value). If the attribute already exists, it is replaced.

Overrides:
attr in class Node
Parameters:
attributeKey - The attribute key.
attributeValue - The attribute value.
Returns:
this (for chaining)

hasAttr

public boolean hasAttr(String attributeKey)
Description copied from class: Node
Test if this element has an attribute.

Overrides:
hasAttr in class Node
Parameters:
attributeKey - The attribute key to check.
Returns:
true if the attribute exists, false if not.

removeAttr

public Node removeAttr(String attributeKey)
Description copied from class: Node
Remove an attribute from this element.

Overrides:
removeAttr in class Node
Parameters:
attributeKey - The attribute to remove.
Returns:
this (for chaining)

absUrl

public String absUrl(String attributeKey)
Description copied from class: Node
Get an absolute URL from a URL attribute that may be relative (i.e. an <a href> or <img src>).

E.g.: String absUrl = linkEl.absUrl("href");

If the attribute value is already absolute (i.e. it starts with a protocol, like http:// or https:// etc), and it successfully parses as a URL, the attribute is returned directly. Otherwise, it is treated as a URL relative to the element's Node.baseUri, and made absolute using that.

As an alternate, you can use the Node.attr(java.lang.String) method with the abs: prefix, e.g.: String absUrl = linkEl.attr("abs:href");

Overrides:
absUrl in class Node
Parameters:
attributeKey - The attribute key
Returns:
An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or could not be made successfully into a URL.
See Also:
Node.attr(java.lang.String), URL.URL(java.net.URL, String)


Copyright © 2009-2011 Jonathan Hedley. All Rights Reserved.