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: ClassGenerator.java,v 1.2.4.1 2005/09/05 11:07:09 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler.util;
25  
26  import com.sun.org.apache.bcel.internal.classfile.Method;
27  import com.sun.org.apache.bcel.internal.generic.ALOAD;
28  import com.sun.org.apache.bcel.internal.generic.ClassGen;
29  import com.sun.org.apache.bcel.internal.generic.Instruction;
30  import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.Parser;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet;
33  
34  /**
35   * The class that implements any class that inherits from
36   * <tt>AbstractTranslet</tt>, i.e. any translet. Methods in this
37   * class may be of the following kinds:
38   *
39   * 1. Main method: applyTemplates, implemented by intances of
40   * <tt>MethodGenerator</tt>.
41   *
42   * 2. Named methods: for named templates, implemented by instances
43   * of <tt>NamedMethodGenerator</tt>.
44   *
45   * 3. Rt methods: for result tree fragments, implemented by
46   * instances of <tt>RtMethodGenerator</tt>.
47   * @author Jacek Ambroziak
48   * @author Santiago Pericas-Geertsen
49   */
50  public class ClassGenerator extends ClassGen {
51      protected final static int TRANSLET_INDEX = 0;
52      protected static int INVALID_INDEX  = -1;
53  
54      private Stylesheet _stylesheet;
55      private final Parser _parser;               // --> can be moved to XSLT
56      // a  single instance cached here
57      private final Instruction _aloadTranslet;
58      private final String _domClass;
59      private final String _domClassSig;
60      private final String _applyTemplatesSig;
61          private final String _applyTemplatesSigForImport;
62  
63      public ClassGenerator(String class_name, String super_class_name,
64                            String file_name,
65                            int access_flags, String[] interfaces,
66                            Stylesheet stylesheet) {
67          super(class_name, super_class_name, file_name,
68                access_flags, interfaces);
69          _stylesheet = stylesheet;
70          _parser = stylesheet.getParser();
71          _aloadTranslet = new ALOAD(TRANSLET_INDEX);
72  
73          if (stylesheet.isMultiDocument()) {
74              _domClass = "com.sun.org.apache.xalan.internal.xsltc.dom.MultiDOM";
75              _domClassSig = "Lcom/sun/org/apache/xalan/internal/xsltc/dom/MultiDOM;";
76          }
77          else {
78              _domClass = "com.sun.org.apache.xalan.internal.xsltc.dom.DOMAdapter";
79              _domClassSig = "Lcom/sun/org/apache/xalan/internal/xsltc/dom/DOMAdapter;";
80          }
81          _applyTemplatesSig = "("
82              + Constants.DOM_INTF_SIG
83              + Constants.NODE_ITERATOR_SIG
84              + Constants.TRANSLET_OUTPUT_SIG
85              + ")V";
86  
87      _applyTemplatesSigForImport = "("
88          + Constants.DOM_INTF_SIG
89          + Constants.NODE_ITERATOR_SIG
90          + Constants.TRANSLET_OUTPUT_SIG
91          + Constants.NODE_FIELD_SIG
92          + ")V";
93      }
94  
95      public final Parser getParser() {
96          return _parser;
97      }
98  
99      public final Stylesheet getStylesheet() {
100         return _stylesheet;
101     }
102 
103     /**
104      * Pretend this is the stylesheet class. Useful when compiling
105      * references to global variables inside a predicate.
106      */
107     public final String getClassName() {
108         return _stylesheet.getClassName();
109     }
110 
111     public Instruction loadTranslet() {
112         return _aloadTranslet;
113     }
114 
115     public final String getDOMClass() {
116         return _domClass;
117     }
118 
119     public final String getDOMClassSig() {
120         return _domClassSig;
121     }
122 
123     public final String getApplyTemplatesSig() {
124         return _applyTemplatesSig;
125     }
126 
127     public final String getApplyTemplatesSigForImport() {
128     return _applyTemplatesSigForImport;
129     }
130 
131     /**
132      * Returns <tt>true</tt> or <tt>false</tt> depending on whether
133      * this class inherits from <tt>AbstractTranslet</tt> or not.
134      */
135     public boolean isExternal() {
136         return false;
137     }
138     public void addMethod(MethodGenerator methodGen) {
139         Method[] methodsToAdd = methodGen.getGeneratedMethods(this);
140         for (int i = 0; i < methodsToAdd.length; i++) {
141             addMethod(methodsToAdd[i]);
142 }
143     }
144 }