KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > Count


1 package com.icl.saxon.functions;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.NodeEnumeration;
4 import com.icl.saxon.expr.*;
5
6 public class Count extends Function {
7     
8     /**
9     * Function name (for diagnostics)
10     */

11
12     public String getName() {
13         return "count";
14     };
15
16     /**
17     * Determine the data type of the expression
18     * @return Value.NUMBER
19     */

20
21     public int getDataType() {
22         return Value.NUMBER;
23     }
24
25     /**
26     * Simplify and validate.
27     */

28
29     public Expression simplify() throws XPathException {
30         checkArgumentCount(1, 1);
31         argument[0] = argument[0].simplify();
32
33         if (argument[0] instanceof Value) {
34             return evaluate(null);
35         }
36         return this;
37     }
38
39     /**
40     * Evaluate the function in a numeric context
41     */

42
43     public double evaluateAsNumber(Context c) throws XPathException {
44         int n = 0;
45         NodeEnumeration enum = argument[0].enumerate(c, true);
46         while (enum.hasMoreElements()) {
47             enum.nextElement();
48             n++;
49         }
50         return (double)n;
51     }
52
53     /**
54     * Evaluate in a general context
55     */

56
57     public Value evaluate(Context c) throws XPathException {
58         return new NumericValue(evaluateAsNumber(c));
59     }
60
61     /**
62     * Determine the dependencies
63     */

64
65     public int getDependencies() {
66         return argument[0].getDependencies();
67     }
68
69     /**
70     * Reduce the dependencies
71     */

72
73     public Expression reduce(int dep, Context c) throws XPathException {
74         Count f = new Count();
75         f.addArgument(argument[0].reduce(dep, c));
76         f.setStaticContext(getStaticContext());
77         return f.simplify();
78     }
79
80 }
81
82
83
84 //
85
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
86
// you may not use this file except in compliance with the License. You may obtain a copy of the
87
// License at http://www.mozilla.org/MPL/
88
//
89
// Software distributed under the License is distributed on an "AS IS" basis,
90
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
91
// See the License for the specific language governing rights and limitations under the License.
92
//
93
// The Original Code is: all this file.
94
//
95
// The Initial Developer of the Original Code is
96
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
97
//
98
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
99
//
100
// Contributor(s): none.
101
//
102
Popular Tags