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.Compiler;
  18. /**
  19. * @author Dmitri Plotnikov
  20. * @version $Revision: 1.9 $ $Date: 2004/02/29 14:17:39 $
  21. */
  22. public class Step {
  23. private int axis;
  24. private NodeTest nodeTest;
  25. private Expression[] predicates;
  26. protected Step(int axis, NodeTest nodeTest, Expression[] predicates) {
  27. this.axis = axis;
  28. this.nodeTest = nodeTest;
  29. this.predicates = predicates;
  30. }
  31. public int getAxis() {
  32. return axis;
  33. }
  34. public NodeTest getNodeTest() {
  35. return nodeTest;
  36. }
  37. public Expression[] getPredicates() {
  38. return predicates;
  39. }
  40. public boolean isContextDependent() {
  41. if (predicates != null) {
  42. for (int i = 0; i < predicates.length; i++) {
  43. if (predicates[i].isContextDependent()) {
  44. return true;
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. public String toString() {
  51. StringBuffer buffer = new StringBuffer();
  52. int axis = getAxis();
  53. if (axis == Compiler.AXIS_CHILD) {
  54. buffer.append(nodeTest);
  55. }
  56. else if (axis == Compiler.AXIS_ATTRIBUTE) {
  57. buffer.append('@');
  58. buffer.append(nodeTest);
  59. }
  60. else if (axis == Compiler.AXIS_SELF
  61. && nodeTest instanceof NodeTypeTest
  62. && ((NodeTypeTest) nodeTest).getNodeType()
  63. == Compiler.NODE_TYPE_NODE) {
  64. buffer.append(".");
  65. }
  66. else if (axis == Compiler.AXIS_PARENT
  67. && nodeTest instanceof NodeTypeTest
  68. && ((NodeTypeTest) nodeTest).getNodeType()
  69. == Compiler.NODE_TYPE_NODE) {
  70. buffer.append("..");
  71. }
  72. else if (axis == Compiler.AXIS_DESCENDANT_OR_SELF
  73. && nodeTest instanceof NodeTypeTest
  74. && ((NodeTypeTest) nodeTest).getNodeType()
  75. == Compiler.NODE_TYPE_NODE
  76. && (predicates == null || predicates.length == 0)) {
  77. buffer.append("");
  78. }
  79. else {
  80. buffer.append(axisToString(axis));
  81. buffer.append("::");
  82. buffer.append(nodeTest);
  83. }
  84. Expression[] predicates = getPredicates();
  85. if (predicates != null) {
  86. for (int i = 0; i < predicates.length; i++) {
  87. buffer.append('[');
  88. buffer.append(predicates[i]);
  89. buffer.append(']');
  90. }
  91. }
  92. return buffer.toString();
  93. }
  94. public static String axisToString(int axis) {
  95. switch (axis) {
  96. case Compiler.AXIS_SELF :
  97. return "self";
  98. case Compiler.AXIS_CHILD :
  99. return "child";
  100. case Compiler.AXIS_PARENT :
  101. return "parent";
  102. case Compiler.AXIS_ANCESTOR :
  103. return "ancestor";
  104. case Compiler.AXIS_ATTRIBUTE :
  105. return "attribute";
  106. case Compiler.AXIS_NAMESPACE :
  107. return "namespace";
  108. case Compiler.AXIS_PRECEDING :
  109. return "preceding";
  110. case Compiler.AXIS_FOLLOWING :
  111. return "following";
  112. case Compiler.AXIS_DESCENDANT :
  113. return "descendant";
  114. case Compiler.AXIS_ANCESTOR_OR_SELF :
  115. return "ancestor-or-self";
  116. case Compiler.AXIS_FOLLOWING_SIBLING :
  117. return "following-sibling";
  118. case Compiler.AXIS_PRECEDING_SIBLING :
  119. return "preceding-sibling";
  120. case Compiler.AXIS_DESCENDANT_OR_SELF :
  121. return "descendant-or-self";
  122. }
  123. return "UNKNOWN";
  124. }
  125. }