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