[Htmlparser-cvs] htmlparser/src/org/htmlparser/nodes AbstractNode.java,1.4,1.5
Brought to you by:
derrickoswald
|
From: Derrick O. <der...@us...> - 2005-10-26 22:01:36
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/nodes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11299/src/org/htmlparser/nodes Modified Files: AbstractNode.java Log Message: Incorporate patch #1338534 Support get first/last child, previous/next sibling from Ian Macfarlane. No unit tests. Index: AbstractNode.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/nodes/AbstractNode.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** AbstractNode.java 10 Apr 2005 23:20:44 -0000 1.4 --- AbstractNode.java 26 Oct 2005 22:01:23 -0000 1.5 *************** *** 273,276 **** --- 273,367 ---- this.children = children; } + + /** + * Get the first child of this node. + * @return The first child in the list of children contained by this node, + * <code>null</code> otherwise. + */ + public Node getFirstChild () + { + if (children == null) + return null; + if (children.size() == 0) + return null; + return children.elementAt(0); + } + + /** + * Get the last child of this node. + * @return The last child in the list of children contained by this node, + * <code>null</code> otherwise. + */ + public Node getLastChild () + { + if (children == null) + return null; + int numChildren = children.size(); + if (numChildren == 0) + return null; + return children.elementAt(numChildren - 1); + } + + /** + * Get the previous sibling to this node. + * @return The previous sibling to this node if one exists, + * <code>null</code> otherwise. + */ + public Node getPreviousSibling () + { + Node parentNode = this.getParent(); + if (parentNode == null)//root node + return null; + NodeList siblings = parentNode.getChildren(); + if (siblings == null)//this should actually be an error + return null; + int numSiblings = siblings.size(); + if (numSiblings < 2)//need at least one other node to have a chance of having any siblings + return null; + int positionInParent = -1; + for (int i = 0; i < numSiblings; i++) + { + if (siblings.elementAt(i) == this) + { + positionInParent = i; + break; + } + } + if (positionInParent < 1)//no previous siblings + return null; + return siblings.elementAt(positionInParent - 1); + } + + /** + * Get the next sibling to this node. + * @return The next sibling to this node if one exists, + * <code>null</code> otherwise. + */ + public Node getNextSibling () + { + Node parentNode = this.getParent(); + if (parentNode == null)//root node + return null; + NodeList siblings = parentNode.getChildren(); + if (siblings == null)//this should actually be an error + return null; + int numSiblings = siblings.size(); + if (numSiblings < 2)//need at least one other node to have a chance of having any siblings + return null; + int positionInParent = -1; + for (int i = 0; i < numSiblings; i++) + { + if (siblings.elementAt(i) == this) + { + positionInParent = i; + break; + } + } + if (positionInParent == -1)//this should actually be an error + return null; + if (positionInParent == (numSiblings - 1))//no next sibling + return null; + return siblings.elementAt(positionInParent + 1); + } /** |