Search in sources :

Example 1 with RegexIterator

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

the class FunAnalyzeString method match.

private void match(final MemTreeBuilder builder, final RegexIterator regexIterator) throws net.sf.saxon.trans.XPathException {
    builder.startElement(QN_MATCH, null);
    regexIterator.processMatchingSubstring(new RegexIterator.MatchHandler() {

        @Override
        public void characters(final CharSequence s) {
            builder.characters(s);
        }

        @Override
        public void onGroupStart(final int groupNumber) throws net.sf.saxon.trans.XPathException {
            final AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute("", QN_NR.getLocalPart(), QN_NR.getLocalPart(), "int", Integer.toString(groupNumber));
            builder.startElement(QN_GROUP, attributes);
        }

        @Override
        public void onGroupEnd(final int groupNumber) throws net.sf.saxon.trans.XPathException {
            builder.endElement();
        }
    });
    builder.endElement();
}
Also used : RegexIterator(net.sf.saxon.regex.RegexIterator) AttributesImpl(org.xml.sax.helpers.AttributesImpl)

Example 2 with RegexIterator

use of net.sf.saxon.regex.RegexIterator 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

RegexIterator (net.sf.saxon.regex.RegexIterator)2 ArrayList (java.util.ArrayList)1 Configuration (net.sf.saxon.Configuration)1 Item (net.sf.saxon.om.Item)1 RegularExpression (net.sf.saxon.regex.RegularExpression)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1