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.dynamic;
  17. import java.util.Locale;
  18. import org.apache.commons.jxpath.DynamicPropertyHandler;
  19. import org.apache.commons.jxpath.JXPathIntrospector;
  20. import org.apache.commons.jxpath.ri.QName;
  21. import org.apache.commons.jxpath.ri.model.NodeIterator;
  22. import org.apache.commons.jxpath.ri.model.NodePointer;
  23. import org.apache.commons.jxpath.ri.model.beans.PropertyIterator;
  24. import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
  25. import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
  26. /**
  27. * A Pointer that points to an object with Dynamic Properties. It is used for
  28. * the first element of a path; following elements will by of type
  29. * PropertyPointer.
  30. *
  31. * @author Dmitri Plotnikov
  32. * @version $Revision: 1.6 $ $Date: 2004/02/29 14:17:44 $
  33. */
  34. public class DynamicPointer extends PropertyOwnerPointer {
  35. private QName name;
  36. private Object bean;
  37. private DynamicPropertyHandler handler;
  38. private String[] names;
  39. public DynamicPointer(QName name, Object bean,
  40. DynamicPropertyHandler handler, Locale locale)
  41. {
  42. super(null, locale);
  43. this.name = name;
  44. this.bean = bean;
  45. this.handler = handler;
  46. }
  47. public DynamicPointer(NodePointer parent, QName name,
  48. Object bean, DynamicPropertyHandler handler)
  49. {
  50. super(parent);
  51. this.name = name;
  52. this.bean = bean;
  53. this.handler = handler;
  54. }
  55. public PropertyPointer getPropertyPointer() {
  56. return new DynamicPropertyPointer(this, handler);
  57. }
  58. public NodeIterator createNodeIterator(
  59. String property, boolean reverse, NodePointer startWith)
  60. {
  61. return new PropertyIterator(this, property, reverse, startWith);
  62. }
  63. public NodeIterator attributeIterator(QName name) {
  64. return new DynamicAttributeIterator(this, name);
  65. }
  66. public QName getName() {
  67. return name;
  68. }
  69. public boolean isDynamicPropertyDeclarationSupported() {
  70. return true;
  71. }
  72. /**
  73. * Returns the DP object iself.
  74. */
  75. public Object getBaseValue() {
  76. return bean;
  77. }
  78. public boolean isLeaf() {
  79. Object value = getNode();
  80. return value == null
  81. || JXPathIntrospector.getBeanInfo(value.getClass()).isAtomic();
  82. }
  83. public boolean isCollection() {
  84. return false;
  85. }
  86. /**
  87. * Returns 1.
  88. */
  89. public int getLength() {
  90. return 1;
  91. }
  92. public String asPath() {
  93. if (parent != null) {
  94. return super.asPath();
  95. }
  96. return "/";
  97. }
  98. public int hashCode() {
  99. return System.identityHashCode(bean) + name.hashCode();
  100. }
  101. public boolean equals(Object object) {
  102. if (object == this) {
  103. return true;
  104. }
  105. if (!(object instanceof DynamicPointer)) {
  106. return false;
  107. }
  108. DynamicPointer other = (DynamicPointer) object;
  109. return bean == other.bean && name.equals(other.name);
  110. }
  111. }