001    /*
002     * Copyright 2005 John G. Wilson
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     *
016     */
017    
018    package groovy.util.slurpersupport;
019    
020    import groovy.lang.Closure;
021    import groovy.lang.GroovyObject;
022    import groovy.lang.GroovyRuntimeException;
023    
024    import java.io.IOException;
025    import java.io.Writer;
026    import java.util.Iterator;
027    import java.util.Map;
028    
029    /**
030     * @author John Wilson
031     *
032     */
033    
034    public class NodeChild extends GPathResult {
035      private final Node node;
036    
037      public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
038        super(parent, node.name(), namespacePrefix, namespaceTagHints);
039        this.node = node;
040      }
041    
042      public NodeChild(final Node node, final GPathResult parent, final Map namespaceTagHints) {
043        this(node, parent, "*", namespaceTagHints);
044      }
045    
046      /* (non-Javadoc)
047       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#size()
048       */
049      public int size() {
050        return 1;
051      }
052    
053      /* (non-Javadoc)
054       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#text()
055       */
056      public String text() {
057        return this.node.text();
058      }
059    
060      /* (non-Javadoc)
061       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#parents()
062       */
063      public GPathResult parents() {
064        // TODO Auto-generated method stub
065        throw new GroovyRuntimeException("parents() not implemented yet");
066      }
067    
068      /* (non-Javadoc)
069       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#iterator()
070       */
071      public Iterator iterator() {
072        return new Iterator() {
073          private boolean hasNext = true;
074          
075          public boolean hasNext() {
076            return this.hasNext;
077          }
078          
079          public Object next() {
080            try {
081              return (this.hasNext) ? NodeChild.this : null;
082            } finally {
083              this.hasNext = false;
084            }
085          }
086          
087          public void remove() {
088            throw new UnsupportedOperationException();
089          }
090        };
091      }
092    
093      /* (non-Javadoc)
094       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#iterator()
095       */
096      public Iterator nodeIterator() {
097        return new Iterator() {
098          private boolean hasNext = true;
099          
100          public boolean hasNext() {
101            return this.hasNext;
102          }
103          
104          public Object next() {
105            try {
106              return (this.hasNext) ? NodeChild.this.node : null;
107            } finally {
108              this.hasNext = false;
109            }
110          }
111          
112          public void remove() {
113            throw new UnsupportedOperationException();
114          }
115        };
116      }
117    
118      /* (non-Javadoc)
119       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#getAt(int)
120       */
121      public Object getAt(final int index) {
122        if (index == 0) {
123          return node;
124        } else {
125          throw new ArrayIndexOutOfBoundsException(index);
126        }
127      }
128      public Iterator childNodes() {
129        return this.node.childNodes();
130      }
131      /* (non-Javadoc)
132       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#find(groovy.lang.Closure)
133       */
134      public GPathResult find(final Closure closure) {
135        if (((Boolean)closure.call(new Object[]{this})).booleanValue()) {
136          return this;
137        } else {
138          return null;
139        }
140      }
141    
142      /* (non-Javadoc)
143       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#findAll(groovy.lang.Closure)
144       */
145      public GPathResult findAll(final Closure closure) {
146        return find(closure);
147      }
148    
149      /* (non-Javadoc)
150       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#build(groovy.lang.GroovyObject)
151       */
152      public void build(final GroovyObject builder) {
153        this.node.build(builder, this.namespaceMap, this.namespaceTagHints);
154      }
155    
156      /* (non-Javadoc)
157       * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#writeTo(java.io.Writer)
158       */
159      public Writer writeTo(final Writer out) throws IOException {
160        return this.node.writeTo(out);
161      }
162    }