Search in sources :

Example 1 with RegexSyntaxException

use of org.exist.thirdparty.net.sf.saxon.functions.regex.RegexSyntaxException in project exist by eXist-db.

the class RegexUtil method translateRegexp.

/**
 * Translates the Regular Expression from XPath3 syntax to Java regex
 * syntax.
 *
 * @param context the context expression - used for error reporting
 * @param pattern a String containing a regular expression in the syntax of XPath Functions and Operators 3.0.
 * @param ignoreWhitespace true if whitespace is to be ignored ('x' flag)
 * @param caseBlind true if case is to be ignored ('i' flag)
 *
 * @return The Java Regular Expression
 *
 * @throws XPathException if the XQuery Regular Expression is invalid.
 */
public static String translateRegexp(final Expression context, final String pattern, final boolean ignoreWhitespace, final boolean caseBlind) throws XPathException {
    // convert pattern to Java regex syntax
    try {
        final int options = RegularExpression.XML11 | RegularExpression.XPATH30;
        int flagbits = 0;
        if (ignoreWhitespace) {
            flagbits |= Pattern.COMMENTS;
        }
        if (caseBlind) {
            flagbits |= Pattern.CASE_INSENSITIVE;
        }
        final List<RegexSyntaxException> warnings = new ArrayList<>();
        return JDK15RegexTranslator.translate(pattern, options, flagbits, warnings);
    } catch (final RegexSyntaxException e) {
        throw new XPathException(context, ErrorCodes.FORX0002, "Conversion from XPath F&O 3.0 regular expression syntax to Java regular expression syntax failed: " + e.getMessage(), new StringValue(pattern), e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) ArrayList(java.util.ArrayList) RegexSyntaxException(org.exist.thirdparty.net.sf.saxon.functions.regex.RegexSyntaxException) StringValue(org.exist.xquery.value.StringValue)

Aggregations

ArrayList (java.util.ArrayList)1 RegexSyntaxException (org.exist.thirdparty.net.sf.saxon.functions.regex.RegexSyntaxException)1 XPathException (org.exist.xquery.XPathException)1 StringValue (org.exist.xquery.value.StringValue)1