|
41 | 41 | import org.exist.xquery.value.StringValue;
|
42 | 42 | import org.exist.xquery.value.Type;
|
43 | 43 |
|
| 44 | +import java.util.ArrayList; |
44 | 45 | import java.util.List;
|
45 | 46 | import java.util.regex.Matcher;
|
46 | 47 | import java.util.regex.Pattern;
|
47 | 48 | import java.util.regex.PatternSyntaxException;
|
| 49 | +import net.sf.saxon.regex.RegularExpression; |
48 | 50 |
|
49 | 51 | import static org.exist.xquery.FunctionDSL.*;
|
50 | 52 | import static org.exist.xquery.functions.fn.FnModule.functionSignatures;
|
@@ -492,29 +494,39 @@ private Sequence evalFallback(final NodeSet nodes, final String pattern, final i
|
492 | 494 | private Sequence evalGeneric(final Sequence contextSequence, final Item contextItem, final Sequence input) throws XPathException {
|
493 | 495 | final String string = input.getStringValue();
|
494 | 496 |
|
495 |
| - final int flags; |
| 497 | + final String xmlRegexFlags; |
496 | 498 | if (getSignature().getArgumentCount() == 3) {
|
497 |
| - flags = parseFlags(this, getArgument(2).eval(contextSequence, contextItem).getStringValue()); |
| 499 | + xmlRegexFlags = getArgument(2).eval(contextSequence, contextItem).getStringValue(); |
498 | 500 | } else {
|
499 |
| - flags = 0; |
| 501 | + xmlRegexFlags = ""; |
500 | 502 | }
|
501 | 503 |
|
502 |
| - final String pattern; |
| 504 | + final String pattern = getArgument(1).eval(contextSequence, contextItem).getStringValue(); |
503 | 505 | if (isCalledAs("matches-regex")) {
|
504 |
| - pattern = getArgument(1).eval(contextSequence, contextItem).getStringValue(); |
| 506 | + final int flags = parseFlags(this, xmlRegexFlags); |
| 507 | + return BooleanValue.valueOf(match(string, pattern,flags)); |
505 | 508 | } else {
|
506 |
| - final boolean literal = hasLiteral(flags); |
507 |
| - if (literal) { |
508 |
| - // no need to change anything |
509 |
| - pattern = getArgument(1).eval(contextSequence, contextItem).getStringValue(); |
510 |
| - } else { |
511 |
| - final boolean ignoreWhitespace = hasIgnoreWhitespace(flags); |
512 |
| - final boolean caseBlind = hasCaseInsensitive(flags); |
513 |
| - pattern = translateRegexp(this, getArgument(1).eval(contextSequence, contextItem).getStringValue(), ignoreWhitespace, caseBlind); |
514 |
| - } |
| 509 | + return BooleanValue.valueOf(matchXmlRegex(string, pattern, xmlRegexFlags)); |
515 | 510 | }
|
| 511 | + } |
| 512 | + |
| 513 | + |
| 514 | + private boolean matchXmlRegex(final String string, final String pattern, final String flags) throws XPathException { |
| 515 | + try { |
| 516 | + List<String> warnings = new ArrayList<>(1); |
| 517 | + RegularExpression regex = context.getBroker().getBrokerPool() |
| 518 | + .getSaxonConfiguration() |
| 519 | + .compileRegularExpression(pattern, flags, "XP30", warnings); |
516 | 520 |
|
517 |
| - return BooleanValue.valueOf(match(string, pattern, flags)); |
| 521 | + for (final String warning : warnings) { |
| 522 | + LOG.warn(warning); |
| 523 | + } |
| 524 | + |
| 525 | + return regex.containsMatch(string); |
| 526 | + |
| 527 | + } catch (final net.sf.saxon.trans.XPathException e) { |
| 528 | + throw new XPathException(this, ErrorCodes.FORX0001, "Invalid regular expression: " + e.getMessage(), new StringValue(pattern), e); |
| 529 | + } |
518 | 530 | }
|
519 | 531 |
|
520 | 532 | /**
|
|
0 commit comments