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.container;
  17. import java.util.Locale;
  18. import org.apache.commons.jxpath.Container;
  19. import org.apache.commons.jxpath.ri.QName;
  20. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  21. import org.apache.commons.jxpath.ri.model.NodeIterator;
  22. import org.apache.commons.jxpath.ri.model.NodePointer;
  23. import org.apache.commons.jxpath.util.ValueUtils;
  24. /**
  25. * Transparent pointer to a Container. The getValue() method
  26. * returns the contents of the container, rather than the container
  27. * itself.
  28. *
  29. * @author Dmitri Plotnikov
  30. * @version $Revision: 1.13 $ $Date: 2004/04/04 22:06:36 $
  31. */
  32. public class ContainerPointer extends NodePointer {
  33. private Container container;
  34. private NodePointer valuePointer;
  35. public ContainerPointer(Container container, Locale locale) {
  36. super(null, locale);
  37. this.container = container;
  38. }
  39. public ContainerPointer(NodePointer parent, Container container) {
  40. super(parent);
  41. this.container = container;
  42. }
  43. /**
  44. * This type of node is auxiliary.
  45. */
  46. public boolean isContainer() {
  47. return true;
  48. }
  49. public QName getName() {
  50. return null;
  51. }
  52. public Object getBaseValue() {
  53. return container;
  54. }
  55. public boolean isCollection() {
  56. Object value = getBaseValue();
  57. return value != null && ValueUtils.isCollection(value);
  58. }
  59. public int getLength() {
  60. Object value = getBaseValue();
  61. if (value == null) {
  62. return 1;
  63. }
  64. return ValueUtils.getLength(value);
  65. }
  66. public boolean isLeaf() {
  67. return getValuePointer().isLeaf();
  68. }
  69. public Object getImmediateNode() {
  70. Object value = getBaseValue();
  71. if (index != WHOLE_COLLECTION) {
  72. if (index >= 0 && index < getLength()) {
  73. return ValueUtils.getValue(value, index);
  74. }
  75. else {
  76. return null;
  77. }
  78. }
  79. else {
  80. return ValueUtils.getValue(value);
  81. }
  82. }
  83. public void setValue(Object value) {
  84. // TODO: what if this is a collection?
  85. container.setValue(value);
  86. }
  87. public NodePointer getImmediateValuePointer() {
  88. if (valuePointer == null) {
  89. Object value = getImmediateNode();
  90. valuePointer =
  91. NodePointer.newChildNodePointer(this, getName(), value);
  92. }
  93. return valuePointer;
  94. }
  95. public int hashCode() {
  96. return System.identityHashCode(container) + index;
  97. }
  98. public boolean equals(Object object) {
  99. if (object == this) {
  100. return true;
  101. }
  102. if (!(object instanceof ContainerPointer)) {
  103. return false;
  104. }
  105. ContainerPointer other = (ContainerPointer) object;
  106. return container == other.container && index == other.index;
  107. }
  108. public NodeIterator childIterator(
  109. NodeTest test,
  110. boolean reverse,
  111. NodePointer startWith)
  112. {
  113. return getValuePointer().childIterator(test, reverse, startWith);
  114. }
  115. public NodeIterator attributeIterator(QName name) {
  116. return getValuePointer().attributeIterator(name);
  117. }
  118. public NodeIterator namespaceIterator() {
  119. return getValuePointer().namespaceIterator();
  120. }
  121. public NodePointer namespacePointer(String namespace) {
  122. return getValuePointer().namespacePointer(namespace);
  123. }
  124. public boolean testNode(NodeTest nodeTest) {
  125. return getValuePointer().testNode(nodeTest);
  126. }
  127. public int compareChildNodePointers(
  128. NodePointer pointer1,
  129. NodePointer pointer2)
  130. {
  131. return pointer1.getIndex() - pointer2.getIndex();
  132. }
  133. public String getNamespaceURI(String prefix) {
  134. return getValuePointer().getNamespaceURI(prefix);
  135. }
  136. public String asPath() {
  137. if (parent != null) {
  138. return parent.asPath();
  139. }
  140. return "/";
  141. }
  142. }