KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > GlobalParam


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.expr.ErrorExpression;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.ValueRepresentation;
7 import net.sf.saxon.style.StandardNames;
8 import net.sf.saxon.trans.DynamicError;
9 import net.sf.saxon.trans.XPathException;
10
11 /**
12 * The compiled form of a global xsl:param element in the stylesheet or an
13 * external variable declared in the prolog of a Query. <br>
14 * The xsl:param element in XSLT has mandatory attribute name and optional attribute select. It can also
15 * be specified as required="yes" or required="no". In standard XQuery external variables are always required,
16 * and no default value can be specified; but Saxon provides an extension pragma that allows a query
17 * to specify a default.
18 */

19
20 public final class GlobalParam extends GlobalVariable {
21
22     /**
23     * Get the name of this instruction for diagnostic and tracing purposes
24     */

25
26     public int getInstructionNameCode() {
27         return StandardNames.XSL_PARAM;
28     }
29
30     /**
31     * Evaluate the variable
32     */

33
34     public ValueRepresentation evaluateVariable(XPathContext context) throws XPathException {
35         Controller controller = context.getController();
36         Bindery b = controller.getBindery();
37         boolean wasSupplied = b.useGlobalParameter(getVariableFingerprint(), this, context);
38
39         ValueRepresentation val = b.getGlobalVariableValue(this);
40         if (wasSupplied || val!=null) {
41             return val;
42         } else {
43             if (isRequiredParam()) {
44                 DynamicError e = new DynamicError("No value supplied for required parameter $" +
45                         context.getController().getNamePool().getDisplayName(getVariableFingerprint()));
46                 e.setXPathContext(context);
47                 e.setLocator(getSourceLocator());
48                 e.setErrorCode("XTDE0050");
49                 throw e;
50             }
51
52             // This is the first reference to a global variable; try to evaluate it now.
53
// But first set a flag to stop looping. This flag is set in the Bindery because
54
// the VariableReference itself can be used by multiple threads simultaneously
55

56             try {
57                 b.setExecuting(this, true);
58                 ValueRepresentation value = getSelectValue(context);
59                 b.defineGlobalVariable(this, value);
60                 b.setExecuting(this, false);
61                 return value;
62
63             } catch (XPathException err) {
64                 b.setExecuting(this, false);
65                 if (err instanceof XPathException.Circularity) {
66                     DynamicError e = new DynamicError("Circular definition of parameter " + getVariableName());
67                     e.setXPathContext(context);
68                     int lang = context.getController().getExecutable().getHostLanguage();
69                     e.setErrorCode(lang == Configuration.XQUERY ? "XQST0054" : "XTDE0640");
70                     // Detect it more quickly the next time (in a pattern, the error is recoverable)
71
select = new ErrorExpression(e);
72                     throw e;
73                 } else {
74                     throw err;
75                 }
76             }
77         }
78     }
79 }
80
81 //
82
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
83
// you may not use this file except in compliance with the License. You may obtain a copy of the
84
// License at http://www.mozilla.org/MPL/
85
//
86
// Software distributed under the License is distributed on an "AS IS" basis,
87
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
88
// See the License for the specific language governing rights and limitations under the License.
89
//
90
// The Original Code is: all this file.
91
//
92
// The Initial Developer of the Original Code is Michael H. Kay.
93
//
94
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
95
//
96
// Contributor(s): none.
97
//
98
Popular Tags