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