Method from org.exolab.castor.types.AnyNode Detail: |
public void addAnyNode(AnyNode node) {
if (node == null) {
throw new IllegalArgumentException("null argument in addAnyNode");
}
switch(node.getNodeType()) {
case ATTRIBUTE:
addAttribute(node);
break;
case NAMESPACE:
addNamespace(node);
break;
default:
addChild(node);
break;
}
}
Adds an AnyNode to the current node |
public void addAttribute(AnyNode node) {
if (node == null) {
throw new IllegalArgumentException("null argument in addAttribute");
}
if (node.getNodeType() != ATTRIBUTE) {
throw new IllegalArgumentException("Only attribute can be added as an attribute");
}
if (_firstChildNode == null) {
_firstChildNode = node;
} else {
if (_firstChildNode.getNodeType() == ATTRIBUTE) {
//if we reach an attribute then we add the node as its sibling
_firstChildNode.appendSibling(node);
} else if (_firstChildNode.getNodeType() == NAMESPACE) {
//if we reach an namespace the attributre should be added to
//the first child of the namespace
_firstChildNode.addAttribute(node);
} else {
//unplug the current firstNode to add a new one
node.addChild(_firstChildNode);
_firstChildNode = node;
}
}
}
Adds an attribute to the current node. |
public void addChild(AnyNode node) {
if (node == null) {
throw new IllegalArgumentException("null argument in appendChild");
}
if (node.getNodeType() == ATTRIBUTE || node.getNodeType() == NAMESPACE) {
throw new IllegalArgumentException("An Attribute or an Namespace can't be added as a child");
}
if (this.getNodeType() == TEXT) {
throw new IllegalArgumentException("a TEXT node can't have children.");
}
if (_firstChildNode == null) {
_firstChildNode = node;
} else if (_firstChildNode.getNodeType() == ATTRIBUTE || _firstChildNode.getNodeType() == NAMESPACE) {
_firstChildNode.addChild(node);
} else {
_firstChildNode.appendSibling(node);
}
}
Adds a child AnyNode to this node. A 'child' can be either an ELEMENT
node, a COMMENT node, a TEXT node or a PROCESSING INSTRUCTION. If the
current node already has a child then the node to add will be append as a
sibling.
Note: you cannot add a child to a TEXT node.
|
public void addNamespace(AnyNode node) {
if (node == null) {
throw new IllegalArgumentException("null argument in addNamespace");
}
if (node.getNodeType() != NAMESPACE) {
throw new IllegalArgumentException("Only namespace can be added as an namespace");
}
if (_firstChildNode == null) {
_firstChildNode = node;
} else {
if (_firstChildNode.getNodeType() == NAMESPACE) {
//if we reach an namepace then we add the node as its sibling
_firstChildNode.appendSibling(node);
} else if (_firstChildNode.getNodeType() == ATTRIBUTE) {
//if we reach an attribute the attributre should be added to
//the first child of the attribute
_firstChildNode.addNamespace(node);
} else {
//unplug the current firstNode to add a new one
node.addChild(_firstChildNode);
_firstChildNode = node;
}
}
}
Appends an namespace to the current node. |
protected void appendSibling(AnyNode node) {
if (node == null) {
throw new IllegalArgumentException();
}
if ( ((node.getNodeType() == ATTRIBUTE) || (node.getNodeType()== NAMESPACE)) &&
(this.getNodeType() != node.getNodeType())) {
String err = "a NAMESPACE or an ATTRIBUTE can only be add as a sibling to a node of the same type";
throw new UnsupportedOperationException(err);
}
if (_nextSiblingNode == null) {
//if we already have a TEXT node - > merge
if ((node.getNodeType() == TEXT) && (this.getNodeType() == TEXT)) {
mergeTextNode(this,node);
} else {
_nextSiblingNode = node;
}
} else {
_nextSiblingNode.appendSibling(node);
}
}
Appends a sibling AnyNode to the current node. The node to append will be
added at the end of the sibling branch. |
public AnyNode getFirstAttribute() {
if (this.getNodeType() != ELEMENT) {
String err = "This node type can not contain attributes";
throw new UnsupportedOperationException(err);
}
boolean found = false;
AnyNode tempNode = this.getFirstChildNode();
while (tempNode != null && !found) {
short type = tempNode.getNodeType();
//if the child is not an attribute or a namespace
//this element does not have any attribute
if (type == ELEMENT || type == COMMENT || type == TEXT || type == PI) {
tempNode = null;
} else if (type == NAMESPACE) {
tempNode = tempNode.getFirstChildNode();
} else {
found = true;
}
}
return tempNode;
}
Returns the first attribute of the current ELEMENT node
or null.
The next attribute,if any,is the sibling of the returned node. |
public AnyNode getFirstChild() {
//an ATTRIBUTE or a NAMESPACE can not
//have children
if (this.getNodeType() == ATTRIBUTE || this.getNodeType() == NAMESPACE) {
return null;
}
//loop througth the first two (in the worst case) nodes
//and then return the firstChild if any
AnyNode tempNode = this.getFirstChildNode();
boolean found = false;
while (tempNode != null && !found) {
short type = tempNode.getNodeType();
if (type == ELEMENT || type == COMMENT || type == TEXT || type == PI) {
found = true;
} else if (type == ATTRIBUTE || type == NAMESPACE) {
tempNode = tempNode.getFirstChildNode();
}
}
return tempNode;
}
Returns the first Child node of this node. A 'child' can be either an
ELEMENT node, a COMMENT node, a TEXT node or a PROCESSING INSTRUCTION. |
protected AnyNode getFirstChildNode() {
return _firstChildNode;
}
Returns the first child node in the tree. |
public AnyNode getFirstNamespace() {
if (this.getNodeType() != ELEMENT) {
String err = "This node type can not contain namespaces";
throw new UnsupportedOperationException(err);
}
AnyNode tempNode = this.getFirstChildNode();
boolean found = false;
while (tempNode != null && !found) {
short type = tempNode.getNodeType();
//if the child is not an attribute or a namespace
//this element does not have any namespace
if (type == ELEMENT || type == COMMENT || type == TEXT || type == PI) {
tempNode = null;
} else if (type == ATTRIBUTE) {
tempNode = tempNode.getFirstChildNode();
} else {
found = true;
}
}
return tempNode;
}
Returns the first namespace of the current ELEMENT node or null. The next
attribute if any is the sibling of the returned node. |
public String getLocalName() {
return _localName;
}
Returns the local name of the node. Returns the local name of an element
or attribute, the prefix of a namespace node, the target of a processing
instruction, or null for all other node types. |
public String getNamespacePrefix() {
return _prefix;
}
Returns the namespace prefix associated with the namespace URI of this
node. Returns null if no prefix. is defined for this namespace URI.
Returns an empty string if the default prefix is associated with this
namespace URI. |
public String getNamespaceURI() {
return _uri;
}
Returns the namespace URI of the node. Returns the namespace URI of an
element, attribute or namespace node, or null for all other node types. |
public AnyNode getNextSibling() {
return _nextSiblingNode;
}
Returns the next sibling of the current node. When the AnyNode is an
ATTRIBUTE, it will return the next ATTRIBUTE node. When the AnyNode is a
NAMESPACE, it will return the next NAMESPACE node. |
public short getNodeType() {
return _nodeType;
}
Returns the type of this node. |
public String getStringValue() {
switch(_nodeType) {
case ATTRIBUTE:
case TEXT:
return _value;
case NAMESPACE:
return _uri;
//not yet supported
case PI:
return "";
case COMMENT:
return _value;
case ELEMENT:
StringBuffer result = new StringBuffer(4096);
AnyNode tempNode = this.getNextSibling();
while (tempNode != null && tempNode.getNodeType() == TEXT) {
result.append(tempNode.getStringValue());
tempNode = tempNode.getNextSibling();
}
tempNode = this.getFirstChild();
while (tempNode != null) {
result.append(tempNode.getStringValue());
tempNode = tempNode.getNextSibling();
}
tempNode = null;
return result.toString();
default:
return null;
}
}
Returns the string value of the node. The string value of a text node or
an attribute node is its text value. The string value of an element or a
root node is the concatenation of the string value of all its child
nodes. The string value of a namespace node is its namespace URI. The
string value of a processing instruction is the instruction, and the
string value of a comment is the comment text. |
public String toString() {
Serializer serializer = new BackwardCompatibilityContext().getSerializer();
if (serializer == null) {
throw new RuntimeException("Unable to obtain serializer");
}
StringWriter writer = new StringWriter();
serializer.setOutputCharStream(writer);
try {
AnyNode2SAX.fireEvents(this, serializer.asDocumentHandler());
} catch (java.io.IOException ioe) {
return privateToString();
} catch (org.xml.sax.SAXException saxe) {
throw new RuntimeException(saxe.getMessage());
}
return writer.toString();
}
Returns the String representation of this AnyNode. The String
representation is a xml well-formed fragment corresponding to the
representation of this node. |