View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.compiler;
19  
20  import org.apache.commons.jxpath.ri.EvalContext;
21  import org.apache.commons.jxpath.ri.axes.InitialContext;
22  
23  /**
24   * A location path expression.
25   */
26  public class LocationPath extends Path {
27  
28      private final boolean absolute;
29  
30      /**
31       * Constructs a new LocationPath.
32       *
33       * @param absolute whether this is an absolute path
34       * @param steps    to evaluate
35       */
36      public LocationPath(final boolean absolute, final Step[] steps) {
37          super(steps);
38          this.absolute = absolute;
39      }
40  
41      @Override
42      public Object compute(final EvalContext context) {
43          // Create a chain of contexts
44          EvalContext rootContext;
45          if (isAbsolute()) {
46              rootContext = context.getRootContext().getAbsoluteRootContext();
47          } else {
48              rootContext = new InitialContext(context);
49          }
50          return evalSteps(rootContext);
51      }
52  
53      @Override
54      public boolean computeContextDependent() {
55          return !absolute || super.computeContextDependent();
56      }
57  
58      @Override
59      public Object computeValue(final EvalContext context) {
60          // Create a chain of contexts
61          EvalContext rootContext;
62          if (isAbsolute()) {
63              rootContext = context.getRootContext().getAbsoluteRootContext();
64          } else {
65              rootContext = new InitialContext(context);
66          }
67          return getSingleNodePointerForSteps(rootContext);
68      }
69  
70      /**
71       * Tests whether this LocationPath is absolute.
72       *
73       * @return boolean
74       */
75      public boolean isAbsolute() {
76          return absolute;
77      }
78  
79      @Override
80      public String toString() {
81          final StringBuilder buffer = new StringBuilder();
82          final Step[] steps = getSteps();
83          if (steps != null) {
84              for (int i = 0; i < steps.length; i++) {
85                  if (i > 0 || absolute) {
86                      buffer.append('/');
87                  }
88                  buffer.append(steps[i]);
89              }
90          }
91          return buffer.toString();
92      }
93  }