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: ApplyTemplates.java,v 1.21 2004/02/24 03:55:47 zongaro 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.ConstantPoolGen;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  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.xalan.internal.xsltc.compiler.util.ClassGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  32. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
  33. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  35. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  36. import com.sun.org.apache.xml.internal.utils.XMLChar;
  37. /**
  38. * @author Jacek Ambroziak
  39. * @author Santiago Pericas-Geertsen
  40. */
  41. final class ApplyTemplates extends Instruction {
  42. private Expression _select;
  43. private Type _type = null;
  44. private QName _modeName;
  45. private String _functionName;
  46. public void display(int indent) {
  47. indent(indent);
  48. Util.println("ApplyTemplates");
  49. indent(indent + IndentIncrement);
  50. Util.println("select " + _select.toString());
  51. if (_modeName != null) {
  52. indent(indent + IndentIncrement);
  53. Util.println("mode " + _modeName);
  54. }
  55. }
  56. public boolean hasWithParams() {
  57. return hasContents();
  58. }
  59. public void parseContents(Parser parser) {
  60. final String select = getAttribute("select");
  61. final String mode = getAttribute("mode");
  62. if (select.length() > 0) {
  63. _select = parser.parseExpression(this, "select", null);
  64. }
  65. if (mode.length() > 0) {
  66. if (!XMLChar.isValidQName(mode)) {
  67. ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this);
  68. parser.reportError(Constants.ERROR, err);
  69. }
  70. _modeName = parser.getQNameIgnoreDefaultNs(mode);
  71. }
  72. // instantiate Mode if needed, cache (apply temp) function name
  73. _functionName =
  74. parser.getTopLevelStylesheet().getMode(_modeName).functionName();
  75. parseChildren(parser);// with-params
  76. }
  77. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  78. if (_select != null) {
  79. _type = _select.typeCheck(stable);
  80. if (_type instanceof NodeType || _type instanceof ReferenceType) {
  81. _select = new CastExpr(_select, Type.NodeSet);
  82. _type = Type.NodeSet;
  83. }
  84. if (_type instanceof NodeSetType||_type instanceof ResultTreeType) {
  85. typeCheckContents(stable); // with-params
  86. return Type.Void;
  87. }
  88. throw new TypeCheckError(this);
  89. }
  90. else {
  91. typeCheckContents(stable); // with-params
  92. return Type.Void;
  93. }
  94. }
  95. /**
  96. * Translate call-template. A parameter frame is pushed only if
  97. * some template in the stylesheet uses parameters.
  98. */
  99. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  100. boolean setStartNodeCalled = false;
  101. final Stylesheet stylesheet = classGen.getStylesheet();
  102. final ConstantPoolGen cpg = classGen.getConstantPool();
  103. final InstructionList il = methodGen.getInstructionList();
  104. final int current = methodGen.getLocalIndex("current");
  105. // check if sorting nodes is required
  106. final Vector sortObjects = new Vector();
  107. final Enumeration children = elements();
  108. while (children.hasMoreElements()) {
  109. final Object child = children.nextElement();
  110. if (child instanceof Sort) {
  111. sortObjects.addElement(child);
  112. }
  113. }
  114. // Push a new parameter frame
  115. if (stylesheet.hasLocalParams() || hasContents()) {
  116. il.append(classGen.loadTranslet());
  117. final int pushFrame = cpg.addMethodref(TRANSLET_CLASS,
  118. PUSH_PARAM_FRAME,
  119. PUSH_PARAM_FRAME_SIG);
  120. il.append(new INVOKEVIRTUAL(pushFrame));
  121. // translate with-params
  122. translateContents(classGen, methodGen);
  123. }
  124. il.append(classGen.loadTranslet());
  125. // The 'select' expression is a result-tree
  126. if ((_type != null) && (_type instanceof ResultTreeType)) {
  127. // <xsl:sort> cannot be applied to a result tree - issue warning
  128. if (sortObjects.size() > 0) {
  129. ErrorMsg err = new ErrorMsg(ErrorMsg.RESULT_TREE_SORT_ERR,this);
  130. getParser().reportError(WARNING, err);
  131. }
  132. // Put the result tree (a DOM adapter) on the stack
  133. _select.translate(classGen, methodGen);
  134. // Get back the DOM and iterator (not just iterator!!!)
  135. _type.translateTo(classGen, methodGen, Type.NodeSet);
  136. }
  137. else {
  138. il.append(methodGen.loadDOM());
  139. // compute node iterator for applyTemplates
  140. if (sortObjects.size() > 0) {
  141. Sort.translateSortIterator(classGen, methodGen,
  142. _select, sortObjects);
  143. int setStartNode = cpg.addInterfaceMethodref(NODE_ITERATOR,
  144. SET_START_NODE,
  145. "(I)"+
  146. NODE_ITERATOR_SIG);
  147. il.append(methodGen.loadCurrentNode());
  148. il.append(new INVOKEINTERFACE(setStartNode,2));
  149. setStartNodeCalled = true;
  150. }
  151. else {
  152. if (_select == null)
  153. Mode.compileGetChildren(classGen, methodGen, current);
  154. else
  155. _select.translate(classGen, methodGen);
  156. }
  157. }
  158. if (_select != null && !setStartNodeCalled) {
  159. _select.startIterator(classGen, methodGen);
  160. }
  161. //!!! need to instantiate all needed modes
  162. final String className = classGen.getStylesheet().getClassName();
  163. il.append(methodGen.loadHandler());
  164. final String applyTemplatesSig = classGen.getApplyTemplatesSig();
  165. final int applyTemplates = cpg.addMethodref(className,
  166. _functionName,
  167. applyTemplatesSig);
  168. il.append(new INVOKEVIRTUAL(applyTemplates));
  169. // Pop parameter frame
  170. if (stylesheet.hasLocalParams() || hasContents()) {
  171. il.append(classGen.loadTranslet());
  172. final int popFrame = cpg.addMethodref(TRANSLET_CLASS,
  173. POP_PARAM_FRAME,
  174. POP_PARAM_FRAME_SIG);
  175. il.append(new INVOKEVIRTUAL(popFrame));
  176. }
  177. }
  178. }