KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.style.StandardNames;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.ItemType;
7
8 import java.io.PrintStream JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11
12 /**
13 * Handler for saxon:while elements in stylesheet. <br>
14 * The saxon:while element has a mandatory attribute test, a boolean expression.
15 * The content is output repeatedly so long as the test condition is true.
16 */

17
18 public class While extends Instruction {
19
20     private Expression test;
21     private Expression action;
22
23     public While(Expression test, Expression action) {
24         this.test = test;
25         this.action = action;
26         adoptChildExpression(test);
27         adoptChildExpression(action);
28     }
29
30     /**
31     * Get the name of this instruction for diagnostic and tracing purposes
32     * @return the string "saxon:while"
33     */

34
35     public int getInstructionNameCode() {
36         return StandardNames.SAXON_WHILE;
37     }
38
39     /**
40      * Get the action expression (the content of the for-each)
41      */

42
43     public Expression getActionExpression() {
44         return action;
45     }
46
47     /**
48      * Simplify an expression. This performs any static optimization (by rewriting the expression
49      * as a different expression).
50      *
51      * @exception XPathException if an error is discovered during expression
52      * rewriting
53      * @return the simplified expression
54      */

55
56     public Expression simplify(StaticContext env) throws XPathException {
57         test = test.simplify(env);
58         action = action.simplify(env);
59         return this;
60     }
61
62     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
63         test = test.typeCheck(env, contextItemType);
64         adoptChildExpression(test);
65         action = action.typeCheck(env, contextItemType);
66         adoptChildExpression(action);
67         return this;
68     }
69
70     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
71             test = test.optimize(opt, env, contextItemType);
72             adoptChildExpression(test);
73             action = action.optimize(opt, env, contextItemType);
74             adoptChildExpression(action);
75             return this;
76         }
77
78
79     /**
80      * Get the item type of the items returned by evaluating this instruction
81      *
82      * @return the static item type of the instruction
83      */

84
85     public ItemType getItemType() {
86         return action.getItemType();
87     }
88
89     /**
90      * Handle promotion offers, that is, non-local tree rewrites.
91      * @param offer The type of rewrite being offered
92      * @throws XPathException
93      */

94
95     protected void promoteInst(PromotionOffer offer) throws XPathException {
96         test = doPromotion(test, offer);
97         action = doPromotion(action, offer);
98     }
99
100     /**
101      * Determine whether this instruction creates new nodes.
102      * This implementation returns true if the "action" creates new nodes.
103      * (Nodes created by the condition can't contribute to the result).
104      */

105
106     public final boolean createsNewNodes() {
107         int props = action.getSpecialProperties();
108         return ((props & StaticProperty.NON_CREATIVE) == 0);
109     }
110
111     /**
112      * Get all the XPath expressions associated with this instruction
113      * (in XSLT terms, the expression present on attributes of the instruction,
114      * as distinct from the child instructions in a sequence construction)
115      */

116
117     public Iterator JavaDoc iterateSubExpressions() {
118         return new PairIterator(test, action);
119     }
120
121     public TailCall processLeavingTail(XPathContext context) throws XPathException {
122         while (test.effectiveBooleanValue(context)) {
123             action.process(context);
124         }
125         return null;
126     }
127
128
129     /**
130      * Diagnostic print of expression structure. The expression is written to the System.err
131      * output stream
132      *
133      * @param level indentation level for this expression
134      * @param out
135      */

136
137     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
138         out.println(ExpressionTool.indent(level) + "while");
139         test.display(level+1, pool, out);
140         out.println(ExpressionTool.indent(level) + "do");
141         action.display(level+1, pool, out);
142     }
143 }
144
145 //
146
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
147
// you may not use this file except in compliance with the License. You may obtain a copy of the
148
// License at http://www.mozilla.org/MPL/
149
//
150
// Software distributed under the License is distributed on an "AS IS" basis,
151
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
152
// See the License for the specific language governing rights and limitations under the License.
153
//
154
// The Original Code is: all this file.
155
//
156
// The Initial Developer of the Original Code is Michael H. Kay.
157
//
158
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
159
//
160
// Contributor(s): none.
161
//
162
Popular Tags