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