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 package org.apache.commons.jxpath.ri;
18
19 import java.io.Serializable;
20
21
22 /**
23 * A qualified name: a combination of an optional namespace prefix
24 * and an local name.
25 *
26 * @author Dmitri Plotnikov
27 * @version $Revision: 652925 $ $Date: 2008-05-02 18:05:41 -0400 (Fri, 02 May 2008) $
28 */
29 public class QName implements Serializable {
30 private static final long serialVersionUID = 7616199282015091496L;
31
32 private String prefix;
33 private String name;
34 private String qualifiedName;
35
36 /**
37 * Create a new QName.
38 * @param qualifiedName value
39 */
40 public QName(String qualifiedName) {
41 this.qualifiedName = qualifiedName;
42 int index = qualifiedName.indexOf(':');
43 prefix = index < 0 ? null : qualifiedName.substring(0, index);
44 name = index < 0 ? qualifiedName : qualifiedName.substring(index + 1);
45 }
46
47 /**
48 * Create a new QName.
49 * @param prefix ns
50 * @param localName String
51 */
52 public QName(String prefix, String localName) {
53 this.prefix = prefix;
54 this.name = localName;
55 this.qualifiedName = prefix == null ? localName : prefix + ':' + localName;
56 }
57
58 /**
59 * Get the prefix of this QName.
60 * @return String
61 */
62 public String getPrefix() {
63 return prefix;
64 }
65
66 /**
67 * Get the local name.
68 * @return String
69 */
70 public String getName() {
71 return name;
72 }
73
74 public String toString() {
75 return qualifiedName;
76 }
77
78 public int hashCode() {
79 return name.hashCode();
80 }
81
82 public boolean equals(Object object) {
83 if (this == object) {
84 return true;
85 }
86 if (!(object instanceof QName)) {
87 return false;
88 }
89 return qualifiedName.equals(((QName) object).qualifiedName);
90 }
91 }