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.beans;
  17. import org.apache.commons.jxpath.JXPathContext;
  18. import org.apache.commons.jxpath.ri.QName;
  19. import org.apache.commons.jxpath.ri.model.NodePointer;
  20. /**
  21. * Used when there is a need to construct a Pointer for a collection element
  22. * that does not exist. For example, if the path is "foo[3]", but the
  23. * collection "foo" only has one element or is empty or is null, the
  24. * NullElementPointer can be used to capture this situation without putting a
  25. * regular NodePointer into an invalid state. Just create a NullElementPointer
  26. * with index 2 (= 3 - 1) and a "foo" pointer as the parent.
  27. *
  28. * @author Dmitri Plotnikov
  29. * @version $Revision: 1.17 $ $Date: 2004/03/25 03:49:50 $
  30. */
  31. public class NullElementPointer extends CollectionPointer {
  32. public NullElementPointer(NodePointer parent, int index) {
  33. super(parent, (Object) null);
  34. this.index = index;
  35. }
  36. public QName getName() {
  37. return null;
  38. }
  39. public Object getBaseValue() {
  40. return null;
  41. }
  42. public Object getImmediateNode() {
  43. return null;
  44. }
  45. public boolean isLeaf() {
  46. return true;
  47. }
  48. public boolean isCollection() {
  49. return false;
  50. }
  51. public PropertyPointer getPropertyPointer() {
  52. return new NullPropertyPointer(this);
  53. }
  54. public NodePointer getValuePointer() {
  55. return new NullPointer(this, getName());
  56. }
  57. public void setValue(Object value) {
  58. throw new UnsupportedOperationException(
  59. "Collection element does not exist: " + this);
  60. }
  61. public boolean isActual() {
  62. return false;
  63. }
  64. public boolean isContainer() {
  65. return true;
  66. }
  67. public NodePointer createPath(JXPathContext context) {
  68. return parent.createChild(context, null, index);
  69. }
  70. public NodePointer createPath(JXPathContext context, Object value) {
  71. return parent.createChild(context, null, index, value);
  72. }
  73. public int hashCode() {
  74. return getImmediateParentPointer().hashCode() + index;
  75. }
  76. public boolean equals(Object object) {
  77. if (object == this) {
  78. return true;
  79. }
  80. if (!(object instanceof NullElementPointer)) {
  81. return false;
  82. }
  83. NullElementPointer other = (NullElementPointer) object;
  84. return getImmediateParentPointer() == other.getImmediateParentPointer()
  85. && index == other.index;
  86. }
  87. public int getLength() {
  88. return 0;
  89. }
  90. public String asPath() {
  91. StringBuffer buffer = new StringBuffer();
  92. NodePointer parent = getImmediateParentPointer();
  93. if (parent != null) {
  94. buffer.append(parent.asPath());
  95. }
  96. if (index != WHOLE_COLLECTION) {
  97. // Address the list[1][2] case
  98. if (parent != null && parent.getIndex() != WHOLE_COLLECTION) {
  99. buffer.append("/.");
  100. }
  101. else if (parent != null
  102. && parent.getImmediateParentPointer() != null
  103. && parent.getImmediateParentPointer().getIndex() !=
  104. WHOLE_COLLECTION)
  105. {
  106. buffer.append("/.");
  107. }
  108. buffer.append("[").append(index + 1).append(']');
  109. }
  110. return buffer.toString();
  111. }
  112. }