KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.MappingFunction;
4 import net.sf.saxon.expr.StaticContext;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.value.*;
10
11 /**
12  * Implements the XPath number() function. This can also be used as a mapping function
13  * in a MappingIterator to map a sequence of values to numbers.
14  */

15
16 public class NumberFn extends SystemFunction implements MappingFunction {
17
18     /**
19     * Simplify and validate.
20     * This is a pure function so it can be simplified in advance if the arguments are known
21     */

22
23      public Expression simplify(StaticContext env) throws XPathException {
24         useContextItemAsDefault();
25         return simplifyArguments(env);
26     }
27
28     /**
29     * Evaluate in a general context
30     */

31
32     public Item evaluateItem(XPathContext context) throws XPathException {
33         Item arg0 = argument[0].evaluateItem(context);
34         if (arg0==null) {
35             return DoubleValue.NaN;
36         }
37         if (arg0 instanceof BooleanValue || arg0 instanceof NumericValue) {
38             return ((AtomicValue)arg0).convert(Type.DOUBLE, context);
39         }
40         CharSequence JavaDoc s = arg0.getStringValueCS();
41         try {
42             return new DoubleValue(Value.stringToNumber(s));
43         } catch (NumberFormatException JavaDoc e) {
44             return DoubleValue.NaN;
45         }
46     }
47
48     /**
49      * Static method to perform the same conversion as the number() function. This is different from the
50      * convert(Type.DOUBLE) in that it produces NaN rather than an error for non-numeric operands.
51      */

52
53     public static DoubleValue convert(AtomicValue value) {
54         try {
55             if (value==null) {
56                 return DoubleValue.NaN;
57             }
58             if (value instanceof BooleanValue || value instanceof NumericValue) {
59                 return (DoubleValue)value.convert(Type.DOUBLE, null);
60             }
61             CharSequence JavaDoc s = value.getStringValueCS();
62             return new DoubleValue(Value.stringToNumber(s));
63         } catch (NumberFormatException JavaDoc e) {
64             return DoubleValue.NaN;
65         } catch (XPathException e) {
66             return DoubleValue.NaN;
67         }
68     }
69
70     /**
71      * Mapping function for use when converting a sequence of atomic values to doubles
72      * using the rules of the number() function
73      */

74
75     public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
76         return convert((AtomicValue)item);
77     }
78 }
79
80 //
81
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
82
// you may not use this file except in compliance with the License. You may obtain a copy of the
83
// License at http://www.mozilla.org/MPL/
84
//
85
// Software distributed under the License is distributed on an "AS IS" basis,
86
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
87
// See the License for the specific language governing rights and limitations under the License.
88
//
89
// The Original Code is: all this file.
90
//
91
// The Initial Developer of the Original Code is Michael H. Kay.
92
//
93
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
94
//
95
// Contributor(s): none.
96
//
97
Popular Tags