    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.jdom;
    17
    18  import org.apache.commons.jxpath.ri.QName;
    19  import org.apache.commons.jxpath.ri.model.NodePointer;
    20
    21  /***
    22   * Represents a namespace node.
    23   *
    24   * @author Dmitri Plotnikov
    25   * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:31 $
    26   */
    27  public class JDOMNamespacePointer extends NodePointer {
    28      private String prefix;
    29      private String namespaceURI;
    30
    31      public JDOMNamespacePointer(NodePointer parent, String prefix) {
    32          super(parent);
    33          this.prefix = prefix;
    34      }
    35
    36      public JDOMNamespacePointer(
    37              NodePointer parent,
    38              String prefix,
    39              String namespaceURI)
    40      {
    41          super(parent);
    42          this.prefix = prefix;
    43          this.namespaceURI = namespaceURI;
    44      }
    45
    46      public QName getName() {
    47          return new QName(prefix);
    48      }
    49
    50      public Object getBaseValue() {
    51          return null;
    52      }
    53
    54      public boolean isCollection() {
    55          return false;
    56      }
    57
    58      public int getLength() {
    59          return 1;
    60      }
    61
    62      public Object getImmediateNode() {
    63          return getNamespaceURI();
    64      }
    65
    66      public String getNamespaceURI() {
    67          if (namespaceURI == null) {
    68              namespaceURI = parent.getNamespaceURI(prefix);
    69          }
    70          return namespaceURI;
    71      }
    72
    73      public boolean isLeaf() {
    74          return true;
    75      }
    76
    77      /***
    78       * Throws UnsupportedOperationException.
    79       */
    80      public void setValue(Object value) {
    81          throw new UnsupportedOperationException("Cannot modify a namespace");
    82      }
    83
    84      public String asPath() {
    85          StringBuffer buffer = new StringBuffer();
    86          if (parent != null) {
    87              buffer.append(parent.asPath());
    88              if (buffer.length() == 0
    89                  || buffer.charAt(buffer.length() - 1) != '/') {
    90                  buffer.append('/');
    91              }
    92          }
    93          buffer.append("namespace::");
    94          buffer.append(prefix);
    95          return buffer.toString();
    96      }
    97
    98      public int hashCode() {
    99          return prefix.hashCode();
    100     }
    101
    102     public boolean equals(Object object) {
    103         if (object == this) {
    104             return true;
    105         }
    106
    107         if (!(object instanceof JDOMNamespacePointer)) {
    108             return false;
    109         }
    110
    111         JDOMNamespacePointer other = (JDOMNamespacePointer) object;
    112         return prefix.equals(other.prefix);
    113     }
    114
    115     public int compareChildNodePointers(
    116         NodePointer pointer1,
    117         NodePointer pointer2)
    118     {
    119         // Won't happen - namespaces don't have children
    120         return 0;
    121     }
    122  }