01: /*
02: * Copyright 2005 John G. Wilson
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package groovy.util.slurpersupport;
18:
19: import java.util.Iterator;
20: import java.util.Map;
21:
22: import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
23:
24: import groovy.lang.Closure;
25:
26: /**
27: * @author John Wilson
28: */
29:
30: public class FilteredNodeChildren extends NodeChildren {
31: private final Closure closure;
32:
33: public FilteredNodeChildren(final GPathResult parent,
34: final Closure closure, final Map namespaceTagHints) {
35: super (parent, parent.name, namespaceTagHints);
36: this .closure = closure;
37: }
38:
39: public Iterator iterator() {
40: return new Iterator() {
41: final Iterator iter = FilteredNodeChildren.this .parent
42: .iterator();
43: Object next = null;
44:
45: public boolean hasNext() {
46: while (this .iter.hasNext()) {
47: final Object childNode = this .iter.next();
48:
49: if (closureYieldsTrueForNode(childNode)) {
50: this .next = childNode;
51: return true;
52: }
53: }
54:
55: return false;
56: }
57:
58: public Object next() {
59: return this .next;
60: }
61:
62: public void remove() {
63: throw new UnsupportedOperationException();
64: }
65: };
66: }
67:
68: public Iterator nodeIterator() {
69: return new NodeIterator(this .parent.nodeIterator()) {
70: protected Object getNextNode(final Iterator iter) {
71: while (iter.hasNext()) {
72: final Object node = iter.next();
73:
74: if (closureYieldsTrueForNode(new NodeChild(
75: (Node) node,
76: FilteredNodeChildren.this .parent,
77: FilteredNodeChildren.this .namespaceTagHints))) {
78: return node;
79: }
80: }
81: return null;
82: }
83: };
84: }
85:
86: private boolean closureYieldsTrueForNode(Object childNode) {
87: return DefaultTypeTransformation
88: .castToBoolean(FilteredNodeChildren.this .closure
89: .call(new Object[] { childNode }));
90: }
91:
92: }
|