    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.compiler;
    17
    18  import org.apache.commons.jxpath.ri.EvalContext;
    19  import org.apache.commons.jxpath.ri.axes.InitialContext;
    20
    21  /***
    22   * @author Dmitri Plotnikov
    23   * @version $Revision: 1.11 $ $Date: 2004/02/29 14:17:39 $
    24   */
    25  public class LocationPath extends Path {
    26
    27      private boolean absolute;
    28
    29      public LocationPath(boolean absolute, Step[] steps) {
    30          super(steps);
    31          this.absolute = absolute;
    32      }
    33
    34      public boolean isAbsolute() {
    35          return absolute;
    36      }
    37
    38      public boolean computeContextDependent() {
    39          if (!absolute) {
    40              return true;
    41          }
    42
    43          return super.computeContextDependent();
    44      }
    45
    46      public String toString() {
    47          StringBuffer buffer = new StringBuffer();
    48          Step steps[] = getSteps();
    49          if (steps != null) {
    50              for (int i = 0; i < steps.length; i++) {
    51                  if (i > 0 || absolute) {
    52                      buffer.append('/');
    53                  }
    54                  buffer.append(steps[i]);
    55              }
    56          }
    57          return buffer.toString();
    58      }
    59
    60      public Object compute(EvalContext context) {
    61          // Create a chain of contexts
    62          EvalContext rootContext;
    63          if (isAbsolute()) {
    64              rootContext = context.getRootContext().getAbsoluteRootContext();
    65          }
    66          else {
    67              rootContext = new InitialContext(context);
    68          }
    69          return evalSteps(rootContext);
    70      }
    71
    72
    73      public Object computeValue(EvalContext context) {
    74          // Create a chain of contexts
    75          EvalContext rootContext;
    76          if (isAbsolute()) {
    77              rootContext = context.getRootContext().getAbsoluteRootContext();
    78          }
    79          else {
    80              rootContext = new InitialContext(context);
    81          }
    82          return getSingleNodePointerForSteps(rootContext);
    83      }
    84  }