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 NoChildren extends GPathResult {
035      /**
036       * @param parent
037       * @param name
038       */
039      public NoChildren(final GPathResult parent, final String name, final Map namespaceTagHints) {
040        super(parent, name, "*", namespaceTagHints);
041      }
042    
043      /* (non-Javadoc)
044       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#size()
045       */
046      public int size() {
047        return 0;
048      }
049    
050      /* (non-Javadoc)
051       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#text()
052       */
053      public String text() {
054        return "";
055      }
056    
057      /* (non-Javadoc)
058       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#parents()
059       */
060      public GPathResult parents() {
061        // TODO Auto-generated method stub
062        throw new GroovyRuntimeException("parents() not implemented yet");
063      }
064    
065      /* (non-Javadoc)
066       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#childNodes()
067       */
068      public Iterator childNodes() {
069        return iterator();
070      }
071    
072      /* (non-Javadoc)
073       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#iterator()
074       */
075      public Iterator iterator() {
076        return new Iterator() {
077          public boolean hasNext() {
078            return false;
079          }
080          
081          public Object next() {
082            return null;
083          }
084          
085          public void remove() {
086            throw new UnsupportedOperationException();
087          }
088        };
089      }
090    
091      /* (non-Javadoc)
092       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#find(groovy.lang.Closure)
093       */
094      public GPathResult find(final Closure closure) {
095        return this;
096      }
097    
098      /* (non-Javadoc)
099       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#findAll(groovy.lang.Closure)
100       */
101      public GPathResult findAll(final Closure closure) {
102        return this;
103      }
104    
105      /* (non-Javadoc)
106       * @see org.codehaus.groovy.sandbox.util.slurpersupport.GPathResult#nodeIterator()
107       */
108      public Iterator nodeIterator() {
109        return iterator();
110      }
111    
112      /* (non-Javadoc)
113       * @see groovy.lang.Writable#writeTo(java.io.Writer)
114       */
115      public Writer writeTo(final Writer out) throws IOException {
116        return out;
117      }
118    
119      /* (non-Javadoc)
120       * @see org.codehaus.groovy.sandbox.markup.Buildable#build(groovy.lang.GroovyObject)
121       */
122      public void build(final GroovyObject builder) {
123      }
124    
125      protected void replaceNode(final Closure newValue) {
126        // No elements match GPath expression - do nothing
127      }
128    
129      protected void replaceBody(final Object newValue) {
130        // No elements match GPath expression - do nothing   
131      }
132    
133      protected void appendNode(final Object newValue) {
134        // TODO consider creating an element for this
135      }
136    }