KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.om.SingletonIterator;
6 import net.sf.saxon.pattern.AnyNodeTest;
7 import net.sf.saxon.trans.StaticError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.type.AtomicType;
10 import net.sf.saxon.type.ItemType;
11
12
13
14 /**
15 * A node set expression that will always return zero or one nodes
16 */

17
18 public abstract class SingleNodeExpression extends ComputedExpression {
19
20     /**
21     * Type-check the expression.
22     */

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

59
60     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
61         // repeat the check: in XSLT insufficient information is available the first time
62
return typeCheck(env, contextItemType);
63     }
64
65
66     /**
67     * Specify that the expression returns a singleton
68     */

69
70     public int computeCardinality() {
71         return StaticProperty.ALLOWS_ZERO_OR_ONE;
72     }
73
74     /**
75     * Determine the data type of the items returned by this expression
76     * @return Type.NODE
77     */

78
79     public ItemType getItemType() {
80         return AnyNodeTest.getInstance();
81     }
82
83     /**
84     * Determine which aspects of the context the expression depends on. The result is
85     * a bitwise-or'ed value composed from constants such as StaticProperty.VARIABLES and
86     * StaticProperty.CURRENT_NODE
87     */

88
89     public int getIntrinsicDependencies() {
90         return StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
91     }
92
93     public int computeSpecialProperties() {
94         return StaticProperty.ORDERED_NODESET |
95                 StaticProperty.CONTEXT_DOCUMENT_NODESET |
96                 StaticProperty.SINGLE_DOCUMENT_NODESET |
97                 StaticProperty.NON_CREATIVE;
98     }
99
100     /**
101     * Get the single node to which this expression refers. Returns null if the node-set is empty
102     */

103
104     public abstract NodeInfo getNode(XPathContext context) throws XPathException;
105
106     /**
107     * Evaluate the expression in a given context to return an iterator
108     * @param context the evaluation context
109     */

110
111     public SequenceIterator iterate(XPathContext context) throws XPathException {
112         return SingletonIterator.makeIterator(getNode(context));
113     }
114
115     public Item evaluateItem(XPathContext context) throws XPathException {
116         return getNode(context);
117     }
118
119     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
120         return getNode(context) != null;
121     }
122
123 }
124
125
126
127 //
128
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
129
// you may not use this file except in compliance with the License. You may obtain a copy of the
130
// License at http://www.mozilla.org/MPL/
131
//
132
// Software distributed under the License is distributed on an "AS IS" basis,
133
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
134
// See the License for the specific language governing rights and limitations under the License.
135
//
136
// The Original Code is: all this file.
137
//
138
// The Initial Developer of the Original Code is Michael H. Kay.
139
//
140
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
141
//
142
// Contributor(s): none.
143
//
144
Popular Tags