Debian Codegraph @

net/sf/saxon/functions/RegexFunction.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.Literal;
13 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
14 import net.sf.saxon.expr.parser.ExpressionVisitor;
15 import net.sf.saxon.om.Sequence;
16 import net.sf.saxon.regex.RegularExpression;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.value.StringValue;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 /**
24 * This class implements any of the functions matches(), replace(), tokenize(), analyze-string(), in the
25 * version where a flags argument is present in the argument list
26 */
27 public abstract class RegexFunction extends SystemFunction implements StatefulSystemFunction {
28
29 private RegularExpression staticRegex;
30
31 public RegularExpression getStaticRegex() {
32 return staticRegex;
33 }
34
35 private void tryToBindRegularExpression(Expression[] arguments) {
36 // For all these functions, the regular expression is the second argument, and the flags
37 // argument is the last argument.
38 if (arguments[1] instanceof Literal && arguments[arguments.length - 1] instanceof Literal) {
39 try {
40 Configuration config = getRetainedStaticContext().getConfiguration();
41 String re = ((Literal) arguments[1]).getValue().getStringValue();
42 String flags = ((Literal) arguments[arguments.length - 1]).getValue().getStringValue();
43 String hostLang = "XP30";
44 if (config.getXsdVersion() == Configuration.XSD11) {
45 hostLang += "/XSD11";
46 }
47 List<String> warnings = new ArrayList<>(1);
48 staticRegex = config.compileRegularExpression(re, flags, hostLang, warnings);
49 if (!allowRegexMatchingEmptyString() && staticRegex.matches("")) {
50 staticRegex = null;
51 // will cause a dynamic error
52 }
53 } catch (XPathException err) {
54 // If the regex is invalid, we leave it to be evaluated again at execution time
55 }
56 }
57 }
58
59 /**
60 * Make a copy of this SystemFunction. This is required only for system functions such as regex
61 * functions that maintain state on behalf of a particular caller.
62 *
63 * @return a copy of the system function able to contain its own copy of the state on behalf of
64 * the caller.
65 */
66 @Override
67 public RegexFunction copy() {
68 RegexFunction copy = (RegexFunction) SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity());
69 copy.staticRegex = staticRegex;
70 return copy;
71 }
72
73
74 protected abstract boolean allowRegexMatchingEmptyString();
75
76 /**
77 * Make an expression that either calls this function, or that is equivalent to a call
78 * on this function
79 *
80 * @param arguments the supplied arguments to the function call
81 * @return either a function call on this function, or an expression that delivers
82 * the same result
83 */
84 @Override
85 public Expression makeFunctionCall(Expression... arguments) {
86 tryToBindRegularExpression(arguments);
87 return super.makeFunctionCall(arguments);
88 }
89
90 /**
91 * Allow the function to create an optimized call based on the values of the actual arguments
92 *
93 * @param visitor the expression visitor
94 * @param contextInfo information about the context item
95 * @param arguments the supplied arguments to the function call. Note: modifying the contents
96 * of this array should not be attempted, it is likely to have no effect.
97 * @return either a function call on this function, or an expression that delivers
98 * the same result, or null indicating that no optimization has taken place
99 * @throws XPathException if an error is detected
100 */
101 @Override
102 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
103 tryToBindRegularExpression(arguments);
104 return super.makeOptimizedFunctionCall(visitor, contextInfo, arguments);
105 }
106
107
108 /**
109 * Get the regular expression at evaluation time
110 *
111 * @param args the argument values in the function call
112 * @return the compiled regular expression; either the expression pre-compiled statically,
113 * or the result of compiling it dynamically
114 * @throws XPathException if the regular expression is invalid
115 */
116
117 protected RegularExpression getRegularExpression(Sequence[] args) throws XPathException {
118 if (staticRegex != null) {
119 return staticRegex;
120 }
121 Configuration config = getRetainedStaticContext().getConfiguration();
122 StringValue regexArg = (StringValue)args[1].head();
123 String re = regexArg.getStringValue();
124 String flags = args[args.length - 1].head().getStringValue();
125 String hostLang = "XP30";
126 if (config.getXsdVersion() == Configuration.XSD11) {
127 hostLang += "/XSD11";
128 }
129 List<String> warnings = new ArrayList<>(1);
130 RegularExpression regex = config.compileRegularExpression(re, flags, hostLang, warnings);
131
132 if (!allowRegexMatchingEmptyString() && regex.matches("")) {
133 throw new XPathException("The regular expression must not be one that matches a zero-length string", "FORX0003");
134 }
135 return regex;
136 }
137
138 }
139