Dear Jena Developers,
I am doing some strange stuff with building regexes from data inside a
sparql store. Sometimes that leads to invalid regexes which ends up
throwing PatternSyntaxExceptions.
For my use case it is fine that these would not evaluate properly. I
believe however that the specific exception about pattern syntax should
be caught and turned into a ExprEvalException so that evaluation of
queries can continue.
i.e. the following stacktrace (against jena 3.4.0) should be caught and
not propegated.
java.util.regex.PatternSyntaxException: Illegal repetition near index 10
^(?:-*[^-]){-9}
^
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.closure(Pattern.java:3157)
at java.util.regex.Pattern.group0(Pattern.java:2912)
at java.util.regex.Pattern.sequence(Pattern.java:2051)
at java.util.regex.Pattern.expr(Pattern.java:1996)
at java.util.regex.Pattern.compile(Pattern.java:1696)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1054)
at
org.apache.jena.sparql.expr.nodevalue.XSDFuncOp.strReplace(XSDFuncOp.java:474)
at
org.apache.jena.sparql.expr.nodevalue.XSDFuncOp.strReplace(XSDFuncOp.java:518)
I suggest modifying the function
public static NodeValue strReplace(NodeValue nvStr, NodeValue nvPattern,
NodeValue nvReplacement, NodeValue nvFlags) {
String pat = checkAndGetStringLiteral("replace",
nvPattern).getLiteralLexicalForm() ;
int flags = 0 ;
if ( nvFlags != null ) {
String flagsStr = checkAndGetStringLiteral("replace",
nvFlags).getLiteralLexicalForm() ;
flags = RegexJava.makeMask(flagsStr) ;
}
return strReplace(nvStr, Pattern.compile(pat, flags),
nvReplacement) ;
}
to be something like
public static NodeValue strReplace(NodeValue nvStr, NodeValue nvPattern,
NodeValue nvReplacement, NodeValue nvFlags) {
String pat = checkAndGetStringLiteral("replace",
nvPattern).getLiteralLexicalForm() ;
int flags = 0 ;
if ( nvFlags != null ) {
String flagsStr = checkAndGetStringLiteral("replace",
nvFlags).getLiteralLexicalForm() ;
flags = RegexJava.makeMask(flagsStr) ;
}
try {
return strReplace(nvStr, Pattern.compile(pat, flags),
nvReplacement) ;
} catch (PatternSyntaxException ex){
throw new ExprEvalException("PatternSyntaxException", ex) ;
}
}
Does that seem sensible?
Regards,
Jerven
--
-------------------------------------------------------------------
Jerven Bolleman Jerven.Bolleman@sib.swiss
SIB Swiss Institute of Bioinformatics Tel: +41 (0)22 379 58 85
CMU, rue Michel Servet 1 Fax: +41 (0)22 379 58 58
1211 Geneve 4,
Switzerland www.sib.swiss - www.uniprot.org
Follow us at https://twitter.com/#!/uniprot
-------------------------------------------------------------------