001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.compiler;
017:
018: import org.apache.commons.jxpath.ri.Compiler;
019:
020: /**
021: * @author Dmitri Plotnikov
022: * @version $Revision: 1.9 $ $Date: 2004/02/29 14:17:39 $
023: */
024: public class Step {
025: private int axis;
026: private NodeTest nodeTest;
027: private Expression[] predicates;
028:
029: protected Step(int axis, NodeTest nodeTest, Expression[] predicates) {
030: this .axis = axis;
031: this .nodeTest = nodeTest;
032: this .predicates = predicates;
033: }
034:
035: public int getAxis() {
036: return axis;
037: }
038:
039: public NodeTest getNodeTest() {
040: return nodeTest;
041: }
042:
043: public Expression[] getPredicates() {
044: return predicates;
045: }
046:
047: public boolean isContextDependent() {
048: if (predicates != null) {
049: for (int i = 0; i < predicates.length; i++) {
050: if (predicates[i].isContextDependent()) {
051: return true;
052: }
053: }
054: }
055: return false;
056: }
057:
058: public String toString() {
059: StringBuffer buffer = new StringBuffer();
060: int axis = getAxis();
061: if (axis == Compiler.AXIS_CHILD) {
062: buffer.append(nodeTest);
063: } else if (axis == Compiler.AXIS_ATTRIBUTE) {
064: buffer.append('@');
065: buffer.append(nodeTest);
066: } else if (axis == Compiler.AXIS_SELF
067: && nodeTest instanceof NodeTypeTest
068: && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
069: buffer.append(".");
070: } else if (axis == Compiler.AXIS_PARENT
071: && nodeTest instanceof NodeTypeTest
072: && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE) {
073: buffer.append("..");
074: } else if (axis == Compiler.AXIS_DESCENDANT_OR_SELF
075: && nodeTest instanceof NodeTypeTest
076: && ((NodeTypeTest) nodeTest).getNodeType() == Compiler.NODE_TYPE_NODE
077: && (predicates == null || predicates.length == 0)) {
078: buffer.append("");
079: } else {
080: buffer.append(axisToString(axis));
081: buffer.append("::");
082: buffer.append(nodeTest);
083: }
084: Expression[] predicates = getPredicates();
085: if (predicates != null) {
086: for (int i = 0; i < predicates.length; i++) {
087: buffer.append('[');
088: buffer.append(predicates[i]);
089: buffer.append(']');
090: }
091: }
092: return buffer.toString();
093: }
094:
095: public static String axisToString(int axis) {
096: switch (axis) {
097: case Compiler.AXIS_SELF:
098: return "self";
099: case Compiler.AXIS_CHILD:
100: return "child";
101: case Compiler.AXIS_PARENT:
102: return "parent";
103: case Compiler.AXIS_ANCESTOR:
104: return "ancestor";
105: case Compiler.AXIS_ATTRIBUTE:
106: return "attribute";
107: case Compiler.AXIS_NAMESPACE:
108: return "namespace";
109: case Compiler.AXIS_PRECEDING:
110: return "preceding";
111: case Compiler.AXIS_FOLLOWING:
112: return "following";
113: case Compiler.AXIS_DESCENDANT:
114: return "descendant";
115: case Compiler.AXIS_ANCESTOR_OR_SELF:
116: return "ancestor-or-self";
117: case Compiler.AXIS_FOLLOWING_SIBLING:
118: return "following-sibling";
119: case Compiler.AXIS_PRECEDING_SIBLING:
120: return "preceding-sibling";
121: case Compiler.AXIS_DESCENDANT_OR_SELF:
122: return "descendant-or-self";
123: }
124: return "UNKNOWN";
125: }
126: }
|