KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.expr.*;
4 import com.icl.saxon.om.NodeInfo;
5
6 import java.util.*;
7 import java.text.*;
8
9
10
11 public class NumberFn extends Function {
12
13     /**
14     * Function name (for diagnostics)
15     */

16
17     public String JavaDoc getName() {
18         return "number";
19     };
20
21     /**
22     * Determine the data type of the expression
23     * @return Value.NUMBER
24     */

25
26     public int getDataType() {
27         return Value.NUMBER;
28     }
29
30     /**
31     * Simplify and validate.
32     * This is a pure function so it can be simplified in advance if the arguments are known
33     */

34
35     public Expression simplify() throws XPathException {
36         int numArgs = checkArgumentCount(0, 1);
37         if (numArgs==1) {
38             argument[0] = argument[0].simplify();
39             if (argument[0].getDataType()==Value.NUMBER) {
40                 return argument[0];
41             }
42             if (argument[0] instanceof Value) {
43                 return new NumericValue(((Value)argument[0]).asNumber());
44             }
45         }
46         return this;
47     }
48
49     /**
50     * Evaluate the function in a numeric context
51     */

52
53     public double evaluateAsNumber(Context c) throws XPathException {
54         if (getNumberOfArguments()==1) {
55             return argument[0].evaluateAsNumber(c);
56         } else {
57             return Value.stringToNumber(
58                     (c.getContextNodeInfo()).getStringValue());
59         }
60     }
61
62     /**
63     * Evaluate in a general context
64     */

65
66     public Value evaluate(Context c) throws XPathException {
67         return new NumericValue(evaluateAsNumber(c));
68     }
69
70     /**
71     * Determine the dependencies
72     */

73
74     public int getDependencies() {
75         if (getNumberOfArguments()==1) {
76             return argument[0].getDependencies();
77         } else {
78             return Context.CONTEXT_NODE;
79         }
80     }
81
82     /**
83     * Reduce the dependencies
84     */

85
86     public Expression reduce(int dep, Context c) throws XPathException {
87         if (getNumberOfArguments()==1) {
88             NumberFn f = new NumberFn();
89             f.addArgument(argument[0].reduce(dep, c));
90             f.setStaticContext(getStaticContext());
91             return f.simplify();
92         } else {
93             if ((dep & Context.CONTEXT_NODE) != 0) {
94                 return evaluate(c);
95             } else {
96                 return this;
97             }
98         }
99     }
100
101 }
102
103 //
104
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
105
// you may not use this file except in compliance with the License. You may obtain a copy of the
106
// License at http://www.mozilla.org/MPL/
107
//
108
// Software distributed under the License is distributed on an "AS IS" basis,
109
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
110
// See the License for the specific language governing rights and limitations under the License.
111
//
112
// The Original Code is: all this file.
113
//
114
// The Initial Developer of the Original Code is
115
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
116
//
117
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
118
//
119
// Contributor(s): none.
120
//
121
Popular Tags