001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package org.apache.xmlgraphics.util;
021:
022: import java.io.Serializable;
023:
024: /**
025: * Represents a qualified name of an XML element or an XML attribute.
026: * <p>
027: * Note: This class allows to carry a namespace prefix but it is not used in the equals() and
028: * hashCode() methods.
029: */
030: public class QName implements Serializable {
031:
032: private static final long serialVersionUID = -5225376740044770690L;
033:
034: private String namespaceURI;
035: private String localName;
036: private String prefix;
037: private int hashCode;
038:
039: /**
040: * Main constructor.
041: * @param namespaceURI the namespace URI
042: * @param prefix the namespace prefix, may be null
043: * @param localName the local name
044: */
045: public QName(String namespaceURI, String prefix, String localName) {
046: if (localName == null) {
047: throw new NullPointerException(
048: "Parameter localName must not be null");
049: }
050: if (localName.length() == 0) {
051: throw new IllegalArgumentException(
052: "Parameter localName must not be empty");
053: }
054: this .namespaceURI = namespaceURI;
055: this .prefix = prefix;
056: this .localName = localName;
057: this .hashCode = toHashString().hashCode();
058: }
059:
060: /**
061: * Main constructor.
062: * @param namespaceURI the namespace URI
063: * @param qName the qualified name
064: */
065: public QName(String namespaceURI, String qName) {
066: if (qName == null) {
067: throw new NullPointerException(
068: "Parameter localName must not be null");
069: }
070: if (qName.length() == 0) {
071: throw new IllegalArgumentException(
072: "Parameter localName must not be empty");
073: }
074: this .namespaceURI = namespaceURI;
075: int p = qName.indexOf(':');
076: if (p > 0) {
077: this .prefix = qName.substring(0, p);
078: this .localName = qName.substring(p + 1);
079: } else {
080: this .prefix = null;
081: this .localName = qName;
082: }
083: this .hashCode = toHashString().hashCode();
084: }
085:
086: /** @return the namespace URI */
087: public String getNamespaceURI() {
088: return this .namespaceURI;
089: }
090:
091: /** @return the namespace prefix */
092: public String getPrefix() {
093: return this .prefix;
094: }
095:
096: /** @return the local name */
097: public String getLocalName() {
098: return this .localName;
099: }
100:
101: /** @return the fully qualified name */
102: public String getQName() {
103: return getPrefix() != null ? getPrefix() + ':' + getLocalName()
104: : getLocalName();
105: }
106:
107: /** @see java.lang.Object#hashCode() */
108: public int hashCode() {
109: return this .hashCode;
110: }
111:
112: /** @see java.lang.Object#equals(java.lang.Object) */
113: public boolean equals(Object obj) {
114: if (obj == null) {
115: return false;
116: } else if (obj == this ) {
117: return true;
118: } else {
119: if (obj instanceof QName) {
120: QName other = (QName) obj;
121: if ((getNamespaceURI() == null && other
122: .getNamespaceURI() == null)
123: || getNamespaceURI().equals(
124: other.getNamespaceURI())) {
125: return getLocalName().equals(other.getLocalName());
126: }
127: }
128: }
129: return false;
130: }
131:
132: /** @see java.lang.Object#toString() */
133: public String toString() {
134: return prefix != null ? (prefix + ":" + localName)
135: : toHashString();
136: }
137:
138: private String toHashString() {
139: return (namespaceURI != null ? ("{" + namespaceURI + "}" + localName)
140: : localName);
141: }
142:
143: }
|