| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 21 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 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 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
|