KickJava   Java API By Example, From Geeks To Geeks.

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


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.om.SingletonIterator;
6 import net.sf.saxon.trans.StaticError;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.ItemType;
9 import net.sf.saxon.type.Type;
10
11 import java.io.PrintStream JavaDoc;
12
13
14 /**
15 * This class represents the expression "(dot)", which always returns the context item.
16 * This may be a AtomicValue or a Node.
17 */

18
19 public final class ContextItemExpression extends ComputedExpression {
20
21     ItemType itemType = Type.ITEM_TYPE;
22
23     public ContextItemExpression() {}
24
25     /**
26     * Type-check the expression.
27     */

28
29     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
30         if (contextItemType == null) {
31             StaticError err = new StaticError("The context item is undefined at this point");
32             err.setErrorCode("XPDY0002");
33             err.setIsTypeError(true);
34             err.setLocator(this);
35             throw err;
36         }
37         itemType = contextItemType;
38         return this;
39     }
40
41     /**
42      * Perform optimisation of an expression and its subexpressions.
43      * <p/>
44      * <p>This method is called after all references to functions and variables have been resolved
45      * to the declaration of the function or variable, and after all type checking has been done.</p>
46      *
47      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
48      * different optimization strategies to be used in different circumstances.
49      * @param env the static context of the expression
50      * @param contextItemType the static type of "." at the point where this expression is invoked.
51      * The parameter is set to null if it is known statically that the context item will be undefined.
52      * If the type of the context item is not known statically, the argument is set to
53      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
54      * @return the original expression, rewritten if appropriate to optimize execution
55      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
56      * (typically a type error)
57      */

58
59     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
60         // In XSLT, we don't catch this error at the typeCheck() phase because it's done one XPath expression
61
// at a time. So we repeat the check here.
62
return typeCheck(env, contextItemType);
63     }
64
65     /**
66     * Determine the item type
67     */

68
69     public ItemType getItemType() {
70         return itemType;
71     }
72
73     /**
74     * Get the static cardinality
75     */

76
77     public int computeCardinality() {
78         return StaticProperty.EXACTLY_ONE;
79     }
80
81     /**
82      * Determine the special properties of this expression
83      * @return {@link StaticProperty#NON_CREATIVE}
84      */

85
86     public int computeSpecialProperties() {
87         int p = super.computeSpecialProperties();
88         return p | StaticProperty.NON_CREATIVE;
89     }
90
91     /**
92     * Is this expression the same as another expression?
93     */

94
95     public boolean equals(Object JavaDoc other) {
96         return (other instanceof ContextItemExpression);
97     }
98
99     /**
100     * get HashCode for comparing two expressions
101     */

102
103     public int hashCode() {
104         return "ContextItemExpression".hashCode();
105     }
106
107     public int getIntrinsicDependencies() {
108         return StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
109     }
110
111     /**
112     * Iterate over the value of the expression
113     */

114
115     public SequenceIterator iterate(XPathContext context) throws XPathException {
116         Item item = context.getContextItem();
117         if (item==null) {
118             dynamicError("The context item is not set", context);
119         }
120         return SingletonIterator.makeIterator(item);
121     }
122
123     /**
124     * Evaluate the expression
125     */

126
127     public Item evaluateItem(XPathContext context) throws XPathException {
128         Item item = context.getContextItem();
129         if (item==null) {
130             dynamicError("The context item is not set", context);
131         }
132         return item;
133     }
134
135     /**
136     * Diagnostic print of expression structure
137     */

138
139     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
140         out.println(ExpressionTool.indent(level) + '.');
141     }
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