You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Jerven Tjalling Bolleman <je...@sib.swiss> on 2017/10/23 08:23:02 UTC

strReplace -> j.u.regex.PatternSyntaxException should throw a ExprEvalException instead

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
-------------------------------------------------------------------

Re: strReplace -> j.u.regex.PatternSyntaxException should throw a ExprEvalException instead

Posted by aj...@apache.org.
Jerven, thanks for a good bug report (and double-thanks for accompanying it with a PR fix!).

Jena is as strong as we make it together.


ajs6f

Jerven Tjalling Bolleman wrote on 10/23/17 2:40 PM:
> Dear Andy and all other Jena developers,
>
> Thank you for taking and improving the patch!
> As well as all your excellent work over the years.
>
> Regards,
> Jerven
>
> On 10/23/2017 12:23 PM, Andy Seaborne wrote:
>>  > Does that seem sensible?
>>
>> Yes - recorded as JENA-1403.
>>
>> The static compilation case (the constructor E_strReplase) should also throw a ExprException.
>>
>>      Thanks
>>      Andy
>>
>> https://issues.apache.org/jira/browse/JENA-1403
>>
>> On 23/10/17 09:23, Jerven Tjalling Bolleman wrote:
>>> 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
>

Re: strReplace -> j.u.regex.PatternSyntaxException should throw a ExprEvalException instead

Posted by Jerven Tjalling Bolleman <je...@sib.swiss>.
Dear Andy and all other Jena developers,

Thank you for taking and improving the patch!
As well as all your excellent work over the years.

Regards,
Jerven

On 10/23/2017 12:23 PM, Andy Seaborne wrote:
>  > Does that seem sensible?
> 
> Yes - recorded as JENA-1403.
> 
> The static compilation case (the constructor E_strReplase) should also 
> throw a ExprException.
> 
>      Thanks
>      Andy
> 
> https://issues.apache.org/jira/browse/JENA-1403
> 
> On 23/10/17 09:23, Jerven Tjalling Bolleman wrote:
>> 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
-------------------------------------------------------------------

Re: strReplace -> j.u.regex.PatternSyntaxException should throw a ExprEvalException instead

Posted by Andy Seaborne <an...@apache.org>.
 > Does that seem sensible?

Yes - recorded as JENA-1403.

The static compilation case (the constructor E_strReplase) should also 
throw a ExprException.

     Thanks
     Andy

https://issues.apache.org/jira/browse/JENA-1403

On 23/10/17 09:23, Jerven Tjalling Bolleman wrote:
> 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