KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.NamePool;
6 import net.sf.saxon.om.NodeInfo;
7 import net.sf.saxon.style.StandardNames;
8 import net.sf.saxon.trans.DynamicError;
9 import net.sf.saxon.trans.Mode;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.ItemType;
12
13 import java.io.PrintStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18 * An xsl:apply-imports element in the stylesheet
19 */

20
21 public class ApplyImports extends Instruction {
22
23     WithParam[] actualParams = null;
24     WithParam[] tunnelParams = null;
25     private boolean backwardsCompatible;
26
27     public ApplyImports(boolean backwardsCompatible) {
28         this.backwardsCompatible = backwardsCompatible;
29     }
30
31     /**
32      * Set the actual parameters on the call
33      */

34
35     public void setActualParameters(
36                         WithParam[] actualParams,
37                         WithParam[] tunnelParams ) {
38         this.actualParams = actualParams;
39         this.tunnelParams = tunnelParams;
40     }
41
42     /**
43     * Get the name of this instruction for diagnostic and tracing purposes
44     */

45     public int getInstructionNameCode() {
46         return StandardNames.XSL_APPLY_IMPORTS;
47     }
48
49     /**
50      * Simplify an expression. This performs any static optimization (by rewriting the expression
51      * as a different expression).
52      *
53      * @exception net.sf.saxon.trans.XPathException if an error is discovered during expression
54      * rewriting
55      * @return the simplified expression
56      */

57
58     public Expression simplify(StaticContext env) throws XPathException {
59         WithParam.simplify(actualParams, env);
60         WithParam.simplify(tunnelParams, env);
61         return this;
62     }
63
64     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
65         WithParam.typeCheck(actualParams, env, contextItemType);
66         WithParam.typeCheck(tunnelParams, env, contextItemType);
67         return this;
68     }
69
70     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
71         WithParam.optimize(opt, actualParams, env, contextItemType);
72         WithParam.optimize(opt, tunnelParams, env, contextItemType);
73         return this;
74     }
75
76     /**
77      * Determine whether this instruction creates new nodes.
78      * This implementation returns true (which is almost invariably the case, so it's not worth
79      * doing any further analysis to find out more precisely).
80      */

81
82     public final boolean createsNewNodes() {
83         return true;
84     }
85
86     /**
87      * Handle promotion offers, that is, non-local tree rewrites.
88      * @param offer The type of rewrite being offered
89      * @throws XPathException
90      */

91
92     protected void promoteInst(PromotionOffer offer) throws XPathException {
93         WithParam.promoteParams(actualParams, offer);
94         WithParam.promoteParams(tunnelParams, offer);
95     }
96
97     /**
98      * Get all the XPath expressions associated with this instruction
99      * (in XSLT terms, the expression present on attributes of the instruction,
100      * as distinct from the child instructions in a sequence construction)
101      */

102
103     public Iterator JavaDoc iterateSubExpressions() {
104         ArrayList JavaDoc list = new ArrayList JavaDoc(10);
105         WithParam.getXPathExpressions(actualParams, list);
106         WithParam.getXPathExpressions(tunnelParams, list);
107         return list.iterator();
108     }
109
110     public TailCall processLeavingTail(XPathContext context) throws XPathException {
111
112         Controller controller = context.getController();
113         // handle parameters if any
114

115         ParameterSet params = assembleParams(context, actualParams);
116         ParameterSet tunnels = assembleTunnelParams(context, tunnelParams);
117
118         Template currentTemplate = context.getCurrentTemplate();
119         if (currentTemplate==null) {
120             DynamicError e = new DynamicError("There is no current template rule");
121             e.setXPathContext(context);
122             e.setErrorCode("XTDE0560");
123             e.setLocator(this);
124             throw e;
125         }
126
127         int min = currentTemplate.getMinImportPrecedence();
128         int max = currentTemplate.getPrecedence()-1;
129         Mode mode = context.getCurrentMode();
130         if (mode == null) {
131             mode = controller.getRuleManager().getMode(Mode.DEFAULT_MODE);
132         }
133         if (context.getCurrentIterator()==null) {
134             DynamicError e = new DynamicError("Cannot call xsl:apply-imports when there is no context item");
135             e.setXPathContext(context);
136             e.setErrorCode("XTDE0565");
137             e.setLocator(this);
138             throw e;
139         }
140         Item currentItem = context.getCurrentIterator().current();
141         if (!(currentItem instanceof NodeInfo)) {
142             DynamicError e = new DynamicError("Cannot call xsl:apply-imports when context item is not a node");
143             e.setXPathContext(context);
144             e.setErrorCode("XTDE0565");
145             e.setLocator(this);
146             throw e;
147         }
148         NodeInfo node = (NodeInfo)currentItem;
149         Template nh = controller.getRuleManager().getTemplateRule(node, mode, min, max, context);
150
151         if (nh==null) { // use the default action for the node
152
ApplyTemplates.defaultAction(node, params, tunnels, context, backwardsCompatible, getLocationId());
153         } else {
154             XPathContextMajor c2 = context.newContext();
155             c2.setOrigin(this);
156             c2.setLocalParameters(params);
157             c2.setTunnelParameters(tunnels);
158             c2.openStackFrame(nh.getStackFrameMap());
159             nh.process(c2);
160         }
161         return null;
162                 // We never treat apply-imports as a tail call, though we could
163
}
164
165
166
167     /**
168      * Diagnostic print of expression structure. The expression is written to the System.err
169      * output stream
170      *
171      * @param level indentation level for this expression
172      * @param out
173      */

174
175     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
176         out.println(ExpressionTool.indent(level) + "apply-imports");
177     }
178
179 }
180
181 //
182
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
183
// you may not use this file except in compliance with the License. You may obtain a copy of the
184
// License at http://www.mozilla.org/MPL/
185
//
186
// Software distributed under the License is distributed on an "AS IS" basis,
187
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
188
// See the License for the specific language governing rights and limitations under the License.
189
//
190
// The Original Code is: all this file.
191
//
192
// The Initial Developer of the Original Code is Michael H. Kay.
193
//
194
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
195
//
196
// Contributor(s): none.
197
//
198
Popular Tags