net.sourceforge.jsxe.dom
Class AdapterNode

java.lang.Object
  extended bynet.sourceforge.jsxe.dom.AdapterNode

public class AdapterNode
extends Object

The AdapterNode class is meant to provide extensions to the W3C Node interface by wrapping around existing nodes created after a document is parsed. It provides some extra event functionality and some methods for editing nodes in a DOM tree.

Because AdapterNodes are part of an XMLDocument they must be created by their owning XMLDocument object via the newAdapterNode methods

Version:
$Id: AdapterNode.java 803 2006-02-16 23:14:42Z ian_lewis $
Author:
Ian Lewis (IanLewis@member.fsf.org), Bilel Remmache (rbilel@users.sourceforge.net)
See Also:
XMLDocument, XMLDocument#addAdapterNode(AdapterNode, String, String, short)

Field Summary
static short ATTRIBUTE_NODE
           
static short CDATA_SECTION_NODE
           
static short COMMENT_NODE
           
static short DOCUMENT_FRAGMENT_NODE
           
static short DOCUMENT_NODE
           
static short DOCUMENT_TYPE_NODE
           
static short ELEMENT_NODE
           
static short ENTITY_NODE
           
static short ENTITY_REFERENCE_NODE
           
static short NOTATION_NODE
           
static short PROCESSING_INSTRUCTION_NODE
           
static short TEXT_NODE
           
 
Method Summary
 AdapterNode addAdapterNode(AdapterNode node)
          Adds an already existing AdapterNode to this node as a child.
 AdapterNode addAdapterNode(String name, String value, short type, int index)
          Adds a new child to this node given the node name, value, and type.
 AdapterNode addAdapterNodeAt(AdapterNode node, int location)
          Adds an already existing AdapterNode to this node at a specified location.
 void addAdapterNodeListener(AdapterNodeListener listener)
          Adds an AdapterNodeListener to be notified when this node changes
 AdapterNode child(int index)
          Gets the child node at the given index.
 int childCount()
          Gets the number of children that this node has.
 AdapterNode copy(boolean deep)
          Copies this node into a new AdapterNode.
 boolean equals(Object node)
          Compares this AdapterNode with another.
 void fireStructureChanged()
           
 List getAllowedElements()
          Gets a sorted list of all the elements (ElementDecl objects) allowed as children of this node as defined in the DTD or Schema.
 String getAttribute(String name)
          Gets the value of an attribute associated with this node.
 String getAttributeAt(int index)
          Gets the value of an attribute at the given index
 String getAttributeNameAt(int index)
          Gets the qualified name of an attribute at the given index
 NamedNodeMap getAttributes()
          Gets the attributes associated with this node.
 ElementDecl getElementDecl()
          Gets the Element declaration that defines this element
 String getLocalName()
          Gets the local name of this node.
 String getNodeName()
          Gets the full qualified name for this node including the local name and namespace prefix.
 short getNodeType()
          Gets the type of the node specified in the W3C Node interface.
 String getNodeValue()
          Gets the current value if this node.
 String getNSPrefix()
          Gets the namespace prefix for this node.
 XMLDocument getOwnerDocument()
          Gets the XMLDocument that owns this AdapterNode
 AdapterNode getParentNode()
          Gets the parent AdapterNode object.
 String getProperty(String key)
          Gets a property for the key given.
 String getProperty(String key, String defaultValue)
          Gets a property for the key given or returns the default value if there is no property for the given key.
 int index(AdapterNode child)
          Returns the index of the given AdapterNode if it is a child.
 void remove(AdapterNode child)
          Removes a child from this node.
 void removeAdapterNodeListener(AdapterNodeListener listener)
          Removes a listener from this node if it exists
 void removeAttribute(String attr)
          Removes an attribute by name.
 void removeAttributeAt(int index)
          Removes an attribute at the given index.
 String serializeToString()
          Serializes this Node to a string based on the last owning XMLDocument's properties.
 void setAttribute(String name, String value)
          Sets an attribute of this node.
 void setLocalName(String localName)
          Sets the local name of the node.
 void setNodeName(String qualifiedName)
          Sets the full qualified name of this node.
 void setNodeValue(String str)
          Sets the value of the node.
 void setNSPrefix(String prefix)
          Sets the namespace prefix for this node.
 String setProperty(String key, String value)
          Sets a property of the AdapterNode
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ATTRIBUTE_NODE

public static final short ATTRIBUTE_NODE
See Also:
Constant Field Values

CDATA_SECTION_NODE

public static final short CDATA_SECTION_NODE
See Also:
Constant Field Values

COMMENT_NODE

public static final short COMMENT_NODE
See Also:
Constant Field Values

DOCUMENT_FRAGMENT_NODE

public static final short DOCUMENT_FRAGMENT_NODE
See Also:
Constant Field Values

DOCUMENT_NODE

public static final short DOCUMENT_NODE
See Also:
Constant Field Values

DOCUMENT_TYPE_NODE

public static final short DOCUMENT_TYPE_NODE
See Also:
Constant Field Values

ELEMENT_NODE

public static final short ELEMENT_NODE
See Also:
Constant Field Values

ENTITY_NODE

public static final short ENTITY_NODE
See Also:
Constant Field Values

ENTITY_REFERENCE_NODE

public static final short ENTITY_REFERENCE_NODE
See Also:
Constant Field Values

NOTATION_NODE

public static final short NOTATION_NODE
See Also:
Constant Field Values

PROCESSING_INSTRUCTION_NODE

public static final short PROCESSING_INSTRUCTION_NODE
See Also:
Constant Field Values

TEXT_NODE

public static final short TEXT_NODE
See Also:
Constant Field Values
Method Detail

getOwnerDocument

public XMLDocument getOwnerDocument()
Gets the XMLDocument that owns this AdapterNode

Returns:
The owning XMLDocument

getProperty

public String getProperty(String key)
Gets a property for the key given.

Parameters:
key - the key to the properties list
Returns:
the value of the property for the given key.

getProperty

public String getProperty(String key,
                          String defaultValue)
Gets a property for the key given or returns the default value if there is no property for the given key.

Parameters:
key - the key to the properties list
defaultValue - the default value for the property requested
Returns:
the value of the property for the given key.

setProperty

public String setProperty(String key,
                          String value)
Sets a property of the AdapterNode

Parameters:
key - the key to the property
value - the value of the property
Returns:
the old value of the property

index

public int index(AdapterNode child)

Returns the index of the given AdapterNode if it is a child.

Parameters:
child - the child node of this node
Returns:
the index where the child is located. -1 if the AdapterNode is not a child

copy

public AdapterNode copy(boolean deep)
Copies this node into a new AdapterNode. The copy will have no parent node.

Parameters:
deep - if true then the subtree is copied as well.
Since:
jsXe 0.4 pre3

child

public AdapterNode child(int index)

Gets the child node at the given index.

Parameters:
index - the index of the requested node
Returns:
an AdapterNode representing the node at the given index, null if the index is out of bounds

childCount

public int childCount()

Gets the number of children that this node has.

Returns:
the number of children of this node

getNSPrefix

public String getNSPrefix()
Gets the namespace prefix for this node. If this node is not a member of a namespace then this method returns null.

Returns:
the namespace prefix for this node. null if no namespace

setNSPrefix

public void setNSPrefix(String prefix)
                 throws DOMException
Sets the namespace prefix for this node. To remove this node from a namespace this method should be passed null.

Parameters:
prefix - The new prefix for this node
Throws:
DOMException - if this namespace prefix is not valid. INVALID_CHARACTER_ERR: Raised if the specified prefix contains an illegal character, per the XML 1.0 specification . NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. NAMESPACE_ERR: Raised if the specified prefix is malformed per the Namespaces in XML specification, if the namespaceURI of this node is null, if the specified prefix is "xml" and the namespaceURI of this node is different from "http://www.w3.org/XML/1998/namespace", if this node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from " http://www.w3.org/2000/xmlns/", or if this node is an attribute and the qualifiedName of this node is "xmlns" .

getNodeName

public String getNodeName()
Gets the full qualified name for this node including the local name and namespace prefix.

Returns:
the full qualified name of this node

setNodeName

public void setNodeName(String qualifiedName)
                 throws DOMException
Sets the full qualified name of this node. This method is namespace aware

Parameters:
qualifiedName - the new qualified name
Throws:
DOMException

getLocalName

public String getLocalName()

Gets the local name of this node.

Returns:
the local name of the node

setLocalName

public void setLocalName(String localName)
                  throws DOMException

Sets the local name of the node.

Throws:
DOMException - INVALID_CHARACTER_ERR: Raised if the specified name contains an illegal character.

getNodeValue

public String getNodeValue()

Gets the current value if this node.

Returns:
the current value associated with this node

setNodeValue

public void setNodeValue(String str)
                  throws DOMException

Sets the value of the node.

Parameters:
str - the new value of the node
Throws:
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
DOMException - DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a DOMString variable on the implementation platform.

getNodeType

public short getNodeType()

Gets the type of the node specified in the W3C Node interface.

Returns:
the node type

getParentNode

public AdapterNode getParentNode()

Gets the parent AdapterNode object.

Returns:
the AdapterNode that is the parent of this node

getAttributes

public NamedNodeMap getAttributes()

Gets the attributes associated with this node.

Returns:
a map of the attributes associated with this node. null if this is not an element node

addAdapterNode

public AdapterNode addAdapterNode(String name,
                                  String value,
                                  short type,
                                  int index)
                           throws DOMException

Adds a new child to this node given the node name, value, and type.

Parameters:
name - the name of the new child node
value - the value of the new child node
type - the type of the new child node as specified by the W3C Node interface
Returns:
the new child that was created
Throws:
DOMException - INVALID_CHARACTER_ERR: Raised if the specified name or value contains an illegal character.
DOMException - NOT_SUPPORTED_ERR: Raised if the node type is not supported.
DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself.
DOMException - WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

addAdapterNode

public AdapterNode addAdapterNode(AdapterNode node)
                           throws DOMException
Adds an already existing AdapterNode to this node as a child. The node is added after all child nodes that this node contains.

Parameters:
node - the node to be added.
Returns:
a reference to the node that was added.
Throws:
DOMException - HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors or this node itself.
DOMException - WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous parent of the node being inserted is readonly.

addAdapterNodeAt

public AdapterNode addAdapterNodeAt(AdapterNode node,
                                    int location)
                             throws DOMException
Adds an already existing AdapterNode to this node at a specified location. The location is zero indexed so it can be any number greater than or equal to zero and less than or equal to the number of children contained currently. Using a location that is one index greater than the last child's index (location == childCount()) then the node is added at the end.

Parameters:
node - the node to add to this parent node.
location - the location to add it at.
Returns:
the node added.
Throws:
DOMException - if the addition of the node is not allowed or the location is invalid.

remove

public void remove(AdapterNode child)
            throws DOMException

Removes a child from this node.

Parameters:
child - the child node to remove from this node
Throws:
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
DOMException - NOT_FOUND_ERR: Raised if oldChild is not a child of this node.

setAttribute

public void setAttribute(String name,
                         String value)
                  throws DOMException

Sets an attribute of this node. If the specified attribute does not exist it is created.

Parameters:
name - the qualified name of the attribute
value - the new value of the attribute
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node
DOMException - INVALID_CHARACTER_ERR: Raised if the specified qualified name contains an illegal character, per the XML 1.0 specification
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly

getAttribute

public String getAttribute(String name)
                    throws DOMException

Gets the value of an attribute associated with this node.

Parameters:
name - the qualified name of the attribute
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node

removeAttributeAt

public void removeAttributeAt(int index)
                       throws DOMException

Removes an attribute at the given index.

Note: Attributes are sorted alphabetically.

Parameters:
index - the index of the node to remove
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly

removeAttribute

public void removeAttribute(String attr)
                     throws DOMException

Removes an attribute by name.

Parameters:
attr - the qualified name of the attribute to remove
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node
DOMException - NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly

getAttributeNameAt

public String getAttributeNameAt(int index)
                          throws DOMException

Gets the qualified name of an attribute at the given index

Note: Attributes are sorted alphabetically.

Parameters:
index - the index of the attribute to get
Returns:
the qualified name of the attribute at the index
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node

getAttributeAt

public String getAttributeAt(int index)
                      throws DOMException

Gets the value of an attribute at the given index

Note: Attributes are sorted alphabetically.

Parameters:
index - the index of the attribute to get
Returns:
the value of the attribute at the index
Throws:
DOMException - NOT_SUPPORTED_ERR: if this is not an element node

getAllowedElements

public List getAllowedElements()
Gets a sorted list of all the elements (ElementDecl objects) allowed as children of this node as defined in the DTD or Schema.

Returns:
a list of ElementDecl objects.
Since:
jsXe 0.4 pre1

getElementDecl

public ElementDecl getElementDecl()
Gets the Element declaration that defines this element


equals

public boolean equals(Object node)

Compares this AdapterNode with another.

Parameters:
node - the AdapterNode to compare to
Returns:
true if the underlying Node object for the two AdapterNodes is is the same.

addAdapterNodeListener

public void addAdapterNodeListener(AdapterNodeListener listener)

Adds an AdapterNodeListener to be notified when this node changes

Parameters:
listener - the listener to add

removeAdapterNodeListener

public void removeAdapterNodeListener(AdapterNodeListener listener)

Removes a listener from this node if it exists

Parameters:
listener - the listener to remove

serializeToString

public String serializeToString()
Serializes this Node to a string based on the last owning XMLDocument's properties.

Returns:
the string representation of this node.

toString

public String toString()

fireStructureChanged

public void fireStructureChanged()