01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.ExpressionTool;
04: import net.sf.saxon.expr.StaticContext;
05: import net.sf.saxon.expr.XPathContext;
06: import net.sf.saxon.expr.Optimizer;
07: import net.sf.saxon.om.Item;
08: import net.sf.saxon.trans.XPathException;
09: import net.sf.saxon.value.BooleanValue;
10:
11: /** Implement the exists() and empty() functions **/
12:
13: public class Existence extends SystemFunction {
14:
15: public static final int EXISTS = 0;
16: public static final int EMPTY = 1;
17:
18: /**
19: * Static analysis: prevent sorting of the argument
20: */
21:
22: public void checkArguments(StaticContext env) throws XPathException {
23: super .checkArguments(env);
24: Optimizer opt = env.getConfiguration().getOptimizer();
25: argument[0] = ExpressionTool.unsorted(opt, argument[0], false);
26: }
27:
28: /**
29: * Evaluate the function in a boolean context
30: */
31:
32: public boolean effectiveBooleanValue(XPathContext c)
33: throws XPathException {
34: switch (operation) {
35: case EXISTS:
36: return argument[0].iterate(c).next() != null;
37: case EMPTY:
38: return argument[0].iterate(c).next() == null;
39: }
40: return false;
41: }
42:
43: /**
44: * Evaluate in a general context
45: */
46:
47: public Item evaluateItem(XPathContext c) throws XPathException {
48: return BooleanValue.get(effectiveBooleanValue(c));
49: }
50:
51: }
52:
53: //
54: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
55: // you may not use this file except in compliance with the License. You may obtain a copy of the
56: // License at http://www.mozilla.org/MPL/
57: //
58: // Software distributed under the License is distributed on an "AS IS" basis,
59: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
60: // See the License for the specific language governing rights and limitations under the License.
61: //
62: // The Original Code is: all this file.
63: //
64: // The Initial Developer of the Original Code is Michael H. Kay.
65: //
66: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
67: //
68: // Contributor(s): none.
69: //
|