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: WithParam.java,v 1.17 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.ConstantPoolGen;
  21. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  22. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  23. import com.sun.org.apache.bcel.internal.generic.PUSH;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  31. import com.sun.org.apache.xml.internal.utils.XMLChar;
  32. /**
  33. * @author Jacek Ambroziak
  34. * @author Santiago Pericas-Geertsen
  35. * @author Morten Jorgensen
  36. * @author John Howard <JohnH@schemasoft.com>
  37. */
  38. final class WithParam extends Instruction {
  39. /**
  40. * Parameter's name.
  41. */
  42. private QName _name;
  43. /**
  44. * The escaped qname of the with-param.
  45. */
  46. protected String _escapedName;
  47. /**
  48. * Parameter's default value.
  49. */
  50. private Expression _select;
  51. /**
  52. * %OPT% This is set to true when the WithParam is used in a CallTemplate
  53. * for a simple named template. If this is true, the parameters are
  54. * passed to the named template through method arguments rather than
  55. * using the expensive Translet.addParameter() call.
  56. */
  57. private boolean _doParameterOptimization = false;
  58. /**
  59. * Displays the contents of this element
  60. */
  61. public void display(int indent) {
  62. indent(indent);
  63. Util.println("with-param " + _name);
  64. if (_select != null) {
  65. indent(indent + IndentIncrement);
  66. Util.println("select " + _select.toString());
  67. }
  68. displayContents(indent + IndentIncrement);
  69. }
  70. /**
  71. * Returns the escaped qname of the parameter
  72. */
  73. public String getEscapedName() {
  74. return _escapedName;
  75. }
  76. /**
  77. * Return the name of this WithParam.
  78. */
  79. public QName getName() {
  80. return _name;
  81. }
  82. /**
  83. * Set the name of the variable or paremeter. Escape all special chars.
  84. */
  85. public void setName(QName name) {
  86. _name = name;
  87. _escapedName = Util.escape(name.getStringRep());
  88. }
  89. /**
  90. * Set the do parameter optimization flag
  91. */
  92. public void setDoParameterOptimization(boolean flag) {
  93. _doParameterOptimization = flag;
  94. }
  95. /**
  96. * The contents of a <xsl:with-param> elements are either in the element's
  97. * 'select' attribute (this has precedence) or in the element body.
  98. */
  99. public void parseContents(Parser parser) {
  100. final String name = getAttribute("name");
  101. if (name.length() > 0) {
  102. if (!XMLChar.isValidQName(name)) {
  103. ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name,
  104. this);
  105. parser.reportError(Constants.ERROR, err);
  106. }
  107. setName(parser.getQNameIgnoreDefaultNs(name));
  108. }
  109. else {
  110. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "name");
  111. }
  112. final String select = getAttribute("select");
  113. if (select.length() > 0) {
  114. _select = parser.parseExpression(this, "select", null);
  115. }
  116. parseChildren(parser);
  117. }
  118. /**
  119. * Type-check either the select attribute or the element body, depending
  120. * on which is in use.
  121. */
  122. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  123. if (_select != null) {
  124. final Type tselect = _select.typeCheck(stable);
  125. if (tselect instanceof ReferenceType == false) {
  126. _select = new CastExpr(_select, Type.Reference);
  127. }
  128. }
  129. else {
  130. typeCheckContents(stable);
  131. }
  132. return Type.Void;
  133. }
  134. /**
  135. * Compile the value of the parameter, which is either in an expression in
  136. * a 'select' attribute, or in the with-param element's body
  137. */
  138. public void translateValue(ClassGenerator classGen,
  139. MethodGenerator methodGen) {
  140. // Compile expression is 'select' attribute if present
  141. if (_select != null) {
  142. _select.translate(classGen, methodGen);
  143. _select.startIterator(classGen, methodGen);
  144. }
  145. // If not, compile result tree from parameter body if present.
  146. else if (hasContents()) {
  147. compileResultTree(classGen, methodGen);
  148. }
  149. // If neither are present then store empty string in parameter slot
  150. else {
  151. final ConstantPoolGen cpg = classGen.getConstantPool();
  152. final InstructionList il = methodGen.getInstructionList();
  153. il.append(new PUSH(cpg, Constants.EMPTYSTRING));
  154. }
  155. }
  156. /**
  157. * This code generates a sequence of bytecodes that call the
  158. * addParameter() method in AbstractTranslet. The method call will add
  159. * (or update) the parameter frame with the new parameter value.
  160. */
  161. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  162. final ConstantPoolGen cpg = classGen.getConstantPool();
  163. final InstructionList il = methodGen.getInstructionList();
  164. // Translate the value and put it on the stack
  165. if (_doParameterOptimization) {
  166. translateValue(classGen, methodGen);
  167. return;
  168. }
  169. // Make name acceptable for use as field name in class
  170. String name = Util.escape(getEscapedName());
  171. // Load reference to the translet (method is in AbstractTranslet)
  172. il.append(classGen.loadTranslet());
  173. // Load the name of the parameter
  174. il.append(new PUSH(cpg, name)); // TODO: namespace ?
  175. // Generete the value of the parameter (use value in 'select' by def.)
  176. translateValue(classGen, methodGen);
  177. // Mark this parameter value is not being the default value
  178. il.append(new PUSH(cpg, false));
  179. // Pass the parameter to the template
  180. il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
  181. ADD_PARAMETER,
  182. ADD_PARAMETER_SIG)));
  183. il.append(POP); // cleanup stack
  184. }
  185. }