< prev index next >

jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/WithParam.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2001-2004 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: WithParam.java,v 1.2.4.1 2005/09/12 11:38:01 pvedula Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  25 



  26 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;

  27 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  28 import com.sun.org.apache.bcel.internal.generic.InstructionList;

  29 import com.sun.org.apache.bcel.internal.generic.PUSH;
  30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  37 import com.sun.org.apache.xml.internal.utils.XML11Char;
  38 
  39 /**
  40  * @author Jacek Ambroziak
  41  * @author Santiago Pericas-Geertsen
  42  * @author Morten Jorgensen
  43  * @author John Howard <JohnH@schemasoft.com>
  44  */
  45 final class WithParam extends Instruction {
  46 
  47     /**
  48      * Parameter's name.
  49      */
  50     private QName _name;
  51 
  52     /**
  53      * The escaped qname of the with-param.
  54      */
  55     protected String _escapedName;
  56 
  57     /**
  58      * Parameter's default value.
  59      */
  60     private Expression _select;
  61 
  62     /**





  63      * %OPT% This is set to true when the WithParam is used in a CallTemplate
  64      * for a simple named template. If this is true, the parameters are
  65      * passed to the named template through method arguments rather than
  66      * using the expensive Translet.addParameter() call.
  67      */
  68     private boolean _doParameterOptimization = false;
  69 
  70     /**
  71      * Displays the contents of this element
  72      */
  73     public void display(int indent) {
  74         indent(indent);
  75         Util.println("with-param " + _name);
  76         if (_select != null) {
  77             indent(indent + IndentIncrement);
  78             Util.println("select " + _select.toString());
  79         }
  80         displayContents(indent + IndentIncrement);
  81     }
  82 


 147             }
 148         }
 149         else {
 150             typeCheckContents(stable);
 151         }
 152         return Type.Void;
 153     }
 154 
 155     /**
 156      * Compile the value of the parameter, which is either in an expression in
 157      * a 'select' attribute, or in the with-param element's body
 158      */
 159     public void translateValue(ClassGenerator classGen,
 160                                MethodGenerator methodGen) {
 161         // Compile expression is 'select' attribute if present
 162         if (_select != null) {
 163             _select.translate(classGen, methodGen);
 164             _select.startIterator(classGen, methodGen);
 165         }
 166         // If not, compile result tree from parameter body if present.

 167         else if (hasContents()) {

 168             compileResultTree(classGen, methodGen);



 169         }
 170         // If neither are present then store empty string in parameter slot
 171         else {
 172             final ConstantPoolGen cpg = classGen.getConstantPool();
 173             final InstructionList il = methodGen.getInstructionList();
 174             il.append(new PUSH(cpg, Constants.EMPTYSTRING));
 175         }
 176     }
 177 
 178     /**
 179      * This code generates a sequence of bytecodes that call the
 180      * addParameter() method in AbstractTranslet. The method call will add
 181      * (or update) the parameter frame with the new parameter value.
 182      */
 183     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
 184         final ConstantPoolGen cpg = classGen.getConstantPool();
 185         final InstructionList il = methodGen.getInstructionList();
 186 
 187         // Translate the value and put it on the stack
 188         if (_doParameterOptimization) {


 191         }
 192 
 193         // Make name acceptable for use as field name in class
 194         String name = Util.escape(getEscapedName());
 195 
 196         // Load reference to the translet (method is in AbstractTranslet)
 197         il.append(classGen.loadTranslet());
 198 
 199         // Load the name of the parameter
 200         il.append(new PUSH(cpg, name)); // TODO: namespace ?
 201         // Generete the value of the parameter (use value in 'select' by def.)
 202         translateValue(classGen, methodGen);
 203         // Mark this parameter value is not being the default value
 204         il.append(new PUSH(cpg, false));
 205         // Pass the parameter to the template
 206         il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
 207                                                      ADD_PARAMETER,
 208                                                      ADD_PARAMETER_SIG)));
 209         il.append(POP); // cleanup stack
 210     }






















 211 }
   1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.

   3  */
   4 /*
   5  * Copyright 2001-2004 The Apache Software Foundation.
   6  *
   7  * Licensed under the Apache License, Version 2.0 (the "License");
   8  * you may not use this file except in compliance with the License.
   9  * You may obtain a copy of the License at
  10  *
  11  *     http://www.apache.org/licenses/LICENSE-2.0
  12  *
  13  * Unless required by applicable law or agreed to in writing, software
  14  * distributed under the License is distributed on an "AS IS" BASIS,
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16  * See the License for the specific language governing permissions and
  17  * limitations under the License.
  18  */



  19 
  20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
  21 
  22 import com.sun.org.apache.bcel.internal.generic.ALOAD;
  23 import com.sun.org.apache.bcel.internal.generic.ASTORE;
  24 import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
  25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  26 import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  27 import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  28 import com.sun.org.apache.bcel.internal.generic.InstructionList;
  29 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
  30 import com.sun.org.apache.bcel.internal.generic.PUSH;
  31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  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 import com.sun.org.apache.xml.internal.utils.XML11Char;
  39 
  40 /**
  41  * @author Jacek Ambroziak
  42  * @author Santiago Pericas-Geertsen
  43  * @author Morten Jorgensen
  44  * @author John Howard <JohnH@schemasoft.com>
  45  */
  46 final class WithParam extends Instruction {
  47 
  48     /**
  49      * Parameter's name.
  50      */
  51     private QName _name;
  52 
  53     /**
  54      * The escaped qname of the with-param.
  55      */
  56     protected String _escapedName;
  57 
  58     /**
  59      * Parameter's default value.
  60      */
  61     private Expression _select;
  62 
  63     /**
  64      * Reference to JVM variable holding temporary result tree.
  65      */
  66     private LocalVariableGen _domAdapter;
  67 
  68     /**
  69      * %OPT% This is set to true when the WithParam is used in a CallTemplate
  70      * for a simple named template. If this is true, the parameters are
  71      * passed to the named template through method arguments rather than
  72      * using the expensive Translet.addParameter() call.
  73      */
  74     private boolean _doParameterOptimization = false;
  75 
  76     /**
  77      * Displays the contents of this element
  78      */
  79     public void display(int indent) {
  80         indent(indent);
  81         Util.println("with-param " + _name);
  82         if (_select != null) {
  83             indent(indent + IndentIncrement);
  84             Util.println("select " + _select.toString());
  85         }
  86         displayContents(indent + IndentIncrement);
  87     }
  88 


 153             }
 154         }
 155         else {
 156             typeCheckContents(stable);
 157         }
 158         return Type.Void;
 159     }
 160 
 161     /**
 162      * Compile the value of the parameter, which is either in an expression in
 163      * a 'select' attribute, or in the with-param element's body
 164      */
 165     public void translateValue(ClassGenerator classGen,
 166                                MethodGenerator methodGen) {
 167         // Compile expression is 'select' attribute if present
 168         if (_select != null) {
 169             _select.translate(classGen, methodGen);
 170             _select.startIterator(classGen, methodGen);
 171         }
 172         // If not, compile result tree from parameter body if present.
 173         // Store result tree into local variable for releasing it later
 174         else if (hasContents()) {
 175             final InstructionList il = methodGen.getInstructionList();
 176             compileResultTree(classGen, methodGen);
 177             _domAdapter = methodGen.addLocalVariable2("@" + _escapedName, Type.ResultTree.toJCType(), il.getEnd());
 178             il.append(DUP);
 179             il.append(new ASTORE(_domAdapter.getIndex()));
 180         }
 181         // If neither are present then store empty string in parameter slot
 182         else {
 183             final ConstantPoolGen cpg = classGen.getConstantPool();
 184             final InstructionList il = methodGen.getInstructionList();
 185             il.append(new PUSH(cpg, Constants.EMPTYSTRING));
 186         }
 187     }
 188 
 189     /**
 190      * This code generates a sequence of bytecodes that call the
 191      * addParameter() method in AbstractTranslet. The method call will add
 192      * (or update) the parameter frame with the new parameter value.
 193      */
 194     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
 195         final ConstantPoolGen cpg = classGen.getConstantPool();
 196         final InstructionList il = methodGen.getInstructionList();
 197 
 198         // Translate the value and put it on the stack
 199         if (_doParameterOptimization) {


 202         }
 203 
 204         // Make name acceptable for use as field name in class
 205         String name = Util.escape(getEscapedName());
 206 
 207         // Load reference to the translet (method is in AbstractTranslet)
 208         il.append(classGen.loadTranslet());
 209 
 210         // Load the name of the parameter
 211         il.append(new PUSH(cpg, name)); // TODO: namespace ?
 212         // Generete the value of the parameter (use value in 'select' by def.)
 213         translateValue(classGen, methodGen);
 214         // Mark this parameter value is not being the default value
 215         il.append(new PUSH(cpg, false));
 216         // Pass the parameter to the template
 217         il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
 218                                                      ADD_PARAMETER,
 219                                                      ADD_PARAMETER_SIG)));
 220         il.append(POP); // cleanup stack
 221     }
 222 
 223     /**
 224      * Release the compiled result tree.
 225      */
 226     public void releaseResultTree(ClassGenerator classGen, MethodGenerator methodGen) {
 227         if (_domAdapter != null) {
 228             final ConstantPoolGen cpg = classGen.getConstantPool();
 229             final InstructionList il = methodGen.getInstructionList();
 230             if (classGen.getStylesheet().callsNodeset() && classGen.getDOMClass().equals(MULTI_DOM_CLASS)) {
 231                 final int removeDA = cpg.addMethodref(MULTI_DOM_CLASS, "removeDOMAdapter", "(" + DOM_ADAPTER_SIG + ")V");
 232                 il.append(methodGen.loadDOM());
 233                 il.append(new CHECKCAST(cpg.addClass(MULTI_DOM_CLASS)));
 234                 il.append(new ALOAD(_domAdapter.getIndex()));
 235                 il.append(new CHECKCAST(cpg.addClass(DOM_ADAPTER_CLASS)));
 236                 il.append(new INVOKEVIRTUAL(removeDA));
 237             }
 238             final int release = cpg.addInterfaceMethodref(DOM_IMPL_CLASS, "release", "()V");
 239             il.append(new ALOAD(_domAdapter.getIndex()));
 240             il.append(new INVOKEINTERFACE(release, 1));
 241             _domAdapter = null;
 242          }
 243      }
 244 }
< prev index next >