org.openpipeline.pipeline.item
Class Node

java.lang.Object
  extended by org.openpipeline.pipeline.item.Node

public class Node
extends Object

A Node represents an element in a XML tree. A Node can be a tag or an attribute (name/value pair) within a tag. It can also have a block of text (a "value").

All node types can carry annotations, though it's most common to add them only to leaf nodes. An annotation is really just an arbitrary named object. It can be anything that should be attached to the node.

By convention, there are certain standard types of annotations. An object named "token" is a TokenList of the tokens in the text. An object named "skip" is a TokenList of skip tokens. (Skip tokens are spans of the text that should be skipped for text analytics purposes.)

Annotations can be helpful when passing an item from stage to stage; an earlier stage can attach some information to the node which can be consumed by a later stage. To attach information to the Item itself, attach it to the root node.


Constructor Summary
Node(Item item, Node parent)
           
 
Method Summary
 void addAttribute(Node attr)
           
 Node addAttribute(String name, String value)
           
 Node addNode(String name)
           
 Node addNode(String name, char[] array, int offset, int size)
           
 Node addNode(String name, String value)
          Add a node with the given name and value.
 void addValue(char[] array, int off, int size)
          Adds a text value to this node.
 void addValue(String text)
          Adds a text value to this node.
 void appendXMLtoBuffer(FastStringBuffer xmlBuf, boolean includeAnnotations)
          Append the contents of this object to the specified buffer in the form of a fragment of XML.
 TextValue findValue(String name)
          Search for a node with the specified name and return its value.
 Object getAnnotations(String name)
          Retrieve an object that was attached to this node by a call to putAnnotations().
 Map getAnnotationsMap()
          Return the object's internal map that maps names to annotations.
 int getAttributeCount()
           
 NodeList getAttributes()
           
 int getChildCount()
           
 NodeList getChildren()
           
 Item getItem()
           
 String getName()
           
 Node getParent()
           
 TextValue getValue()
          Return an underlying span of text that can be analyzed.
 boolean hasAttributes()
           
 boolean hasChildren()
           
 boolean isEmpty()
          Returns true if this node has no children, attributes, or text.
 void putAnnotations(String name, Object annotations)
          Attach a named object to this node.
 void remove()
          Remove this node from the parent.
 void setName(String name)
           
 void setParent(Node tag)
           
 void setValue(CharSpan text)
           
 void setValue(String text)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Node

public Node(Item item,
            Node parent)
Method Detail

addAttribute

public Node addAttribute(String name,
                         String value)

addAttribute

public void addAttribute(Node attr)

getAttributes

public NodeList getAttributes()

addNode

public Node addNode(String name)

addNode

public Node addNode(String name,
                    char[] array,
                    int offset,
                    int size)

addNode

public Node addNode(String name,
                    String value)
Add a node with the given name and value. The name can be null for a text-only node.

Parameters:
name - the name of the node. If not null, becomes a tag in the xml.
value - the text underneath the tag
Returns:
the node that was added

addValue

public void addValue(char[] array,
                     int off,
                     int size)
Adds a text value to this node. If the node already has a text value, then the existing value is demoted to a child, and the new one is also added as a child. If the node does not have an existing value, then this method behaves like setValue() and assigns it.

Parameters:
array - a char array that contains the text to add
off - the offset within the char array where the text to add starts
size - the number of chars to add

addValue

public void addValue(String text)
Adds a text value to this node. If the node already has a text value, then the existing value is demoted to a child, and the new one is also added as a child. If the node does not have an existing value, then this method behaves like setValue() and assigns it.

Parameters:
text - the text to add

appendXMLtoBuffer

public void appendXMLtoBuffer(FastStringBuffer xmlBuf,
                              boolean includeAnnotations)
Append the contents of this object to the specified buffer in the form of a fragment of XML.

Parameters:
xmlBuf - the buffer to receive the data

getAttributeCount

public int getAttributeCount()

hasAttributes

public boolean hasAttributes()

getAnnotations

public Object getAnnotations(String name)
Retrieve an object that was attached to this node by a call to putAnnotations().

Parameters:
name - the name of the object
Returns:
the object, or null if no object with this name is found

getAnnotationsMap

public Map getAnnotationsMap()
Return the object's internal map that maps names to annotations.

Returns:
a Map, or null if there are no annotations

getChildCount

public int getChildCount()

getChildren

public NodeList getChildren()

getItem

public Item getItem()

getParent

public Node getParent()

getValue

public TextValue getValue()
Return an underlying span of text that can be analyzed.

Returns:
the span of text

hasChildren

public boolean hasChildren()

putAnnotations

public void putAnnotations(String name,
                           Object annotations)
Attach a named object to this node.

Parameters:
name - the name of the object
annotations - any arbitrary object

setName

public void setName(String name)

setParent

public void setParent(Node tag)

setValue

public void setValue(String text)

setValue

public void setValue(CharSpan text)

getName

public String getName()

remove

public void remove()
Remove this node from the parent. Effectively deletes it from the item.


isEmpty

public boolean isEmpty()
Returns true if this node has no children, attributes, or text.

Returns:
true if this is an empty node

findValue

public TextValue findValue(String name)
Search for a node with the specified name and return its value. Does not return values found in text-only nodes; this method is only for finding values in nodes with both a name and text.

Parameters:
name - name of the node (tag) that contains the value
Returns:
the value, or null if not found