View Javadoc

1   /*
2    * Copyright 2005 John G. Wilson
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package groovy.util.slurpersupport;
19  
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.codehaus.groovy.runtime.InvokerHelper;
24  
25  import groovy.lang.Closure;
26  
27  /***
28   * @author John Wilson
29   *
30   */
31  
32  public class FilteredNodeChildren extends NodeChildren {
33    private final Closure closure;
34    
35    public FilteredNodeChildren(final GPathResult parent, final Closure closure, final Map namespaceTagHints) {
36      super(parent, parent.name, namespaceTagHints);
37      this.closure = closure;
38    }
39  
40    /* (non-Javadoc)
41     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#iterator()
42     */
43    public Iterator nodeIterator() {
44      return new NodeIterator(this.parent.iterator()) {
45                /* (non-Javadoc)
46                 * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeIterator#getNextNode(java.util.Iterator)
47                 */
48                protected Object getNextNode(final Iterator iter) {
49                  while (iter.hasNext()) {
50                  final Object node = iter.next();
51                  
52                    if (InvokerHelper.asBool(FilteredNodeChildren.this.closure.call(new Object[]{node}))) {
53                      return node;
54                    }
55                  }
56                  
57                  return null;
58                }   
59              };
60    }
61    
62  }