View Javadoc

    1   /*
    2    * Created on Jul 15, 2006
    3    *
    4    * Copyright 2006 John G. Wilson
    5    *
    6    *   Licensed under the Apache License, Version 2.0 (the "License");
    7    *   you may not use this file except in compliance with the License.
    8    *   You may obtain a copy of the License at
    9    *
    10   *       http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *   Unless required by applicable law or agreed to in writing, software
    13   *   distributed under the License is distributed on an "AS IS" BASIS,
    14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   *   See the License for the specific language governing permissions and
    16   *   limitations under the License.
    17   *
    18   */
    19  package groovy.util.slurpersupport;
    20
    21  import groovy.lang.Closure;
    22  import groovy.lang.GroovyObject;
    23  import groovy.lang.GroovyRuntimeException;
    24
    25  import java.io.IOException;
    26  import java.io.Writer;
    27  import java.util.Iterator;
    28  import java.util.Map;
    29
    30  import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
    31
    32  public class Attribute extends GPathResult {
    33      private final String value;
    34
    35      public Attribute(final String name, final String value, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
    36        super(parent, name, namespacePrefix, namespaceTagHints);
    37        this.value = value;
    38      }
    39
    40      public String name() {
    41          // this name contains @name we need to return name
    42          return this.name.substring(1);
    43      }
    44
    45      public int size() {
    46          return 1;
    47      }
    48
    49      public String text() {
    50           return this.value;
    51      }
    52
    53      public GPathResult parents() {
    54          // TODO Auto-generated method stub
    55          throw new GroovyRuntimeException("parents() not implemented yet");
    56      }
    57
    58      public Iterator childNodes() {
    59          throw new GroovyRuntimeException("can't call childNodes() in the attribute " + this.name);
    60      }
    61
    62      public Iterator iterator() {
    63          return nodeIterator();
    64      }
    65
    66      public GPathResult find(final Closure closure) {
    67          if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{this}))) {
    68              return this;
    69            } else {
    70              return new NoChildren(this, "", this.namespaceTagHints);
    71            }
    72      }
    73
    74      public GPathResult findAll(final Closure closure) {
    75          return find(closure);
    76      }
    77
    78      public Iterator nodeIterator() {
    79          return new Iterator() {
    80              private boolean hasNext = true;
    81
    82              public boolean hasNext() {
    83                return this.hasNext;
    84              }
    85
    86              public Object next() {
    87                try {
    88                  return (this.hasNext) ? Attribute.this : null;
    89                } finally {
    90                  this.hasNext = false;
    91                }
    92              }
    93
    94              public void remove() {
    95                throw new UnsupportedOperationException();
    96              }
    97          };
    98      }
    99
    100     public Writer writeTo(final Writer out) throws IOException {
    101         out.write(this.value);
    102         return out;
    103     }
    104
    105     public void build(final GroovyObject builder) {
    106         builder.getProperty("mkp");
    107         builder.invokeMethod("yield", new Object[]{this.value});
    108     }
    109
    110     protected void replaceNode(final Closure newValue) {
    111     }
    112
    113     protected void replaceBody(final Object newValue) {
    114     }
    115
    116     protected void appendNode(final Object newValue) {
    117     }
    118 }