01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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()
65: .getAbsoluteRootContext();
66: } else {
67: rootContext = new InitialContext(context);
68: }
69: return evalSteps(rootContext);
70: }
71:
72: public Object computeValue(EvalContext context) {
73: // Create a chain of contexts
74: EvalContext rootContext;
75: if (isAbsolute()) {
76: rootContext = context.getRootContext()
77: .getAbsoluteRootContext();
78: } else {
79: rootContext = new InitialContext(context);
80: }
81: return getSingleNodePointerForSteps(rootContext);
82: }
83: }
|