001 /*
002 /*
003 * Copyright 2005 John G. Wilson
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018
019 package groovy.util.slurpersupport;
020
021 import java.util.Iterator;
022 import java.util.Map;
023
024 import groovy.lang.Closure;
025
026 /**
027 * @author John Wilson
028 *
029 */
030
031 public class FilteredAttributes extends Attributes {
032 private final Closure closure;
033
034 public FilteredAttributes(final GPathResult parent, final Closure closure, final Map namespaceTagHints) {
035 super(parent, parent.name, namespaceTagHints);
036 this.closure = closure;
037 }
038
039 /* (non-Javadoc)
040 * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#iterator()
041 */
042 public Iterator nodeIterator() {
043 return new NodeIterator(this.parent.iterator()) {
044 /* (non-Javadoc)
045 * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeIterator#getNextNode(java.util.Iterator)
046 */
047 protected Object getNextNode(final Iterator iter) {
048 while (iter.hasNext()) {
049 final Object node = iter.next();
050 final Boolean result = (Boolean)FilteredAttributes.this.closure.call(new Object[]{node});
051
052 if (result != null && result.booleanValue()) {
053 return node;
054 }
055 }
056
057 return null;
058 }
059 };
060 }
061
062 }