View Javadoc

    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;
    17
    18
    19  /***
    20   * A qualified name: a combination of an optional namespace prefix
    21   * and an local name.
    22   *
    23   * @author Dmitri Plotnikov
    24   * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:45 $
    25   */
    26  public class QName {
    27      private String prefix;
    28      private String name;
    29
    30      public QName(String qualifiedName) {
    31          int index = qualifiedName.indexOf(':');
    32          if (index == -1) {
    33              prefix = null;
    34              name = qualifiedName;
    35          }
    36          else {
    37              prefix = qualifiedName.substring(0, index);
    38              name = qualifiedName.substring(index + 1);
    39          }
    40      }
    41
    42      public QName(String prefix, String localName) {
    43          this.prefix = prefix;
    44          this.name = localName;
    45      }
    46
    47      public String getPrefix() {
    48          return prefix;
    49      }
    50
    51      public String getName() {
    52          return name;
    53      }
    54
    55      public String toString() {
    56          if (prefix != null) {
    57              return prefix + ':' + name;
    58          }
    59          return name;
    60      }
    61
    62      public int hashCode() {
    63          return name.hashCode();
    64      }
    65
    66      public boolean equals(Object object) {
    67          if (!(object instanceof QName)) {
    68              return false;
    69          }
    70          if (this == object) {
    71              return true;
    72          }
    73          QName that = (QName) object;
    74          if (!this.name.equals(that.name)) {
    75              return false;
    76          }
    77
    78          if ((this.prefix == null && that.prefix != null)
    79              || (this.prefix != null && !this.prefix.equals(that.prefix))) {
    80              return false;
    81          }
    82
    83          return true;
    84      }
    85  }