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;
  17. import org.apache.commons.jxpath.AbstractFactory;
  18. import org.apache.commons.jxpath.JXPathContext;
  19. import org.apache.commons.jxpath.JXPathException;
  20. import org.apache.commons.jxpath.JXPathIntrospector;
  21. import org.apache.commons.jxpath.Variables;
  22. import org.apache.commons.jxpath.ri.QName;
  23. import org.apache.commons.jxpath.ri.compiler.NodeTest;
  24. import org.apache.commons.jxpath.ri.model.beans.NullPointer;
  25. import org.apache.commons.jxpath.util.ValueUtils;
  26. /**
  27. * Pointer to a context variable.
  28. *
  29. * @author Dmitri Plotnikov
  30. * @version $Revision: 1.18 $ $Date: 2004/04/04 22:06:36 $
  31. */
  32. public class VariablePointer extends NodePointer {
  33. private Variables variables;
  34. private QName name;
  35. private NodePointer valuePointer;
  36. private boolean actual;
  37. public VariablePointer(Variables variables, QName name) {
  38. super(null);
  39. this.variables = variables;
  40. this.name = name;
  41. actual = true;
  42. }
  43. public VariablePointer(QName name) {
  44. super(null);
  45. this.name = name;
  46. actual = false;
  47. }
  48. public boolean isContainer() {
  49. return true;
  50. }
  51. public QName getName() {
  52. return name;
  53. }
  54. public Object getBaseValue() {
  55. if (!actual) {
  56. throw new JXPathException("Undefined variable: " + name);
  57. }
  58. return variables.getVariable(name.toString());
  59. }
  60. public boolean isLeaf() {
  61. Object value = getNode();
  62. return value == null
  63. || JXPathIntrospector.getBeanInfo(value.getClass()).isAtomic();
  64. }
  65. public boolean isCollection() {
  66. Object value = getBaseValue();
  67. return value != null && ValueUtils.isCollection(value);
  68. }
  69. public Object getImmediateNode() {
  70. Object value = getBaseValue();
  71. if (index != WHOLE_COLLECTION) {
  72. return ValueUtils.getValue(value, index);
  73. }
  74. else {
  75. return ValueUtils.getValue(value);
  76. }
  77. }
  78. public void setValue(Object value) {
  79. if (!actual) {
  80. throw new JXPathException("Cannot set undefined variable: " + name);
  81. }
  82. valuePointer = null;
  83. if (index != WHOLE_COLLECTION) {
  84. Object collection = getBaseValue();
  85. ValueUtils.setValue(collection, index, value);
  86. }
  87. else {
  88. variables.declareVariable(name.toString(), value);
  89. }
  90. }
  91. public boolean isActual() {
  92. return actual;
  93. }
  94. public void setIndex(int index) {
  95. super.setIndex(index);
  96. valuePointer = null;
  97. }
  98. public NodePointer getImmediateValuePointer() {
  99. if (valuePointer == null) {
  100. Object value = null;
  101. if (actual) {
  102. value = getImmediateNode();
  103. valuePointer =
  104. NodePointer.newChildNodePointer(this, null, value);
  105. }
  106. else {
  107. return new NullPointer(this, getName()) {
  108. public Object getImmediateNode() {
  109. throw new JXPathException(
  110. "Undefined variable: " + name);
  111. }
  112. };
  113. }
  114. }
  115. return valuePointer;
  116. }
  117. public int getLength() {
  118. if (actual) {
  119. Object value = getBaseValue();
  120. if (value == null) {
  121. return 1;
  122. }
  123. return ValueUtils.getLength(value);
  124. }
  125. return 0;
  126. }
  127. public NodePointer createPath(JXPathContext context, Object value) {
  128. if (actual) {
  129. setValue(value);
  130. return this;
  131. }
  132. NodePointer ptr = createPath(context);
  133. ptr.setValue(value);
  134. return ptr;
  135. }
  136. public NodePointer createPath(JXPathContext context) {
  137. if (!actual) {
  138. AbstractFactory factory = getAbstractFactory(context);
  139. if (!factory.declareVariable(context, name.toString())) {
  140. throw new JXPathException(
  141. "Factory cannot define variable '"
  142. + name
  143. + "' for path: "
  144. + asPath());
  145. }
  146. findVariables(context);
  147. // Assert: actual == true
  148. }
  149. return this;
  150. }
  151. public NodePointer createChild(
  152. JXPathContext context,
  153. QName name,
  154. int index)
  155. {
  156. Object collection = createCollection(context, index);
  157. if (!isActual() || (index != 0 && index != WHOLE_COLLECTION)) {
  158. AbstractFactory factory = getAbstractFactory(context);
  159. boolean success =
  160. factory.createObject(
  161. context,
  162. this,
  163. collection,
  164. getName().toString(),
  165. index);
  166. if (!success) {
  167. throw new JXPathException(
  168. "Factory could not create object path: " + asPath());
  169. }
  170. NodePointer cln = (NodePointer) clone();
  171. cln.setIndex(index);
  172. return cln;
  173. }
  174. return this;
  175. }
  176. public NodePointer createChild(
  177. JXPathContext context,
  178. QName name,
  179. int index,
  180. Object value)
  181. {
  182. Object collection = createCollection(context, index);
  183. ValueUtils.setValue(collection, index, value);
  184. NodePointer cl = (NodePointer) clone();
  185. cl.setIndex(index);
  186. return cl;
  187. }
  188. private Object createCollection(JXPathContext context, int index) {
  189. createPath(context);
  190. Object collection = getBaseValue();
  191. if (collection == null) {
  192. throw new JXPathException(
  193. "Factory did not assign a collection to variable '"
  194. + name
  195. + "' for path: "
  196. + asPath());
  197. }
  198. if (index == WHOLE_COLLECTION) {
  199. index = 0;
  200. }
  201. else if (index < 0) {
  202. throw new JXPathException("Index is less than 1: " + asPath());
  203. }
  204. if (index >= getLength()) {
  205. collection = ValueUtils.expandCollection(collection, index + 1);
  206. variables.declareVariable(name.toString(), collection);
  207. }
  208. return collection;
  209. }
  210. public void remove() {
  211. if (actual) {
  212. if (index == WHOLE_COLLECTION) {
  213. variables.undeclareVariable(name.toString());
  214. }
  215. else {
  216. if (index < 0) {
  217. throw new JXPathException(
  218. "Index is less than 1: " + asPath());
  219. }
  220. Object collection = getBaseValue();
  221. if (collection != null && index < getLength()) {
  222. collection = ValueUtils.remove(collection, index);
  223. variables.declareVariable(name.toString(), collection);
  224. }
  225. }
  226. }
  227. }
  228. protected void findVariables(JXPathContext context) {
  229. valuePointer = null;
  230. JXPathContext varCtx = context;
  231. while (varCtx != null) {
  232. variables = varCtx.getVariables();
  233. if (variables.isDeclaredVariable(name.toString())) {
  234. actual = true;
  235. break;
  236. }
  237. varCtx = varCtx.getParentContext();
  238. variables = null;
  239. }
  240. }
  241. public int hashCode() {
  242. return (actual ? System.identityHashCode(variables) : 0)
  243. + name.hashCode()
  244. + index;
  245. }
  246. public boolean equals(Object object) {
  247. if (object == this) {
  248. return true;
  249. }
  250. if (!(object instanceof VariablePointer)) {
  251. return false;
  252. }
  253. VariablePointer other = (VariablePointer) object;
  254. return variables == other.variables
  255. && name.equals(other.name)
  256. && index == other.index;
  257. }
  258. public String asPath() {
  259. StringBuffer buffer = new StringBuffer();
  260. buffer.append('$');
  261. buffer.append(name);
  262. if (!actual) {
  263. if (index != WHOLE_COLLECTION) {
  264. buffer.append('[').append(index + 1).append(']');
  265. }
  266. }
  267. else if (
  268. index != WHOLE_COLLECTION
  269. && (getNode() == null || isCollection())) {
  270. buffer.append('[').append(index + 1).append(']');
  271. }
  272. return buffer.toString();
  273. }
  274. public NodeIterator childIterator(
  275. NodeTest test,
  276. boolean reverse,
  277. NodePointer startWith)
  278. {
  279. return getValuePointer().childIterator(test, reverse, startWith);
  280. }
  281. public NodeIterator attributeIterator(QName name) {
  282. return getValuePointer().attributeIterator(name);
  283. }
  284. public NodeIterator namespaceIterator() {
  285. return getValuePointer().namespaceIterator();
  286. }
  287. public NodePointer namespacePointer(String name) {
  288. return getValuePointer().namespacePointer(name);
  289. }
  290. public boolean testNode(NodeTest nodeTest) {
  291. return getValuePointer().testNode(nodeTest);
  292. }
  293. private AbstractFactory getAbstractFactory(JXPathContext context) {
  294. AbstractFactory factory = context.getFactory();
  295. if (factory == null) {
  296. throw new JXPathException(
  297. "Factory is not set on the JXPathContext - cannot create path: "
  298. + asPath());
  299. }
  300. return factory;
  301. }
  302. public int compareChildNodePointers(
  303. NodePointer pointer1,
  304. NodePointer pointer2)
  305. {
  306. return pointer1.getIndex() - pointer2.getIndex();
  307. }
  308. }