001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import com.caucho.util.L10N;
032:
033: import org.w3c.dom.DOMException;
034:
035: import java.io.Serializable;
036:
037: public class QName implements Comparable, Serializable {
038: protected static L10N L = new L10N(QName.class);
039:
040: private String _prefix; // preferred prefix
041: private String _localName; // the real name
042: private String _namespace; // URL
043:
044: private String _fullName; // foo:bar
045: private String _canonicalName; // http://www.w3.org?bar
046:
047: public QName(String qName) {
048: this (qName, "");
049: }
050:
051: public QName(String qName, String namespace) {
052: _fullName = qName;
053:
054: if (namespace == null) {
055: _prefix = null;
056: _namespace = null;
057: _localName = _fullName;
058: } else if (namespace.equals("")) {
059: _prefix = null;
060: _namespace = "";
061: _localName = _fullName;
062: } else {
063: _namespace = namespace;
064:
065: int p = qName.indexOf(':');
066: if (p > 0) {
067: _prefix = qName.substring(0, p);
068: _localName = qName.substring(p + 1);
069: } else {
070: _prefix = null;
071: _localName = _fullName;
072: }
073: }
074: }
075:
076: public QName(String prefix, String localName, String namespace) {
077: init(prefix, localName, namespace);
078: }
079:
080: public QName(String qName, String prefix, String localName,
081: String namespace) {
082: _fullName = qName;
083:
084: if (prefix != null)
085: _prefix = prefix;
086:
087: if (localName != null)
088: _localName = localName;
089:
090: if (namespace != null)
091: _namespace = namespace;
092: }
093:
094: private void init(String prefix, String localName, String namespace) {
095: if (localName == null || localName.equals(""))
096: throw new QDOMException(
097: DOMException.INVALID_CHARACTER_ERR,
098: L
099: .l(
100: "`{0}' is an invalid XML name because the local name is empty. XML names must be `prefix:name' or simply `name'.",
101: prefix + ":"));
102:
103: if (prefix == null || prefix.equals(""))
104: _prefix = null;
105: else
106: _prefix = prefix;
107:
108: _localName = localName;
109:
110: if (_prefix != null && _prefix != "")
111: _fullName = (_prefix + ":" + localName);
112: else
113: _fullName = _localName;
114:
115: if ("".equals(namespace)) {
116: _namespace = "";
117: _localName = _fullName;
118: } else if (namespace != null)
119: _namespace = namespace;
120: }
121:
122: public String getName() {
123: return _fullName;
124: }
125:
126: public String getPrefix() {
127: return _prefix;
128: }
129:
130: public String getLocalName() {
131: return _localName;
132: }
133:
134: public String getCanonicalName() {
135: if (_canonicalName == null) {
136: if (_namespace != null)
137: _canonicalName = ("{" + _namespace + "}" + _localName);
138: else
139: _canonicalName = _fullName;
140: }
141:
142: return _canonicalName;
143: }
144:
145: public String getNamespace() {
146: return _namespace;
147: }
148:
149: public String getNamespaceURI() {
150: return _namespace;
151: }
152:
153: /**
154: * Returns the hashcode of the qname.
155: */
156: public int hashCode() {
157: if (_namespace != null)
158: return _localName.hashCode() * 65521
159: + _namespace.hashCode();
160: else
161: return _localName.hashCode();
162: }
163:
164: /**
165: * Returns true if the two qnames are equivalent.
166: */
167: public boolean equals(Object b) {
168: if (this == b)
169: return true;
170:
171: if (!(b instanceof QName))
172: return false;
173:
174: QName name = (QName) b;
175:
176: if (!_localName.equals(name._localName))
177: return false;
178:
179: if (_namespace == name._namespace)
180: return true;
181: else
182: return _namespace != null
183: && _namespace.equals(name._namespace);
184: }
185:
186: public int compareTo(Object b) {
187: if (this == b)
188: return 0;
189:
190: else if (!(b instanceof QName))
191: return -1;
192:
193: QName name = (QName) b;
194:
195: return getCanonicalName().compareTo(name.getCanonicalName());
196: /*
197: int cmp = getName().compareTo(name.getName());
198:
199: if (cmp != 0)
200: return cmp;
201: else if (_namespace == null)
202: return name._namespace == null ? 0 : -1;
203: else if (name._namespace == null)
204: return 1;
205: else
206: return _namespace.compareTo(name._namespace);
207: */
208: }
209:
210: public String toString() {
211: if (_prefix != null)
212: return "QName[" + _prefix + ":" + getCanonicalName() + "]";
213: else
214: return "QName[" + getCanonicalName() + "]";
215: }
216: }
|