View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri;
19  
20  import java.io.Serializable;
21  
22  /**
23   * A qualified name: a combination of an optional namespace prefix and an local name.
24   */
25  public class QName implements Serializable {
26  
27      private static final long serialVersionUID = 7616199282015091496L;
28  
29      /**
30       * Prefix.
31       */
32      private final String prefix;
33  
34      /**
35       * Name.
36       */
37      private final String name;
38  
39      /**
40       * Qualified name.
41       */
42      private final String qualifiedName;
43  
44      /**
45       * Constructs a new QName.
46       *
47       * @param qualifiedName value
48       */
49      public QName(final String qualifiedName) {
50          this.qualifiedName = qualifiedName;
51          final int index = qualifiedName.indexOf(':');
52          prefix = index < 0 ? null : qualifiedName.substring(0, index);
53          name = index < 0 ? qualifiedName : qualifiedName.substring(index + 1);
54      }
55  
56      /**
57       * Constructs a new QName.
58       *
59       * @param prefix    ns
60       * @param localName String
61       */
62      public QName(final String prefix, final String localName) {
63          this.prefix = prefix;
64          this.name = localName;
65          this.qualifiedName = prefix == null ? localName : prefix + ':' + localName;
66      }
67  
68      @Override
69      public boolean equals(final Object object) {
70          if (this == object) {
71              return true;
72          }
73          if (!(object instanceof QName)) {
74              return false;
75          }
76          return qualifiedName.equals(((QName) object).qualifiedName);
77      }
78  
79      /**
80       * Gets the local name.
81       *
82       * @return String
83       */
84      public String getName() {
85          return name;
86      }
87  
88      /**
89       * Gets the prefix of this QName.
90       *
91       * @return String
92       */
93      public String getPrefix() {
94          return prefix;
95      }
96  
97      @Override
98      public int hashCode() {
99          return name.hashCode();
100     }
101 
102     @Override
103     public String toString() {
104         return qualifiedName;
105     }
106 }