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: UseAttributeSets.java,v 1.5 2005/09/28 13:48:17 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.StringTokenizer;
27  import java.util.Vector;
28  
29  import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
30  import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
31  import com.sun.org.apache.bcel.internal.generic.InstructionList;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
33  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
34  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
35  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
36  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
37  
38  /**
39   * @author Jacek Ambroziak
40   * @author Santiago Pericas-Geertsen
41   * @author Morten Jorgensen
42   */
43  final class UseAttributeSets extends Instruction {
44  
45      // Only error that can occur:
46      private final static String ATTR_SET_NOT_FOUND =
47          "";
48  
49      // Contains the names of all references attribute sets
50      private final Vector _sets = new Vector(2);
51  
52      /**
53       * Constructur - define initial attribute sets to use
54       */
55      public UseAttributeSets(String setNames, Parser parser) {
56          setParser(parser);
57          addAttributeSets(setNames);
58      }
59  
60      /**
61       * This method is made public to enable an AttributeSet object to merge
62       * itself with another AttributeSet (including any other AttributeSets
63       * the two may inherit from).
64       */
65      public void addAttributeSets(String setNames) {
66          if ((setNames != null) && (!setNames.equals(Constants.EMPTYSTRING))) {
67              final StringTokenizer tokens = new StringTokenizer(setNames);
68              while (tokens.hasMoreTokens()) {
69                  final QName qname =
70                      getParser().getQNameIgnoreDefaultNs(tokens.nextToken());
71                  _sets.add(qname);
72              }
73          }
74      }
75  
76      /**
77       * Do nada.
78       */
79      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
80          return Type.Void;
81      }
82  
83      /**
84       * Generate a call to the method compiled for this attribute set
85       */
86      public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
87  
88          final ConstantPoolGen cpg = classGen.getConstantPool();
89          final InstructionList il = methodGen.getInstructionList();
90          final SymbolTable symbolTable = getParser().getSymbolTable();
91  
92          // Go through each attribute set and generate a method call
93          for (int i=0; i<_sets.size(); i++) {
94              // Get the attribute set name
95              final QName name = (QName)_sets.elementAt(i);
96              // Get the AttributeSet reference from the symbol table
97              final AttributeSet attrs = symbolTable.lookupAttributeSet(name);
98              // Compile the call to the set's method if the set exists
99              if (attrs != null) {
100                 final String methodName = attrs.getMethodName();
101                 il.append(classGen.loadTranslet());
102                 il.append(methodGen.loadDOM());
103                 il.append(methodGen.loadIterator());
104                 il.append(methodGen.loadHandler());
105                 il.append(methodGen.loadCurrentNode());
106                 final int method = cpg.addMethodref(classGen.getClassName(),
107                                                     methodName, ATTR_SET_SIG);
108                 il.append(new INVOKESPECIAL(method));
109             }
110             // Generate an error if the attribute set does not exist
111             else {
112                 final Parser parser = getParser();
113                 final String atrs = name.toString();
114                 reportError(this, parser, ErrorMsg.ATTRIBSET_UNDEF_ERR, atrs);
115             }
116         }
117     }
118 }