1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.util;
22 
23 /**
24  *
25  *  01/05/15
26  * @author Daniel A. Parker (daniel.parker@servingxml.com)
27  */
28 
29 public class QualifiedName extends Name {
30 
31   private final String namespaceUri;
32   private final String localName;
33   private final int hashCode;
34 
35   /**
36    * Creates a name with an empty namespace URI and an empty local name.
37    */
38 
QualifiedName()39   public QualifiedName() {
40     this.namespaceUri = "";
41     this.localName = "";
42     this.hashCode = localName.hashCode();
43   }
44 
45   /**
46    * Creates a name with a local name, but no namespace URI.
47    */
48 
QualifiedName(String localName)49   public QualifiedName(String localName) {
50     this.namespaceUri = "";
51     this.localName = localName;
52     this.hashCode = localName.hashCode();
53   }
54 
55   /**
56    * Creates a name with the namespace URI and local name.
57    */
58 
QualifiedName(String namespaceUri, String localName)59   public QualifiedName(String namespaceUri, String localName) {
60     this.namespaceUri = namespaceUri;
61     this.localName = localName;
62     this.hashCode = localName.hashCode();
63   }
64 
toUri()65   public String toUri() {
66     return namespaceUri + localName;
67   }
68 
isEmpty()69   public boolean isEmpty() {
70     return (namespaceUri.length() + localName.length()) == 0;
71   }
72 
getNamespaceUri()73   public String getNamespaceUri() {
74     return namespaceUri;
75   }
76 
getLocalName()77   public String getLocalName() {
78     return localName;
79   }
80 
toQname(PrefixMap prefixMap)81   public String toQname(PrefixMap prefixMap) {
82     String prefix = prefixMap.getPrefix(namespaceUri);
83     return prefix.length() == 0 ? localName : prefix + ":" + localName;
84   }
85 
toQname(QnameContext context)86   public String toQname(QnameContext context) {
87     String prefix = context.getPrefix(namespaceUri);
88     return prefix.length() == 0 ? localName : prefix + ":" + localName;
89   }
90 
91   /**
92    * Returns a hash code value for this name.
93    *
94    * @return a hash code value for this name.
95    */
96 
hashCode()97   public int hashCode() {
98     return hashCode;
99   }
100 
equals(Object o)101   public boolean equals(Object o) {
102     boolean isEqual = true;
103     if (this != o) {
104       Name other = (Name)o;
105       isEqual = other.getLocalName().equals(localName) && other.getNamespaceUri().equals(namespaceUri);
106     }
107     return isEqual;
108   }
109 
toString()110   public String toString() {
111     String s;
112 
113     if (namespaceUri.length() > 0) {
114       StringBuilder buf = new StringBuilder();
115       buf.append("{");
116       buf.append(namespaceUri);
117       buf.append("}");
118       buf.append(localName);
119       s = buf.toString();
120     } else {
121       s = localName;
122     }
123     return s;
124   }
125 
compareTo(Object o)126   public int compareTo(Object o) {
127     int diff = 0;
128 
129     if (this != o) {
130       Name b = (Name)o;
131 
132       diff = namespaceUri.compareTo(b.getNamespaceUri());
133       if (diff == 0) {
134         diff = localName.compareTo(b.getLocalName());
135       }
136     }
137     return diff;
138   }
139 }
140