KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > ErrorExpression


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.AnyItemType;
8 import net.sf.saxon.type.ItemType;
9
10 import java.io.PrintStream JavaDoc;
11
12
13 /**
14 * Error expression: this expression is generated when the supplied expression cannot be
15 * parsed, and the containing element enables forwards-compatible processing. It defers
16 * the generation of an error message until an attempt is made to evaluate the expression
17 */

18
19 public class ErrorExpression extends ComputedExpression {
20
21     private XPathException exception; // the error found when parsing this expression
22

23     /**
24     * Constructor
25     * @param exception the error found when parsing this expression
26     */

27
28     public ErrorExpression(XPathException exception) {
29         this.exception = exception;
30         exception.setLocator(this); // to remove any links to the compile-time stylesheet objects
31
};
32
33     /**
34     * Type-check the expression.
35     */

36
37     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
38         return this;
39     }
40
41     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
42         return this;
43     }
44
45     /**
46     * Evaluate the expression. This always throws the exception registered when the expression
47     * was first parsed.
48     */

49
50     public Item evaluateItem(XPathContext context) throws XPathException {
51         // copy the exception for thread-safety, because we want to add context information
52
DynamicError err = new DynamicError(exception.getMessage());
53         err.setLocator(ExpressionTool.getLocator(this));
54         err.setErrorCode(exception.getErrorCodeLocalPart());
55         err.setXPathContext(context);
56         throw err;
57     }
58
59     /**
60     * Iterate over the expression. This always throws the exception registered when the expression
61     * was first parsed.
62     */

63
64     public SequenceIterator iterate(XPathContext context) throws XPathException {
65         evaluateItem(context);
66         return null; // to fool the compiler
67
}
68
69     /**
70     * Determine the data type of the expression, if possible
71     * @return Type.ITEM (meaning not known in advance)
72     */

73
74     public ItemType getItemType() {
75         return AnyItemType.getInstance();
76     }
77
78     /**
79     * Determine the static cardinality
80     */

81
82     public int computeCardinality() {
83         return StaticProperty.ALLOWS_ZERO_OR_MORE;
84         // we return a liberal value, so that we never get a type error reported
85
// statically
86
}
87
88     /**
89     * Diagnostic print of expression structure
90     */

91
92     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
93         out.println(ExpressionTool.indent(level) + "**ERROR** (" + exception.getMessage() + ')');
94     }
95
96 }
97
98 //
99
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
100
// you may not use this file except in compliance with the License. You may obtain a copy of the
101
// License at http://www.mozilla.org/MPL/
102
//
103
// Software distributed under the License is distributed on an "AS IS" basis,
104
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
105
// See the License for the specific language governing rights and limitations under the License.
106
//
107
// The Original Code is: all this file.
108
//
109
// The Initial Developer of the Original Code is Michael H. Kay.
110
//
111
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
112
//
113
// Contributor(s): none.
114
//
115
Popular Tags