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. import org.apache.commons.jxpath.ri.QName;
  19. /**
  20. * @author Dmitri Plotnikov
  21. * @version $Revision: 1.10 $ $Date: 2004/02/29 14:17:39 $
  22. */
  23. public class TreeCompiler implements Compiler {
  24. private static final QName QNAME_NAME = new QName(null, "name");
  25. public Object number(String value) {
  26. return new Constant(new Double(value));
  27. }
  28. public Object literal(String value) {
  29. return new Constant(value);
  30. }
  31. public Object qname(String prefix, String name) {
  32. return new QName(prefix, name);
  33. }
  34. public Object sum(Object[] arguments) {
  35. return new CoreOperationAdd(toExpressionArray(arguments));
  36. }
  37. public Object minus(Object left, Object right) {
  38. return new CoreOperationSubtract(
  39. (Expression) left,
  40. (Expression) right);
  41. }
  42. public Object multiply(Object left, Object right) {
  43. return new CoreOperationMultiply((Expression) left, (Expression) right);
  44. }
  45. public Object divide(Object left, Object right) {
  46. return new CoreOperationDivide((Expression) left, (Expression) right);
  47. }
  48. public Object mod(Object left, Object right) {
  49. return new CoreOperationMod((Expression) left, (Expression) right);
  50. }
  51. public Object lessThan(Object left, Object right) {
  52. return new CoreOperationLessThan((Expression) left, (Expression) right);
  53. }
  54. public Object lessThanOrEqual(Object left, Object right) {
  55. return new CoreOperationLessThanOrEqual(
  56. (Expression) left,
  57. (Expression) right);
  58. }
  59. public Object greaterThan(Object left, Object right) {
  60. return new CoreOperationGreaterThan(
  61. (Expression) left,
  62. (Expression) right);
  63. }
  64. public Object greaterThanOrEqual(Object left, Object right) {
  65. return new CoreOperationGreaterThanOrEqual(
  66. (Expression) left,
  67. (Expression) right);
  68. }
  69. public Object equal(Object left, Object right) {
  70. if (isNameAttributeTest((Expression) left)) {
  71. return new NameAttributeTest((Expression) left, (Expression) right);
  72. }
  73. else {
  74. return new CoreOperationEqual(
  75. (Expression) left,
  76. (Expression) right);
  77. }
  78. }
  79. public Object notEqual(Object left, Object right) {
  80. return new CoreOperationNotEqual(
  81. (Expression) left,
  82. (Expression) right);
  83. }
  84. public Object minus(Object argument) {
  85. return new CoreOperationNegate((Expression) argument);
  86. }
  87. public Object variableReference(Object qName) {
  88. return new VariableReference((QName) qName);
  89. }
  90. public Object function(int code, Object[] args) {
  91. return new CoreFunction(code, toExpressionArray(args));
  92. }
  93. public Object function(Object name, Object[] args) {
  94. return new ExtensionFunction((QName) name, toExpressionArray(args));
  95. }
  96. public Object and(Object arguments[]) {
  97. return new CoreOperationAnd(
  98. toExpressionArray(arguments));
  99. }
  100. public Object or(Object arguments[]) {
  101. return new CoreOperationOr(
  102. toExpressionArray(arguments));
  103. }
  104. public Object union(Object[] arguments) {
  105. return new CoreOperationUnion(
  106. toExpressionArray(arguments));
  107. }
  108. public Object locationPath(boolean absolute, Object[] steps) {
  109. return new LocationPath(absolute, toStepArray(steps));
  110. }
  111. public Object expressionPath(
  112. Object expression,
  113. Object[] predicates,
  114. Object[] steps)
  115. {
  116. return new ExpressionPath(
  117. (Expression) expression,
  118. toExpressionArray(predicates),
  119. toStepArray(steps));
  120. }
  121. public Object nodeNameTest(Object qname) {
  122. return new NodeNameTest((QName) qname);
  123. }
  124. public Object nodeTypeTest(int nodeType) {
  125. return new NodeTypeTest(nodeType);
  126. }
  127. public Object processingInstructionTest(String instruction) {
  128. return new ProcessingInstructionTest(instruction);
  129. }
  130. public Object step(int axis, Object nodeTest, Object[] predicates) {
  131. return new Step(
  132. axis,
  133. (NodeTest) nodeTest,
  134. toExpressionArray(predicates));
  135. }
  136. private Expression[] toExpressionArray(Object[] array) {
  137. Expression expArray[] = null;
  138. if (array != null) {
  139. expArray = new Expression[array.length];
  140. for (int i = 0; i < expArray.length; i++) {
  141. expArray[i] = (Expression) array[i];
  142. }
  143. }
  144. return expArray;
  145. }
  146. private Step[] toStepArray(Object[] array) {
  147. Step stepArray[] = null;
  148. if (array != null) {
  149. stepArray = new Step[array.length];
  150. for (int i = 0; i < stepArray.length; i++) {
  151. stepArray[i] = (Step) array[i];
  152. }
  153. }
  154. return stepArray;
  155. }
  156. private boolean isNameAttributeTest(Expression arg) {
  157. if (!(arg instanceof LocationPath)) {
  158. return false;
  159. }
  160. Step[] steps = ((LocationPath) arg).getSteps();
  161. if (steps.length != 1) {
  162. return false;
  163. }
  164. if (steps[0].getAxis() != Compiler.AXIS_ATTRIBUTE) {
  165. return false;
  166. }
  167. NodeTest test = steps[0].getNodeTest();
  168. if (!(test instanceof NodeNameTest)) {
  169. return false;
  170. }
  171. if (!((NodeNameTest) test).getNodeName().equals(QNAME_NAME)) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. }