Skip to content

Commit

Permalink
Merge pull request #4886 from line-o/fix/matches-error-code
Browse files Browse the repository at this point in the history
[bugfix] fn:matches error codes
  • Loading branch information
joewiz committed May 8, 2023
2 parents d39050a + a5a3b74 commit 83086c3
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,10 @@ private void analyzeString(final MemTreeBuilder builder, final String input, Str
}
} 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());
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, ErrorCodes.ERROR, e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,20 @@ private boolean matchXmlRegex(final String string, final String pattern, final S
return regex.containsMatch(string);

} catch (final net.sf.saxon.trans.XPathException e) {
throw new XPathException(this, ErrorCodes.FORX0001, "Invalid regular expression: " + e.getMessage(), new StringValue(this, pattern), e);
switch (e.getErrorCodeLocalPart()) {
case "FORX0001" -> throw new XPathException(this, ErrorCodes.FORX0001, "Invalid regular expression: " + e.getMessage());
case "FORX0002" -> throw new XPathException(this, ErrorCodes.FORX0002, "Invalid regular expression: " + e.getMessage());
// no FORX0003 here since fn:matches is allowed to match an empty string
default -> throw new XPathException(this, ErrorCodes.ERROR, e.getMessage());
}
}
}

/**
* @param string the value
* @param pattern the pattern
* @param flags the flags
* @return Whether or not the string matches the given pattern with the given flags
* @return Whether the string matches the given pattern with the given flags
* @throws XPathException if an error occurs
*/
private boolean match(final String string, final String pattern, final int flags) throws XPathException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,11 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro

} 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());
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, ErrorCodes.ERROR, e.getMessage());
}
}
}
Expand Down
73 changes: 73 additions & 0 deletions exist-core/src/test/xquery/xquery3/matches.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(:
: eXist-db Open Source Native XML Database
: Copyright (C) 2001 The eXist-db Authors
:
: [email protected]
: http://www.exist-db.org
:
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; either
: version 2.1 of the License, or (at your option) any later version.
:
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
:
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
:)
xquery version "3.1";

module namespace mt="http://exist-db.org/xquery/test/matches";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
%test:assertTrue
function mt:allowEmptyMatch() {
matches("","")
};

declare
%test:args("a", "[A-Z]", "")
%test:assertFalse
%test:args("a", "\d", "")
%test:assertFalse
%test:args("a", "\w", "")
%test:assertTrue
%test:args("a", "[A-Z]", "i")
%test:assertTrue
function mt:allowEmptyMatch($v, $p, $f) {
matches($v,$p, $f)
};

declare
%test:args("\")
%test:assertError("err:FORX0002")
%test:args("(")
%test:assertError("err:FORX0002")
%test:args("[")
%test:assertError("err:FORX0002")
%test:args("{")
%test:assertError("err:FORX0002")
%test:args("?")
%test:assertError("err:FORX0002")
function mt:invalid-pattern($regex as xs:string) {
matches("",$regex)
};

declare
%test:args("1")
%test:assertError("err:FORX0001")
%test:args("?")
%test:assertError("err:FORX0001")
%test:args("[")
%test:assertError("err:FORX0001")
%test:args("p")
%test:assertError("err:FORX0001")
function mt:invalid-flag($flag as xs:string) {
matches("","", $flag)
};
80 changes: 80 additions & 0 deletions exist-core/src/test/xquery/xquery3/replace.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
(:
: eXist-db Open Source Native XML Database
: Copyright (C) 2001 The eXist-db Authors
:
: [email protected]
: http://www.exist-db.org
:
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; either
: version 2.1 of the License, or (at your option) any later version.
:
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
:
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
:)
xquery version "3.1";

module namespace rt="http://exist-db.org/xquery/test/replace";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
%test:args("")
%test:assertError("err:FORX0003")
%test:args(".?")
%test:assertError("err:FORX0003")
%test:args(".*")
%test:assertError("err:FORX0003")
%test:args("(.*)")
%test:assertError("err:FORX0003")
function rt:empty-match-fails($p as xs:string) {
replace("",$p,"")
};

declare
%test:args("a", "[A-Z]", "")
%test:assertEquals("a")
%test:args("a1", "\d", "")
%test:assertEquals("a")
%test:args("a", "\w", "")
%test:assertEquals("")
%test:args("a", "[A-Z]", "")
%test:assertEquals("a")
function rt:replace-string($v as xs:string, $p as xs:string, $r as xs:string) {
replace($v, $p, $r)
};

declare
%test:args("\")
%test:assertError("err:FORX0002")
%test:args("(")
%test:assertError("err:FORX0002")
%test:args("[")
%test:assertError("err:FORX0002")
%test:args("{")
%test:assertError("err:FORX0002")
%test:args("?")
%test:assertError("err:FORX0002")
function rt:invalid-pattern($regex as xs:string) {
replace("",$regex,"")
};

declare
%test:args("1")
%test:assertError("err:FORX0001")
%test:args("?")
%test:assertError("err:FORX0001")
%test:args("[")
%test:assertError("err:FORX0001")
%test:args("p")
%test:assertError("err:FORX0001")
function rt:invalid-flag($flag as xs:string) {
replace("",".+","", $flag)
};

0 comments on commit 83086c3

Please sign in to comment.