001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.axes;
017:
018: import org.apache.commons.jxpath.Function;
019: import org.apache.commons.jxpath.JXPathContext;
020: import org.apache.commons.jxpath.NodeSet;
021: import org.apache.commons.jxpath.ri.EvalContext;
022: import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
023: import org.apache.commons.jxpath.ri.NamespaceResolver;
024: import org.apache.commons.jxpath.ri.QName;
025: import org.apache.commons.jxpath.ri.model.NodePointer;
026:
027: /**
028: * EvalContext that is used to hold the root node for the path traversal.
029: *
030: * @author Dmitri Plotnikov
031: * @version $Revision: 1.18 $ $Date: 2004/04/01 02:55:31 $
032: */
033: public class RootContext extends EvalContext {
034: private JXPathContextReferenceImpl jxpathContext;
035: private NodePointer pointer;
036: private Object registers[];
037: private int availableRegister = 0;
038: private NamespaceResolver namespaceResolver;
039: public static final Object UNKNOWN_VALUE = new Object();
040: private static final int MAX_REGISTER = 4;
041:
042: public RootContext(JXPathContextReferenceImpl jxpathContext,
043: NodePointer pointer) {
044: super (null);
045: this .jxpathContext = jxpathContext;
046: this .pointer = pointer;
047: if (pointer != null) {
048: pointer.setNamespaceResolver(jxpathContext
049: .getNamespaceResolver());
050: }
051: }
052:
053: public JXPathContext getJXPathContext() {
054: return jxpathContext;
055: }
056:
057: public RootContext getRootContext() {
058: return this ;
059: }
060:
061: public EvalContext getAbsoluteRootContext() {
062: return jxpathContext.getAbsoluteRootContext();
063: }
064:
065: public NodePointer getCurrentNodePointer() {
066: return pointer;
067: }
068:
069: public Object getValue() {
070: return pointer;
071: }
072:
073: public int getCurrentPosition() {
074: throw new UnsupportedOperationException();
075: }
076:
077: public boolean nextNode() {
078: throw new UnsupportedOperationException();
079: }
080:
081: public boolean nextSet() {
082: throw new UnsupportedOperationException();
083: }
084:
085: public boolean setPosition(int position) {
086: throw new UnsupportedOperationException();
087: }
088:
089: public EvalContext getConstantContext(Object constant) {
090: if (constant instanceof NodeSet) {
091: return new NodeSetContext(new RootContext(jxpathContext,
092: null), (NodeSet) constant);
093: }
094:
095: NodePointer pointer;
096: if (constant instanceof NodePointer) {
097: pointer = (NodePointer) constant;
098: } else {
099: pointer = NodePointer.newNodePointer(new QName(null, ""),
100: constant, null);
101: }
102: return new InitialContext(new RootContext(jxpathContext,
103: pointer));
104: }
105:
106: public EvalContext getVariableContext(QName variableName) {
107: return new InitialContext(new RootContext(jxpathContext,
108: jxpathContext.getVariablePointer(variableName)));
109: }
110:
111: public Function getFunction(QName functionName, Object[] parameters) {
112: return jxpathContext.getFunction(functionName, parameters);
113: }
114:
115: public Object getRegisteredValue(int id) {
116: if (registers == null || id >= MAX_REGISTER || id == -1) {
117: return UNKNOWN_VALUE;
118: }
119: return registers[id];
120: }
121:
122: public int setRegisteredValue(Object value) {
123: if (registers == null) {
124: registers = new Object[MAX_REGISTER];
125: for (int i = 0; i < MAX_REGISTER; i++) {
126: registers[i] = UNKNOWN_VALUE;
127: }
128: }
129: if (availableRegister >= MAX_REGISTER) {
130: return -1;
131: }
132: registers[availableRegister] = value;
133: availableRegister++;
134: return availableRegister - 1;
135: }
136:
137: public String toString() {
138: return super .toString() + ":" + pointer.asPath();
139: }
140: }
|