KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.trans.DynamicError;
7 import net.sf.saxon.trans.StaticError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.ItemType;
10
11 import java.io.PrintStream JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 /**
16 * This instruction throws an error which was detected at compile time but is not to be
17 * raised unless the instruction is actually executed.
18 */

19
20 public class DeferredError extends Instruction {
21
22     private StaticError error;
23     private int nameCode;
24
25     public DeferredError(int nameCode, StaticError error) {
26         if (error==null) {
27             throw new NullPointerException JavaDoc("Deferred Error: no error value supplied");
28         }
29         this.error = error;
30         this.nameCode = nameCode;
31     }
32
33     /**
34     * Get the namecode of the instruction for use in diagnostics
35     */

36
37     public int getInstructionNameCode() {
38         return nameCode;
39     };
40
41     /**
42      * Simplify an expression. This performs any static optimization (by rewriting the expression
43      * as a different expression).
44      *
45      * @exception XPathException if an error is discovered during expression
46      * rewriting
47      * @return the simplified expression
48      */

49
50     public Expression simplify(StaticContext env) throws XPathException {
51         return this;
52     }
53
54     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
55         return this;
56     }
57
58     /**
59      * Perform optimisation of an expression and its subexpressions.
60      * <p/>
61      * <p>This method is called after all references to functions and variables have been resolved
62      * to the declaration of the function or variable, and after all type checking has been done.</p>
63      *
64      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
65      * different optimization strategies to be used in different circumstances.
66      * @param env the static context of the expression
67      * @param contextItemType the static type of "." at the point where this expression is invoked.
68      * The parameter is set to null if it is known statically that the context item will be undefined.
69      * If the type of the context item is not known statically, the argument is set to
70      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
71      * @return the original expression, rewritten if appropriate to optimize execution
72      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
73      * (typically a type error)
74      */

75
76     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
77         return this;
78     }
79
80     /**
81      * Handle promotion offers, that is, non-local tree rewrites.
82      * @param offer The type of rewrite being offered
83      * @throws XPathException
84      */

85
86     protected void promoteInst(PromotionOffer offer) throws XPathException {
87     }
88
89     /**
90      * Get all the XPath expressions associated with this instruction
91      * (in XSLT terms, the expression present on attributes of the instruction,
92      * as distinct from the child instructions in a sequence construction)
93      */

94
95     public Iterator iterateSubExpressions() {
96         return Collections.EMPTY_LIST.iterator();
97     }
98
99     public TailCall processLeavingTail(XPathContext context) throws XPathException {
100         throw DynamicError.makeDynamicError(error);
101     }
102
103     /**
104      * Evaluate an expression as a single item. This always returns either a single Item or
105      * null (denoting the empty sequence). No conversion is done. This method should not be
106      * used unless the static type of the expression is a subtype of "item" or "item?": that is,
107      * it should not be called if the expression may return a sequence. There is no guarantee that
108      * this condition will be detected.
109      *
110      * @param context The context in which the expression is to be evaluated
111      * @exception XPathException if any dynamic error occurs evaluating the
112      * expression
113      * @return the node or atomic value that results from evaluating the
114      * expression; or null to indicate that the result is an empty
115      * sequence
116      */

117
118     public Item evaluateItem(XPathContext context) throws XPathException {
119         throw DynamicError.makeDynamicError(error);
120     }
121
122     /**
123      * Return an Iterator to iterate over the values of a sequence. The value of every
124      * expression can be regarded as a sequence, so this method is supported for all
125      * expressions. This default implementation relies on the process() method: it
126      * "pushes" the results of the instruction to a sequence in memory, and then
127      * iterates over this in-memory sequence.
128      *
129      * In principle instructions should implement a pipelined iterate() method that
130      * avoids the overhead of intermediate storage.
131      *
132      * @exception XPathException if any dynamic error occurs evaluating the
133      * expression
134      * @param context supplies the context for evaluation
135      * @return a SequenceIterator that can be used to iterate over the result
136      * of the expression
137      */

138
139     public SequenceIterator iterate(XPathContext context) throws XPathException {
140         evaluateItem(context);
141         return null;
142     }
143
144     /**
145      * Diagnostic print of expression structure. The expression is written to the System.err
146      * output stream
147      *
148      * @param level indentation level for this expression
149      * @param out
150      */

151
152     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
153         out.println(ExpressionTool.indent(level) + "error: " + error.getMessage());
154     }
155 }
156
157 //
158
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
159
// you may not use this file except in compliance with the License. You may obtain a copy of the
160
// License at http://www.mozilla.org/MPL/
161
//
162
// Software distributed under the License is distributed on an "AS IS" basis,
163
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
164
// See the License for the specific language governing rights and limitations under the License.
165
//
166
// The Original Code is: all this file.
167
//
168
// The Initial Developer of the Original Code is Michael H. Kay.
169
//
170
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
171
//
172
// Contributor(s):
173
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
174
//
175
Popular Tags