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