Debian Codegraph @

net/sf/saxon/ma/map/MapUntypedContains.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.ma.map;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.functions.SystemFunction;
12 import net.sf.saxon.lib.ConversionRules;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.*;
16 import net.sf.saxon.value.AtomicValue;
17 import net.sf.saxon.value.BooleanValue;
18 import net.sf.saxon.value.UntypedAtomicValue;
19
20 /**
21 * Implementation of an internal function map:untyped-contains(Map, key) => boolean,
22 * which is like map:contains except that if the supplied key is untyped atomic, it
23 * is converted to all the possible types present in the map and returns true if the
24 * key after conversion is present. In addition, if the supplied key is NaN then the
25 * result is always false.
26 */
27 public class MapUntypedContains extends SystemFunction {
28
29 public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException {
30 ConversionRules rules = context.getConfiguration().getConversionRules();
31 MapItem map = (MapItem) arguments[0].head();
32 AtomicValue key = (AtomicValue) arguments[1].head();
33 if (key instanceof UntypedAtomicValue) {
34 for (PrimitiveUType prim : map.getKeyUType().decompose()) {
35 BuiltInAtomicType t = (BuiltInAtomicType)prim.toItemType();
36 StringConverter converter = t.getStringConverter(rules);
37 ConversionResult av = converter.convert(key);
38 if (av instanceof ValidationFailure) {
39 // in the case of xs:decimal, try conversion via xs:double
40 if (prim.equals(PrimitiveUType.DECIMAL)) {
41 converter = BuiltInAtomicType.DOUBLE.getStringConverter(rules);
42 av = converter.convert(key);
43 if (av instanceof AtomicValue) {
44 if (map.get(av.asAtomic()) != null) {
45 return BooleanValue.TRUE;
46 }
47 }
48 }
49 } else if (map.get(av.asAtomic()) != null) {
50 return BooleanValue.TRUE;
51 }
52 }
53 return BooleanValue.FALSE;
54 } else if (key.isNaN()) {
55 return BooleanValue.FALSE;
56 }
57 boolean result = map.get(key) != null;
58 return BooleanValue.get(result);
59 }
60
61 }
62
63 // Copyright (c) 2018 Saxonica Limited.
64