001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.expr.*;
004: import net.sf.saxon.om.ValueRepresentation;
005: import net.sf.saxon.style.StandardNames;
006: import net.sf.saxon.trans.DynamicError;
007: import net.sf.saxon.trans.XPathException;
008:
009: import java.util.Collections;
010: import java.util.Iterator;
011:
012: /**
013: * The compiled form of an xsl:param element in the stylesheet or an
014: * external variable in a Query. <br>
015: * The xsl:param element in XSLT has mandatory attribute name and optional attribute select. It can also
016: * be specified as required="yes" or required="no". In standard XQuery external variables are always required,
017: * and no default value can be specified; but Saxon provides an extension pragma that allows a query
018: * to specify a default.
019: */
020:
021: public final class LocalParam extends GeneralVariable {
022:
023: private Expression conversion = null;
024:
025: /**
026: * Define a conversion that is to be applied to the supplied parameter value.
027: * @param convertor
028: */
029: public void setConversion(Expression convertor) {
030: conversion = convertor;
031: }
032:
033: /**
034: * Get the name of this instruction for diagnostic and tracing purposes
035: */
036:
037: public int getInstructionNameCode() {
038: return StandardNames.XSL_PARAM;
039: }
040:
041: /**
042: * Get all the XPath expressions associated with this instruction
043: * (in XSLT terms, the expression present on attributes of the instruction,
044: * as distinct from the child instructions in a sequence construction)
045: */
046:
047: public Iterator iterateSubExpressions() {
048: if (select != null && conversion != null) {
049: return new PairIterator(select, conversion);
050: } else if (select != null) {
051: return new MonoIterator(select);
052: } else if (conversion != null) {
053: return new MonoIterator(conversion);
054: } else {
055: return Collections.EMPTY_LIST.iterator();
056: }
057: }
058:
059: /**
060: * Process the local parameter declaration
061: */
062:
063: public TailCall processLeavingTail(XPathContext context)
064: throws XPathException {
065: boolean wasSupplied = context.useLocalParameter(
066: getVariableFingerprint(), this , isTunnelParam());
067: if (wasSupplied) {
068: // if a parameter was supplied by the caller, we may need to convert it to the type required
069: if (conversion != null) {
070: context.setLocalVariable(getSlotNumber(),
071: ExpressionTool.eagerEvaluate(conversion,
072: context));
073: // We do an eager evaluation here for safety, because the result of the
074: // type conversion overwrites the slot where the actual supplied parameter
075: // is contained.
076: }
077:
078: // don't evaluate the default if a value has been supplied or if it has already been
079: // evaluated by virtue of a forwards reference
080:
081: } else {
082: if (isRequiredParam()) {
083: DynamicError e = new DynamicError(
084: "No value supplied for required parameter");
085: e.setXPathContext(context);
086: e.setErrorCode("XTDE0700");
087: throw e;
088: }
089: context.setLocalVariable(getSlotNumber(),
090: getSelectValue(context));
091: }
092: return null;
093: }
094:
095: /**
096: * Evaluate the variable
097: */
098:
099: public ValueRepresentation evaluateVariable(XPathContext c) {
100: return c.evaluateLocalVariable(getSlotNumber());
101: }
102: }
103:
104: //
105: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
106: // you may not use this file except in compliance with the License. You may obtain a copy of the
107: // License at http://www.mozilla.org/MPL/
108: //
109: // Software distributed under the License is distributed on an "AS IS" basis,
110: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
111: // See the License for the specific language governing rights and limitations under the License.
112: //
113: // The Original Code is: all this file.
114: //
115: // The Initial Developer of the Original Code is Michael H. Kay.
116: //
117: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118: //
119: // Contributor(s): none.
120: //
|