/** * Adds the text value of a TEXT node to another TEXT node. * * @param node1 the AnyNode that receives the text value * @param node2 the AnyNode that needs to be merges with node1. */ private void mergeTextNode(AnyNode node1, AnyNode node2) { if (node1.getNodeType() != node2.getNodeType()) { return; } if (node1.getNodeType() != AnyNode.TEXT) { return; } node1._value = node1.getStringValue() + node2.getStringValue(); node2 = null; }
/** * Appends a sibling AnyNode to the current node. The node to append will be added at the end of * the sibling branch. * * @param node the node to add */ 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. * * @param node * the node to add */ 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); } }
/** * <p> * 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. * <p> * Note: you cannot add a child to a TEXT node. * * @param node the node to add. */ 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 the text value of a TEXT node to another * TEXT node. * @param node1 the AnyNode that receives the text value * @param node2 the AnyNode that needs to be merges with node1. */ private void mergeTextNode(AnyNode node1, AnyNode node2) { if (node1.getNodeType() != node2.getNodeType()) { return; } if (node1.getNodeType() != AnyNode.TEXT) { return; } StringBuffer temp = new StringBuffer(node1.getStringValue()); temp.append(node2.getStringValue()); node1._value = temp.toString(); node2 = null; }
/** * 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. * * @return the first child of this 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. * * @return the first child of this 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; }
/** * <p> * 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. * <p> * Note: you cannot add a child to a TEXT node. * * @param node * the node to add. */ 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); } }
/** * Returns the first namespace of the current ELEMENT node or null. The next * attribute if any is the sibling of the returned node. * @return the first namespace of the current ELEMENT node or null. */ 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 attribute of the current ELEMENT node or null. The next attribute,if any,is * the sibling of the returned node. */ 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; }
/** * Adds an attribute to the current node. * @param node the attribute to add. */ 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; } } } //addAttribute
/** * Returns the first namespace of the current ELEMENT node or null. The next attribute if any is * the sibling of the returned node. * * @return the first namespace of the current ELEMENT node or null. */ 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 attribute of the current ELEMENT node * or null. * The next attribute,if any,is the sibling of the returned node. */ 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; }
/** * Adds an attribute to the current node. * * @param node the attribute to add. */ 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; } } } // addAttribute
/** * Appends an namespace to the current node. * * @param node the attribute to add. */ 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; } } } // addNamespace
/** * Appends an namespace to the current node. * @param node the attribute to add. */ 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; } } } //addNamespace
/** * Adds an AnyNode to the current node * @param node the node to append */ 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 * * @param node the node to append */ 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; } }
/** * Returns the concatenation of all the TEXT nodes in the given * AnyNode in document order * * @param node the AnyNode to return the String value of * @param buffer the StringBuffer to append to. */ static final void getStringValue(AnyNode node, StringBuffer buffer) { switch(node.getNodeType()) { case AnyNode.ELEMENT: AnyNode child = node.getFirstChild(); while (child != null) { getStringValue(child, buffer); child = child.getNextSibling(); } break; case AnyNode.TEXT: buffer.append(node.getStringValue()); break; default: break; } } //-- getStringValue
/** * Returns the concatenation of all the TEXT nodes in the given AnyNode in document order * * @param node the AnyNode to return the String value of * @param buffer the StringBuffer to append to. */ static final void getStringValue(AnyNode node, StringBuilder buffer) { switch (node.getNodeType()) { case AnyNode.ELEMENT: AnyNode child = node.getFirstChild(); while (child != null) { getStringValue(child, buffer); child = child.getNextSibling(); } break; case AnyNode.TEXT: buffer.append(node.getStringValue()); break; default: break; } } // -- getStringValue