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: CallTemplate.java,v 1.18 2004/02/24 02:57:28 zongaro Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.ALOAD;
  21. import com.sun.org.apache.bcel.internal.generic.ASTORE;
  22. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  24. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  25. import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
  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.Type;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  32. import com.sun.org.apache.xml.internal.utils.XMLChar;
  33. import java.util.Vector;
  34. /**
  35. * @author Jacek Ambroziak
  36. * @author Santiago Pericas-Geertsen
  37. * @author Erwin Bolwidt <ejb@klomp.org>
  38. */
  39. final class CallTemplate extends Instruction {
  40. /**
  41. * Name of template to call.
  42. */
  43. private QName _name;
  44. /**
  45. * The array of effective parameters in this CallTemplate. An object in
  46. * this array can be either a WithParam or a Param if no WithParam
  47. * exists for a particular parameter.
  48. */
  49. private Object[] _parameters = null;
  50. /**
  51. * The corresponding template which this CallTemplate calls.
  52. */
  53. private Template _calleeTemplate = null;
  54. public void display(int indent) {
  55. indent(indent);
  56. System.out.print("CallTemplate");
  57. Util.println(" name " + _name);
  58. displayContents(indent + IndentIncrement);
  59. }
  60. public boolean hasWithParams() {
  61. return elementCount() > 0;
  62. }
  63. public void parseContents(Parser parser) {
  64. final String name = getAttribute("name");
  65. if (name.length() > 0) {
  66. if (!XMLChar.isValidQName(name)) {
  67. ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
  68. parser.reportError(Constants.ERROR, err);
  69. }
  70. _name = parser.getQNameIgnoreDefaultNs(name);
  71. }
  72. else {
  73. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");
  74. }
  75. parseChildren(parser);
  76. }
  77. /**
  78. * Verify that a template with this name exists.
  79. */
  80. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  81. final Template template = stable.lookupTemplate(_name);
  82. if (template != null) {
  83. typeCheckContents(stable);
  84. }
  85. else {
  86. ErrorMsg err = new ErrorMsg(ErrorMsg.TEMPLATE_UNDEF_ERR,_name,this);
  87. throw new TypeCheckError(err);
  88. }
  89. return Type.Void;
  90. }
  91. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  92. final Stylesheet stylesheet = classGen.getStylesheet();
  93. final ConstantPoolGen cpg = classGen.getConstantPool();
  94. final InstructionList il = methodGen.getInstructionList();
  95. // If there are Params in the stylesheet or WithParams in this call?
  96. if (stylesheet.hasLocalParams() || hasContents()) {
  97. _calleeTemplate = getCalleeTemplate();
  98. // Build the parameter list if the called template is simple named
  99. if (_calleeTemplate != null) {
  100. buildParameterList();
  101. }
  102. // This is only needed when the called template is not
  103. // a simple named template.
  104. else {
  105. // Push parameter frame
  106. final int push = cpg.addMethodref(TRANSLET_CLASS,
  107. PUSH_PARAM_FRAME,
  108. PUSH_PARAM_FRAME_SIG);
  109. il.append(classGen.loadTranslet());
  110. il.append(new INVOKEVIRTUAL(push));
  111. translateContents(classGen, methodGen);
  112. }
  113. }
  114. // Generate a valid Java method name
  115. final String className = stylesheet.getClassName();
  116. String methodName = Util.escape(_name.toString());
  117. // Load standard arguments
  118. il.append(classGen.loadTranslet());
  119. il.append(methodGen.loadDOM());
  120. il.append(methodGen.loadIterator());
  121. il.append(methodGen.loadHandler());
  122. il.append(methodGen.loadCurrentNode());
  123. // Initialize prefix of method signature
  124. StringBuffer methodSig = new StringBuffer("(" + DOM_INTF_SIG
  125. + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + NODE_SIG);
  126. // If calling a simply named template, push actual arguments
  127. if (_calleeTemplate != null) {
  128. Vector calleeParams = _calleeTemplate.getParameters();
  129. int numParams = _parameters.length;
  130. for (int i = 0; i < numParams; i++) {
  131. SyntaxTreeNode node = (SyntaxTreeNode)_parameters[i];
  132. methodSig.append(OBJECT_SIG); // append Object to signature
  133. // Push 'null' if Param to indicate no actual parameter specified
  134. if (node instanceof Param) {
  135. il.append(ACONST_NULL);
  136. }
  137. else { // translate WithParam
  138. node.translate(classGen, methodGen);
  139. }
  140. }
  141. }
  142. // Complete signature and generate invokevirtual call
  143. methodSig.append(")V");
  144. il.append(new INVOKEVIRTUAL(cpg.addMethodref(className,
  145. methodName,
  146. methodSig.toString())));
  147. // Do not need to call Translet.popParamFrame() if we are
  148. // calling a simple named template.
  149. if (_calleeTemplate == null && (stylesheet.hasLocalParams() || hasContents())) {
  150. // Pop parameter frame
  151. final int pop = cpg.addMethodref(TRANSLET_CLASS,
  152. POP_PARAM_FRAME,
  153. POP_PARAM_FRAME_SIG);
  154. il.append(classGen.loadTranslet());
  155. il.append(new INVOKEVIRTUAL(pop));
  156. }
  157. }
  158. /**
  159. * Return the simple named template which this CallTemplate calls.
  160. * Return false if there is no matched template or the matched
  161. * template is not a simple named template.
  162. */
  163. public Template getCalleeTemplate() {
  164. Stylesheet stylesheet = getXSLTC().getStylesheet();
  165. Vector templates = stylesheet.getAllValidTemplates();
  166. int size = templates.size();
  167. for (int i = 0; i < size; i++) {
  168. Template t = (Template)templates.elementAt(i);
  169. if (t.getName() == _name && t.isSimpleNamedTemplate()) {
  170. return t;
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Build the list of effective parameters in this CallTemplate.
  177. * The parameters of the called template are put into the array first.
  178. * Then we visit the WithParam children of this CallTemplate and replace
  179. * the Param with a corresponding WithParam having the same name.
  180. */
  181. private void buildParameterList() {
  182. // Put the parameters from the called template into the array first.
  183. // This is to ensure the order of the parameters.
  184. Vector defaultParams = _calleeTemplate.getParameters();
  185. int numParams = defaultParams.size();
  186. _parameters = new Object[numParams];
  187. for (int i = 0; i < numParams; i++) {
  188. _parameters[i] = defaultParams.elementAt(i);
  189. }
  190. // Replace a Param with a WithParam if they have the same name.
  191. int count = elementCount();
  192. for (int i = 0; i < count; i++) {
  193. Object node = elementAt(i);
  194. // Ignore if not WithParam
  195. if (node instanceof WithParam) {
  196. WithParam withParam = (WithParam)node;
  197. QName name = withParam.getName();
  198. // Search for a Param with the same name
  199. for (int k = 0; k < numParams; k++) {
  200. Object object = _parameters[k];
  201. if (object instanceof Param
  202. && ((Param)object).getName() == name) {
  203. withParam.setDoParameterOptimization(true);
  204. _parameters[k] = withParam;
  205. break;
  206. }
  207. else if (object instanceof WithParam
  208. && ((WithParam)object).getName() == name) {
  209. withParam.setDoParameterOptimization(true);
  210. _parameters[k] = withParam;
  211. break;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }