KickJava   Java API By Example, From Geeks To Geeks.

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


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: UseAttributeSets.java,v 1.12 2004/02/16 22:25:10 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import java.util.StringTokenizer JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.bcel.generic.ConstantPoolGen;
26 import org.apache.bcel.generic.INVOKESPECIAL;
27 import org.apache.bcel.generic.InstructionList;
28 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
29 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
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
34 /**
35  * @author Jacek Ambroziak
36  * @author Santiago Pericas-Geertsen
37  * @author Morten Jorgensen
38  */

39 final class UseAttributeSets extends Instruction {
40
41     // Only error that can occur:
42
private final static String JavaDoc ATTR_SET_NOT_FOUND =
43     "";
44
45     // Contains the names of all references attribute sets
46
private final Vector JavaDoc _sets = new Vector JavaDoc(2);
47
48     /**
49      * Constructur - define initial attribute sets to use
50      */

51     public UseAttributeSets(String JavaDoc setNames, Parser parser) {
52     setParser(parser);
53     addAttributeSets(setNames);
54     }
55
56     /**
57      * This method is made public to enable an AttributeSet object to merge
58      * itself with another AttributeSet (including any other AttributeSets
59      * the two may inherit from).
60      */

61     public void addAttributeSets(String JavaDoc setNames) {
62     if ((setNames != null) && (!setNames.equals(Constants.EMPTYSTRING))) {
63         final StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(setNames);
64         while (tokens.hasMoreTokens()) {
65         final QName qname =
66             getParser().getQNameIgnoreDefaultNs(tokens.nextToken());
67         _sets.add(qname);
68         }
69     }
70     }
71
72     /**
73      * Do nada.
74      */

75     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
76     return Type.Void;
77     }
78
79     /**
80      * Generate a call to the method compiled for this attribute set
81      */

82     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
83
84     final ConstantPoolGen cpg = classGen.getConstantPool();
85     final InstructionList il = methodGen.getInstructionList();
86     final SymbolTable symbolTable = getParser().getSymbolTable();
87
88     // Go through each attribute set and generate a method call
89
for (int i=0; i<_sets.size(); i++) {
90         // Get the attribute set name
91
final QName name = (QName)_sets.elementAt(i);
92         // Get the AttributeSet reference from the symbol table
93
final AttributeSet attrs = symbolTable.lookupAttributeSet(name);
94         // Compile the call to the set's method if the set exists
95
if (attrs != null) {
96         final String JavaDoc methodName = attrs.getMethodName();
97         il.append(classGen.loadTranslet());
98         il.append(methodGen.loadDOM());
99         il.append(methodGen.loadIterator());
100         il.append(methodGen.loadHandler());
101         final int method = cpg.addMethodref(classGen.getClassName(),
102                             methodName, ATTR_SET_SIG);
103         il.append(new INVOKESPECIAL(method));
104         }
105         // Generate an error if the attribute set does not exist
106
else {
107         final Parser parser = getParser();
108         final String JavaDoc atrs = name.toString();
109         reportError(this, parser, ErrorMsg.ATTRIBSET_UNDEF_ERR, atrs);
110         }
111     }
112     }
113 }
114
Popular Tags