1. /*
  2. * Copyright 1999-2004 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. package org.apache.commons.jxpath.ri;
  17. /**
  18. * A qualified name: a combination of an optional namespace prefix
  19. * and an local name.
  20. *
  21. * @author Dmitri Plotnikov
  22. * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:45 $
  23. */
  24. public class QName {
  25. private String prefix;
  26. private String name;
  27. public QName(String qualifiedName) {
  28. int index = qualifiedName.indexOf(':');
  29. if (index == -1) {
  30. prefix = null;
  31. name = qualifiedName;
  32. }
  33. else {
  34. prefix = qualifiedName.substring(0, index);
  35. name = qualifiedName.substring(index + 1);
  36. }
  37. }
  38. public QName(String prefix, String localName) {
  39. this.prefix = prefix;
  40. this.name = localName;
  41. }
  42. public String getPrefix() {
  43. return prefix;
  44. }
  45. public String getName() {
  46. return name;
  47. }
  48. public String toString() {
  49. if (prefix != null) {
  50. return prefix + ':' + name;
  51. }
  52. return name;
  53. }
  54. public int hashCode() {
  55. return name.hashCode();
  56. }
  57. public boolean equals(Object object) {
  58. if (!(object instanceof QName)) {
  59. return false;
  60. }
  61. if (this == object) {
  62. return true;
  63. }
  64. QName that = (QName) object;
  65. if (!this.name.equals(that.name)) {
  66. return false;
  67. }
  68. if ((this.prefix == null && that.prefix != null)
  69. || (this.prefix != null && !this.prefix.equals(that.prefix))) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. }