001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.jxpath.ri.compiler;
018
019 import org.apache.commons.jxpath.ri.EvalContext;
020 import org.apache.commons.jxpath.ri.axes.InitialContext;
021
022 /**
023 * @author Dmitri Plotnikov
024 * @version $Revision: 652845 $ $Date: 2008-05-02 12:46:46 -0500 (Fri, 02 May 2008) $
025 */
026 public class LocationPath extends Path {
027
028 private boolean absolute;
029
030 /**
031 * Create a new LocationPath.
032 * @param absolute whether this is an absolute path
033 * @param steps to evaluate
034 */
035 public LocationPath(boolean absolute, Step[] steps) {
036 super(steps);
037 this.absolute = absolute;
038 }
039
040 /**
041 * Learn whether this LocationPath is absolute.
042 * @return boolean
043 */
044 public boolean isAbsolute() {
045 return absolute;
046 }
047
048 public boolean computeContextDependent() {
049 return !absolute || super.computeContextDependent();
050 }
051
052 public String toString() {
053 StringBuffer buffer = new StringBuffer();
054 Step[] steps = getSteps();
055 if (steps != null) {
056 for (int i = 0; i < steps.length; i++) {
057 if (i > 0 || absolute) {
058 buffer.append('/');
059 }
060 buffer.append(steps[i]);
061 }
062 }
063 return buffer.toString();
064 }
065
066 public Object compute(EvalContext context) {
067 // Create a chain of contexts
068 EvalContext rootContext;
069 if (isAbsolute()) {
070 rootContext = context.getRootContext().getAbsoluteRootContext();
071 }
072 else {
073 rootContext = new InitialContext(context);
074 }
075 return evalSteps(rootContext);
076 }
077
078 public Object computeValue(EvalContext context) {
079 // Create a chain of contexts
080 EvalContext rootContext;
081 if (isAbsolute()) {
082 rootContext = context.getRootContext().getAbsoluteRootContext();
083 }
084 else {
085 rootContext = new InitialContext(context);
086 }
087 return getSingleNodePointerForSteps(rootContext);
088 }
089 }