com.googlecode.jinahya.el
Class ElementLocator<D>

java.lang.Object
  extended by com.googlecode.jinahya.el.ElementLocator<D>
Type Parameters:
D - document type parameter
Direct Known Subclasses:
DOMElementLocator, JDOMElementLocator, KDOMElementLocator, XOMElementLocator

public abstract class ElementLocator<D>
extends Object

Abstract element locator.

Author:
Jin Kwon

Constructor Summary
protected ElementLocator(ELElement root)
          Creates a new instance.
 
Method Summary
 ElementLocator<D> addChild(String localName)
          Adds a child element whose name is localName with no namespace and locate it.
 ElementLocator<D> addChild(String namespaceURI, String localName)
          Adds a child element whose name is localName with given namespaceURI and locate it.
 String getAttribute(String localName)
          Returns attribute value.
 String getAttribute(String namespaceURI, String localName)
          Returns the value of attribute which has name in space.
 int getChildCount(String localName)
          Returns the number of child elements with given localName with no namespace.
 int getChildCount(String namespaceURI, String localName)
          Returns the number of child elements of currently located element with the specified local name and name space URI.
protected  ELElement getCurrent()
          Returns currently located element.
 String getText()
          Returns text value of current element.
 String getText(boolean parent)
          Returns text value of current element and locate parent if specified.
 ElementLocator<D> locateChild(String localName, int index)
          Locates child element which has given localName with no namespace at index.
 ElementLocator<D> locateChild(String namespaceURI, String localName, int index)
          Locate a child with localName at index in namespaceURI.
 ElementLocator<D> locateParent()
          Locate to the parent of the current element.
 ElementLocator<D> locateRoot()
          Locate the root.
 void print(D document)
          Prints contents to given document.
protected abstract  void print(ELElement root, D document, Map<String,String> namespaceMap)
          Prints given root to specified document.
 void removeCurrent()
          Removes current element and locate parent.
 ElementLocator<D> setAttribute(String localName, String value)
          Sets attribute value.
 ElementLocator<D> setAttribute(String namespaceURI, String localName, String value)
          Sets the value of attribute which has localName in namespaceURI.
 ElementLocator<D> setText(String text)
          Remove all child elements and add given text value to the current element.
 ElementLocator<D> setText(String text, boolean parent)
          Remove all child elements and add given text value to the current element and locate parent if specified.
 String toJSON()
          Returns JSOM representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElementLocator

protected ElementLocator(ELElement root)
Creates a new instance.

Parameters:
root - root element
Method Detail

getChildCount

public final int getChildCount(String localName)
Returns the number of child elements with given localName with no namespace.

Parameters:
localName - local name
Returns:
number of children

getChildCount

public final int getChildCount(String namespaceURI,
                               String localName)
Returns the number of child elements of currently located element with the specified local name and name space URI.

Parameters:
namespaceURI - The name space URI.
localName - The local name.
Returns:
child count
See Also:
#getCount(String)

locateRoot

public final ElementLocator<D> locateRoot()
Locate the root.

Returns:
self

locateParent

public final ElementLocator<D> locateParent()
Locate to the parent of the current element. An IllegalStateException will be thrown if this locator is already on the root.

Returns:
self

locateChild

public final ElementLocator<D> locateChild(String localName,
                                           int index)
Locates child element which has given localName with no namespace at index.

Parameters:
localName - local name
index - index
Returns:
self

locateChild

public final ElementLocator<D> locateChild(String namespaceURI,
                                           String localName,
                                           int index)
Locate a child with localName at index in namespaceURI.

Parameters:
namespaceURI - element's name space URI
localName - element's local name
index - target index to locate
Returns:
self
See Also:
locateChild(String, int)

addChild

public final ElementLocator<D> addChild(String localName)
Adds a child element whose name is localName with no namespace and locate it.

Parameters:
localName - local name
Returns:
self

addChild

public final ElementLocator<D> addChild(String namespaceURI,
                                        String localName)
Adds a child element whose name is localName with given namespaceURI and locate it.

Parameters:
namespaceURI -
localName -
Returns:

getText

public final String getText()
Returns text value of current element.

Returns:
text value

getText

public final String getText(boolean parent)
Returns text value of current element and locate parent if specified.

Parameters:
parent - flag for locating parent.
Returns:
text value.

setText

public final ElementLocator<D> setText(String text)
Remove all child elements and add given text value to the current element.

Parameters:
text - text value; may be null
Returns:
self

setText

public final ElementLocator<D> setText(String text,
                                       boolean parent)
Remove all child elements and add given text value to the current element and locate parent if specified.

Parameters:
text - text value; may be null
parent - flag for locating parent
Returns:
self

getAttribute

public final String getAttribute(String localName)
Returns attribute value.

Parameters:
localName - attribute's local name
Returns:
attribute's value

getAttribute

public final String getAttribute(String namespaceURI,
                                 String localName)
Returns the value of attribute which has name in space.

Parameters:
namespaceURI - attribute's name space URI
localName - attribute's local name
Returns:
attribute's value

setAttribute

public final ElementLocator<D> setAttribute(String localName,
                                            String value)
Sets attribute value.

Parameters:
localName - local name
value - attribute value
Returns:
self

setAttribute

public final ElementLocator<D> setAttribute(String namespaceURI,
                                            String localName,
                                            String value)
Sets the value of attribute which has localName in namespaceURI.

Parameters:
namespaceURI - attribute's name space URI
localName - attribute's local name
value - attribute's value
Returns:
self

print

public final void print(D document)
Prints contents to given document.

Parameters:
document - output document

print

protected abstract void print(ELElement root,
                              D document,
                              Map<String,String> namespaceMap)
Prints given root to specified document.

Parameters:
root - root element
document - target document
namespaceMap - provided namespace URI/prefix map

removeCurrent

public final void removeCurrent()
Removes current element and locate parent. An IllegalStateException will be thrown if currently on the root.


getCurrent

protected final ELElement getCurrent()
Returns currently located element.

Returns:
current element

toJSON

public final String toJSON()
Returns JSOM representation.

Returns:
a JSON string.


Copyright © 2010-2011. All Rights Reserved.