KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Existence


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.ExpressionTool;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.expr.Optimizer;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.value.BooleanValue;
9
10 /** Implement the exists() and empty() functions **/
11
12 public class Existence extends SystemFunction {
13
14     public static final int EXISTS = 0;
15     public static final int EMPTY = 1;
16
17     /**
18      * Static analysis: prevent sorting of the argument
19      */

20
21     public void checkArguments(StaticContext env) throws XPathException {
22         super.checkArguments(env);
23         Optimizer opt = env.getConfiguration().getOptimizer();
24         argument[0] = ExpressionTool.unsorted(opt, argument[0], false);
25     }
26
27     /**
28     * Evaluate the function in a boolean context
29     */

30
31     public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
32         switch (operation) {
33             case EXISTS: return argument[0].iterate(c).next() != null;
34             case EMPTY: return argument[0].iterate(c).next() == null;
35         }
36         return false;
37     }
38
39     /**
40     * Evaluate in a general context
41     */

42
43     public Item evaluateItem(XPathContext c) throws XPathException {
44         return BooleanValue.get(effectiveBooleanValue(c));
45     }
46
47 }
48
49 //
50
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
51
// you may not use this file except in compliance with the License. You may obtain a copy of the
52
// License at http://www.mozilla.org/MPL/
53
//
54
// Software distributed under the License is distributed on an "AS IS" basis,
55
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
56
// See the License for the specific language governing rights and limitations under the License.
57
//
58
// The Original Code is: all this file.
59
//
60
// The Initial Developer of the Original Code is Michael H. Kay.
61
//
62
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
63
//
64
// Contributor(s): none.
65
//
66
Popular Tags