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.model.jdom;
  17. import org.apache.commons.jxpath.ri.QName;
  18. import org.apache.commons.jxpath.ri.model.NodePointer;
  19. /**
  20. * Represents a namespace node.
  21. *
  22. * @author Dmitri Plotnikov
  23. * @version $Revision: 1.9 $ $Date: 2004/04/01 02:55:31 $
  24. */
  25. public class JDOMNamespacePointer extends NodePointer {
  26. private String prefix;
  27. private String namespaceURI;
  28. public JDOMNamespacePointer(NodePointer parent, String prefix) {
  29. super(parent);
  30. this.prefix = prefix;
  31. }
  32. public JDOMNamespacePointer(
  33. NodePointer parent,
  34. String prefix,
  35. String namespaceURI)
  36. {
  37. super(parent);
  38. this.prefix = prefix;
  39. this.namespaceURI = namespaceURI;
  40. }
  41. public QName getName() {
  42. return new QName(prefix);
  43. }
  44. public Object getBaseValue() {
  45. return null;
  46. }
  47. public boolean isCollection() {
  48. return false;
  49. }
  50. public int getLength() {
  51. return 1;
  52. }
  53. public Object getImmediateNode() {
  54. return getNamespaceURI();
  55. }
  56. public String getNamespaceURI() {
  57. if (namespaceURI == null) {
  58. namespaceURI = parent.getNamespaceURI(prefix);
  59. }
  60. return namespaceURI;
  61. }
  62. public boolean isLeaf() {
  63. return true;
  64. }
  65. /**
  66. * Throws UnsupportedOperationException.
  67. */
  68. public void setValue(Object value) {
  69. throw new UnsupportedOperationException("Cannot modify a namespace");
  70. }
  71. public String asPath() {
  72. StringBuffer buffer = new StringBuffer();
  73. if (parent != null) {
  74. buffer.append(parent.asPath());
  75. if (buffer.length() == 0
  76. || buffer.charAt(buffer.length() - 1) != '/') {
  77. buffer.append('/');
  78. }
  79. }
  80. buffer.append("namespace::");
  81. buffer.append(prefix);
  82. return buffer.toString();
  83. }
  84. public int hashCode() {
  85. return prefix.hashCode();
  86. }
  87. public boolean equals(Object object) {
  88. if (object == this) {
  89. return true;
  90. }
  91. if (!(object instanceof JDOMNamespacePointer)) {
  92. return false;
  93. }
  94. JDOMNamespacePointer other = (JDOMNamespacePointer) object;
  95. return prefix.equals(other.prefix);
  96. }
  97. public int compareChildNodePointers(
  98. NodePointer pointer1,
  99. NodePointer pointer2)
  100. {
  101. // Won't happen - namespaces don't have children
  102. return 0;
  103. }
  104. }