Debian Codegraph @

net/sf/saxon/functions/Number_1.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.FunctionCall;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
15 import net.sf.saxon.expr.parser.ExpressionVisitor;
16 import net.sf.saxon.om.Item;
17 import net.sf.saxon.om.One;
18 import net.sf.saxon.om.ZeroOrOne;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.ConversionResult;
21 import net.sf.saxon.type.Converter;
22 import net.sf.saxon.type.ValidationFailure;
23 import net.sf.saxon.value.*;
24
25 /**
26 * Implements the XPath fn:number() function when called with one argument.
27 * Note: this function accepts the value "+INF" whether or not XSD 1.1 is enabled.
28 */
29
30 public class Number_1 extends ScalarSystemFunction {
31
32 @Override
33 public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException {
34 return toNumber((AtomicValue)arg);
35 }
36
37 @Override
38 public ZeroOrOne<NumericValue> resultWhenEmpty() {
39 return new One<>(DoubleValue.NaN);
40 }
41
42 /**
43 * Type-check a calling expression.
44 */
45
46 /*@NotNull*/
47 public Expression typeCheckCaller(FunctionCall caller, ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) {
48 if (caller.getArg(0).isCallOn(Number_1.class)) {
49 // happens through repeated rewriting
50 caller.setArg(0, ((FunctionCall)caller.getArg(0)).getArg(0));
51 }
52 return caller;
53 }
54
55 /**
56 * The actual implementation logic
57 * @param arg0 the atomic value to be converted
58 * @return the result of the conversion
59 */
60
61 public static DoubleValue toNumber(AtomicValue arg0) {
62 if (arg0 instanceof BooleanValue) {
63 return Converter.BooleanToDouble.INSTANCE.convert((BooleanValue)arg0);
64 } else if (arg0 instanceof NumericValue) {
65 return (DoubleValue) Converter.NumericToDouble.INSTANCE.convert((NumericValue)arg0).asAtomic();
66 } else if (arg0 instanceof StringValue && !(arg0 instanceof AnyURIValue)) {
67 // Always use the XSD 1.1 rules, which permit "+INF"
68 ConversionResult cr = StringToDouble11.getInstance().convert((StringValue)arg0);
69 if (cr instanceof ValidationFailure) {
70 return DoubleValue.NaN;
71 } else {
72 return (DoubleValue) cr;
73 }
74 } else {
75 return DoubleValue.NaN;
76 }
77 }
78
79 /**
80 * Static method to perform the same conversion as the number() function. This is different from the
81 * convert(Type.DOUBLE) in that it produces NaN rather than an error for non-numeric operands.
82 *
83 * @param value the value to be converted
84 * @param config the Saxon configuration
85 * @return the result of the conversion
86 */
87
88 public static DoubleValue convert(AtomicValue value, Configuration config) {
89 try {
90 if (value == null) {
91 return DoubleValue.NaN;
92 }
93 if (value instanceof BooleanValue) {
94 return new DoubleValue(((BooleanValue) value).getBooleanValue() ? 1.0e0 : 0.0e0);
95 }
96 if (value instanceof DoubleValue) {
97 return (DoubleValue) value;
98 }
99 if (value instanceof NumericValue) {
100 return new DoubleValue(((NumericValue) value).getDoubleValue());
101 }
102 if (value instanceof StringValue && !(value instanceof AnyURIValue)) {
103 double d = config.getConversionRules().getStringToDoubleConverter().stringToNumber(value.getStringValueCS());
104 return new DoubleValue(d);
105 }
106 return DoubleValue.NaN;
107 } catch (NumberFormatException e) {
108 return DoubleValue.NaN;
109 }
110 }
111
112 public String getCompilerName() {
113 return "NumberFnCompiler";
114 }
115
116
117 }
118