KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > ApplyImports


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: ApplyImports.java,v 1.13 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.util.Enumeration JavaDoc;
23
24 import org.apache.bcel.generic.ConstantPoolGen;
25 import org.apache.bcel.generic.INVOKESPECIAL;
26 import org.apache.bcel.generic.INVOKEVIRTUAL;
27 import org.apache.bcel.generic.InstructionList;
28 import org.apache.bcel.generic.NEW;
29 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
30 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
31 import org.apache.xalan.xsltc.compiler.util.Type;
32 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
33 import org.apache.xalan.xsltc.compiler.util.Util;
34
35 /**
36  * @author Morten Jorgensen
37  */

38 final class ApplyImports extends Instruction {
39
40     private QName _modeName;
41     private String JavaDoc _functionName;
42     private int _precedence;
43
44     public void display(int indent) {
45     indent(indent);
46     Util.println("ApplyTemplates");
47     indent(indent + IndentIncrement);
48     if (_modeName != null) {
49         indent(indent + IndentIncrement);
50         Util.println("mode " + _modeName);
51     }
52     }
53
54     /**
55      * Returns true if this <xsl:apply-imports/> element has parameters
56      */

57     public boolean hasWithParams() {
58     return hasContents();
59     }
60
61     /**
62      * Determine the lowest import precedence for any stylesheet imported
63      * or included by the stylesheet in which this <xsl:apply-imports/>
64      * element occured. The templates that are imported by the stylesheet in
65      * which this element occured will all have higher import precedence than
66      * the integer returned by this method.
67      */

68     private int getMinPrecedence(int max) {
69     Stylesheet stylesheet = getStylesheet();
70     Stylesheet root = getParser().getTopLevelStylesheet();
71
72     int min = max;
73
74     Enumeration JavaDoc templates = root.getContents().elements();
75     while (templates.hasMoreElements()) {
76         SyntaxTreeNode child = (SyntaxTreeNode)templates.nextElement();
77         if (child instanceof Template) {
78         Stylesheet curr = child.getStylesheet();
79         while ((curr != null) && (curr != stylesheet)) {
80             if (curr._importedFrom != null)
81             curr = curr._importedFrom;
82             else if (curr._includedFrom != null)
83             curr = curr._includedFrom;
84             else
85             curr = null;
86         }
87         if (curr == stylesheet) {
88             int prec = child.getStylesheet().getImportPrecedence();
89             if (prec < min) min = prec;
90         }
91         }
92     }
93     return (min);
94     }
95
96     /**
97      * Parse the attributes and contents of an <xsl:apply-imports/> element.
98      */

99     public void parseContents(Parser parser) {
100     // Indicate to the top-level stylesheet that all templates must be
101
// compiled into separate methods.
102
Stylesheet stylesheet = getStylesheet();
103     stylesheet.setTemplateInlining(false);
104
105     // Get the mode we are currently in (might not be any)
106
Template template = getTemplate();
107     _modeName = template.getModeName();
108     _precedence = template.getImportPrecedence();
109
110     // Get the method name for <xsl:apply-imports/> in this mode
111
stylesheet = parser.getTopLevelStylesheet();
112
113     // Get the [min,max> precedence of all templates imported under the
114
// current stylesheet
115
final int maxPrecedence = _precedence;
116     final int minPrecedence = getMinPrecedence(maxPrecedence);
117     final Mode mode = stylesheet.getMode(_modeName);
118     _functionName = mode.functionName(minPrecedence, maxPrecedence);
119
120     parseChildren(parser); // with-params
121
}
122
123     /**
124      * Type-check the attributes/contents of an <xsl:apply-imports/> element.
125      */

126     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
127     typeCheckContents(stable); // with-params
128
return Type.Void;
129     }
130
131     /**
132      * Translate call-template. A parameter frame is pushed only if
133      * some template in the stylesheet uses parameters.
134      */

135     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
136     final Stylesheet stylesheet = classGen.getStylesheet();
137     final ConstantPoolGen cpg = classGen.getConstantPool();
138     final InstructionList il = methodGen.getInstructionList();
139     final int current = methodGen.getLocalIndex("current");
140
141     // Push the arguments that are passed to applyTemplates()
142
il.append(classGen.loadTranslet());
143     il.append(methodGen.loadDOM());
144     // Wrap the current node inside an iterator
145
int init = cpg.addMethodref(SINGLETON_ITERATOR,
146                     "<init>", "("+NODE_SIG+")V");
147     il.append(new NEW(cpg.addClass(SINGLETON_ITERATOR)));
148     il.append(DUP);
149     il.append(methodGen.loadCurrentNode());
150     il.append(new INVOKESPECIAL(init));
151
152     il.append(methodGen.loadHandler());
153
154     // Construct the translet class-name and the signature of the method
155
final String JavaDoc className = classGen.getStylesheet().getClassName();
156     final String JavaDoc signature = classGen.getApplyTemplatesSig();
157     final int applyTemplates = cpg.addMethodref(className,
158                             _functionName,
159                             signature);
160     il.append(new INVOKEVIRTUAL(applyTemplates));
161     }
162
163 }
164
Popular Tags