Debian Codegraph @

net/sf/saxon/functions/Matches.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.XPathContext;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.regex.RegularExpression;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.value.AtomicValue;
16 import net.sf.saxon.value.BooleanValue;
17 import net.sf.saxon.value.StringValue;
18
19
20 /**
21 * This class implements the 3-argument matches() function for regular expression matching
22 */
23
24 public class Matches extends RegexFunction {
25
26 protected boolean allowRegexMatchingEmptyString() {
27 return true;
28 }
29
30 /**
31 * Interface used by compiled bytecode
32 *
33 * @param input the value to be tested
34 * @param regex the regular expression
35 * @param flags the flags
36 * @param context the dynamic context
37 * @return true if the string matches the regex
38 * @throws XPathException if a dynamic error occurs
39 */
40
41 public boolean evalMatches(AtomicValue input, AtomicValue regex, CharSequence flags, XPathContext context) throws XPathException {
42 RegularExpression re;
43
44 if (regex == null) {
45 return false;
46 }
47
48 try {
49 String lang = "XP30";
50 if (context.getConfiguration().getXsdVersion() == Configuration.XSD11) {
51 lang += "/XSD11";
52 }
53 re = context.getConfiguration().compileRegularExpression(
54 regex.getStringValueCS(), flags.toString(), lang, null);
55
56 } catch (XPathException err) {
57 XPathException de = new XPathException(err);
58 de.maybeSetErrorCode("FORX0002");
59 de.setXPathContext(context);
60 throw de;
61 }
62 return re.containsMatch(input.getStringValueCS());
63 }
64
65 /**
66 * Evaluate the expression
67 *
68 * @param context the dynamic evaluation context
69 * @param arguments the values of the arguments, supplied as Sequences
70 * @return the result of the evaluation, in the form of a Sequence
71 * @throws net.sf.saxon.trans.XPathException
72 * if a dynamic error occurs during the evaluation of the expression
73 */
74 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
75 RegularExpression re = getRegularExpression(arguments);
76 StringValue arg = (StringValue)arguments[0].head();
77 CharSequence in = arg==null ? "" : arg.getStringValueCS();
78 boolean result = re.containsMatch(in);
79 return BooleanValue.get(result);
80 }
81
82 public String getCompilerName() {
83 return "MatchesCompiler";
84 }
85
86
87 }
88