Debian Codegraph @

net/sf/saxon/functions/ContainsToken.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.expr.XPathContext;
11 import net.sf.saxon.lib.StringCollator;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.SequenceIterator;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.tree.iter.UnfailingIterator;
17 import net.sf.saxon.value.BooleanValue;
18 import net.sf.saxon.value.StringValue;
19 import net.sf.saxon.value.Whitespace;
20
21 /**
22 * Implements the fn:contains-token() function with the collation already bound.
23 * This function was introduced in XPath 3.1
24 */
25 public class ContainsToken extends CollatingFunctionFixed {
26
27 @Override
28 public boolean isSubstringMatchingFunction() {
29 return true;
30 }
31
32 private static boolean containsToken(SequenceIterator arg0, StringValue arg1, StringCollator collator) throws XPathException {
33 if (arg1 == null) {
34 return false;
35 }
36 String search = Whitespace.trim(arg1.getPrimitiveStringValue().toString());
37 if (search.isEmpty()) {
38 return false;
39 }
40 Item item;
41 while ((item = arg0.next()) != null) {
42 UnfailingIterator tokens = new Whitespace.Tokenizer(item.getStringValueCS());
43 Item token;
44 while ((token = tokens.next()) != null) {
45 if (collator.comparesEqual(search, token.getStringValue())) {
46 tokens.close();
47 arg0.close();
48 return true;
49 }
50 }
51 }
52 return false;
53 }
54
55 /**
56 * Dynamic evaluation
57 * @param context the dynamic evaluation context
58 * @param arguments the values of the arguments, supplied as Sequences.
59 *
60 * @return true if the search token is present in the input
61 * @throws XPathException if a dynamic error occurs
62 */
63
64 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
65 return BooleanValue.get(
66 containsToken(arguments[0].iterate(), (StringValue) arguments[1].head(), getStringCollator()));
67
68 }
69
70 }
71