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.XPathException;
007: import net.sf.saxon.value.Closure;
008: import net.sf.saxon.value.SequenceExtent;
009: import net.sf.saxon.value.SequenceType;
010: import net.sf.saxon.value.Value;
011:
012: /**
013: * saxon:assign element in stylesheet.
014: *
015: * The saxon:assign element has mandatory attribute name and optional attribute expr.
016: * It also allows xsl:extension-element-prefixes etc.
017: */
018:
019: public class Assign extends GeneralVariable implements BindingReference {
020:
021: private Binding binding; // link to the variable declaration
022:
023: public Assign() {
024: }
025:
026: public void setStaticType(SequenceType type, Value constantValue,
027: int properties) {
028: }
029:
030: public void fixup(Binding binding) {
031: this .binding = binding;
032: }
033:
034: /**
035: * Determine whether this instruction creates new nodes.
036: * This implementation returns true: although the instruction doesn't create new
037: * nodes, pretending that it does causes the optimizer to avoid pulling the instruction out of a loop.
038: */
039:
040: public final boolean createsNewNodes() {
041: return true;
042: }
043:
044: /**
045: * Offer promotion for this subexpression. This needs careful handling in the
046: * case of saxon:assign
047: *
048: * @param offer details of the offer, for example the offer to move
049: * expressions that don't depend on the context to an outer level in
050: * the containing expression
051: * @exception net.sf.saxon.trans.XPathException if any error is detected
052: * @return if the offer is not accepted, return this expression unchanged.
053: * Otherwise return the result of rewriting the expression to promote
054: * this subexpression
055: */
056:
057: public Expression promote(PromotionOffer offer)
058: throws XPathException {
059: switch (offer.action) {
060: case PromotionOffer.RANGE_INDEPENDENT:
061: case PromotionOffer.FOCUS_INDEPENDENT:
062: return this ;
063:
064: case PromotionOffer.REPLACE_CURRENT:
065: case PromotionOffer.INLINE_VARIABLE_REFERENCES:
066: case PromotionOffer.UNORDERED:
067: return super .promote(offer);
068:
069: default:
070: throw new UnsupportedOperationException(
071: "Unknown promotion action " + offer.action);
072: }
073: }
074:
075: /**
076: * Get the name of this instruction for diagnostic and tracing purposes
077: */
078:
079: public int getInstructionNameCode() {
080: return StandardNames.SAXON_ASSIGN;
081: }
082:
083: public TailCall processLeavingTail(XPathContext context)
084: throws XPathException {
085: if (binding == null) {
086: throw new IllegalStateException(
087: "saxon:assign binding has not been fixed up");
088: }
089: ValueRepresentation value = getSelectValue(context);
090: if (value instanceof Closure) {
091: value = SequenceExtent.makeSequenceExtent(((Closure) value)
092: .iterate(null));
093: }
094: if (binding instanceof GeneralVariable) {
095: if (binding.isGlobal()) {
096: context.getController().getBindery()
097: .assignGlobalVariable((GlobalVariable) binding,
098: value);
099: } else {
100: throw new UnsupportedOperationException(
101: "Local variables are not assignable");
102: }
103: } else {
104:
105: }
106: return null;
107: }
108:
109: /**
110: * Evaluate the variable (method exists only to satisfy the interface)
111: */
112:
113: public ValueRepresentation evaluateVariable(XPathContext context)
114: throws XPathException {
115: throw new UnsupportedOperationException();
116: }
117: }
118:
119: //
120: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
121: // you may not use this file except in compliance with the License. You may obtain a copy of the
122: // License at http://www.mozilla.org/MPL/
123: //
124: // Software distributed under the License is distributed on an "AS IS" basis,
125: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
126: // See the License for the specific language governing rights and limitations under the License.
127: //
128: // The Original Code is: all this file.
129: //
130: // The Initial Developer of the Original Code is Michael H. Kay.
131: //
132: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
133: //
134: // Contributor(s): none.
135: //
|