Repository navigation
Expand file tree
/
Copy pathHFilter.java
More file actions
565 lines (480 loc) · 16.6 KB
/
Copy pathHFilter.java
File metadata and controls
565 lines (480 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//
// Copyright (c) 2011, Brian Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 04 Oct 2011 Brian Frank Creation
//
package org.projecthaystack;
import java.io.StringReader;
import java.util.*;
import org.projecthaystack.io.HZincReader;
import org.projecthaystack.io.HaystackToken;
import org.projecthaystack.io.HaystackTokenizer;
/**
* HFilter models a parsed tag query string.
*
* @see <a href='http://project-haystack.org/doc/Filters'>Project Haystack</a>
*/
public abstract class HFilter
{
//////////////////////////////////////////////////////////////////////////
// Encoding
//////////////////////////////////////////////////////////////////////////
/** Convenience for "make(s, true)" */
public static HFilter make(String s) { return make(s, true); }
/** Decode a string into a HFilter; return null or throw
ParseException if not formatted correctly */
public static HFilter make(String s, boolean checked)
{
try
{
return new FilterParser(s).parse();
}
catch (Exception e)
{
if (!checked) return null;
if (e instanceof ParseException) throw (ParseException)e;
throw new ParseException(s, e);
}
}
//////////////////////////////////////////////////////////////////////////
// Factories
//////////////////////////////////////////////////////////////////////////
/**
* Match records which have the specified tag path defined.
*/
public static HFilter has(String path) { return new Has(Path.make(path)); }
/**
* Match records which do not define the specified tag path.
*/
public static HFilter missing(String path) { return new Missing(Path.make(path)); }
/**
* Match records which have a tag are equal to the specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter eq(String path, HVal val) { return new Eq(Path.make(path), val); }
/**
* Match records which have a tag not equal to the specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter ne(String path, HVal val) { return new Ne(Path.make(path), val); }
/**
* Match records which have tags less than the specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter lt(String path, HVal val) { return new Lt(Path.make(path), val); }
/**
* Match records which have tags less than or equals to specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter le(String path, HVal val) { return new Le(Path.make(path), val); }
/**
* Match records which have tags greater than specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter gt(String path, HVal val) { return new Gt(Path.make(path), val); }
/**
* Match records which have tags greater than or equal to specified value.
* If the path is not defined then it is unmatched.
*/
public static HFilter ge(String path, HVal val) { return new Ge(Path.make(path), val); }
/**
* Return a query which is the logical-and of this and that query.
*/
public HFilter and(HFilter that) { return new And(this, that); }
/**
* Return a query which is the logical-or of this and that query.
*/
public HFilter or(HFilter that) { return new Or(this, that); }
//////////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////////
/** Package private constructor subclasses */
HFilter() {}
//////////////////////////////////////////////////////////////////////////
// Access
//////////////////////////////////////////////////////////////////////////
/* Return if given tags entity matches this query. */
public abstract boolean include(HDict dict, Pather pather);
/** String encoding */
public final String toString()
{
if (string == null) string = toStr();
return string;
}
private String string;
/* Used to lazily build toString */
abstract String toStr();
/** Hash code is based on string encoding */
public final int hashCode() { return toString().hashCode(); }
/** Equality is based on string encoding */
public final boolean equals(Object that)
{
if (!(that instanceof HFilter)) return false;
return toString().equals(that.toString());
}
//////////////////////////////////////////////////////////////////////////
// HFilter.Path
//////////////////////////////////////////////////////////////////////////
/** Pather is a callback interface used to resolve query paths. */
public interface Pather
{
/**
* Given a HRef string identifier, resolve to an entity's
* HDict respresentation or ref is not found return null.
*/
public HDict find(String ref);
}
//////////////////////////////////////////////////////////////////////////
// HFilter.Path
//////////////////////////////////////////////////////////////////////////
/** Path is a simple name or a complex path using the "->" separator */
static abstract class Path
{
/** Construct a new Path from string or throw ParseException */
public static Path make(String path)
{
try
{
// optimize for common single name case
int dash = path.indexOf('-');
if (dash < 0) return new Path1(path);
// parse
int s = 0;
ArrayList acc = new ArrayList();
while (true)
{
String n = path.substring(s, dash);
if (n.length() == 0) throw new Exception();
acc.add(n);
if (path.charAt(dash+1) != '>') throw new Exception();
s = dash+2;
dash = path.indexOf('-', s);
if (dash < 0)
{
n = path.substring(s, path.length());
if (n.length() == 0) throw new Exception();
acc.add(n);
break;
}
}
return new PathN(path, (String[])acc.toArray(new String[acc.size()]));
}
catch (Exception e) {}
throw new ParseException("Path: " + path);
}
/** Number of names in the path. */
public abstract int size();
/** Get name at given index. */
public abstract String get(int i);
/** Hashcode is based on string. */
public int hashCode() { return toString().hashCode(); }
/** Equality is based on string. */
public boolean equals(Object that) { return toString().equals(that.toString()); }
/** Get string encoding. */
public abstract String toString();
}
static final class Path1 extends Path
{
Path1(String n) { this.name = n; }
public int size() { return 1; }
public String get(int i) { if (i == 0) return name; throw new IndexOutOfBoundsException(""+i); }
public String toString() { return name; }
private final String name;
}
static final class PathN extends Path
{
PathN(String s, String[] n) { this.string = s; this.names = n; }
public int size() { return names.length; }
public String get(int i) { return names[i]; }
public String toString() { return string; }
private final String string;
private final String[] names;
}
//////////////////////////////////////////////////////////////////////////
// PathFilter
//////////////////////////////////////////////////////////////////////////
static abstract class PathFilter extends HFilter
{
PathFilter(Path p) { path = p; }
public final boolean include(HDict dict, Pather pather)
{
HVal val = dict.get(path.get(0), false);
if (path.size() != 1)
{
if (pather == null) val = null;
else
{
HDict nt = dict;
for (int i=1; i<path.size(); ++i)
{
if (val instanceof HDict)
nt = (HDict)val;
else if (val instanceof HRef)
nt = pather.find(((HRef)val).val);
else { val = null; break; }
val = nt.get(path.get(i), false);
}
}
}
return doInclude(val);
}
abstract boolean doInclude(HVal val);
final Path path;
}
//////////////////////////////////////////////////////////////////////////
// Has
//////////////////////////////////////////////////////////////////////////
static final class Has extends PathFilter
{
Has(Path p) { super(p); }
final boolean doInclude(HVal v) { return v != null; }
final String toStr() { return path.toString(); }
}
//////////////////////////////////////////////////////////////////////////
// Missing
//////////////////////////////////////////////////////////////////////////
static final class Missing extends PathFilter
{
Missing(Path p) { super(p); }
final boolean doInclude(HVal v) { return v == null; }
final String toStr() { return "not " + path; }
}
//////////////////////////////////////////////////////////////////////////
// CmpFilter
//////////////////////////////////////////////////////////////////////////
static abstract class CmpFilter extends PathFilter
{
CmpFilter(Path p, HVal val) { super(p); this.val = val; }
final String toStr()
{
StringBuffer s = new StringBuffer();
s.append(path).append(cmpStr()).append(val.toZinc());
return s.toString();
}
final boolean sameType(HVal v) { return v != null && v.getClass() == val.getClass(); }
abstract String cmpStr();
final HVal val;
}
//////////////////////////////////////////////////////////////////////////
// Eq
//////////////////////////////////////////////////////////////////////////
static final class Eq extends CmpFilter
{
Eq(Path p, HVal v) { super(p, v); }
final String cmpStr() { return "=="; }
final boolean doInclude(HVal v) { return v != null && v.equals(val); }
}
//////////////////////////////////////////////////////////////////////////
// Ne
//////////////////////////////////////////////////////////////////////////
static final class Ne extends CmpFilter
{
Ne(Path p, HVal v) { super(p, v); }
final String cmpStr() { return "!="; }
final boolean doInclude(HVal v) { return v != null && !v.equals(val); }
}
//////////////////////////////////////////////////////////////////////////
// Lt
//////////////////////////////////////////////////////////////////////////
static final class Lt extends CmpFilter
{
Lt(Path p, HVal v) { super(p, v); }
final String cmpStr() { return "<"; }
final boolean doInclude(HVal v) { return sameType(v) && v.compareTo(val) < 0; }
}
//////////////////////////////////////////////////////////////////////////
// Le
//////////////////////////////////////////////////////////////////////////
static final class Le extends CmpFilter
{
Le(Path p, HVal v) { super(p, v); }
final String cmpStr() { return "<="; }
final boolean doInclude(HVal v) { return sameType(v) && v.compareTo(val) <= 0; }
}
//////////////////////////////////////////////////////////////////////////
// Gt
//////////////////////////////////////////////////////////////////////////
static final class Gt extends CmpFilter
{
Gt(Path p, HVal v) { super(p, v); }
final String cmpStr() { return ">"; }
final boolean doInclude(HVal v) { return sameType(v) && v.compareTo(val) > 0; }
}
//////////////////////////////////////////////////////////////////////////
// Ge
//////////////////////////////////////////////////////////////////////////
static final class Ge extends CmpFilter
{
Ge(Path p, HVal v) { super(p, v); }
final String cmpStr() { return ">="; }
final boolean doInclude(HVal v) { return sameType(v) && v.compareTo(val) >= 0; }
}
//////////////////////////////////////////////////////////////////////////
// Compound
//////////////////////////////////////////////////////////////////////////
static abstract class CompoundFilter extends HFilter
{
CompoundFilter(HFilter a, HFilter b) { this.a = a; this.b = b; }
abstract String keyword();
final String toStr()
{
boolean deep = a instanceof CompoundFilter || b instanceof CompoundFilter;
StringBuffer s = new StringBuffer();
if (a instanceof CompoundFilter) s.append('(').append(a).append(')');
else s.append(a);
s.append(' ').append(keyword()).append(' ');
if (b instanceof CompoundFilter) s.append('(').append(b).append(')');
else s.append(b);
return s.toString();
}
final HFilter a;
final HFilter b;
}
//////////////////////////////////////////////////////////////////////////
// And
//////////////////////////////////////////////////////////////////////////
static final class And extends CompoundFilter
{
And(HFilter a, HFilter b) { super(a, b); }
final String keyword() { return "and"; }
public final boolean include(HDict dict, Pather pather)
{
return a.include(dict, pather) && b.include(dict, pather);
}
}
//////////////////////////////////////////////////////////////////////////
// Or
//////////////////////////////////////////////////////////////////////////
static final class Or extends CompoundFilter
{
Or(HFilter a, HFilter b) { super(a, b); }
final String keyword() { return "or"; }
public final boolean include(HDict dict, Pather pather)
{
return a.include(dict, pather) || b.include(dict, pather);
}
}
//////////////////////////////////////////////////////////////////////////
// FilterParser
//////////////////////////////////////////////////////////////////////////
static final class FilterParser
{
public FilterParser(String in)
{
this.tokenizer = new HaystackTokenizer(new StringReader(in));
consume();
consume();
}
public HFilter parse()
{
HFilter f = condOr();
verify(HaystackToken.eof);
return f;
}
private HFilter condOr()
{
HFilter lhs = condAnd();
if (!isKeyword("or")) return lhs;
consume();
return lhs.or(condOr());
}
private HFilter condAnd()
{
HFilter lhs = term();
if (!isKeyword("and")) return lhs;
consume();
return lhs.and(condAnd());
}
private HFilter term()
{
if (cur == HaystackToken.lparen)
{
consume();
HFilter f = condOr();
consume(HaystackToken.rparen);
return f;
}
if (isKeyword("not") && peek == HaystackToken.id)
{
consume();
return new Missing(path());
}
Path p = path();
if (cur == HaystackToken.eq){ consume(); return new Eq(p, val()); }
if (cur == HaystackToken.notEq) { consume(); return new Ne(p, val()); }
if (cur == HaystackToken.lt) { consume(); return new Lt(p, val()); }
if (cur == HaystackToken.ltEq) { consume(); return new Le(p ,val()); }
if (cur == HaystackToken.gt) { consume(); return new Gt(p, val()); }
if (cur == HaystackToken.gtEq) { consume(); return new Ge(p, val()); }
return new Has(p);
}
private Path path()
{
String id = pathName();
if (cur != HaystackToken.arrow)
return Path1.make(id);
List segments = new ArrayList();
segments.add(id);
StringBuffer s = new StringBuffer().append(id);
while (cur == HaystackToken.arrow)
{
consume(HaystackToken.arrow);
id = pathName();
segments.add(id);
s.append(HaystackToken.arrow).append(id);
}
return new PathN(s.toString(), (String[])segments.toArray(new String[segments.size()]));
}
private String pathName()
{
if (cur != HaystackToken.id) throw err("Expecting tag name, not " + curToStr());
String id = (String)curVal;
consume();
return id;
}
private HVal val()
{
if (cur.literal)
{
HVal val = (HVal)curVal;
consume();
return val;
}
if (cur == HaystackToken.id)
{
if ("true".equals(curVal)) { consume(); return HBool.TRUE; }
if ("false".equals(curVal)) { consume(); return HBool.FALSE; }
}
throw err("Expecting value literal, not " + curToStr());
}
private boolean isKeyword(String n)
{
return cur == HaystackToken.id && n.equals(curVal);
}
private void verify(HaystackToken expected)
{
if (cur != expected) throw err("Expected " + expected + " not " + curToStr());
}
private String curToStr()
{
return curVal != null ? "" + cur + " " + curVal : cur.toString();
}
private void consume() { consume(null); }
private void consume(HaystackToken expected)
{
if (expected != null) verify(expected);
cur = peek;
curVal = peekVal;
peek = tokenizer.next();
peekVal = tokenizer.val;
}
private ParseException err(String msg) { return new ParseException(msg); }
private HaystackTokenizer tokenizer;
private HaystackToken cur;
private Object curVal;
private HaystackToken peek;
private Object peekVal;
}
}