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: ForEach.java,v 1.19 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Enumeration;
  21. import java.util.Vector;
  22. import com.sun.org.apache.bcel.internal.generic.BranchHandle;
  23. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  24. import com.sun.org.apache.bcel.internal.generic.GOTO;
  25. import com.sun.org.apache.bcel.internal.generic.IFGT;
  26. import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  27. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
  32. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
  33. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
  35. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  36. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  37. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  38. /**
  39. * @author Jacek Ambroziak
  40. * @author Santiago Pericas-Geertsen
  41. * @author Morten Jorgensen
  42. */
  43. final class ForEach extends Instruction {
  44. private Expression _select;
  45. private Type _type;
  46. public void display(int indent) {
  47. indent(indent);
  48. Util.println("ForEach");
  49. indent(indent + IndentIncrement);
  50. Util.println("select " + _select.toString());
  51. displayContents(indent + IndentIncrement);
  52. }
  53. public void parseContents(Parser parser) {
  54. _select = parser.parseExpression(this, "select", null);
  55. parseChildren(parser);
  56. // make sure required attribute(s) have been set
  57. if (_select.isDummy()) {
  58. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
  59. }
  60. }
  61. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  62. _type = _select.typeCheck(stable);
  63. if (_type instanceof ReferenceType || _type instanceof NodeType) {
  64. _select = new CastExpr(_select, Type.NodeSet);
  65. typeCheckContents(stable);
  66. return Type.Void;
  67. }
  68. if (_type instanceof NodeSetType||_type instanceof ResultTreeType) {
  69. typeCheckContents(stable);
  70. return Type.Void;
  71. }
  72. throw new TypeCheckError(this);
  73. }
  74. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  75. final ConstantPoolGen cpg = classGen.getConstantPool();
  76. final InstructionList il = methodGen.getInstructionList();
  77. // Save current node and current iterator on the stack
  78. il.append(methodGen.loadCurrentNode());
  79. il.append(methodGen.loadIterator());
  80. // Collect sort objects associated with this instruction
  81. final Vector sortObjects = new Vector();
  82. Enumeration children = elements();
  83. while (children.hasMoreElements()) {
  84. final Object child = children.nextElement();
  85. if (child instanceof Sort) {
  86. sortObjects.addElement(child);
  87. }
  88. }
  89. if ((_type != null) && (_type instanceof ResultTreeType)) {
  90. // Store existing DOM on stack - must be restored when loop is done
  91. il.append(methodGen.loadDOM());
  92. // <xsl:sort> cannot be applied to a result tree - issue warning
  93. if (sortObjects.size() > 0) {
  94. ErrorMsg msg = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this);
  95. getParser().reportError(WARNING, msg);
  96. }
  97. // Put the result tree on the stack (DOM)
  98. _select.translate(classGen, methodGen);
  99. // Get an iterator for the whole DOM - excluding the root node
  100. _type.translateTo(classGen, methodGen, Type.NodeSet);
  101. // Store the result tree as the default DOM
  102. il.append(SWAP);
  103. il.append(methodGen.storeDOM());
  104. }
  105. else {
  106. // Compile node iterator
  107. if (sortObjects.size() > 0) {
  108. Sort.translateSortIterator(classGen, methodGen,
  109. _select, sortObjects);
  110. }
  111. else {
  112. _select.translate(classGen, methodGen);
  113. }
  114. if (_type instanceof ReferenceType == false) {
  115. il.append(methodGen.loadContextNode());
  116. il.append(methodGen.setStartNode());
  117. }
  118. }
  119. // Overwrite current iterator
  120. il.append(methodGen.storeIterator());
  121. // Give local variables (if any) default values before starting loop
  122. initializeVariables(classGen, methodGen);
  123. final BranchHandle nextNode = il.append(new GOTO(null));
  124. final InstructionHandle loop = il.append(NOP);
  125. translateContents(classGen, methodGen);
  126. nextNode.setTarget(il.append(methodGen.loadIterator()));
  127. il.append(methodGen.nextNode());
  128. il.append(DUP);
  129. il.append(methodGen.storeCurrentNode());
  130. il.append(new IFGT(loop));
  131. // Restore current DOM (if result tree was used instead for this loop)
  132. if ((_type != null) && (_type instanceof ResultTreeType)) {
  133. il.append(methodGen.storeDOM());
  134. }
  135. // Restore current node and current iterator from the stack
  136. il.append(methodGen.storeIterator());
  137. il.append(methodGen.storeCurrentNode());
  138. }
  139. /**
  140. * The code that is generated by nested for-each loops can appear to some
  141. * JVMs as if it is accessing un-initialized variables. We must add some
  142. * code that pushes the default variable value on the stack and pops it
  143. * into the variable slot. This is done by the Variable.initialize()
  144. * method. The code that we compile for this loop looks like this:
  145. *
  146. * initialize iterator
  147. * initialize variables <-- HERE!!!
  148. * goto Iterate
  149. * Loop: :
  150. * : (code for <xsl:for-each> contents)
  151. * :
  152. * Iterate: node = iterator.next();
  153. * if (node != END) goto Loop
  154. */
  155. public void initializeVariables(ClassGenerator classGen,
  156. MethodGenerator methodGen) {
  157. final int n = elementCount();
  158. for (int i = 0; i < n; i++) {
  159. final Object child = getContents().elementAt(i);
  160. if (child instanceof Variable) {
  161. Variable var = (Variable)child;
  162. var.initialize(classGen, methodGen);
  163. }
  164. }
  165. }
  166. }