Debian Codegraph @

net/sf/saxon/functions/Replace.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.Expression;
11 import net.sf.saxon.expr.StringLiteral;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.regex.RegularExpression;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.StringValue;
17
18
19 /**
20 * This class implements the replace() function for replacing
21 * substrings that match a regular expression
22 */
23
24 public class Replace extends RegexFunction {
25
26 private boolean replacementChecked = false;
27
28 @Override
29 protected boolean allowRegexMatchingEmptyString() {
30 return false;
31 }
32
33 /**
34 * Make an expression that either calls this function, or that is equivalent to a call
35 * on this function
36 *
37 * @param arguments the supplied arguments to the function call
38 * @return either a function call on this function, or an expression that delivers
39 * the same result
40 */
41 @Override
42 public Expression makeFunctionCall(Expression... arguments) {
43 boolean maybeQ = arguments.length == 4 && (!(arguments[3] instanceof StringLiteral) || ((StringLiteral) arguments[3]).getStringValue().contains("q"));
44 if (arguments[2] instanceof StringLiteral && !maybeQ) {
45 // Do early checking of the replacement expression if known statically
46 String rep = ((StringLiteral) arguments[2]).getStringValue();
47 if (checkReplacement(rep) == null) {
48 replacementChecked = true;
49 }
50 }
51 return super.makeFunctionCall(arguments);
52 }
53
54 /**
55 * Evaluate the expression
56 *
57 * @param context the dynamic evaluation context
58 * @param arguments the values of the arguments, supplied as SequenceIterators
59 * @return the result of the evaluation, in the form of a SequenceIterator
60 * @throws net.sf.saxon.trans.XPathException
61 * if a dynamic error occurs during the evaluation of the expression
62 */
63 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
64
65 StringValue arg0 = (StringValue)arguments[0].head();
66 CharSequence input = arg0 == null ? "" : arg0.getStringValueCS();
67
68 StringValue arg2 = (StringValue) arguments[2].head();
69 CharSequence replacement = arg2.getStringValueCS();
70
71 RegularExpression re = getRegularExpression(arguments);
72 if (!re.getFlags().contains("q")) {
73 if (!replacementChecked) {
74 // if it is a string literal, the check was done at compile time
75 String msg = checkReplacement(replacement);
76 if (msg != null) {
77 throw new XPathException(msg, "FORX0004", context);
78 }
79 }
80 }
81 CharSequence res = re.replace(input, replacement);
82 return StringValue.makeStringValue(res);
83 }
84
85 /**
86 * Check the contents of the replacement string
87 *
88 * @param rep the replacement string
89 * @return null if the string is OK, or an error message if not
90 */
91
92 public static String checkReplacement(CharSequence rep) {
93 for (int i = 0; i < rep.length(); i++) {
94 char c = rep.charAt(i);
95 if (c == '$') {
96 if (i + 1 < rep.length()) {
97 char next = rep.charAt(++i);
98 if (next < '0' || next > '9') {
99 return "Invalid replacement string in replace(): $ sign must be followed by digit 0-9";
100 }
101 } else {
102 return "Invalid replacement string in replace(): $ sign at end of string";
103 }
104 } else if (c == '\\') {
105 if (i + 1 < rep.length()) {
106 char next = rep.charAt(++i);
107 if (next != '\\' && next != '$') {
108 return "Invalid replacement string in replace(): \\ character must be followed by \\ or $";
109 }
110 } else {
111 return "Invalid replacement string in replace(): \\ character at end of string";
112 }
113 }
114 }
115 return null;
116 }
117
118 }
119