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: AttributeSet.java,v 1.17 2004/02/24 03:55:47 zongaro Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Enumeration;
  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.AttributeSetMethodGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  32. import com.sun.org.apache.xml.internal.utils.XMLChar;
  33. /**
  34. * @author Jacek Ambroziak
  35. * @author Santiago Pericas-Geertsen
  36. * @author Morten Jorgensen
  37. */
  38. final class AttributeSet extends TopLevelElement {
  39. // This prefix is used for the method name of attribute set methods
  40. private static final String AttributeSetPrefix = "$as$";
  41. // Element contents
  42. private QName _name;
  43. private UseAttributeSets _useSets;
  44. private AttributeSet _mergeSet;
  45. private String _method;
  46. private boolean _ignore = false;
  47. /**
  48. * Returns the QName of this attribute set
  49. */
  50. public QName getName() {
  51. return _name;
  52. }
  53. /**
  54. * Returns the method name of this attribute set. This method name is
  55. * generated by the compiler (XSLTC)
  56. */
  57. public String getMethodName() {
  58. return _method;
  59. }
  60. /**
  61. * Call this method to prevent a method for being compiled for this set.
  62. * This is used in case several <xsl:attribute-set...> elements constitute
  63. * a single set (with one name). The last element will merge itself with
  64. * any previous set(s) with the same name and disable the other set(s).
  65. */
  66. public void ignore() {
  67. _ignore = true;
  68. }
  69. /**
  70. * Parse the contents of this attribute set. Recognised attributes are
  71. * "name" (required) and "use-attribute-sets" (optional).
  72. */
  73. public void parseContents(Parser parser) {
  74. // Get this attribute set's name
  75. final String name = getAttribute("name");
  76. if (!XMLChar.isValidQName(name)) {
  77. ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
  78. parser.reportError(Constants.ERROR, err);
  79. }
  80. _name = parser.getQNameIgnoreDefaultNs(name);
  81. if ((_name == null) || (_name.equals(EMPTYSTRING))) {
  82. ErrorMsg msg = new ErrorMsg(ErrorMsg.UNNAMED_ATTRIBSET_ERR, this);
  83. parser.reportError(Constants.ERROR, msg);
  84. }
  85. // Get any included attribute sets (similar to inheritance...)
  86. final String useSets = getAttribute("use-attribute-sets");
  87. if (useSets.length() > 0) {
  88. if (!Util.isValidQNames(useSets)) {
  89. ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, useSets, this);
  90. parser.reportError(Constants.ERROR, err);
  91. }
  92. _useSets = new UseAttributeSets(useSets, parser);
  93. }
  94. // Parse the contents of this node. All child elements must be
  95. // <xsl:attribute> elements. Other elements cause an error.
  96. final Vector contents = getContents();
  97. final int count = contents.size();
  98. for (int i=0; i<count; i++) {
  99. SyntaxTreeNode child = (SyntaxTreeNode)contents.elementAt(i);
  100. if (child instanceof XslAttribute) {
  101. parser.getSymbolTable().setCurrentNode(child);
  102. child.parseContents(parser);
  103. }
  104. else if (child instanceof Text) {
  105. // ignore
  106. }
  107. else {
  108. ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_CHILD_ERR, this);
  109. parser.reportError(Constants.ERROR, msg);
  110. }
  111. }
  112. // Point the symbol table back at us...
  113. parser.getSymbolTable().setCurrentNode(this);
  114. }
  115. /**
  116. * Type check the contents of this element
  117. */
  118. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  119. if (_ignore) return (Type.Void);
  120. // _mergeSet Point to any previous definition of this attribute set
  121. _mergeSet = stable.addAttributeSet(this);
  122. _method = AttributeSetPrefix + getXSLTC().nextAttributeSetSerial();
  123. if (_useSets != null) _useSets.typeCheck(stable);
  124. typeCheckContents(stable);
  125. return Type.Void;
  126. }
  127. /**
  128. * Compile a method that outputs the attributes in this set
  129. */
  130. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  131. if (_ignore) return;
  132. // Create a new method generator for an attribute set method
  133. methodGen = new AttributeSetMethodGenerator(_method, classGen);
  134. // Generate a reference to previous attribute-set definitions with the
  135. // same name first. Those later in the stylesheet take precedence.
  136. if (_mergeSet != null) {
  137. final ConstantPoolGen cpg = classGen.getConstantPool();
  138. final InstructionList il = methodGen.getInstructionList();
  139. final String methodName = _mergeSet.getMethodName();
  140. il.append(classGen.loadTranslet());
  141. il.append(methodGen.loadDOM());
  142. il.append(methodGen.loadIterator());
  143. il.append(methodGen.loadHandler());
  144. final int method = cpg.addMethodref(classGen.getClassName(),
  145. methodName, ATTR_SET_SIG);
  146. il.append(new INVOKESPECIAL(method));
  147. }
  148. // Translate other used attribute sets first, as local attributes
  149. // take precedence (last attributes overrides first)
  150. if (_useSets != null) _useSets.translate(classGen, methodGen);
  151. // Translate all local attributes
  152. final Enumeration attributes = elements();
  153. while (attributes.hasMoreElements()) {
  154. SyntaxTreeNode element = (SyntaxTreeNode)attributes.nextElement();
  155. if (element instanceof XslAttribute) {
  156. final XslAttribute attribute = (XslAttribute)element;
  157. attribute.translate(classGen, methodGen);
  158. }
  159. }
  160. final InstructionList il = methodGen.getInstructionList();
  161. il.append(RETURN);
  162. methodGen.stripAttributes(true);
  163. methodGen.setMaxLocals();
  164. methodGen.setMaxStack();
  165. methodGen.removeNOPs();
  166. classGen.addMethod(methodGen.getMethod());
  167. }
  168. public String toString() {
  169. StringBuffer buf = new StringBuffer("attribute-set: ");
  170. // Translate all local attributes
  171. final Enumeration attributes = elements();
  172. while (attributes.hasMoreElements()) {
  173. final XslAttribute attribute =
  174. (XslAttribute)attributes.nextElement();
  175. buf.append(attribute);
  176. }
  177. return(buf.toString());
  178. }
  179. }