/*
 * Copyright 2005 John G. Wilson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package groovy.util.slurpersupport;

import java.util.Iterator;
import java.util.Map;

import org.codehaus.groovy.runtime.InvokerHelper;

import groovy.lang.Closure;

/**
 * @author John Wilson
 *
 */

public class FilteredNodeChildren extends NodeChildren {
  private final Closure closure;
  
  public FilteredNodeChildren(final GPathResult parent, final Closure closure, final Map namespaceTagHints) {
    super(parent, parent.name, namespaceTagHints);
    this.closure = closure;
  }

  /* (non-Javadoc)
   * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#iterator()
   */
  public Iterator nodeIterator() {
    return new NodeIterator(this.parent.iterator()) {
              /* (non-Javadoc)
               * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeIterator#getNextNode(java.util.Iterator)
               */
              protected Object getNextNode(final Iterator iter) {
                while (iter.hasNext()) {
                final Object node = iter.next();
                
                  if (InvokerHelper.asBool(FilteredNodeChildren.this.closure.call(new Object[]{node}))) {
                    return node;
                  }
                }
                
                return null;
              }   
            };
  }
  
}

@@ -1084,16 +1084,767 @@
 e;%0A  %7D%0A%0A
+  /* (non-Javadoc)%0A   * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#iterator()%0A   */%0A  public Iterator iterator() %7B%0A    return new Iterator() %7B%0A      final Iterator iter = FilteredNodeChildren.this.parent.iterator();%0A      %0A      public boolean hasNext() %7B%0A        return this.iter.hasNext();%0A      %7D%0A      %0A      public Object next() %7B%0A        while (iter.hasNext()) %7B%0A        final Object childNode = iter.next();%0A      %0A      %0A          if (InvokerHelper.asBool(FilteredNodeChildren.this.closure.call(new Object%5B%5D%7BchildNode%7D))) %7B%0A            return childNode;%0A          %7D%0A        %7D%0A        %0A        return null;%0A      %7D%0A      %0A      public void remove() %7B%0A        throw new UnsupportedOperationException();%0A      %7D%0A    %7D;%0A  %7D%0A%0A
   /* (no
@@ -2009,33 +2009,37 @@
 tor(this.parent.
-i
+nodeI
 terator()) %7B%0A   
@@ -2468,19 +2468,119 @@
 ject%5B%5D%7Bn
-ode
+ew NodeChild((Node)node, FilteredNodeChildren.this.parent, FilteredNodeChildren.this.namespaceTagHints)
 %7D))) %7B%0A 

Fix the behavour of findAll on elements