1 16 19 package com.sun.org.apache.xml.internal.utils; 20 21 import java.util.Stack ; 22 import java.util.StringTokenizer ; 23 24 import com.sun.org.apache.xml.internal.res.XMLErrorResources; 25 import com.sun.org.apache.xml.internal.res.XMLMessages; 26 27 import org.w3c.dom.Element ; 28 29 42 public class QName implements java.io.Serializable 43 { 44 45 49 protected String _localName; 50 51 55 protected String _namespaceURI; 56 57 61 protected String _prefix; 62 63 66 public static final String S_XMLNAMESPACEURI = 67 "http://www.w3.org/XML/1998/namespace"; 68 69 73 private int m_hashCode; 74 75 79 public QName(){} 80 81 88 public QName(String namespaceURI, String localName) 89 { 90 this(namespaceURI, localName, false); 91 } 92 93 102 public QName(String namespaceURI, String localName, boolean validate) 103 { 104 105 if (localName == null) 108 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 109 XMLErrorResources.ER_ARG_LOCALNAME_NULL, null)); 111 if (validate) 112 { 113 if (!XMLChar.isValidNCName(localName)) 114 { 115 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 116 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 118 } 119 120 _namespaceURI = namespaceURI; 121 _localName = localName; 122 m_hashCode = toString().hashCode(); 123 } 124 125 134 public QName(String namespaceURI, String prefix, String localName) 135 { 136 this(namespaceURI, prefix, localName, false); 137 } 138 139 149 public QName(String namespaceURI, String prefix, String localName, boolean validate) 150 { 151 152 if (localName == null) 155 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 156 XMLErrorResources.ER_ARG_LOCALNAME_NULL, null)); 158 if (validate) 159 { 160 if (!XMLChar.isValidNCName(localName)) 161 { 162 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 163 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 165 166 if ((null != prefix) && (!XMLChar.isValidNCName(prefix))) 167 { 168 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 169 XMLErrorResources.ER_ARG_PREFIX_INVALID,null )); } 171 172 } 173 _namespaceURI = namespaceURI; 174 _prefix = prefix; 175 _localName = localName; 176 m_hashCode = toString().hashCode(); 177 } 178 179 186 public QName(String localName) 187 { 188 this(localName, false); 189 } 190 191 199 public QName(String localName, boolean validate) 200 { 201 202 if (localName == null) 205 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 206 XMLErrorResources.ER_ARG_LOCALNAME_NULL, null)); 208 if (validate) 209 { 210 if (!XMLChar.isValidNCName(localName)) 211 { 212 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 213 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 215 } 216 _namespaceURI = null; 217 _localName = localName; 218 m_hashCode = toString().hashCode(); 219 } 220 221 229 public QName(String qname, Stack namespaces) 230 { 231 this(qname, namespaces, false); 232 } 233 234 244 public QName(String qname, Stack namespaces, boolean validate) 245 { 246 247 String namespace = null; 248 String prefix = null; 249 int indexOfNSSep = qname.indexOf(':'); 250 251 if (indexOfNSSep > 0) 252 { 253 prefix = qname.substring(0, indexOfNSSep); 254 255 if (prefix.equals("xml")) 256 { 257 namespace = S_XMLNAMESPACEURI; 258 } 259 else if (prefix.equals("xmlns")) 261 { 262 return; 263 } 264 else 265 { 266 int depth = namespaces.size(); 267 268 for (int i = depth - 1; i >= 0; i--) 269 { 270 NameSpace ns = (NameSpace) namespaces.elementAt(i); 271 272 while (null != ns) 273 { 274 if ((null != ns.m_prefix) && prefix.equals(ns.m_prefix)) 275 { 276 namespace = ns.m_uri; 277 i = -1; 278 279 break; 280 } 281 282 ns = ns.m_next; 283 } 284 } 285 } 286 287 if (null == namespace) 288 { 289 throw new RuntimeException ( 290 XMLMessages.createXMLMessage( 291 XMLErrorResources.ER_PREFIX_MUST_RESOLVE, 292 new Object []{ prefix })); } 294 } 295 296 _localName = (indexOfNSSep < 0) 297 ? qname : qname.substring(indexOfNSSep + 1); 298 299 if (validate) 300 { 301 if ((_localName == null) || (!XMLChar.isValidNCName(_localName))) 302 { 303 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 304 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 306 } 307 _namespaceURI = namespace; 308 _prefix = prefix; 309 m_hashCode = toString().hashCode(); 310 } 311 312 321 public QName(String qname, Element namespaceContext, 322 PrefixResolver resolver) 323 { 324 this(qname, namespaceContext, resolver, false); 325 } 326 327 338 public QName(String qname, Element namespaceContext, 339 PrefixResolver resolver, boolean validate) 340 { 341 342 _namespaceURI = null; 343 344 int indexOfNSSep = qname.indexOf(':'); 345 346 if (indexOfNSSep > 0) 347 { 348 if (null != namespaceContext) 349 { 350 String prefix = qname.substring(0, indexOfNSSep); 351 352 _prefix = prefix; 353 354 if (prefix.equals("xml")) 355 { 356 _namespaceURI = S_XMLNAMESPACEURI; 357 } 358 359 else if (prefix.equals("xmlns")) 361 { 362 return; 363 } 364 else 365 { 366 _namespaceURI = resolver.getNamespaceForPrefix(prefix, 367 namespaceContext); 368 } 369 370 if (null == _namespaceURI) 371 { 372 throw new RuntimeException ( 373 XMLMessages.createXMLMessage( 374 XMLErrorResources.ER_PREFIX_MUST_RESOLVE, 375 new Object []{ prefix })); } 377 } 378 else 379 { 380 381 } 383 } 384 385 _localName = (indexOfNSSep < 0) 386 ? qname : qname.substring(indexOfNSSep + 1); 387 388 if (validate) 389 { 390 if ((_localName == null) || (!XMLChar.isValidNCName(_localName))) 391 { 392 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 393 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 395 } 396 397 m_hashCode = toString().hashCode(); 398 } 399 400 401 409 public QName(String qname, PrefixResolver resolver) 410 { 411 this(qname, resolver, false); 412 } 413 414 424 public QName(String qname, PrefixResolver resolver, boolean validate) 425 { 426 427 String prefix = null; 428 _namespaceURI = null; 429 430 int indexOfNSSep = qname.indexOf(':'); 431 432 if (indexOfNSSep > 0) 433 { 434 prefix = qname.substring(0, indexOfNSSep); 435 436 if (prefix.equals("xml")) 437 { 438 _namespaceURI = S_XMLNAMESPACEURI; 439 } 440 else 441 { 442 _namespaceURI = resolver.getNamespaceForPrefix(prefix); 443 } 444 445 if (null == _namespaceURI) 446 { 447 throw new RuntimeException ( 448 XMLMessages.createXMLMessage( 449 XMLErrorResources.ER_PREFIX_MUST_RESOLVE, 450 new Object []{ prefix })); } 452 _localName = qname.substring(indexOfNSSep + 1); 453 } 454 else if (indexOfNSSep == 0) 455 { 456 throw new RuntimeException ( 457 XMLMessages.createXMLMessage( 458 XMLErrorResources.ER_NAME_CANT_START_WITH_COLON, 459 null)); 460 } 461 else 462 { 463 _localName = qname; 464 } 465 466 if (validate) 467 { 468 if ((_localName == null) || (!XMLChar.isValidNCName(_localName))) 469 { 470 throw new IllegalArgumentException (XMLMessages.createXMLMessage( 471 XMLErrorResources.ER_ARG_LOCALNAME_INVALID,null )); } 473 } 474 475 476 m_hashCode = toString().hashCode(); 477 _prefix = prefix; 478 } 479 480 486 public String getNamespaceURI() 487 { 488 return _namespaceURI; 489 } 490 491 497 public String getPrefix() 498 { 499 return _prefix; 500 } 501 502 507 public String getLocalName() 508 { 509 return _localName; 510 } 511 512 519 public String toString() 520 { 521 522 return _prefix != null 523 ? (_prefix + ":" + _localName) 524 : (_namespaceURI != null 525 ? ("{"+_namespaceURI + "}" + _localName) : _localName); 526 } 527 528 535 public String toNamespacedString() 536 { 537 538 return (_namespaceURI != null 539 ? ("{"+_namespaceURI + "}" + _localName) : _localName); 540 } 541 542 543 548 public String getNamespace() 549 { 550 return getNamespaceURI(); 551 } 552 553 558 public String getLocalPart() 559 { 560 return getLocalName(); 561 } 562 563 568 public int hashCode() 569 { 570 return m_hashCode; 571 } 572 573 583 public boolean equals(String ns, String localPart) 584 { 585 586 String thisnamespace = getNamespaceURI(); 587 588 return getLocalName().equals(localPart) 589 && (((null != thisnamespace) && (null != ns)) 590 ? thisnamespace.equals(ns) 591 : ((null == thisnamespace) && (null == ns))); 592 } 593 594 603 public boolean equals(Object object) 604 { 605 606 if (object == this) 607 return true; 608 609 if (object instanceof QName) { 610 QName qname = (QName) object; 611 String thisnamespace = getNamespaceURI(); 612 String thatnamespace = qname.getNamespaceURI(); 613 614 return getLocalName().equals(qname.getLocalName()) 615 && (((null != thisnamespace) && (null != thatnamespace)) 616 ? thisnamespace.equals(thatnamespace) 617 : ((null == thisnamespace) && (null == thatnamespace))); 618 } 619 else 620 return false; 621 } 622 623 631 public static QName getQNameFromString(String name) 632 { 633 634 StringTokenizer tokenizer = new StringTokenizer (name, "{}", false); 635 QName qname; 636 String s1 = tokenizer.nextToken(); 637 String s2 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null; 638 639 if (null == s2) 640 qname = new QName(null, s1); 641 else 642 qname = new QName(s1, s2); 643 644 return qname; 645 } 646 647 655 public static boolean isXMLNSDecl(String attRawName) 656 { 657 658 return (attRawName.startsWith("xmlns") 659 && (attRawName.equals("xmlns") 660 || attRawName.startsWith("xmlns:"))); 661 } 662 663 671 public static String getPrefixFromXMLNSDecl(String attRawName) 672 { 673 674 int index = attRawName.indexOf(':'); 675 676 return (index >= 0) ? attRawName.substring(index + 1) : ""; 677 } 678 679 686 public static String getLocalPart(String qname) 687 { 688 689 int index = qname.indexOf(':'); 690 691 return (index < 0) ? qname : qname.substring(index + 1); 692 } 693 694 701 public static String getPrefixPart(String qname) 702 { 703 704 int index = qname.indexOf(':'); 705 706 return (index >= 0) ? qname.substring(0, index) : ""; 707 } 708 } 709 | Popular Tags |