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 java.util.Locale;
  18. import org.apache.commons.jxpath.JXPathException;
  19. import org.apache.commons.jxpath.ri.Compiler;
  20. import org.apache.commons.jxpath.ri.QName;
  21. import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
  22. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  23. import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
  24. import org.apache.commons.jxpath.ri.model.NodeIterator;
  25. import org.apache.commons.jxpath.ri.model.NodePointer;
  26. import org.apache.commons.jxpath.util.ValueUtils;
  27. /**
  28. * A pointer describing a node that has properties, each of which could be
  29. * a collection.
  30. *
  31. * @author Dmitri Plotnikov
  32. * @version $Revision: 1.19 $ $Date: 2004/04/04 22:06:36 $
  33. */
  34. public abstract class PropertyOwnerPointer extends NodePointer {
  35. public NodeIterator childIterator(
  36. NodeTest test,
  37. boolean reverse,
  38. NodePointer startWith)
  39. {
  40. if (test == null) {
  41. return createNodeIterator(null, reverse, startWith);
  42. }
  43. else if (test instanceof NodeNameTest) {
  44. NodeNameTest nodeNameTest = (NodeNameTest) test;
  45. QName testName = nodeNameTest.getNodeName();
  46. String property;
  47. if (!isDefaultNamespace(testName.getPrefix())) {
  48. return null;
  49. }
  50. else if (nodeNameTest.isWildcard()) {
  51. property = null;
  52. }
  53. else {
  54. property = testName.getName();
  55. }
  56. return createNodeIterator(property, reverse, startWith);
  57. }
  58. else if (test instanceof NodeTypeTest) {
  59. if (((NodeTypeTest) test).getNodeType()
  60. == Compiler.NODE_TYPE_NODE) {
  61. return createNodeIterator(null, reverse, startWith);
  62. }
  63. }
  64. return null;
  65. }
  66. public NodeIterator createNodeIterator(
  67. String property,
  68. boolean reverse,
  69. NodePointer startWith)
  70. {
  71. return new PropertyIterator(this, property, reverse, startWith);
  72. }
  73. public NodeIterator attributeIterator(QName name) {
  74. return new BeanAttributeIterator(this, name);
  75. }
  76. protected PropertyOwnerPointer(NodePointer parent, Locale locale) {
  77. super(parent, locale);
  78. }
  79. protected PropertyOwnerPointer(NodePointer parent) {
  80. super(parent);
  81. }
  82. public void setIndex(int index) {
  83. if (this.index != index) {
  84. super.setIndex(index);
  85. value = UNINITIALIZED;
  86. }
  87. }
  88. private static final Object UNINITIALIZED = new Object();
  89. private Object value = UNINITIALIZED;
  90. public Object getImmediateNode() {
  91. if (value == UNINITIALIZED) {
  92. if (index == WHOLE_COLLECTION) {
  93. value = ValueUtils.getValue(getBaseValue());
  94. }
  95. else {
  96. value = ValueUtils.getValue(getBaseValue(), index);
  97. }
  98. }
  99. return value;
  100. }
  101. public abstract QName getName();
  102. /**
  103. * Throws an exception if you try to change the root element, otherwise
  104. * forwards the call to the parent pointer.
  105. */
  106. public void setValue(Object value) {
  107. this.value = value;
  108. if (parent.isContainer()) {
  109. parent.setValue(value);
  110. }
  111. else if (parent != null) {
  112. if (index == WHOLE_COLLECTION) {
  113. throw new UnsupportedOperationException(
  114. "Cannot setValue of an object that is not "
  115. + "some other object's property");
  116. }
  117. else {
  118. throw new JXPathException(
  119. "The specified collection element does not exist: " + this);
  120. }
  121. }
  122. else {
  123. throw new UnsupportedOperationException(
  124. "Cannot replace the root object");
  125. }
  126. }
  127. /**
  128. * If this is a root node pointer, throws an exception; otherwise
  129. * forwards the call to the parent node.
  130. */
  131. public void remove() {
  132. this.value = null;
  133. if (parent != null) {
  134. parent.remove();
  135. }
  136. else {
  137. throw new UnsupportedOperationException(
  138. "Cannot remove an object that is not "
  139. + "some other object's property or a collection element");
  140. }
  141. }
  142. public abstract PropertyPointer getPropertyPointer();
  143. /**
  144. * @return true if the property owner can set a property "does not exist".
  145. * A good example is a Map. You can always assign a value to any
  146. * key even if it has never been "declared".
  147. */
  148. public boolean isDynamicPropertyDeclarationSupported() {
  149. return false;
  150. }
  151. public int compareChildNodePointers(
  152. NodePointer pointer1,
  153. NodePointer pointer2)
  154. {
  155. int r =
  156. pointer1.getName().toString().compareTo(
  157. pointer2.getName().toString());
  158. if (r != 0) {
  159. return r;
  160. }
  161. return pointer1.getIndex() - pointer2.getIndex();
  162. }
  163. }