Debian Codegraph @

net/sf/saxon/style/XSLLocalParam.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.style;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.SuppliedParameterReference;
12 import net.sf.saxon.expr.instruct.LocalParam;
13 import net.sf.saxon.expr.instruct.SlotManager;
14 import net.sf.saxon.expr.parser.RoleDiagnostic;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.om.NodeInfo;
17 import net.sf.saxon.om.StandardNames;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.value.SequenceType;
21 import net.sf.saxon.value.Whitespace;
22
23 /**
24 * An xsl:param element representing a local parameter (template or function parameter) in the stylesheet. <br>
25 * The xsl:param element has mandatory attribute name and optional attributes
26 * select, required, as, ...
27 */
28
29 public class XSLLocalParam extends XSLGeneralVariable {
30
31 private int permittedAttributes =
32 SourceBinding.TUNNEL |
33 SourceBinding.REQUIRED |
34 SourceBinding.SELECT |
35 SourceBinding.AS;
36
37 /*@Nullable*/ Expression conversion = null;
38 private int slotNumber = -9876; // initial value designed solely to show up when debugging
39 private LocalParam compiledParam;
40 private boolean prepared = false;
41
42 /**
43 * Ask whether this element contains a binding for a variable with a given name; and if it does,
44 * return the source binding information
45 *
46 * @param name the variable name
47 * @return the binding information if this element binds a variable of this name; otherwise null
48 */
49
50 public SourceBinding getBindingInformation(StructuredQName name) {
51 if (name.equals(sourceBinding.getVariableQName())) {
52 return sourceBinding;
53 } else {
54 return null;
55 }
56 }
57
58 /**
59 * Get the slot number allocated to this variable (its position in the stackframe)
60 *
61 * @return the allocated slot number
62 */
63
64 public int getSlotNumber() {
65 return slotNumber;
66 }
67
68 @Override
69 public void prepareAttributes() throws XPathException {
70 if (!prepared) {
71 prepared = true;
72 sourceBinding.setProperty(SourceBinding.PARAM, true);
73 if (getParent() instanceof XSLFunction) {
74 permittedAttributes &= ~SourceBinding.SELECT;
75 sourceBinding.setProperty(SourceBinding.DISALLOWS_CONTENT, true);
76 }
77 sourceBinding.prepareAttributes(permittedAttributes);
78 if (sourceBinding.hasProperty(SourceBinding.TUNNEL) && !(getParent() instanceof XSLTemplate)) {
79 compileError("For attribute 'tunnel' within an " + getParent().getDisplayName() +
80 " parameter, the only permitted value is 'no'", "XTSE0020");
81 }
82 if (getParent() instanceof XSLFunction && getAttributeValue("", "required") != null) {
83 if (!sourceBinding.hasProperty(SourceBinding.REQUIRED)) {
84 compileError("For attribute 'required' within an " + getParent().getDisplayName() +
85 " parameter, the only permitted value is 'yes'", "XTSE0020");
86 }
87 }
88 }
89 }
90
91 public void prepareTemplateSignatureAttributes() throws XPathException {
92 if (!prepared) {
93 sourceBinding.setProperty(SourceBinding.PARAM, true);
94 sourceBinding.prepareTemplateSignatureAttributes();
95 }
96 }
97
98 public void validate(ComponentDeclaration decl) throws XPathException {
99
100 StructuredQName name = sourceBinding.getVariableQName();
101
102 NodeInfo parent = getParent();
103
104 if (!((parent instanceof StyleElement) && ((StyleElement) parent).mayContainParam())) {
105 compileError("xsl:param must be immediately within a template, function or stylesheet", "XTSE0010");
106 }
107
108 if (hasChildNodes() && getParent() instanceof XSLFunction) {
109 compileError("Function parameters cannot have a default value", "XTSE0760");
110 }
111
112 iterateAxis(AxisInfo.PRECEDING_SIBLING).forEachOrFail(node -> {
113 if (node instanceof XSLLocalParam) {
114 if (name.equals(((XSLLocalParam) node).sourceBinding.getVariableQName())) {
115 compileError("The name of the parameter is not unique", "XTSE0580");
116 }
117 } else if (node instanceof StyleElement && node.getFingerprint() != StandardNames.XSL_CONTEXT_ITEM) {
118 compileError("xsl:param must not be preceded by other instructions", "XTSE0010");
119 } else {
120 // it must be a text node; allow it if all whitespace
121 if (!Whitespace.isWhite(node.getStringValueCS())) {
122 compileError("xsl:param must not be preceded by text", "XTSE0010");
123 }
124 }
125 });
126
127 SlotManager p = getContainingSlotManager();
128 if (p == null) {
129 compileError("Local variable must be declared within a template or function", "XTSE0010");
130 } else {
131 slotNumber = p.allocateSlotNumber(name);
132 }
133
134 if (sourceBinding.hasProperty(SourceBinding.REQUIRED)) {
135 if (sourceBinding.getSelectExpression() != null) {
136 // NB, we do this test before setting the default select attribute
137 compileError("The select attribute must be omitted when required='yes'", "XTSE0010");
138 }
139 if (hasChildNodes()) {
140 compileError("A parameter specifying required='yes' must have empty content", "XTSE0010");
141 }
142 }
143
144 super.validate(decl);
145 }
146
147 public boolean isTunnelParam() {
148 return sourceBinding.hasProperty(SourceBinding.TUNNEL);
149 }
150
151 public boolean isRequiredParam() {
152 return sourceBinding.hasProperty(SourceBinding.REQUIRED);
153 }
154
155 @Override
156 public void fixupReferences() throws XPathException {
157 sourceBinding.fixupReferences(null);
158 super.fixupReferences();
159 }
160
161
162 public Expression compile(Compilation exec, ComponentDeclaration decl) throws XPathException {
163 // if (!"iterate".equals(getParent().getLocalPart()) &&
164 // sourceBinding.getReferences().size() == 0 && !sourceBinding.hasProperty(SourceBinding.REQUIRED)) {
165 // return null;
166 // }
167 if (getParent() instanceof XSLFunction) {
168 // Do nothing. We did everything necessary while compiling the XSLFunction element.
169 return null;
170 } else {
171 SequenceType declaredType = getRequiredType();
172 StructuredQName name = sourceBinding.getVariableQName();
173 int slot = getSlotNumber();
174 if (declaredType != null) {
175 SuppliedParameterReference pref = new SuppliedParameterReference(slot);
176 pref.setRetainedStaticContext(makeRetainedStaticContext());
177 pref.setLocation(allocateLocation());
178 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.PARAM, name.getDisplayName(), 0);
179 //role.setSourceLocator(new ExpressionLocation(this));
180 role.setErrorCode("XTTE0590");
181 conversion = exec.getConfiguration().getTypeChecker(false).staticTypeCheck(
182 pref,
183 declaredType,
184 role, makeExpressionVisitor());
185 }
186
187
188 sourceBinding.handleSequenceConstructor(exec, decl);
189
190 LocalParam binding = new LocalParam();
191 binding.setSelectExpression(sourceBinding.getSelectExpression());
192 binding.setConversion(conversion);
193 binding.setVariableQName(name);
194 binding.setSlotNumber(slot);
195 binding.setRequiredType(getRequiredType());
196 binding.setRequiredParam(sourceBinding.hasProperty(SourceBinding.REQUIRED));
197 binding.setImplicitlyRequiredParam(sourceBinding.hasProperty(SourceBinding.IMPLICITLY_REQUIRED));
198 binding.setTunnel(sourceBinding.hasProperty(SourceBinding.TUNNEL));
199 sourceBinding.fixupBinding(binding);
200 return compiledParam = binding;
201
202 }
203 }
204
205 public LocalParam getCompiledParam() {
206 return compiledParam;
207 }
208
209
210 /**
211 * Get the static type of the parameter. This is the declared type, because we cannot know
212 * the actual value in advance.
213 */
214
215 public SequenceType getRequiredType() {
216 SequenceType declaredType = sourceBinding.getDeclaredType();
217 if (declaredType != null) {
218 return declaredType;
219 } else {
220 return SequenceType.ANY_SEQUENCE;
221 }
222 }
223
224
225 }
226