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.dynabeans;
  17. import java.util.Locale;
  18. import org.apache.commons.beanutils.DynaBean;
  19. import org.apache.commons.jxpath.ri.QName;
  20. import org.apache.commons.jxpath.ri.model.NodePointer;
  21. import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
  22. import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
  23. /**
  24. * A Pointer that points to a DynaBean.
  25. *
  26. * @author Dmitri Plotnikov
  27. * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:40 $
  28. */
  29. public class DynaBeanPointer extends PropertyOwnerPointer {
  30. private QName name;
  31. private DynaBean dynaBean;
  32. public DynaBeanPointer(QName name, DynaBean dynaBean, Locale locale) {
  33. super(null, locale);
  34. this.name = name;
  35. this.dynaBean = dynaBean;
  36. }
  37. /**
  38. * @param name is the name given to the first node
  39. */
  40. public DynaBeanPointer(NodePointer parent, QName name, DynaBean dynaBean) {
  41. super(parent);
  42. this.name = name;
  43. this.dynaBean = dynaBean;
  44. }
  45. public PropertyPointer getPropertyPointer() {
  46. return new DynaBeanPropertyPointer(this, dynaBean);
  47. }
  48. public QName getName() {
  49. return name;
  50. }
  51. /**
  52. * Returns the bean itself
  53. */
  54. public Object getBaseValue() {
  55. return dynaBean;
  56. }
  57. public Object getImmediateNode() {
  58. return dynaBean;
  59. }
  60. public boolean isCollection() {
  61. return false;
  62. }
  63. /**
  64. * Returns 1.
  65. */
  66. public int getLength() {
  67. return 1;
  68. }
  69. public boolean isLeaf() {
  70. return false;
  71. }
  72. public int hashCode() {
  73. return name == null ? 0 : name.hashCode();
  74. }
  75. public boolean equals(Object object) {
  76. if (object == this) {
  77. return true;
  78. }
  79. if (!(object instanceof DynaBeanPointer)) {
  80. return false;
  81. }
  82. DynaBeanPointer other = (DynaBeanPointer) object;
  83. if (parent != other.parent) {
  84. if (parent == null || !parent.equals(other.parent)) {
  85. return false;
  86. }
  87. }
  88. if ((name == null && other.name != null)
  89. || (name != null && !name.equals(other.name))) {
  90. return false;
  91. }
  92. int iThis = (index == WHOLE_COLLECTION ? 0 : index);
  93. int iOther = (other.index == WHOLE_COLLECTION ? 0 : other.index);
  94. if (iThis != iOther) {
  95. return false;
  96. }
  97. return dynaBean == other.dynaBean;
  98. }
  99. /**
  100. * If there's a parent - parent's path, otherwise "/".
  101. */
  102. public String asPath() {
  103. if (parent != null) {
  104. return super.asPath();
  105. }
  106. return "/";
  107. }
  108. }