KickJava   Java API By Example, From Geeks To Geeks.

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


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

20
21 public final class LocalParam extends GeneralVariable {
22
23     private Expression conversion = null;
24
25     /**
26      * Define a conversion that is to be applied to the supplied parameter value.
27      * @param convertor
28      */

29     public void setConversion(Expression convertor) {
30         conversion = convertor;
31     }
32
33     /**
34      * Get the name of this instruction for diagnostic and tracing purposes
35      */

36
37     public int getInstructionNameCode() {
38         return StandardNames.XSL_PARAM;
39     }
40
41     /**
42      * Get all the XPath expressions associated with this instruction
43      * (in XSLT terms, the expression present on attributes of the instruction,
44      * as distinct from the child instructions in a sequence construction)
45      */

46
47     public Iterator JavaDoc iterateSubExpressions() {
48         if (select != null && conversion != null) {
49             return new PairIterator(select, conversion);
50         } else if (select != null) {
51             return new MonoIterator(select);
52         } else if (conversion != null) {
53             return new MonoIterator(conversion);
54         } else {
55             return Collections.EMPTY_LIST.iterator();
56         }
57     }
58
59     /**
60     * Process the local parameter declaration
61     */

62
63     public TailCall processLeavingTail(XPathContext context) throws XPathException {
64         boolean wasSupplied = context.useLocalParameter(getVariableFingerprint(), this, isTunnelParam());
65         if (wasSupplied) {
66             // if a parameter was supplied by the caller, we may need to convert it to the type required
67
if (conversion != null) {
68                 context.setLocalVariable(getSlotNumber(),
69                         ExpressionTool.eagerEvaluate(conversion, context));
70                 // We do an eager evaluation here for safety, because the result of the
71
// type conversion overwrites the slot where the actual supplied parameter
72
// is contained.
73
}
74
75             // don't evaluate the default if a value has been supplied or if it has already been
76
// evaluated by virtue of a forwards reference
77

78         } else {
79             if (isRequiredParam()) {
80                 DynamicError e = new DynamicError("No value supplied for required parameter");
81                 e.setXPathContext(context);
82                 e.setErrorCode("XTDE0700");
83                 throw e;
84             }
85             context.setLocalVariable(getSlotNumber(), getSelectValue(context));
86         }
87         return null;
88     }
89
90     /**
91      * Evaluate the variable
92      */

93
94     public ValueRepresentation evaluateVariable(XPathContext c) {
95         return c.evaluateLocalVariable(getSlotNumber());
96     }
97 }
98
99 //
100
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
101
// you may not use this file except in compliance with the License. You may obtain a copy of the
102
// License at http://www.mozilla.org/MPL/
103
//
104
// Software distributed under the License is distributed on an "AS IS" basis,
105
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
106
// See the License for the specific language governing rights and limitations under the License.
107
//
108
// The Original Code is: all this file.
109
//
110
// The Initial Developer of the Original Code is Michael H. Kay.
111
//
112
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
113
//
114
// Contributor(s): none.
115
//
116
Popular Tags