aboutsummaryrefslogtreecommitdiff
path: root/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Template.java
blob: 61302e271882cb471bea0f4c08f05dec02b79a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * reserved comment block
 * DO NOT REMOVE OR ALTER!
 */
/*
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 * $Id: Template.java,v 1.2.4.1 2005/09/12 11:30:11 pvedula Exp $
 */

package com.sun.org.apache.xalan.internal.xsltc.compiler;

import java.util.Vector;

import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
import com.sun.org.apache.bcel.internal.generic.InstructionList;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NamedMethodGenerator;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
import com.sun.org.apache.xml.internal.utils.XML11Char;


/**
 * @author Jacek Ambroziak
 * @author Santiago Pericas-Geertsen
 * @author Morten Jorgensen
 * @author Erwin Bolwidt <ejb@klomp.org>
 */
public final class Template extends TopLevelElement {

    private QName   _name;     // The name of the template (if any)
    private QName   _mode;     // Mode in which this template is instantiated.
    private Pattern _pattern;  // Matching pattern defined for this template.
    private double  _priority; // Matching priority of this template.
    private int     _position; // Position within stylesheet (prio. resolution)
    private boolean _disabled = false;
    private boolean _compiled = false;//make sure it is compiled only once
    private boolean _simplified = false;

    // True if this is a simple named template. A simple named
    // template is a template which only has a name but no match pattern.
    private boolean _isSimpleNamedTemplate = false;

    // The list of parameters in this template. This is only used
    // for simple named templates.
    private Vector  _parameters = new Vector();

    public boolean hasParams() {
        return _parameters.size() > 0;
    }

    public boolean isSimplified() {
        return(_simplified);
    }

    public void setSimplified() {
        _simplified = true;
    }

    public boolean isSimpleNamedTemplate() {
        return _isSimpleNamedTemplate;
    }

    public void addParameter(Param param) {
        _parameters.addElement(param);
    }

    public Vector getParameters() {
        return _parameters;
    }

    public void disable() {
        _disabled = true;
    }

    public boolean disabled() {
        return(_disabled);
    }

    public double getPriority() {
        return _priority;
    }

    public int getPosition() {
        return(_position);
    }

    public boolean isNamed() {
        return _name != null;
    }

    public Pattern getPattern() {
        return _pattern;
    }

    public QName getName() {
        return _name;
    }

    public void setName(QName qname) {
        if (_name == null) _name = qname;
    }

    public QName getModeName() {
        return _mode;
    }

    /**
     * Compare this template to another. First checks priority, then position.
     */
    public int compareTo(Object template) {
        Template other = (Template)template;
        if (_priority > other._priority)
            return 1;
        else if (_priority < other._priority)
            return -1;
        else if (_position > other._position)
            return 1;
        else if (_position < other._position)
            return -1;
        else
            return 0;
    }

    public void display(int indent) {
        Util.println('\n');
        indent(indent);
        if (_name != null) {
            indent(indent);
            Util.println("name = " + _name);
        }
        else if (_pattern != null) {
            indent(indent);
            Util.println("match = " + _pattern.toString());
        }
        if (_mode != null) {
            indent(indent);
            Util.println("mode = " + _mode);
        }
        displayContents(indent + IndentIncrement);
    }

    private boolean resolveNamedTemplates(Template other, Parser parser) {

        if (other == null) return true;

        SymbolTable stable = parser.getSymbolTable();

        final int us = this.getImportPrecedence();
        final int them = other.getImportPrecedence();

        if (us > them) {
            other.disable();
            return true;
        }
        else if (us < them) {
            stable.addTemplate(other);
            this.disable();
            return true;
        }
        else {
            return false;
        }
    }

    private Stylesheet _stylesheet = null;

    public Stylesheet getStylesheet() {
        return _stylesheet;
    }

    public void parseContents(Parser parser) {

        final String name     = getAttribute("name");
        final String mode     = getAttribute("mode");
        final String match    = getAttribute("match");
        final String priority = getAttribute("priority");

        _stylesheet = super.getStylesheet();

        if (name.length() > 0) {
            if (!XML11Char.isXML11ValidQName(name)) {
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
                parser.reportError(Constants.ERROR, err);
            }
            _name = parser.getQNameIgnoreDefaultNs(name);
        }

        if (mode.length() > 0) {
            if (!XML11Char.isXML11ValidQName(mode)) {
                ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, mode, this);
                parser.reportError(Constants.ERROR, err);
            }
            _mode = parser.getQNameIgnoreDefaultNs(mode);
        }

        if (match.length() > 0) {
            _pattern = parser.parsePattern(this, "match", null);
        }

        if (priority.length() > 0) {
            _priority = Double.parseDouble(priority);
        }
        else {
            if (_pattern != null)
                _priority = _pattern.getPriority();
            else
                _priority = Double.NaN;
        }

        _position = parser.getTemplateIndex();

        // Add the (named) template to the symbol table
        if (_name != null) {
            Template other = parser.getSymbolTable().addTemplate(this);
            if (!resolveNamedTemplates(other, parser)) {
                ErrorMsg err =
                    new ErrorMsg(ErrorMsg.TEMPLATE_REDEF_ERR, _name, this);
                parser.reportError(Constants.ERROR, err);
            }
            // Is this a simple named template?
            if (_pattern == null && _mode == null) {
                _isSimpleNamedTemplate = true;
            }
        }

        if (_parent instanceof Stylesheet) {
            ((Stylesheet)_parent).addTemplate(this);
        }

        parser.setTemplate(this);       // set current template
        parseChildren(parser);
        parser.setTemplate(null);       // clear template
    }

    /**
     * When the parser realises that it is dealign with a simplified stylesheet
     * it will create an empty Stylesheet object with the root element of the
     * stylesheet (a LiteralElement object) as its only child. The Stylesheet
     * object will then create this Template object and invoke this method to
     * force some specific behaviour. What we need to do is:
     *  o) create a pattern matching on the root node
     *  o) add the LRE root node (the only child of the Stylesheet) as our
     *     only child node
     *  o) set the empty Stylesheet as our parent
     *  o) set this template as the Stylesheet's only child
     */
    public void parseSimplified(Stylesheet stylesheet, Parser parser) {

        _stylesheet = stylesheet;
        setParent(stylesheet);

        _name = null;
        _mode = null;
        _priority = Double.NaN;
        _pattern = parser.parsePattern(this, "/");

        final Vector contents = _stylesheet.getContents();
        final SyntaxTreeNode root = (SyntaxTreeNode)contents.elementAt(0);

        if (root instanceof LiteralElement) {
            addElement(root);
            root.setParent(this);
            contents.set(0, this);
            parser.setTemplate(this);
            root.parseContents(parser);
            parser.setTemplate(null);
        }
    }

    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
        if (_pattern != null) {
            _pattern.typeCheck(stable);
        }

        return typeCheckContents(stable);
    }

    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
        final ConstantPoolGen cpg = classGen.getConstantPool();
        final InstructionList il = methodGen.getInstructionList();

        if (_disabled) return;
        // bug fix #4433133, add a call to named template from applyTemplates
        String className = classGen.getClassName();

        if (_compiled && isNamed()){
            String methodName = Util.escape(_name.toString());
            il.append(classGen.loadTranslet());
            il.append(methodGen.loadDOM());
            il.append(methodGen.loadIterator());
            il.append(methodGen.loadHandler());
            il.append(methodGen.loadCurrentNode());
            il.append(new INVOKEVIRTUAL(cpg.addMethodref(className,
                                                         methodName,
                                                         "("
                                                         + DOM_INTF_SIG
                                                         + NODE_ITERATOR_SIG
                                                         + TRANSLET_OUTPUT_SIG
                                                         + "I)V")));
            return;
        }

        if (_compiled) return;
        _compiled = true;

        // %OPT% Special handling for simple named templates.
        if (_isSimpleNamedTemplate && methodGen instanceof NamedMethodGenerator) {
            int numParams = _parameters.size();
            NamedMethodGenerator namedMethodGen = (NamedMethodGenerator)methodGen;

            // Update load/store instructions to access Params from the stack
            for (int i = 0; i < numParams; i++) {
                Param param = (Param)_parameters.elementAt(i);
                param.setLoadInstruction(namedMethodGen.loadParameter(i));
                param.setStoreInstruction(namedMethodGen.storeParameter(i));
            }
        }

        translateContents(classGen, methodGen);
        il.setPositions(true);
    }

}