001: /*
002: * Copyright (c) 1998-2004 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 javax.xml.namespace;
030:
031: /**
032: * Implementation of the XML QName.
033: */
034: public class QName implements java.io.Serializable {
035: private String namespaceURI;
036: private String localPart;
037: private String prefix;
038:
039: public QName(String localPart) {
040: this .localPart = localPart;
041: this .namespaceURI = "";
042: this .prefix = "";
043: }
044:
045: public QName(String uri, String localPart) {
046: this .localPart = localPart;
047:
048: if (uri == null)
049: uri = "";
050: this .namespaceURI = uri;
051:
052: this .prefix = "";
053: }
054:
055: public QName(String uri, String localPart, String prefix) {
056: this .localPart = localPart;
057:
058: if (uri == null)
059: uri = "";
060: this .namespaceURI = uri;
061:
062: if (prefix == null)
063: prefix = "";
064: this .prefix = prefix;
065: }
066:
067: /**
068: * Creates by parsing.
069: */
070: public static QName valueOf(String name) {
071: int p = name.indexOf('}');
072:
073: if (p < 0)
074: return new QName(name);
075: else {
076: return new QName(name.substring(1, p), name
077: .substring(p + 1));
078: }
079: }
080:
081: /**
082: * Returns the local part.
083: */
084: public String getLocalPart() {
085: return this .localPart;
086: }
087:
088: /**
089: * Returns the prefix.
090: */
091: public String getPrefix() {
092: return this .prefix;
093: }
094:
095: /**
096: * Returns the namespace URI.
097: */
098: public String getNamespaceURI() {
099: return this .namespaceURI;
100: }
101:
102: /**
103: * Returns a hash code.
104: */
105: public int hashCode() {
106: return this .namespaceURI.hashCode() * 65521
107: + this .localPart.hashCode();
108: }
109:
110: /**
111: * Tests for equality.
112: */
113: public boolean equals(Object o) {
114: if (!(o instanceof QName))
115: return false;
116:
117: QName name = (QName) o;
118:
119: return (this .localPart.equals(name.localPart) && this .namespaceURI
120: .equals(name.namespaceURI));
121: }
122:
123: /**
124: * Returns a printable representaion.
125: */
126: public String toString() {
127: if ("".equals(this .namespaceURI))
128: return this .localPart;
129: else
130: return "{" + this .namespaceURI + "}" + this.localPart;
131: }
132: }
|