KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLAttributeSet


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.instruct.*;
4 import net.sf.saxon.om.*;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.AnyItemType;
7 import net.sf.saxon.value.EmptySequence;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14 * An xsl:attribute-set element in the stylesheet. <br>
15 */

16
17 public class XSLAttributeSet extends StyleElement implements StylesheetProcedure {
18
19     private String JavaDoc nameAtt;
20                 // the name of the attribute set as written
21

22     private String JavaDoc useAtt;
23                 // the value of the use-attribute-sets attribute, as supplied
24

25     private SlotManager stackFrameMap;
26                 // needed if variables are used
27

28     private List JavaDoc attributeSetElements = null;
29                 // list of XSLAttributeSet objects referenced by this one
30

31     private AttributeSet[] useAttributeSets = null;
32                 // compiled instructions for the attribute sets used by this one
33

34     private AttributeSet procedure = new AttributeSet();
35                 // the compiled form of this attribute set
36

37     private int referenceCount = 0;
38                 // the number of references to this attribute set
39

40     private boolean validated = false;
41
42     public int getAttributeSetFingerprint() {
43         return getObjectFingerprint();
44     }
45
46     public AttributeSet getInstruction() {
47         return procedure;
48     }
49
50     public void incrementReferenceCount() {
51         referenceCount++;
52     }
53
54     public void prepareAttributes() throws XPathException {
55         useAtt = null;
56
57         AttributeCollection atts = getAttributeList();
58
59         for (int a=0; a<atts.getLength(); a++) {
60             int nc = atts.getNameCode(a);
61             String JavaDoc f = getNamePool().getClarkName(nc);
62             if (f==StandardNames.NAME) {
63                 nameAtt = atts.getValue(a).trim();
64             } else if (f==StandardNames.USE_ATTRIBUTE_SETS) {
65                 useAtt = atts.getValue(a);
66             } else {
67                 checkUnknownAttribute(nc);
68             }
69         }
70
71         if (nameAtt==null) {
72             reportAbsence("name");
73             return;
74         }
75
76         try {
77             setObjectNameCode(makeNameCode(nameAtt.trim()));
78         } catch (NamespaceException err) {
79             compileError(err.getMessage(), "XTSE0280");
80         } catch (XPathException err) {
81             compileError(err.getMessage(), "XTSE0280");
82         }
83
84     }
85
86     public void validate() throws XPathException {
87
88         if (validated) return;
89
90         checkTopLevel(null);
91
92         stackFrameMap = getConfiguration().makeSlotManager();
93
94         AxisIterator kids = iterateAxis(Axis.CHILD);
95         while (true) {
96             Item child = kids.next();
97             if (child == null) {
98                 break;
99             }
100             if (!(child instanceof XSLAttribute)) {
101                 compileError("Only xsl:attribute is allowed within xsl:attribute-set", "XTSE0010");
102             }
103         }
104
105         if (useAtt!=null) {
106             // identify any attribute sets that this one refers to
107

108             attributeSetElements = new ArrayList JavaDoc(5);
109             useAttributeSets = getAttributeSets(useAtt, attributeSetElements);
110
111             // check for circularity
112

113             for (Iterator JavaDoc it=attributeSetElements.iterator(); it.hasNext();) {
114                 ((XSLAttributeSet)it.next()).checkCircularity(this);
115             }
116         }
117
118         validated = true;
119     }
120
121     /**
122     * Check for circularity: specifically, check that this attribute set does not contain
123     * a direct or indirect reference to the one supplied as a parameter
124     */

125
126     public void checkCircularity(XSLAttributeSet origin) throws XPathException {
127         if (this==origin) {
128             compileError("The definition of the attribute set is circular", "XTSE0720");
129         } else {
130             if (!validated) {
131                 // if this attribute set isn't validated yet, we don't check it.
132
// The circularity will be detected when the last attribute set in the cycle
133
// gets validated
134
return;
135             }
136             if (attributeSetElements != null) {
137                 for (Iterator JavaDoc it=attributeSetElements.iterator(); it.hasNext();) {
138                     ((XSLAttributeSet)it.next()).checkCircularity(origin);
139                 }
140             }
141         }
142     }
143
144     /**
145     * Get details of stack frame
146     */

147
148     public SlotManager getSlotManager() {
149         return stackFrameMap;
150     }
151     /**
152      * Compile the attribute set
153      * @param exec the Executable
154      * @return a Procedure object representing the compiled attribute set
155      * @throws XPathException if a failure is detected
156      */

157     public Expression compile(Executable exec) throws XPathException {
158         if (referenceCount > 0 ) {
159             Expression body = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), true);
160             if (body == null) {
161                 body = EmptySequence.getInstance();
162             }
163
164             try {
165
166                 body = body.simplify(getStaticContext());
167                 if (getConfiguration().getTraceListener() != null) {
168                     TraceWrapper trace = new TraceInstruction(body, this);
169                     trace.setLocationId(allocateLocationId(getSystemId(), getLineNumber()));
170                     trace.setParentExpression(procedure);
171                     body = trace;
172                 }
173
174                 procedure.setUseAttributeSets(useAttributeSets);
175                 procedure.setNameCode(getObjectNameCode());
176                 procedure.setBody(body);
177                 procedure.setSystemId(getSystemId());
178                 procedure.setLineNumber(getLineNumber());
179                 procedure.setExecutable(exec);
180
181                 Expression exp2 = body.optimize(getConfiguration().getOptimizer(), staticContext, AnyItemType.getInstance());
182                 if (body != exp2) {
183                     procedure.setBody(exp2);
184                     body = exp2;
185                 }
186
187                 super.allocateSlots(body);
188                 procedure.setStackFrameMap(stackFrameMap);
189             } catch (XPathException e) {
190                 compileError(e);
191             }
192         }
193         return null;
194     }
195
196     /**
197      * Get the type of construct. This will be a constant in
198      * class {@link net.sf.saxon.trace.Location}. This method is part of
199      * the {@link net.sf.saxon.trace.InstructionInfo} interface
200      */

201
202     public int getConstructType() {
203         return StandardNames.XSL_ATTRIBUTE_SET;
204     }
205
206
207 }
208
209 //
210
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
211
// you may not use this file except in compliance with the License. You may obtain a copy of the
212
// License at http://www.mozilla.org/MPL/
213
//
214
// Software distributed under the License is distributed on an "AS IS" basis,
215
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
216
// See the License for the specific language governing rights and limitations under the License.
217
//
218
// The Original Code is: all this file.
219
//
220
// The Initial Developer of the Original Code is Michael H. Kay.
221
//
222
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
223
//
224
// Contributor(s): none.
225
//
226
Popular Tags