String msg = checkReplacement(replacement);
if (msg != null) {
dynamicError(msg, "FORX0004", c);
}
RegularExpression re = regexp;
if (re == null) {
AtomicValue arg1 = (AtomicValue)argument[1].evaluateItem(c);
CharSequence flags;
if (argument.length == 3) {
flags = "";
} else {
AtomicValue arg3 = (AtomicValue)argument[3].evaluateItem(c);
flags = arg3.getStringValueCS();
}
try {
final Platform platform = Configuration.getPlatform();
final int xmlVersion = c.getConfiguration().getXMLVersion();
re = platform.compileRegularExpression(
arg1.getStringValueCS(), xmlVersion, RegularExpression.XPATH_SYNTAX, flags);
} catch (XPathException err) {
XPathException de = new XPathException(err);
de.setErrorCode("FORX0002");
de.setXPathContext(c);
de.setLocator(this);
throw de;
} catch (PatternSyntaxException err) {
XPathException de = new XPathException(err);
de.setErrorCode("FORX0002");
de.setXPathContext(c);
de.setLocator(this);
throw de;
}
// check that it's not a pattern that matches ""
if (re.matches("")) {
dynamicError(
"The regular expression in replace() must not be one that matches a zero-length string",
"FORX0003", c);
}
}
String input = arg0.getStringValue();
CharSequence res = re.replace(input, replacement);
return StringValue.makeStringValue(res);
}