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