KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > compiler > AttributeSet


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
20 package com.sun.org.apache.xalan.internal.xsltc.compiler;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
27 import com.sun.org.apache.bcel.internal.generic.InstructionList;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.AttributeSetMethodGenerator;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
35 import com.sun.org.apache.xml.internal.utils.XMLChar;
36
37 /**
38  * @author Jacek Ambroziak
39  * @author Santiago Pericas-Geertsen
40  * @author Morten Jorgensen
41  */

42 final class AttributeSet extends TopLevelElement {
43
44     // This prefix is used for the method name of attribute set methods
45
private static final String JavaDoc AttributeSetPrefix = "$as$";
46     
47     // Element contents
48
private QName _name;
49     private UseAttributeSets _useSets;
50     private AttributeSet _mergeSet;
51     private String JavaDoc _method;
52     private boolean _ignore = false;
53     
54     /**
55      * Returns the QName of this attribute set
56      */

57     public QName getName() {
58     return _name;
59     }
60
61     /**
62      * Returns the method name of this attribute set. This method name is
63      * generated by the compiler (XSLTC)
64      */

65     public String JavaDoc getMethodName() {
66     return _method;
67     }
68
69     /**
70      * Call this method to prevent a method for being compiled for this set.
71      * This is used in case several <xsl:attribute-set...> elements constitute
72      * a single set (with one name). The last element will merge itself with
73      * any previous set(s) with the same name and disable the other set(s).
74      */

75     public void ignore() {
76     _ignore = true;
77     }
78
79     /**
80      * Parse the contents of this attribute set. Recognised attributes are
81      * "name" (required) and "use-attribute-sets" (optional).
82      */

83     public void parseContents(Parser parser) {
84     
85     // Get this attribute set's name
86
final String JavaDoc name = getAttribute("name");
87         
88         if (!XMLChar.isValidQName(name)) {
89             ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
90             parser.reportError(Constants.ERROR, err);
91         }
92         _name = parser.getQNameIgnoreDefaultNs(name);
93     if ((_name == null) || (_name.equals(EMPTYSTRING))) {
94         ErrorMsg msg = new ErrorMsg(ErrorMsg.UNNAMED_ATTRIBSET_ERR, this);
95         parser.reportError(Constants.ERROR, msg);
96     }
97
98     // Get any included attribute sets (similar to inheritance...)
99
final String JavaDoc useSets = getAttribute("use-attribute-sets");
100     if (useSets.length() > 0) {
101             if (!Util.isValidQNames(useSets)) {
102                 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, useSets, this);
103                 parser.reportError(Constants.ERROR, err);
104             }
105         _useSets = new UseAttributeSets(useSets, parser);
106     }
107
108     // Parse the contents of this node. All child elements must be
109
// <xsl:attribute> elements. Other elements cause an error.
110
final Vector JavaDoc contents = getContents();
111     final int count = contents.size();
112     for (int i=0; i<count; i++) {
113         SyntaxTreeNode child = (SyntaxTreeNode)contents.elementAt(i);
114         if (child instanceof XslAttribute) {
115         parser.getSymbolTable().setCurrentNode(child);
116         child.parseContents(parser);
117         }
118         else if (child instanceof Text) {
119         // ignore
120
}
121         else {
122         ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_CHILD_ERR, this);
123         parser.reportError(Constants.ERROR, msg);
124         }
125     }
126
127     // Point the symbol table back at us...
128
parser.getSymbolTable().setCurrentNode(this);
129     }
130
131     /**
132      * Type check the contents of this element
133      */

134     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
135
136     if (_ignore) return (Type.Void);
137
138         // _mergeSet Point to any previous definition of this attribute set
139
_mergeSet = stable.addAttributeSet(this);
140
141     _method = AttributeSetPrefix + getXSLTC().nextAttributeSetSerial();
142
143     if (_useSets != null) _useSets.typeCheck(stable);
144     typeCheckContents(stable);
145     return Type.Void;
146     }
147
148     /**
149      * Compile a method that outputs the attributes in this set
150      */

151     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
152
153     if (_ignore) return;
154
155     // Create a new method generator for an attribute set method
156
methodGen = new AttributeSetMethodGenerator(_method, classGen);
157
158         // Generate a reference to previous attribute-set definitions with the
159
// same name first. Those later in the stylesheet take precedence.
160
if (_mergeSet != null) {
161             final ConstantPoolGen cpg = classGen.getConstantPool();
162             final InstructionList il = methodGen.getInstructionList();
163             final String JavaDoc methodName = _mergeSet.getMethodName();
164
165             il.append(classGen.loadTranslet());
166             il.append(methodGen.loadDOM());
167             il.append(methodGen.loadIterator());
168             il.append(methodGen.loadHandler());
169             final int method = cpg.addMethodref(classGen.getClassName(),
170                                                 methodName, ATTR_SET_SIG);
171             il.append(new INVOKESPECIAL(method));
172         }
173
174     // Translate other used attribute sets first, as local attributes
175
// take precedence (last attributes overrides first)
176
if (_useSets != null) _useSets.translate(classGen, methodGen);
177
178     // Translate all local attributes
179
final Enumeration JavaDoc attributes = elements();
180     while (attributes.hasMoreElements()) {
181         SyntaxTreeNode element = (SyntaxTreeNode)attributes.nextElement();
182         if (element instanceof XslAttribute) {
183         final XslAttribute attribute = (XslAttribute)element;
184         attribute.translate(classGen, methodGen);
185         }
186     }
187     final InstructionList il = methodGen.getInstructionList();
188     il.append(RETURN);
189     
190     methodGen.stripAttributes(true);
191     methodGen.setMaxLocals();
192     methodGen.setMaxStack();
193     methodGen.removeNOPs();
194     classGen.addMethod(methodGen.getMethod());
195     }
196
197     public String JavaDoc toString() {
198     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("attribute-set: ");
199     // Translate all local attributes
200
final Enumeration JavaDoc attributes = elements();
201     while (attributes.hasMoreElements()) {
202         final XslAttribute attribute =
203         (XslAttribute)attributes.nextElement();
204         buf.append(attribute);
205     }
206     return(buf.toString());
207     }
208 }
209
Popular Tags