KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.instruct.*;
4 import net.sf.saxon.om.Axis;
5 import net.sf.saxon.om.AxisIterator;
6 import net.sf.saxon.om.Navigator;
7 import net.sf.saxon.om.NodeInfo;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.value.SequenceType;
10 import net.sf.saxon.value.Whitespace;
11
12 /**
13 * An xsl:param element in the stylesheet. <br>
14 * The xsl:param element has mandatory attribute name and optional attributes
15  * select, required, as, ...
16 */

17
18 public class XSLParam extends XSLVariableDeclaration {
19
20     Expression conversion = null;
21
22     protected boolean allowsValue() {
23         return !(getParent() instanceof XSLFunction);
24         // function parameters cannot take a default value
25
}
26
27     protected boolean allowsRequired() {
28         return !(getParent() instanceof XSLFunction);
29         // function parameters cannot take the "required" attribute
30
}
31
32     protected boolean allowsTunnelAttribute() {
33         return true;
34     }
35
36     public void validate() throws XPathException {
37
38         NodeInfo parent = getParent();
39         boolean local = (parent instanceof XSLTemplate || parent instanceof XSLFunction);
40         global = (parent instanceof XSLStylesheet);
41
42         if (!local && !global) {
43             compileError("xsl:param must be immediately within a template, function or stylesheet", "XTSE0010");
44         }
45
46         if (!global) {
47             AxisIterator preceding = iterateAxis(Axis.PRECEDING_SIBLING);
48             while (true) {
49                 NodeInfo node = (NodeInfo)preceding.next();
50                 if (node == null) {
51                     break;
52                 }
53                 if (node instanceof XSLParam) {
54                     if (this.getVariableFingerprint() == ((XSLParam)node).getVariableFingerprint()) {
55                         compileError("The name of the parameter is not unique", "XTSE0580");
56                     }
57                 } else if (node instanceof StyleElement) {
58                     compileError("xsl:param must be the first element within a template or function", "XTSE0010");
59                 } else {
60                     // it must be a text node; allow it if all whitespace
61
if (!Whitespace.isWhite(node.getStringValueCS())) {
62                         compileError("xsl:param must not be preceded by text", "XTSE0010");
63                     }
64                 }
65             }
66
67             SlotManager p = getContainingSlotManager();
68             if (p==null) {
69                 compileError("Local variable must be declared within a template or function", "XTSE0010");
70             } else {
71                 setSlotNumber(p.allocateSlotNumber(getVariableFingerprint()));
72             }
73
74         }
75
76         if (requiredParam) {
77             if (select != null) {
78                 // NB, we do this test before setting the default select attribute
79
compileError("The select attribute should be omitted when required='yes'", "XTSE0010");
80             }
81             if (hasChildNodes()) {
82                 compileError("A parameter specifying required='yes' must have empty content", "XTSE0010");
83             }
84         }
85
86         super.validate();
87     }
88
89     /**
90     * Compile: this ensures space is available for local variables declared within
91     * this global variable
92     */

93
94     public Expression compile(Executable exec) throws XPathException {
95
96         if (getParent() instanceof XSLFunction) {
97             // Do nothing. We did everything necessary while compiling the XSLFunction element.
98
return null;
99         } else {
100             int slot = getSlotNumber();
101             if (requiredType != null) {
102                 SuppliedParameterReference pref = new SuppliedParameterReference(slot);
103                 pref.setLocationId(staticContext.getLocationMap().allocateLocationId(getSystemId(), getLineNumber()));
104                 RoleLocator role = new RoleLocator(RoleLocator.VARIABLE, getVariableName(), 0, null);
105                 role.setSourceLocator(new ExpressionLocation(this));
106                 conversion = TypeChecker.staticTypeCheck(
107                         pref,
108                         requiredType,
109                         false,
110                         role, getStaticContext());
111             }
112
113             GeneralVariable inst;
114             if (global) {
115                 inst = new GlobalParam();
116                 ((GlobalParam)inst).setExecutable(getExecutable());
117                 if (isRequiredParam()) {
118                     getExecutable().addRequiredParam(getVariableFingerprint());
119                 }
120                 if (select instanceof ComputedExpression) {
121                     ((ComputedExpression)select).setParentExpression(inst);
122                 }
123             } else {
124                 inst = new LocalParam();
125                 ((LocalParam)inst).setConversion(conversion);
126             }
127             initializeInstruction(exec, inst);
128             inst.setVariableName(getVariableName());
129             inst.setSlotNumber(slot);
130             inst.setRequiredType(getRequiredType());
131             ExpressionTool.makeParentReferences(inst);
132             fixupBinding(inst);
133             return inst;
134         }
135     }
136
137
138     /**
139     * Get the static type of the parameter. This is the declared type, because we cannot know
140     * the actual value in advance.
141     */

142
143     public SequenceType getRequiredType() {
144         if (requiredType!=null) {
145             return requiredType;
146         } else {
147             return SequenceType.ANY_SEQUENCE;
148         }
149     }
150
151 }
152
153 //
154
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
155
// you may not use this file except in compliance with the License. You may obtain a copy of the
156
// License at http://www.mozilla.org/MPL/
157
//
158
// Software distributed under the License is distributed on an "AS IS" basis,
159
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
160
// See the License for the specific language governing rights and limitations under the License.
161
//
162
// The Original Code is: all this file.
163
//
164
// The Initial Developer of the Original Code is Michael H. Kay.
165
//
166
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
167
//
168
// Contributor(s): none.
169
//
170
Popular Tags