Search in sources :

Example 1 with RegularExpression

use of net.sf.saxon.regex.RegularExpression in project exist by eXist-db.

the class FunReplace method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final Sequence result;
    final Sequence stringArg = getArgument(0).eval(contextSequence, contextItem);
    if (stringArg.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final String flags;
        if (getSignature().getArgumentCount() == 4) {
            flags = getArgument(3).eval(contextSequence, contextItem).getStringValue();
        } else {
            flags = "";
        }
        final String string = stringArg.getStringValue();
        final Sequence patternSeq = getArgument(1).eval(contextSequence, contextItem);
        final String pattern = patternSeq.getStringValue();
        final Sequence replaceSeq = getArgument(2).eval(contextSequence, contextItem);
        final String replace = replaceSeq.getStringValue();
        final Configuration config = context.getBroker().getBrokerPool().getSaxonConfiguration();
        final List<String> warnings = new ArrayList<>(1);
        try {
            final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
            if (!hasLiteral(flags)) {
                final String msg = Replace.checkReplacement(replace);
                if (msg != null) {
                    throw new XPathException(this, ErrorCodes.FORX0004, msg);
                }
            }
            final CharSequence res = regularExpression.replace(string, replace);
            result = new StringValue(res.toString());
        } catch (final net.sf.saxon.trans.XPathException e) {
            switch(e.getErrorCodeLocalPart()) {
                case "FORX0001":
                    throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage());
                case "FORX0002":
                    throw new XPathException(this, ErrorCodes.FORX0002, e.getMessage());
                case "FORX0003":
                    throw new XPathException(this, ErrorCodes.FORX0003, e.getMessage());
                case "FORX0004":
                    throw new XPathException(this, ErrorCodes.FORX0004, e.getMessage());
                default:
                    throw new XPathException(this, e.getMessage());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : RegularExpression(net.sf.saxon.regex.RegularExpression) Configuration(net.sf.saxon.Configuration) XPathException(org.exist.xquery.XPathException) ArrayList(java.util.ArrayList) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 2 with RegularExpression

use of net.sf.saxon.regex.RegularExpression in project exist by eXist-db.

the class FunAnalyzeString method analyzeString.

private void analyzeString(final MemTreeBuilder builder, final String input, String pattern, final String flags) throws XPathException {
    final Configuration config = context.getBroker().getBrokerPool().getSaxonConfiguration();
    final List<String> warnings = new ArrayList<>(1);
    try {
        final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
        // TODO(AR) cache the regular expression... might be possible through Saxon config
        final RegexIterator regexIterator = regularExpression.analyze(input);
        Item item;
        while ((item = regexIterator.next()) != null) {
            if (regexIterator.isMatching()) {
                match(builder, regexIterator);
            } else {
                nonMatch(builder, item);
            }
        }
        for (final String warning : warnings) {
            LOG.warn(warning);
        }
    } catch (final net.sf.saxon.trans.XPathException e) {
        switch(e.getErrorCodeLocalPart()) {
            case "FORX0001":
                throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage());
            case "FORX0002":
                throw new XPathException(this, ErrorCodes.FORX0002, e.getMessage());
            case "FORX0003":
                throw new XPathException(this, ErrorCodes.FORX0003, e.getMessage());
            default:
                throw new XPathException(this, e.getMessage());
        }
    }
}
Also used : RegularExpression(net.sf.saxon.regex.RegularExpression) Item(net.sf.saxon.om.Item) RegexIterator(net.sf.saxon.regex.RegexIterator) Configuration(net.sf.saxon.Configuration) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 Configuration (net.sf.saxon.Configuration)2 RegularExpression (net.sf.saxon.regex.RegularExpression)2 Item (net.sf.saxon.om.Item)1 RegexIterator (net.sf.saxon.regex.RegexIterator)1 XPathException (org.exist.xquery.XPathException)1 Sequence (org.exist.xquery.value.Sequence)1 StringValue (org.exist.xquery.value.StringValue)1