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  import org.codehaus.groovy.runtime.InvokerHelper;
30  
31  /***
32   * @author John Wilson
33   *
34   */
35  
36  public class NodeChild extends GPathResult {
37    private final Node node;
38  
39    public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
40      super(parent, node.name(), namespacePrefix, namespaceTagHints);
41      this.node = node;
42    }
43  
44    public NodeChild(final Node node, final GPathResult parent, final Map namespaceTagHints) {
45      this(node, parent, "*", namespaceTagHints);
46    }
47  
48    /* (non-Javadoc)
49     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#size()
50     */
51    public int size() {
52      return 1;
53    }
54  
55    /* (non-Javadoc)
56     * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#text()
57     */
58    public String text() {
59      return this.node.text();
60    }
61  
62    /* (non-Javadoc)
63     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#parents()
64     */
65    public GPathResult parents() {
66      // TODO Auto-generated method stub
67      throw new GroovyRuntimeException("parents() not implemented yet");
68    }
69  
70    /* (non-Javadoc)
71     * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#iterator()
72     */
73    public Iterator iterator() {
74      return new Iterator() {
75        private boolean hasNext = true;
76        
77        public boolean hasNext() {
78          return this.hasNext;
79        }
80        
81        public Object next() {
82          try {
83            return (this.hasNext) ? NodeChild.this : null;
84          } finally {
85            this.hasNext = false;
86          }
87        }
88        
89        public void remove() {
90          throw new UnsupportedOperationException();
91        }
92      };
93    }
94  
95    /* (non-Javadoc)
96     * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#iterator()
97     */
98    public Iterator nodeIterator() {
99      return new Iterator() {
100       private boolean hasNext = true;
101       
102       public boolean hasNext() {
103         return this.hasNext;
104       }
105       
106       public Object next() {
107         try {
108           return (this.hasNext) ? NodeChild.this.node : null;
109         } finally {
110           this.hasNext = false;
111         }
112       }
113       
114       public void remove() {
115         throw new UnsupportedOperationException();
116       }
117     };
118   }
119 
120   /* (non-Javadoc)
121    * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#getAt(int)
122    */
123   public Object getAt(final int index) {
124     if (index == 0) {
125       return node;
126     } else {
127       throw new ArrayIndexOutOfBoundsException(index);
128     }
129   }
130   
131   public Map attributes() {
132       return this.node.attributes();
133   }
134   
135   public Iterator childNodes() {
136     return this.node.childNodes();
137   }
138   /* (non-Javadoc)
139    * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#find(groovy.lang.Closure)
140    */
141   public GPathResult find(final Closure closure) {
142     if (InvokerHelper.asBool(closure.call(new Object[]{this}))) {
143       return this;
144     } else {
145       return new NoChildren(this, "", this.namespaceTagHints);
146     }
147   }
148 
149   /* (non-Javadoc)
150    * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#findAll(groovy.lang.Closure)
151    */
152   public GPathResult findAll(final Closure closure) {
153     return find(closure);
154   }
155 
156   /* (non-Javadoc)
157    * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#build(groovy.lang.GroovyObject)
158    */
159   public void build(final GroovyObject builder) {
160     this.node.build(builder, this.namespaceMap, this.namespaceTagHints);
161   }
162 
163   /* (non-Javadoc)
164    * @see org.codehaus.groovy.sandbox.util.slurpersupport.Node#writeTo(java.io.Writer)
165    */
166   public Writer writeTo(final Writer out) throws IOException {
167     return this.node.writeTo(out);
168   }
169 }