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. import org.apache.commons.jxpath.util.TypeUtils;
  23. import org.w3c.dom.Attr;
  24. /**
  25. * A Pointer that points to a DOM node.
  26. *
  27. * @author Dmitri Plotnikov
  28. * @version $Revision: 1.15 $ $Date: 2004/04/01 02:55:32 $
  29. */
  30. public class DOMAttributePointer extends NodePointer {
  31. private Attr attr;
  32. public DOMAttributePointer(NodePointer parent, Attr attr) {
  33. super(parent);
  34. this.attr = attr;
  35. }
  36. public QName getName() {
  37. return new QName(
  38. DOMNodePointer.getPrefix(attr),
  39. DOMNodePointer.getLocalName(attr));
  40. }
  41. public String getNamespaceURI() {
  42. String prefix = DOMNodePointer.getPrefix(attr);
  43. if (prefix == null) {
  44. return null;
  45. }
  46. return parent.getNamespaceURI(prefix);
  47. }
  48. public Object getValue() {
  49. String value = attr.getValue();
  50. if (value == null) {
  51. return null;
  52. }
  53. if (value.equals("") && !attr.getSpecified()) {
  54. return null;
  55. }
  56. return value;
  57. }
  58. public Object getBaseValue() {
  59. return attr;
  60. }
  61. public boolean isCollection() {
  62. return false;
  63. }
  64. public int getLength() {
  65. return 1;
  66. }
  67. public Object getImmediateNode() {
  68. return attr;
  69. }
  70. public boolean isActual() {
  71. return true;
  72. }
  73. public boolean isLeaf() {
  74. return true;
  75. }
  76. public boolean testNode(NodeTest nodeTest) {
  77. return nodeTest == null
  78. || ((nodeTest instanceof NodeTypeTest)
  79. && ((NodeTypeTest) nodeTest).getNodeType()
  80. == Compiler.NODE_TYPE_NODE);
  81. }
  82. /**
  83. * Sets the value of this attribute.
  84. */
  85. public void setValue(Object value) {
  86. attr.setValue((String) TypeUtils.convert(value, String.class));
  87. }
  88. public void remove() {
  89. attr.getOwnerElement().removeAttributeNode(attr);
  90. }
  91. /**
  92. */
  93. public String asPath() {
  94. StringBuffer buffer = new StringBuffer();
  95. if (parent != null) {
  96. buffer.append(parent.asPath());
  97. if (buffer.length() == 0
  98. || buffer.charAt(buffer.length() - 1) != '/') {
  99. buffer.append('/');
  100. }
  101. }
  102. buffer.append('@');
  103. buffer.append(getName());
  104. return buffer.toString();
  105. }
  106. public int hashCode() {
  107. return System.identityHashCode(attr);
  108. }
  109. public boolean equals(Object object) {
  110. if (object == this) {
  111. return true;
  112. }
  113. if (!(object instanceof DOMAttributePointer)) {
  114. return false;
  115. }
  116. DOMAttributePointer other = (DOMAttributePointer) object;
  117. return attr == other.attr;
  118. }
  119. public int compareChildNodePointers(
  120. NodePointer pointer1,
  121. NodePointer pointer2)
  122. {
  123. // Won't happen - attributes don't have children
  124. return 0;
  125. }
  126. }