1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
23
24import com.sun.org.apache.bcel.internal.classfile.Method;
25import com.sun.org.apache.bcel.internal.generic.ALOAD;
26import com.sun.org.apache.bcel.internal.generic.ClassGen;
27import com.sun.org.apache.bcel.internal.generic.Instruction;
28import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
29import com.sun.org.apache.xalan.internal.xsltc.compiler.Parser;
30import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet;
31
32/**
33 * The class that implements any class that inherits from
34 * <tt>AbstractTranslet</tt>, i.e. any translet. Methods in this
35 * class may be of the following kinds:
36 *
37 * 1. Main method: applyTemplates, implemented by intances of
38 * <tt>MethodGenerator</tt>.
39 *
40 * 2. Named methods: for named templates, implemented by instances
41 * of <tt>NamedMethodGenerator</tt>.
42 *
43 * 3. Rt methods: for result tree fragments, implemented by
44 * instances of <tt>RtMethodGenerator</tt>.
45 * @author Jacek Ambroziak
46 * @author Santiago Pericas-Geertsen
47 */
48public class ClassGenerator extends ClassGen {
49    protected final static int TRANSLET_INDEX = 0;
50    protected static int INVALID_INDEX  = -1;
51
52    private Stylesheet _stylesheet;
53    private final Parser _parser;               // --> can be moved to XSLT
54    // a  single instance cached here
55    private final Instruction _aloadTranslet;
56    private final String _domClass;
57    private final String _domClassSig;
58    private final String _applyTemplatesSig;
59        private final String _applyTemplatesSigForImport;
60
61    public ClassGenerator(String class_name, String super_class_name,
62                          String file_name,
63                          int access_flags, String[] interfaces,
64                          Stylesheet stylesheet) {
65        super(class_name, super_class_name, file_name,
66              access_flags, interfaces);
67        _stylesheet = stylesheet;
68        _parser = stylesheet.getParser();
69        _aloadTranslet = new ALOAD(TRANSLET_INDEX);
70
71        if (stylesheet.isMultiDocument()) {
72            _domClass = "com.sun.org.apache.xalan.internal.xsltc.dom.MultiDOM";
73            _domClassSig = "Lcom/sun/org/apache/xalan/internal/xsltc/dom/MultiDOM;";
74        }
75        else {
76            _domClass = "com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter";
77            _domClassSig = "Lcom/sun/org/apache/xalan/internal/xsltc/dom/DOMAdapter;";
78        }
79        _applyTemplatesSig = "("
80            + Constants.DOM_INTF_SIG
81            + Constants.NODE_ITERATOR_SIG
82            + Constants.TRANSLET_OUTPUT_SIG
83            + ")V";
84
85    _applyTemplatesSigForImport = "("
86        + Constants.DOM_INTF_SIG
87        + Constants.NODE_ITERATOR_SIG
88        + Constants.TRANSLET_OUTPUT_SIG
89        + Constants.NODE_FIELD_SIG
90        + ")V";
91    }
92
93    public final Parser getParser() {
94        return _parser;
95    }
96
97    public final Stylesheet getStylesheet() {
98        return _stylesheet;
99    }
100
101    /**
102     * Pretend this is the stylesheet class. Useful when compiling
103     * references to global variables inside a predicate.
104     */
105    public final String getClassName() {
106        return _stylesheet.getClassName();
107    }
108
109    public Instruction loadTranslet() {
110        return _aloadTranslet;
111    }
112
113    public final String getDOMClass() {
114        return _domClass;
115    }
116
117    public final String getDOMClassSig() {
118        return _domClassSig;
119    }
120
121    public final String getApplyTemplatesSig() {
122        return _applyTemplatesSig;
123    }
124
125    public final String getApplyTemplatesSigForImport() {
126    return _applyTemplatesSigForImport;
127    }
128
129    /**
130     * Returns <tt>true</tt> or <tt>false</tt> depending on whether
131     * this class inherits from <tt>AbstractTranslet</tt> or not.
132     */
133    public boolean isExternal() {
134        return false;
135    }
136    public void addMethod(MethodGenerator methodGen) {
137        Method[] methodsToAdd = methodGen.getGeneratedMethods(this);
138        for (int i = 0; i < methodsToAdd.length; i++) {
139            addMethod(methodsToAdd[i]);
140}
141    }
142}
143