1 3 7 8 package javax.xml.namespace; 9 10 import java.io.Serializable ; 11 import java.security.AccessController ; 12 import java.security.PrivilegedAction ; 13 import javax.xml.XMLConstants ; 14 15 52 53 public class QName implements Serializable { 54 55 72 private static final long serialVersionUID; 73 76 private static final long defaultSerialVersionUID = -9120448754896609940L; 77 80 private static final long compatibleSerialVersionUID = 4418622981026545151L; 81 84 private static boolean useDefaultSerialVersionUID = true; 85 static { 86 try { 87 String valueUseCompatibleSerialVersionUID = (String ) AccessController.doPrivileged( 89 new PrivilegedAction () { 90 public Object run() { 91 return System.getProperty("com.sun.xml.namespace.QName.useCompatibleSerialVersionUID"); 92 } 93 } 94 ); 95 useDefaultSerialVersionUID = 96 (valueUseCompatibleSerialVersionUID != null && valueUseCompatibleSerialVersionUID.equals("1.0")) ? false : true; 97 } catch (Exception exception) { 98 useDefaultSerialVersionUID = true; 100 } 101 if (useDefaultSerialVersionUID) 103 { 104 serialVersionUID = defaultSerialVersionUID; 105 } else { 106 serialVersionUID = compatibleSerialVersionUID; 107 } 108 } 109 112 private final String namespaceURI; 113 114 117 private final String localPart; 118 119 122 private final String prefix; 123 124 162 public QName(final String namespaceURI, final String localPart) { 163 this(namespaceURI, localPart, XMLConstants.DEFAULT_NS_PREFIX); 164 } 165 166 203 public QName(String namespaceURI, String localPart, String prefix) { 204 205 if (namespaceURI == null) { 207 this.namespaceURI = XMLConstants.NULL_NS_URI; 208 } else { 209 this.namespaceURI = namespaceURI; 210 } 211 212 if (localPart == null) { 214 throw new IllegalArgumentException ("local part cannot be \"null\" when creating a QName"); 215 } 216 this.localPart = localPart; 217 218 if (prefix == null) { 220 throw new IllegalArgumentException ("prefix cannot be \"null\" when creating a QName"); 221 } 222 this.prefix = prefix; 223 } 224 225 260 public QName(String localPart) { 261 this( 262 XMLConstants.NULL_NS_URI, 263 localPart, 264 XMLConstants.DEFAULT_NS_PREFIX); 265 } 266 267 272 public String getNamespaceURI() { 273 return namespaceURI; 274 } 275 276 281 public String getLocalPart() { 282 return localPart; 283 } 284 285 296 public String getPrefix() { 297 return prefix; 298 } 299 300 322 public final boolean equals(Object objectToTest) { 323 if (objectToTest == null || !(objectToTest instanceof QName )) { 324 return false; 325 } 326 327 QName qName = (QName ) objectToTest; 328 329 return namespaceURI.equals(qName.namespaceURI) 330 && localPart.equals(qName.localPart); 331 } 332 333 346 public final int hashCode() { 347 return namespaceURI.hashCode() ^ localPart.hashCode(); 348 } 349 350 372 public String toString() { 373 if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) { 374 return localPart; 375 } else { 376 return "{" + namespaceURI + "}" + localPart; 377 } 378 } 379 380 419 public static QName valueOf(String qNameAsString) { 420 421 if (qNameAsString == null) { 423 throw new IllegalArgumentException ("cannot create QName from \"null\" or \"\" String"); 424 } 425 426 if (qNameAsString.length() == 0) { 428 return new QName ( 429 XMLConstants.NULL_NS_URI, 430 qNameAsString, 431 XMLConstants.DEFAULT_NS_PREFIX); 432 } 433 434 if (qNameAsString.charAt(0) != '{') { 436 return new QName ( 437 XMLConstants.NULL_NS_URI, 438 qNameAsString, 439 XMLConstants.DEFAULT_NS_PREFIX); 440 } 441 442 if (qNameAsString.startsWith("{" + XMLConstants.NULL_NS_URI + "}")) { 444 throw new IllegalArgumentException ( 445 "Namespace URI .equals(XMLConstants.NULL_NS_URI), " 446 + ".equals(\"" + XMLConstants.NULL_NS_URI + "\"), " 447 + "only the local part, " 448 + "\"" + qNameAsString.substring(2 + XMLConstants.NULL_NS_URI.length()) + "\", " 449 + "should be provided."); 450 } 451 452 int endOfNamespaceURI = qNameAsString.indexOf('}'); 454 if (endOfNamespaceURI == -1) { 455 throw new IllegalArgumentException ( 456 "cannot create QName from \"" 457 + qNameAsString 458 + "\", missing closing \"}\""); 459 } 460 return new QName ( 461 qNameAsString.substring(1, endOfNamespaceURI), 462 qNameAsString.substring(endOfNamespaceURI + 1), 463 XMLConstants.DEFAULT_NS_PREFIX); 464 } 465 } 466 | Popular Tags |