Re: [Htmlparser-developer] Added methods to NodeList
Brought to you by:
derrickoswald
From: Derrick O. <Der...@Ro...> - 2005-09-13 22:54:34
|
To answer your second question first, it's a legacy thing, trying to keep the base classes compatible with Java 1.x and avoiding the new Java Collections Framework. This can probably be revisited, since the goal of backward compatiblity has less emphasis these days. Removing nodes from an underlying collection while an iterator is active on it is fraught with peril. It might work in some cases (and I'm a little surprised it worked for you), but I think the better approach it to throw all the nodes to be deleted in a 'garbage bin' and remove them all later. Yes, the NodeList could use a remove(Node) call. You could add the patch to the Patches tracker (http://sourceforge.net/tracker/?group_id=24399&atid=381401) or Request For Enhancement tracker (http://sourceforge.net/tracker/?group_id=24399&atid=381402), but it's probably good enough in the mail list here. Matthew Buckett wrote: > 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; > } > |