Debian Codegraph @

net/sf/saxon/expr/instruct/GlobalParam.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.expr.instruct;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.Controller;
12 import net.sf.saxon.expr.Component;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.om.GroundedValue;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.trans.XPathException;
17
18 /**
19 * The compiled form of a global xsl:param element in an XSLT stylesheet or an
20 * external variable declared in the prolog of a Query. <br>
21 * The xsl:param element in XSLT has mandatory attribute name and optional attribute select. It can also
22 * be specified as required="yes" or required="no". In standard XQuery 1.0 external variables are always required,
23 * and no default value can be specified; but Saxon provides an extension pragma that allows a query
24 * to specify a default. XQuery 1.1 adds standard syntax for defining a default value.
25 */
26
27 public final class GlobalParam extends GlobalVariable {
28
29 private boolean implicitlyRequired;
30
31 public GlobalParam() {
32 }
33
34 /**
35 * Indicate that this parameter is implicitly required, because the default value does not match the type
36 *
37 * @param requiredParam true if this is a required parameter
38 */
39
40 public void setImplicitlyRequiredParam(boolean requiredParam) {
41 this.implicitlyRequired = requiredParam;
42 }
43
44 /**
45 * Ask whether this variable represents a required parameter
46 * @return true if this variable represents a required parameter
47 */
48
49 public boolean isImplicitlyRequiredParam() {
50 return this.implicitlyRequired;
51 }
52
53 /**
54 * Evaluate the variable
55 */
56 @Override
57 public GroundedValue<?> evaluateVariable(XPathContext context, Component target) throws XPathException {
58 Controller controller = context.getController();
59 assert controller != null;
60 Bindery b = controller.getBindery(getPackageData());
61 GroundedValue<?> val = b.getGlobalVariableValue(this);
62 if (val != null) {
63 if (val instanceof Bindery.FailureValue) {
64 throw ((Bindery.FailureValue)val).getObject();
65 }
66 return val;
67 }
68 val = controller.getConvertedParameter(getVariableQName(), getRequiredType(), context);
69 if (val != null) {
70 return b.saveGlobalVariableValue(this, val);
71 }
72 if (isRequiredParam()) {
73 XPathException e = new XPathException("No value supplied for required parameter $" +
74 getVariableQName().getDisplayName());
75 e.setXPathContext(context);
76 e.setLocator(this);
77 e.setErrorCode(getPackageData().getHostLanguage() == Configuration.XSLT ? "XTDE0050" : "XPDY0002");
78 throw e;
79 } else if (isImplicitlyRequiredParam()) {
80 XPathException e = new XPathException("A value must be supplied for parameter $" +
81 getVariableQName().getDisplayName() +
82 " because there is no default value for the required type");
83 e.setXPathContext(context);
84 e.setLocator(this);
85 e.setErrorCode("XTDE0700");
86 throw e;
87 }
88 // evaluate and save the default value
89 return actuallyEvaluate(context, target);
90 }
91
92 /**
93 * Evaluate the variable
94 */
95
96 @Override
97 public GroundedValue<?> evaluateVariable(XPathContext context) throws XPathException {
98 return evaluateVariable(context, null);
99 }
100
101 @Override
102 protected String getFlags() {
103 String f = super.getFlags();
104 if (isImplicitlyRequiredParam()) {
105 f += "i";
106 }
107 return f;
108 }
109 }
110