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 groovy.lang.Closure;
24  
25  /***
26   * @author John Wilson
27   *
28   */
29  
30  public class FilteredNodeChildren extends NodeChildren {
31    private final Closure closure;
32    
33    public FilteredNodeChildren(final GPathResult parent, final Closure closure, final Map namespaceTagHints) {
34      super(parent, parent.name, namespaceTagHints);
35      this.closure = closure;
36    }
37  
38    /* (non-Javadoc)
39     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#iterator()
40     */
41    public Iterator nodeIterator() {
42      return new NodeIterator(this.parent.iterator()) {
43                /* (non-Javadoc)
44                 * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeIterator#getNextNode(java.util.Iterator)
45                 */
46                protected Object getNextNode(final Iterator iter) {
47                  while (iter.hasNext()) {
48                  final Object node = iter.next();
49                  final Boolean result = (Boolean)FilteredNodeChildren.this.closure.call(new Object[]{node});
50                  
51                    if (result != null && result.booleanValue()) {
52                      return node;
53                    }
54                  }
55                  
56                  return null;
57                }   
58              };
59    }
60    
61  }