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 groovy.lang.Closure;
21  import groovy.lang.GroovyObject;
22  import groovy.lang.GroovyRuntimeException;
23  
24  import java.io.IOException;
25  import java.io.Writer;
26  import java.util.Iterator;
27  import java.util.Map;
28  
29  /***
30   * @author John Wilson
31   *
32   */
33  
34  public class NodeChild extends GPathResult {
35    private final Node node;
36  
37    public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
38      super(parent, node.name(), namespacePrefix, namespaceTagHints);
39      this.node = node;
40    }
41  
42    public NodeChild(final Node node, final GPathResult parent, final Map namespaceTagHints) {
43      this(node, parent, "*", namespaceTagHints);
44    }
45  
46    /* (non-Javadoc)
47     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#size()
48     */
49    public int size() {
50      return 1;
51    }
52  
53    /* (non-Javadoc)
54     * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#text()
55     */
56    public String text() {
57      return this.node.text();
58    }
59  
60    /* (non-Javadoc)
61     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#parents()
62     */
63    public GPathResult parents() {
64      // TODO Auto-generated method stub
65      throw new GroovyRuntimeException("parents() not implemented yet");
66    }
67  
68    /* (non-Javadoc)
69     * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#iterator()
70     */
71    public Iterator iterator() {
72      return new Iterator() {
73        private boolean hasNext = true;
74        
75        public boolean hasNext() {
76          return this.hasNext;
77        }
78        
79        public Object next() {
80          try {
81            return (this.hasNext) ? NodeChild.this : null;
82          } finally {
83            this.hasNext = false;
84          }
85        }
86        
87        public void remove() {
88          throw new UnsupportedOperationException();
89        }
90      };
91    }
92  
93    /* (non-Javadoc)
94     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#iterator()
95     */
96    public Iterator nodeIterator() {
97      return new Iterator() {
98        private boolean hasNext = true;
99        
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 }