/** * Remove nodes not matching the given filter. * * @param filter * The filter to use. * @param recursive * If <code>true<code> digs into the children recursively. */ public void keepAllNodesThatMatch(NodeFilter filter, boolean recursive) { Node node; NodeList children; for (int i = 0; i < size; ) { node = nodeData[i]; if (!filter.accept(node)) remove(i); else { if (recursive) { children = node.getChildren(); if (null != children) children.keepAllNodesThatMatch(filter, recursive); } i++; } } }
/** * Filter the list with the given filter. * * @param filter * The filter to use. * @param recursive * If <code>true<code> digs into the children recursively. * @return A new node array containing the nodes accepted by the filter. * This is a linear list and preserves the nested structure of the * returned nodes only. */ public NodeList extractAllNodesThatMatch(NodeFilter filter, boolean recursive) { Node node; NodeList children; NodeList ret; ret = new NodeList(); for (int i = 0; i < size; i++) { node = nodeData[i]; if (filter.accept(node)) ret.add(node); if (recursive) { children = node.getChildren(); if (null != children) ret.add(children.extractAllNodesThatMatch(filter, recursive)); } } return (ret); }