|
1 | | -/* |
2 | | - * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. |
3 | | - * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
4 | | - * |
5 | | - * |
6 | | - * |
7 | | - * |
8 | | - * |
9 | | - * |
10 | | - * |
11 | | - * |
12 | | - * |
13 | | - * |
14 | | - * |
15 | | - * |
16 | | - * |
17 | | - * |
18 | | - * |
19 | | - * |
20 | | - * |
21 | | - * |
22 | | - * |
| 1 | +/* JBoss, the OpenSource WebOS |
23 | 2 | * |
| 3 | + * Distributable under LGPL license. |
| 4 | + * See terms of license at gnu.org. |
| 5 | + * |
| 6 | + * modified for SwingJS by Bob Hanson, hansonr@stolaf.edu |
| 7 | + * |
24 | 8 | */ |
25 | | - |
26 | 9 | package javax.xml.namespace; |
27 | 10 |
|
28 | | -import javax.xml.XMLConstants; |
29 | | - |
30 | | - |
31 | | -public class QName { |
32 | | - private final String ns; |
33 | | - private final String local; |
34 | | - private final String pre; |
35 | | - |
36 | | - public QName(final String ns, final String local) { |
37 | | - this(ns, local, XMLConstants.DEFAULT_NS_PREFIX); |
38 | | - } |
39 | | - |
40 | | - public QName(String ns, String local, String prefix) { |
41 | | - if (ns == null) { |
42 | | - this.ns = XMLConstants.NULL_NS_URI; |
43 | | - } else { |
44 | | - this.ns = ns; |
45 | | - } |
46 | | - if (local == null) { |
47 | | - throw new IllegalArgumentException("local part cannot be \"null\" when creating a QName"); |
48 | | - } |
49 | | - this.local = local; |
50 | | - if (prefix == null) { |
51 | | - throw new IllegalArgumentException("prefix cannot be \"null\" when creating a QName"); |
52 | | - } |
53 | | - this.pre = prefix; |
54 | | - } |
55 | | - |
56 | | - public QName(String local) { |
57 | | - this(XMLConstants.NULL_NS_URI, local, XMLConstants.DEFAULT_NS_PREFIX); |
58 | | - } |
59 | | - |
60 | | - public String getNamespaceURI() { |
61 | | - return ns; |
62 | | - } |
63 | | - |
64 | | - public String getLocalPart() { |
65 | | - return local; |
66 | | - } |
67 | | - |
68 | | - public String getPrefix() { |
69 | | - return pre; |
70 | | - } |
71 | | - |
72 | | - public final boolean equals(Object objectToTest) { |
73 | | - if (objectToTest == this) { |
74 | | - return true; |
75 | | - } |
76 | | - |
77 | | - if (objectToTest == null || !(objectToTest instanceof QName)) { |
78 | | - return false; |
79 | | - } |
80 | | - |
81 | | - QName qName = (QName) objectToTest; |
82 | | - |
83 | | - return local.equals(qName.local) && ns.equals(qName.ns); |
84 | | - } |
85 | | - |
86 | | - public final int hashCode() { |
87 | | - return ns.hashCode() ^ local.hashCode(); |
88 | | - } |
89 | | - |
90 | | - public String toString() { |
91 | | - if (ns.equals(XMLConstants.NULL_NS_URI)) { |
92 | | - return local; |
93 | | - } else { |
94 | | - return "{" + ns + "}" + local; |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - public static QName valueOf(String qNameAsString) { |
99 | | - |
100 | | - if (qNameAsString == null) { |
101 | | - throw new IllegalArgumentException("cannot create QName from \"null\" or \"\" String"); |
102 | | - } |
103 | | - |
104 | | - if (qNameAsString.length() == 0) { |
105 | | - return new QName(XMLConstants.NULL_NS_URI, qNameAsString, XMLConstants.DEFAULT_NS_PREFIX); |
106 | | - } |
107 | | - |
108 | | - if (qNameAsString.charAt(0) != '{') { |
109 | | - return new QName(XMLConstants.NULL_NS_URI, qNameAsString, XMLConstants.DEFAULT_NS_PREFIX); |
110 | | - } |
111 | | - |
112 | | - if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) { |
113 | | - throw new IllegalArgumentException("Namespace URI .equals(XMLConstants.NULL_NS_URI), " + ".equals(\"" |
114 | | - + XMLConstants.NULL_NS_URI + "\"), " + "only the local part, " + "\"" |
115 | | - + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length()) + "\", " + "should be provided."); |
116 | | - } |
117 | | - |
118 | | - int endOfns = qNameAsString.indexOf('}'); |
119 | | - if (endOfns == -1) { |
120 | | - throw new IllegalArgumentException( |
121 | | - "cannot create QName from \"" + qNameAsString + "\", missing closing \"}\""); |
122 | | - } |
123 | | - return new QName(qNameAsString.substring(1, endOfns), qNameAsString.substring(endOfns + 1), |
124 | | - XMLConstants.DEFAULT_NS_PREFIX); |
125 | | - } |
| 11 | +// $Id: QName.java,v 1.2.6.8 2005/04/12 03:35:26 starksm Exp $ |
| 12 | + |
| 13 | +import java.util.StringTokenizer; |
| 14 | + |
| 15 | +/** QName represents an immutable qualified name. |
| 16 | + * The value of a QName contains a Namespace URI, local part and prefix. |
| 17 | + * |
| 18 | + * The prefix is included in QName to retain lexical information when present |
| 19 | + * in an XML input source. The prefix is NOT used in QName.equals(Object) or |
| 20 | + * to compute the QName.hashCode(). Equality and the hash code are defined |
| 21 | + * using only the Namespace URI and local part. |
| 22 | + * |
| 23 | + * If not specified, the Namespace URI is set to "" (the empty string). |
| 24 | + * If not specified, the prefix is set to "" (the empty string). |
| 25 | + * |
| 26 | + * @author Scott.Stark@jboss.org |
| 27 | + * @author Thomas.Diesler@jboss.org |
| 28 | + * @author Jeff Suttor (javadoc) |
| 29 | + * @version $Revision: 1.2.6.8 $ |
| 30 | + */ |
| 31 | +public class QName //implements Serializable |
| 32 | +{ |
| 33 | +// /** @since 4.0.2, compatible with jdk5 by default */ |
| 34 | +// final static long serialVersionUID; |
| 35 | +// static |
| 36 | +// { |
| 37 | +// if (SerialVersion.version == SerialVersion.LEGACY) |
| 38 | +// serialVersionUID = 8217399441836960859L; |
| 39 | +// else |
| 40 | +// serialVersionUID = -3852060120346905000L; |
| 41 | +// } |
| 42 | +// |
| 43 | + private String namespaceURI; |
| 44 | + private String localPart; |
| 45 | + private String prefix; |
| 46 | + |
| 47 | + /** QName derived from parsing the formatted String. |
| 48 | + * If the String is null or does not conform to QName.toString() formatting, |
| 49 | + * an IllegalArgumentException is thrown. |
| 50 | + * |
| 51 | + * The String MUST be in the form returned by QName.toString(). There is NO |
| 52 | + * standard specification for representing a QName as a String. The String |
| 53 | + * format is NOT portable across implementations and will change when a |
| 54 | + * standard String representation is defined. This implementation currently |
| 55 | + * parses a String formatted as: "{" + Namespace URI + "}" + local part. If |
| 56 | + * the Namespace URI .equals(""), only the local part should be provided. |
| 57 | + * |
| 58 | + * The prefix value CANNOT be represented in the String and will be set to "" |
| 59 | + * |
| 60 | + * This method does not do full validation of the resulting QName. In |
| 61 | + * particular, the local part is not validated as a NCName as specified in |
| 62 | + * Namespaces in XML. |
| 63 | + * |
| 64 | + * @see #toString() |
| 65 | + * @param toStringName - a QName string in the format of toString(). |
| 66 | + * @return QName for the toStringName |
| 67 | + */ |
| 68 | + public static QName valueOf(String toStringName) |
| 69 | + { |
| 70 | + String uri = null; |
| 71 | + String localPart = null; |
| 72 | + |
| 73 | + StringTokenizer tokenizer = new StringTokenizer(toStringName, "{}"); |
| 74 | + int tokenCount = tokenizer.countTokens(); |
| 75 | + |
| 76 | + if (tokenCount < 1 || tokenCount > 2) |
| 77 | + throw new IllegalArgumentException("Invalid QName string: " + toStringName); |
| 78 | + |
| 79 | + if (tokenCount > 1) |
| 80 | + uri = tokenizer.nextToken(); |
| 81 | + |
| 82 | + localPart = tokenizer.nextToken(); |
| 83 | + return new QName(uri, localPart); |
| 84 | + } |
| 85 | + |
| 86 | + public QName(String localPart) |
| 87 | + { |
| 88 | + this(null, localPart); |
| 89 | + } |
| 90 | + |
| 91 | + public QName(String namespaceURI, String localPart) |
| 92 | + { |
| 93 | + this(namespaceURI, localPart, ""); |
| 94 | + } |
| 95 | + |
| 96 | + /** QName constructor specifying the Namespace URI, local part and prefix. |
| 97 | + * If the Namespace URI is null, it is set to "". This value represents no |
| 98 | + * explicitly defined Namespace as defined by the Namespaces in XML |
| 99 | + * specification. This action preserves compatible behavior with QName 1.0. |
| 100 | + * |
| 101 | + * If the local part is null, an IllegalArgumentException is thrown. |
| 102 | + * |
| 103 | + * If the prefix is null, an IllegalArgumentException is thrown. Use "" to |
| 104 | + * explicitly indicate that no prefix is present or the prefix is not |
| 105 | + * relevant. |
| 106 | + * |
| 107 | + * @param namespaceURI - Namespace URI of the QName |
| 108 | + * @param localPart - local part of the QName |
| 109 | + * @param prefix - prefix of the QName |
| 110 | + */ |
| 111 | + public QName(String namespaceURI, String localPart, String prefix) |
| 112 | + { |
| 113 | + this.namespaceURI = namespaceURI; |
| 114 | + if (this.namespaceURI == null) |
| 115 | + this.namespaceURI = ""; |
| 116 | + |
| 117 | + if (localPart == null) |
| 118 | + throw new IllegalArgumentException("localPart cannot be null"); |
| 119 | + |
| 120 | + if (localPart.startsWith(":")) |
| 121 | + throw new IllegalArgumentException("Illegal localPart: " + localPart); |
| 122 | + |
| 123 | + this.localPart = localPart; |
| 124 | + |
| 125 | + this.prefix = prefix; |
| 126 | + if (this.prefix == null) |
| 127 | + this.prefix = ""; |
| 128 | + } |
| 129 | + |
| 130 | + public String getNamespaceURI() |
| 131 | + { |
| 132 | + return namespaceURI; |
| 133 | + } |
| 134 | + |
| 135 | + public String getLocalPart() |
| 136 | + { |
| 137 | + return localPart; |
| 138 | + } |
| 139 | + |
| 140 | + public String getPrefix() |
| 141 | + { |
| 142 | + return prefix; |
| 143 | + } |
| 144 | + |
| 145 | + /** There is NO standard specification for representing a QName as a String. |
| 146 | + * The returned String is not portable across implementations and will change when a standard String representation is defined. |
| 147 | + * This implementation currently represents a QName as: "{" + Namespace URI + "}" + local part. |
| 148 | + * If the Namespace URI .equals(""), only the local part is returned. |
| 149 | + * An appropriate use of this method is for debugging or logging for human consumption. |
| 150 | + * |
| 151 | + * Note the prefix value is NOT returned as part of the String representation. |
| 152 | + * |
| 153 | + * @return '{' + namespaceURI + '}' + localPart |
| 154 | + */ |
| 155 | + public String toString() |
| 156 | + { |
| 157 | + if (namespaceURI.equals("")) |
| 158 | + return localPart; |
| 159 | + else |
| 160 | + return '{' + namespaceURI + '}' + localPart; |
| 161 | + } |
| 162 | + |
| 163 | + /** Equality is based on the namespaceURI and localPart |
| 164 | + * @param obj the QName to compare too |
| 165 | + * @return true if both namespaceURI and localPart, false otherwise |
| 166 | + */ |
| 167 | + public boolean equals(Object obj) |
| 168 | + { |
| 169 | + if (obj instanceof QName) |
| 170 | + { |
| 171 | + QName qn = (QName)obj; |
| 172 | + boolean equals = namespaceURI.equals(qn.namespaceURI); |
| 173 | + return equals && localPart.equals(qn.localPart); |
| 174 | + } |
| 175 | + return false; |
| 176 | + } |
| 177 | + |
| 178 | + /** Calculate the hash of namespaceURI and localPart |
| 179 | + * @return namespaceURI.hashCode() + localPart.hashCode() |
| 180 | + */ |
| 181 | + public int hashCode() |
| 182 | + { |
| 183 | + int hashCode = namespaceURI.hashCode() + localPart.hashCode(); |
| 184 | + return hashCode; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Compares this object with the specified object for order. Returns a |
| 189 | + * negative integer, zero, or a positive integer as this object is less |
| 190 | + * than, equal to, or greater than the specified object.<p> |
| 191 | + */ |
| 192 | + public int compareTo(Object o) |
| 193 | + { |
| 194 | + QName other = (QName)o; |
| 195 | + return toString().compareTo(other.toString()); |
| 196 | + } |
126 | 197 | } |
0 commit comments