1. /*
  2. * Copyright 2001-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. /*
  17. * $Id: UnionPathExpr.java,v 1.10 2004/02/16 22:25:10 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Vector;
  21. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  22. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
  24. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  25. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  26. import com.sun.org.apache.bcel.internal.generic.NEW;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31. import com.sun.org.apache.xalan.internal.xsltc.dom.Axis;
  32. import com.sun.org.apache.xml.internal.dtm.DTM;
  33. /**
  34. * @author Jacek Ambroziak
  35. * @author Santiago Pericas-Geertsen
  36. */
  37. final class UnionPathExpr extends Expression {
  38. private final Expression _pathExpr;
  39. private final Expression _rest;
  40. private boolean _reverse = false;
  41. // linearization for top level UnionPathExprs
  42. private Expression[] _components;
  43. public UnionPathExpr(Expression pathExpr, Expression rest) {
  44. _pathExpr = pathExpr;
  45. _rest = rest;
  46. }
  47. public void setParser(Parser parser) {
  48. super.setParser(parser);
  49. // find all expressions in this Union
  50. final Vector components = new Vector();
  51. flatten(components);
  52. final int size = components.size();
  53. _components = (Expression[])components.toArray(new Expression[size]);
  54. for (int i = 0; i < size; i++) {
  55. _components[i].setParser(parser);
  56. _components[i].setParent(this);
  57. if (_components[i] instanceof Step) {
  58. final Step step = (Step)_components[i];
  59. final int axis = step.getAxis();
  60. final int type = step.getNodeType();
  61. // Put attribute iterators first
  62. if ((axis == Axis.ATTRIBUTE) || (type == DTM.ATTRIBUTE_NODE)) {
  63. _components[i] = _components[0];
  64. _components[0] = step;
  65. }
  66. // Check if the union contains a reverse iterator
  67. if (Axis.isReverse[axis]) _reverse = true;
  68. }
  69. }
  70. // No need to reverse anything if another expression lies on top of this
  71. if (getParent() instanceof Expression) _reverse = false;
  72. }
  73. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  74. final int length = _components.length;
  75. for (int i = 0; i < length; i++) {
  76. if (_components[i].typeCheck(stable) != Type.NodeSet) {
  77. _components[i] = new CastExpr(_components[i], Type.NodeSet);
  78. }
  79. }
  80. return _type = Type.NodeSet;
  81. }
  82. public String toString() {
  83. return "union(" + _pathExpr + ", " + _rest + ')';
  84. }
  85. private void flatten(Vector components) {
  86. components.addElement(_pathExpr);
  87. if (_rest != null) {
  88. if (_rest instanceof UnionPathExpr) {
  89. ((UnionPathExpr)_rest).flatten(components);
  90. }
  91. else {
  92. components.addElement(_rest);
  93. }
  94. }
  95. }
  96. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  97. final ConstantPoolGen cpg = classGen.getConstantPool();
  98. final InstructionList il = methodGen.getInstructionList();
  99. final int init = cpg.addMethodref(UNION_ITERATOR_CLASS,
  100. "<init>",
  101. "("+DOM_INTF_SIG+")V");
  102. final int iter = cpg.addMethodref(UNION_ITERATOR_CLASS,
  103. ADD_ITERATOR,
  104. ADD_ITERATOR_SIG);
  105. // Create the UnionIterator and leave it on the stack
  106. il.append(new NEW(cpg.addClass(UNION_ITERATOR_CLASS)));
  107. il.append(DUP);
  108. il.append(methodGen.loadDOM());
  109. il.append(new INVOKESPECIAL(init));
  110. // Add the various iterators to the UnionIterator
  111. final int length = _components.length;
  112. for (int i = 0; i < length; i++) {
  113. _components[i].translate(classGen, methodGen);
  114. il.append(new INVOKEVIRTUAL(iter));
  115. }
  116. // Order the iterator only if strictly needed
  117. if (_reverse) {
  118. final int order = cpg.addInterfaceMethodref(DOM_INTF,
  119. ORDER_ITERATOR,
  120. ORDER_ITERATOR_SIG);
  121. il.append(methodGen.loadDOM());
  122. il.append(SWAP);
  123. il.append(methodGen.loadContextNode());
  124. il.append(new INVOKEINTERFACE(order, 3));
  125. }
  126. }
  127. }