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.axes;
  17. import org.apache.commons.jxpath.Function;
  18. import org.apache.commons.jxpath.JXPathContext;
  19. import org.apache.commons.jxpath.NodeSet;
  20. import org.apache.commons.jxpath.ri.EvalContext;
  21. import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
  22. import org.apache.commons.jxpath.ri.NamespaceResolver;
  23. import org.apache.commons.jxpath.ri.QName;
  24. import org.apache.commons.jxpath.ri.model.NodePointer;
  25. /**
  26. * EvalContext that is used to hold the root node for the path traversal.
  27. *
  28. * @author Dmitri Plotnikov
  29. * @version $Revision: 1.18 $ $Date: 2004/04/01 02:55:31 $
  30. */
  31. public class RootContext extends EvalContext {
  32. private JXPathContextReferenceImpl jxpathContext;
  33. private NodePointer pointer;
  34. private Object registers[];
  35. private int availableRegister = 0;
  36. private NamespaceResolver namespaceResolver;
  37. public static final Object UNKNOWN_VALUE = new Object();
  38. private static final int MAX_REGISTER = 4;
  39. public RootContext(
  40. JXPathContextReferenceImpl jxpathContext,
  41. NodePointer pointer)
  42. {
  43. super(null);
  44. this.jxpathContext = jxpathContext;
  45. this.pointer = pointer;
  46. if (pointer != null) {
  47. pointer.setNamespaceResolver(jxpathContext.getNamespaceResolver());
  48. }
  49. }
  50. public JXPathContext getJXPathContext() {
  51. return jxpathContext;
  52. }
  53. public RootContext getRootContext() {
  54. return this;
  55. }
  56. public EvalContext getAbsoluteRootContext() {
  57. return jxpathContext.getAbsoluteRootContext();
  58. }
  59. public NodePointer getCurrentNodePointer() {
  60. return pointer;
  61. }
  62. public Object getValue() {
  63. return pointer;
  64. }
  65. public int getCurrentPosition() {
  66. throw new UnsupportedOperationException();
  67. }
  68. public boolean nextNode() {
  69. throw new UnsupportedOperationException();
  70. }
  71. public boolean nextSet() {
  72. throw new UnsupportedOperationException();
  73. }
  74. public boolean setPosition(int position) {
  75. throw new UnsupportedOperationException();
  76. }
  77. public EvalContext getConstantContext(Object constant) {
  78. if (constant instanceof NodeSet) {
  79. return new NodeSetContext(
  80. new RootContext(jxpathContext, null),
  81. (NodeSet) constant);
  82. }
  83. NodePointer pointer;
  84. if (constant instanceof NodePointer) {
  85. pointer = (NodePointer) constant;
  86. }
  87. else {
  88. pointer = NodePointer.newNodePointer(
  89. new QName(null, ""),
  90. constant,
  91. null);
  92. }
  93. return new InitialContext(new RootContext(jxpathContext, pointer));
  94. }
  95. public EvalContext getVariableContext(QName variableName) {
  96. return new InitialContext(
  97. new RootContext(
  98. jxpathContext,
  99. jxpathContext.getVariablePointer(variableName)));
  100. }
  101. public Function getFunction(QName functionName, Object[] parameters) {
  102. return jxpathContext.getFunction(functionName, parameters);
  103. }
  104. public Object getRegisteredValue(int id) {
  105. if (registers == null || id >= MAX_REGISTER || id == -1) {
  106. return UNKNOWN_VALUE;
  107. }
  108. return registers[id];
  109. }
  110. public int setRegisteredValue(Object value) {
  111. if (registers == null) {
  112. registers = new Object[MAX_REGISTER];
  113. for (int i = 0; i < MAX_REGISTER; i++) {
  114. registers[i] = UNKNOWN_VALUE;
  115. }
  116. }
  117. if (availableRegister >= MAX_REGISTER) {
  118. return -1;
  119. }
  120. registers[availableRegister] = value;
  121. availableRegister++;
  122. return availableRegister - 1;
  123. }
  124. public String toString() {
  125. return super.toString() + ":" + pointer.asPath();
  126. }
  127. }