KickJava   Java API By Example, From Geeks To Geeks.

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


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.trans.XPathException;
5 import net.sf.saxon.type.ItemType;
6 import net.sf.saxon.value.Cardinality;
7
8 /**
9 * A FirstItemExpression returns the first item in the sequence returned by a given
10 * base expression
11 */

12
13 public final class FirstItemExpression extends UnaryExpression {
14
15     /**
16     * Constructor
17     * @param base A sequence expression denoting sequence whose first item is to be returned
18     */

19
20     public FirstItemExpression(Expression base) {
21         super(base);
22         computeStaticProperties();
23     }
24
25     /**
26      * Perform optimisation of an expression and its subexpressions.
27      * <p/>
28      * <p>This method is called after all references to functions and variables have been resolved
29      * to the declaration of the function or variable, and after all type checking has been done.</p>
30      *
31      * @param opt the optimizer in use. This provides access to supporting functions; it also allows
32      * different optimization strategies to be used in different circumstances.
33      * @param env the static context of the expression
34      * @param contextItemType the static type of "." at the point where this expression is invoked.
35      * The parameter is set to null if it is known statically that the context item will be undefined.
36      * If the type of the context item is not known statically, the argument is set to
37      * {@link net.sf.saxon.type.Type#ITEM_TYPE}
38      * @return the original expression, rewritten if appropriate to optimize execution
39      * @throws net.sf.saxon.trans.StaticError if an error is discovered during this phase
40      * (typically a type error)
41      */

42
43     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
44         operand = operand.optimize(opt, env, contextItemType);
45         if (!Cardinality.allowsMany(operand.getCardinality())) {
46             return operand;
47         }
48         return this;
49     }
50
51     /**
52     * Promote this expression if possible
53     */

54
55     public Expression promote(PromotionOffer offer) throws XPathException {
56         Expression exp = offer.accept(this);
57         if (exp != null) {
58             return exp;
59         } else {
60             if (offer.action != PromotionOffer.UNORDERED) {
61                 // we can't push the UNORDERED property down to the operand, because order is significant
62
operand = doPromotion(operand, offer);
63             }
64             return this;
65         }
66     }
67
68     /**
69     * Get the static cardinality
70     */

71
72     public int computeCardinality() {
73         return operand.getCardinality() & ~StaticProperty.ALLOWS_MANY;
74     }
75
76     /**
77     * Evaluate the expression
78     */

79
80     public Item evaluateItem(XPathContext context) throws XPathException {
81         return operand.iterate(context).next();
82     }
83
84     /**
85     * Diagnostic print of expression structure
86     */

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