1 | /*
|
---|
2 | * Created on Nov 1, 2004
|
---|
3 | * Copyright (C) Andrea Schweer, 2004
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas.profiles;
|
---|
10 |
|
---|
11 | import java.util.*;
|
---|
12 | import java.util.Set;
|
---|
13 | import java.util.TreeSet;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * @author schweer
|
---|
17 | *
|
---|
18 | * TODO To change the template for this generated type comment go to Window -
|
---|
19 | * Preferences - Java - Code Style - Code Templates
|
---|
20 | */
|
---|
21 | public class QueryPredicate extends Predicate {
|
---|
22 |
|
---|
23 | private Set containsWords = new TreeSet();
|
---|
24 | private Set notContainsWords = new TreeSet();
|
---|
25 |
|
---|
26 | public QueryPredicate (String field, String query) {
|
---|
27 | super.field = field;
|
---|
28 | super.value = query;
|
---|
29 |
|
---|
30 | String[] words = query.split("\\s");
|
---|
31 | for (int i = 0; i < words.length; i++) {
|
---|
32 | String word = words[i];
|
---|
33 | if (!word.startsWith("!")) {
|
---|
34 | containsWords.add(word);
|
---|
35 | } else {
|
---|
36 | if (word.length() > 1) {
|
---|
37 | notContainsWords.add(word);
|
---|
38 | } else if (i < words.length - 1) {
|
---|
39 | notContainsWords.add(words[i++]);
|
---|
40 | }
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /* (non-Javadoc)
|
---|
46 | * @see org.greenstone.gsdlas.profiles.Predicate#isSatisfied(java.util.Map)
|
---|
47 | */
|
---|
48 | public boolean isSatisfied(Map event) {
|
---|
49 | // TODO more efficient implementation
|
---|
50 |
|
---|
51 | String value = (String) event.get(field);
|
---|
52 | if (value == null) return false;
|
---|
53 |
|
---|
54 |
|
---|
55 | for (Iterator iter = containsWords.iterator(); iter.hasNext();) {
|
---|
56 | String word = (String) iter.next();
|
---|
57 | if (value.indexOf(word) == -1) {
|
---|
58 | return false;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | for (Iterator iter = notContainsWords.iterator(); iter.hasNext();) {
|
---|
63 | String word = (String) iter.next();
|
---|
64 | if (value.indexOf(word) >= 0) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 | } |
---|