You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QName.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.util;
  18. import java.io.Serializable;
  19. /**
  20. * Represents a qualified name of an XML element or an XML attribute.
  21. * <p>
  22. * Note: This class allows to carry a namespace prefix but it is not used in the equals() and
  23. * hashCode() methods.
  24. */
  25. public class QName implements Serializable {
  26. private static final long serialVersionUID = -5225376740044770690L;
  27. private String namespaceURI;
  28. private String localName;
  29. private String prefix;
  30. private int hashCode;
  31. /**
  32. * Main constructor.
  33. * @param namespaceURI the namespace URI
  34. * @param prefix the namespace prefix, may be null
  35. * @param localName the local name
  36. */
  37. public QName(String namespaceURI, String prefix, String localName) {
  38. if (localName == null) {
  39. throw new NullPointerException("Parameter localName must not be null");
  40. }
  41. if (localName.length() == 0) {
  42. throw new IllegalArgumentException("Parameter localName must not be empty");
  43. }
  44. this.namespaceURI = namespaceURI;
  45. this.prefix = prefix;
  46. this.localName = localName;
  47. this.hashCode = toHashString().hashCode();
  48. }
  49. /** @return the namespace URI */
  50. public String getNamespaceURI() {
  51. return this.namespaceURI;
  52. }
  53. /** @return the namespace prefix */
  54. public String getPrefix() {
  55. return this.prefix;
  56. }
  57. /** @return the local name */
  58. public String getLocalName() {
  59. return this.localName;
  60. }
  61. /** @return the fully qualified name */
  62. public String getQName() {
  63. return getPrefix() != null ? getPrefix() + ':' + getLocalName() : getLocalName();
  64. }
  65. /** @see java.lang.Object#hashCode() */
  66. public int hashCode() {
  67. return this.hashCode;
  68. }
  69. /** @see java.lang.Object#equals(java.lang.Object) */
  70. public boolean equals(Object obj) {
  71. if (obj == null) {
  72. return false;
  73. } else if (obj == this) {
  74. return true;
  75. } else {
  76. if (obj instanceof QName) {
  77. QName other = (QName)obj;
  78. if ((getNamespaceURI() == null && other.getNamespaceURI() == null)
  79. || getNamespaceURI().equals(other.getNamespaceURI())) {
  80. return getLocalName().equals(other.getLocalName());
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. /** @see java.lang.Object#toString() */
  87. public String toString() {
  88. return prefix != null
  89. ? (prefix + ":" + localName)
  90. : toHashString();
  91. }
  92. private String toHashString() {
  93. return (namespaceURI != null
  94. ? ("{" + namespaceURI + "}" + localName)
  95. : localName);
  96. }
  97. }