Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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