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  class Attributes extends NodeChildren {
    35    final String attributeName;
    36
    37    public Attributes(final GPathResult parent, final String name, final String namespacePrefix, final Map namespaceTagHints) {
    38      super(parent, name, namespacePrefix, namespaceTagHints);
    39
    40      this.attributeName = this.name.substring(1);
    41    }
    42
    43    public Attributes(final GPathResult parent, final String name, final Map namespaceTagHints) {
    44      this(parent, name, "*", namespaceTagHints);
    45    }
    46
    47    /* (non-Javadoc)
    48     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#childNodes()
    49     */
    50    public Iterator childNodes() {
    51      throw new GroovyRuntimeException("Can't get the child nodes on a a GPath expression selecting attributes: ...." + this.parent.name() + "." + name() + ".childNodes()");
    52    }
    53
    54    /* (non-Javadoc)
    55     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#iterator()
    56     */
    57    public Iterator iterator() {
    58      return new NodeIterator(this.parent.nodeIterator()) {
    59                    /* (non-Javadoc)
    60                     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeIterator#getNextNode(java.util.Iterator)
    61                     */
    62                    protected Object getNextNode(final Iterator iter) {
    63                      while (iter.hasNext()) {
    64                        final Object value = ((Node)iter.next()).attributes().get(Attributes.this.attributeName);
    65
    66                          if (value != null) {
    67                            return value;
    68                          }
    69                      }
    70
    71                      return null;
    72                    }
    73                  };
    74    }
    75
    76    /* (non-Javadoc)
    77     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#parents()
    78     */
    79    public GPathResult parents() {
    80      return super.parents();
    81    }
    82
    83    /* (non-Javadoc)
    84     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#text()
    85     */
    86    public String text() {
    87    final StringBuffer buf = new StringBuffer();
    88    final Iterator iter = iterator();
    89
    90      while (iter.hasNext()) {
    91        buf.append(iter.next());
    92      }
    93
    94      return buf.toString();
    95    }
    96
    97    /* (non-Javadoc)
    98     * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#find(groovy.lang.Closure)
    99     */
    100   public GPathResult find(final Closure closure) {
    101     return new FilteredAttributes(this, closure, this.namespaceTagHints);
    102   }
    103
    104   /* (non-Javadoc)
    105    * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#writeTo(java.io.Writer)
    106    */
    107   public Writer writeTo(final Writer out) throws IOException {
    108     out.write(text());
    109
    110     return out;
    111   }
    112
    113   /* (non-Javadoc)
    114    * @see org.codehaus.groovy.sandbox.util.slurpersupport.NodeChildren#build(groovy.lang.GroovyObject)
    115    */
    116   public void build(final GroovyObject builder) {
    117     builder.getProperty("mkp");
    118     builder.invokeMethod("yield", new Object[]{text()});
    119   }
    120 }