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: import org.apache.commons.jxpath.ri.QName;
020:
021: /**
022: * @author Dmitri Plotnikov
023: * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:39 $
024: */
025: public class TreeCompiler implements Compiler {
026:
027: private static final QName QNAME_NAME = new QName(null, "name");
028:
029: public Object number(String value) {
030: return new Constant(new Double(value));
031: }
032:
033: public Object literal(String value) {
034: return new Constant(value);
035: }
036:
037: public Object qname(String prefix, String name) {
038: return new QName(prefix, name);
039: }
040:
041: public Object sum(Object[] arguments) {
042: return new CoreOperationAdd(toExpressionArray(arguments));
043: }
044:
045: public Object minus(Object left, Object right) {
046: return new CoreOperationSubtract((Expression) left,
047: (Expression) right);
048: }
049:
050: public Object multiply(Object left, Object right) {
051: return new CoreOperationMultiply((Expression) left,
052: (Expression) right);
053: }
054:
055: public Object divide(Object left, Object right) {
056: return new CoreOperationDivide((Expression) left,
057: (Expression) right);
058: }
059:
060: public Object mod(Object left, Object right) {
061: return new CoreOperationMod((Expression) left,
062: (Expression) right);
063: }
064:
065: public Object lessThan(Object left, Object right) {
066: return new CoreOperationLessThan((Expression) left,
067: (Expression) right);
068: }
069:
070: public Object lessThanOrEqual(Object left, Object right) {
071: return new CoreOperationLessThanOrEqual((Expression) left,
072: (Expression) right);
073: }
074:
075: public Object greaterThan(Object left, Object right) {
076: return new CoreOperationGreaterThan((Expression) left,
077: (Expression) right);
078: }
079:
080: public Object greaterThanOrEqual(Object left, Object right) {
081: return new CoreOperationGreaterThanOrEqual((Expression) left,
082: (Expression) right);
083: }
084:
085: public Object equal(Object left, Object right) {
086: if (isNameAttributeTest((Expression) left)) {
087: return new NameAttributeTest((Expression) left,
088: (Expression) right);
089: } else {
090: return new CoreOperationEqual((Expression) left,
091: (Expression) right);
092: }
093: }
094:
095: public Object notEqual(Object left, Object right) {
096: return new CoreOperationNotEqual((Expression) left,
097: (Expression) right);
098: }
099:
100: public Object minus(Object argument) {
101: return new CoreOperationNegate((Expression) argument);
102: }
103:
104: public Object variableReference(Object qName) {
105: return new VariableReference((QName) qName);
106: }
107:
108: public Object function(int code, Object[] args) {
109: return new CoreFunction(code, toExpressionArray(args));
110: }
111:
112: public Object function(Object name, Object[] args) {
113: return new ExtensionFunction((QName) name,
114: toExpressionArray(args));
115: }
116:
117: public Object and(Object arguments[]) {
118: return new CoreOperationAnd(toExpressionArray(arguments));
119: }
120:
121: public Object or(Object arguments[]) {
122: return new CoreOperationOr(toExpressionArray(arguments));
123: }
124:
125: public Object union(Object[] arguments) {
126: return new CoreOperationUnion(toExpressionArray(arguments));
127: }
128:
129: public Object locationPath(boolean absolute, Object[] steps) {
130: return new LocationPath(absolute, toStepArray(steps));
131: }
132:
133: public Object expressionPath(Object expression,
134: Object[] predicates, Object[] steps) {
135: return new ExpressionPath((Expression) expression,
136: toExpressionArray(predicates), toStepArray(steps));
137: }
138:
139: public Object nodeNameTest(Object qname) {
140: return new NodeNameTest((QName) qname);
141: }
142:
143: public Object nodeTypeTest(int nodeType) {
144: return new NodeTypeTest(nodeType);
145: }
146:
147: public Object processingInstructionTest(String instruction) {
148: return new ProcessingInstructionTest(instruction);
149: }
150:
151: public Object step(int axis, Object nodeTest, Object[] predicates) {
152: return new Step(axis, (NodeTest) nodeTest,
153: toExpressionArray(predicates));
154: }
155:
156: private Expression[] toExpressionArray(Object[] array) {
157: Expression expArray[] = null;
158: if (array != null) {
159: expArray = new Expression[array.length];
160: for (int i = 0; i < expArray.length; i++) {
161: expArray[i] = (Expression) array[i];
162: }
163: }
164: return expArray;
165: }
166:
167: private Step[] toStepArray(Object[] array) {
168: Step stepArray[] = null;
169: if (array != null) {
170: stepArray = new Step[array.length];
171: for (int i = 0; i < stepArray.length; i++) {
172: stepArray[i] = (Step) array[i];
173: }
174: }
175: return stepArray;
176: }
177:
178: private boolean isNameAttributeTest(Expression arg) {
179: if (!(arg instanceof LocationPath)) {
180: return false;
181: }
182:
183: Step[] steps = ((LocationPath) arg).getSteps();
184: if (steps.length != 1) {
185: return false;
186: }
187: if (steps[0].getAxis() != Compiler.AXIS_ATTRIBUTE) {
188: return false;
189: }
190: NodeTest test = steps[0].getNodeTest();
191: if (!(test instanceof NodeNameTest)) {
192: return false;
193: }
194: if (!((NodeNameTest) test).getNodeName().equals(QNAME_NAME)) {
195: return false;
196: }
197: return true;
198: }
199: }
|