    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    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  package org.apache.commons.jxpath.ri.model.dom;
    17
    18  import org.apache.commons.jxpath.ri.Compiler;
    19  import org.apache.commons.jxpath.ri.QName;
    20  import org.apache.commons.jxpath.ri.compiler.NodeTest;
    21  import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
    22  import org.apache.commons.jxpath.ri.model.NodePointer;
    23  import org.apache.commons.jxpath.util.TypeUtils;
    24  import org.w3c.dom.Attr;
    25
    26  /***
    27   * A Pointer that points to a DOM node.
    28   *
    29   * @author Dmitri Plotnikov
    30   * @version $Revision: 1.15 $ $Date: 2004/04/01 02:55:32 $
    31   */
    32  public class DOMAttributePointer extends NodePointer {
    33      private Attr attr;
    34
    35      public DOMAttributePointer(NodePointer parent, Attr attr) {
    36          super(parent);
    37          this.attr = attr;
    38      }
    39
    40      public QName getName() {
    41          return new QName(
    42              DOMNodePointer.getPrefix(attr),
    43              DOMNodePointer.getLocalName(attr));
    44      }
    45
    46      public String getNamespaceURI() {
    47          String prefix = DOMNodePointer.getPrefix(attr);
    48          if (prefix == null) {
    49              return null;
    50          }
    51          return parent.getNamespaceURI(prefix);
    52      }
    53
    54      public Object getValue() {
    55          String value = attr.getValue();
    56          if (value == null) {
    57              return null;
    58          }
    59          if (value.equals("") && !attr.getSpecified()) {
    60              return null;
    61          }
    62          return value;
    63      }
    64
    65      public Object getBaseValue() {
    66          return attr;
    67      }
    68
    69      public boolean isCollection() {
    70          return false;
    71      }
    72
    73      public int getLength() {
    74          return 1;
    75      }
    76
    77      public Object getImmediateNode() {
    78          return attr;
    79      }
    80
    81      public boolean isActual() {
    82          return true;
    83      }
    84
    85      public boolean isLeaf() {
    86          return true;
    87      }
    88
    89      public boolean testNode(NodeTest nodeTest) {
    90          return nodeTest == null
    91              || ((nodeTest instanceof NodeTypeTest)
    92                  && ((NodeTypeTest) nodeTest).getNodeType()
    93                      == Compiler.NODE_TYPE_NODE);
    94      }
    95
    96      /***
    97       * Sets the value of this attribute.
    98       */
    99      public void setValue(Object value) {
    100         attr.setValue((String) TypeUtils.convert(value, String.class));
    101     }
    102
    103     public void remove() {
    104         attr.getOwnerElement().removeAttributeNode(attr);
    105     }
    106
    107     /***
    108      */
    109     public String asPath() {
    110         StringBuffer buffer = new StringBuffer();
    111         if (parent != null) {
    112             buffer.append(parent.asPath());
    113             if (buffer.length() == 0
    114                 || buffer.charAt(buffer.length() - 1) != '/') {
    115                 buffer.append('/');
    116             }
    117         }
    118         buffer.append('@');
    119         buffer.append(getName());
    120         return buffer.toString();
    121     }
    122
    123     public int hashCode() {
    124         return System.identityHashCode(attr);
    125     }
    126
    127     public boolean equals(Object object) {
    128         if (object == this) {
    129             return true;
    130         }
    131
    132         if (!(object instanceof DOMAttributePointer)) {
    133             return false;
    134         }
    135
    136         DOMAttributePointer other = (DOMAttributePointer) object;
    137         return attr == other.attr;
    138     }
    139
    140     public int compareChildNodePointers(
    141         NodePointer pointer1,
    142         NodePointer pointer2)
    143     {
    144         // Won't happen - attributes don't have children
    145         return 0;
    146     }
    147 }