|
14 | 14 | import java.util.Objects; |
15 | 15 | import java.util.regex.Pattern; |
16 | 16 |
|
| 17 | +import org.eclipse.rdf4j.model.IRI; |
| 18 | +import org.eclipse.rdf4j.model.Literal; |
| 19 | +import org.eclipse.rdf4j.model.Triple; |
17 | 20 | import org.eclipse.rdf4j.model.Value; |
| 21 | +import org.eclipse.rdf4j.model.ValueFactory; |
| 22 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 23 | +import org.eclipse.rdf4j.query.algebra.evaluation.util.QueryEvaluationUtility; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
18 | 26 |
|
19 | 27 | /** |
20 | 28 | * @author Håvard Ottestad |
21 | 29 | */ |
22 | 30 | public class PatternFilter extends FilterPlanNode { |
23 | 31 |
|
| 32 | + private static final Logger logger = LoggerFactory.getLogger(PatternFilter.class); |
| 33 | + |
24 | 34 | private final Pattern pattern; |
25 | 35 |
|
26 | 36 | public PatternFilter(PlanNode parent, String pattern, String flags) { |
27 | 37 | super(parent); |
28 | 38 | if (flags != null && !flags.isEmpty()) { |
29 | | - |
30 | 39 | int flag = 0b0; |
31 | 40 |
|
32 | 41 | if (flags.contains("i")) { |
33 | 42 | flag = flag | Pattern.CASE_INSENSITIVE; |
| 43 | + logger.trace("PatternFilter constructed with case insensitive flag"); |
34 | 44 | } |
35 | 45 |
|
36 | 46 | if (flags.contains("d")) { |
37 | 47 | flag = flag | Pattern.UNIX_LINES; |
| 48 | + logger.trace("PatternFilter constructed with UNIX lines flag"); |
38 | 49 | } |
39 | 50 |
|
40 | 51 | if (flags.contains("m")) { |
41 | 52 | flag = flag | Pattern.MULTILINE; |
| 53 | + logger.trace("PatternFilter constructed with multiline flag"); |
42 | 54 | } |
43 | 55 |
|
44 | 56 | if (flags.contains("s")) { |
45 | 57 | flag = flag | Pattern.DOTALL; |
| 58 | + logger.trace("PatternFilter constructed with dotall flag"); |
46 | 59 | } |
47 | 60 |
|
48 | 61 | if (flags.contains("u")) { |
49 | 62 | flag = flag | Pattern.UNICODE_CASE; |
| 63 | + logger.trace("PatternFilter constructed with unicode case flag"); |
50 | 64 | } |
51 | 65 |
|
52 | 66 | if (flags.contains("x")) { |
53 | 67 | flag = flag | Pattern.COMMENTS; |
| 68 | + logger.trace("PatternFilter constructed with comments flag"); |
54 | 69 | } |
55 | 70 |
|
56 | 71 | if (flags.contains("U")) { |
57 | 72 | flag = flag | Pattern.UNICODE_CHARACTER_CLASS; |
| 73 | + logger.trace("PatternFilter constructed with unicode character class flag"); |
58 | 74 | } |
59 | 75 |
|
60 | 76 | this.pattern = Pattern.compile(pattern, flag); |
| 77 | + logger.trace("PatternFilter constructed with pattern: {} and flags: {}", pattern, flags); |
61 | 78 |
|
62 | 79 | } else { |
63 | | - this.pattern = Pattern.compile(pattern); |
64 | | - |
| 80 | + this.pattern = Pattern.compile(pattern, 0b0); |
| 81 | + logger.trace("PatternFilter constructed with pattern: {} and no flags", pattern); |
65 | 82 | } |
| 83 | + } |
66 | 84 |
|
| 85 | + private static Literal str(Value argValue, ValueFactory valueFactory) { |
| 86 | + if (argValue instanceof IRI || argValue instanceof Triple) { |
| 87 | + return valueFactory.createLiteral(argValue.toString()); |
| 88 | + } else if (argValue instanceof Literal) { |
| 89 | + Literal literal = (Literal) argValue; |
| 90 | + |
| 91 | + if (QueryEvaluationUtility.isSimpleLiteral(literal)) { |
| 92 | + return literal; |
| 93 | + } else { |
| 94 | + return valueFactory.createLiteral(literal.getLabel()); |
| 95 | + } |
| 96 | + } else { |
| 97 | + return null; |
| 98 | + } |
67 | 99 | } |
68 | 100 |
|
69 | 101 | @Override |
70 | 102 | boolean checkTuple(ValidationTuple t) { |
71 | 103 | Value literal = t.getValue(); |
| 104 | + literal = str(literal, SimpleValueFactory.getInstance()); |
72 | 105 |
|
73 | | - return pattern.matcher(literal.stringValue()).matches(); |
| 106 | + if (literal == null) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + if (QueryEvaluationUtility.isStringLiteral(literal)) { |
| 111 | + boolean result = pattern.matcher(((Literal) literal).getLabel()).find(); |
| 112 | + if (logger.isTraceEnabled()) { |
| 113 | + logger.trace("PatternFilter value: \"{}\" with pattern: \"{}\" and result: {}", |
| 114 | + ((Literal) literal).getLabel().replace("\n", "\\n").replace("\"", "\\\""), |
| 115 | + pattern.toString().replace("\n", "\\n").replace("\"", "\\\""), result); |
| 116 | + } |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + if (logger.isTraceEnabled()) { |
| 121 | + logger.trace("PatternFilter did not match value because value is not a string literal: {}", literal); |
| 122 | + } |
| 123 | + return false; |
74 | 124 | } |
75 | 125 |
|
76 | 126 | @Override |
|
0 commit comments