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