[Htmlparser-developer] Added methods to NodeList
Brought to you by:
derrickoswald
From: Matthew B. <mat...@ou...> - 2005-09-13 13:25:05
|
First can I thanks for htmlparser, it's really useful. I'm trying to remove a Tag that I am visiting (using a NodeVisitor) from its parent: public void visitTag( Tag tag ) { .... NodeList children = tag.getParent().getChildren(); for (int child = 0; child < children.size(); child++) { if (tag.equals(children.elementAt(child))) { children.remove(child); break; } } and would rather be able todo: public void visitTag( Tag tag ) { .... NodeList children = tag.getParent().getChildren(); children.remove(tag); Comments? I was also wondering if there was a reason why NodeList doesn't implement java.util.List as most Java programmers are already familiar with the semantics of it. I would have attached a patch but I don't seem to be able todo a a CVS diff against sourceforge anonymous CVS at the moment (timeouts) :-( -- Added code-- /** * Check to see if the NodeList contains the supplied Node. * @param node The node to look for. * @return True is the Node is in this NodeList. */ public boolean contains(Node node) { return indexOf(node) != -1; } /** * Finds the index of the supplied Node. * @param node The node to look for. * @return The index of the node in the list or -1 if it isn't found. */ public int indexOf(Node node) { for (int i=0;i<size;i++) { if (nodeData.equals(node)) return i; } return -1; } /** * Remove the supplied Node from the list. * @param node The node to remove. * @return True if the node was found and removed from the list. */ public boolean remove(Node node) { int index = indexOf(node); if (index != -1) { remove(index); return true; } return false; } -- +--Matthew Buckett-----------------------------------------+ | VLE Developer, Learning Technologies Group | | Tel: +44 (0) 1865 283660 http://www.oucs.ox.ac.uk/ | +------------Computing Services, University of Oxford------+ |