]> git.basschouten.com Git - openhab-addons.git/blob
2af9a97713e36fab892db18ab983e873ae4472ae
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.logreader.internal.searchengine;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import java.util.regex.PatternSyntaxException;
20
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * This class implements logic for regular expression based searching.
25  *
26  * @author Pauli Anttila - Initial contribution
27  */
28 public class SearchEngine {
29
30     private List<Pattern> matchers;
31     private List<Pattern> blacklistingMatchers;
32
33     private long matchCount;
34
35     /**
36      * Initialize search patterns.
37      *
38      * @param patterns search patterns.
39      * @param blacklistingPatterns search patterns to bypass results which have found by the initial search patterns.
40      *
41      */
42     public SearchEngine(String patterns, String blacklistingPatterns) throws PatternSyntaxException {
43         matchers = compilePatterns(patterns);
44         blacklistingMatchers = compilePatterns(blacklistingPatterns);
45     }
46
47     /**
48      * Check if data is matching to one of the provided search patterns.
49      *
50      * @param data data against search will be done.
51      * @return true if one of the search patterns found.
52      */
53     public boolean isMatching(String data) {
54         if (isMatching(matchers, data)) {
55             if (notBlacklisted(data)) {
56                 matchCount++;
57                 return true;
58             }
59         }
60         return false;
61     }
62
63     public long getMatchCount() {
64         return matchCount;
65     }
66
67     public void setMatchCount(long matchCount) {
68         this.matchCount = matchCount;
69     }
70
71     public void clearMatchCount() {
72         setMatchCount(0);
73     }
74
75     /**
76      * Split pattern string and precompile search patterns.
77      *
78      * @param patterns patterns which will handled.
79      * @return list of precompiled patterns. If pattern parameter is null, empty list is returned.
80      */
81     private List<Pattern> compilePatterns(@Nullable String patterns) throws PatternSyntaxException {
82         List<Pattern> patternsList = new ArrayList<>();
83
84         if (patterns != null && !patterns.isEmpty()) {
85             String list[] = patterns.split("\\|");
86             if (list.length > 0) {
87                 for (String patternStr : list) {
88                     patternsList.add(Pattern.compile(patternStr));
89                 }
90             }
91         }
92         return patternsList;
93     }
94
95     private boolean notBlacklisted(String data) {
96         return !isMatching(blacklistingMatchers, data);
97     }
98
99     private boolean isMatching(@Nullable List<Pattern> patterns, String data) {
100         if (patterns != null) {
101             for (Pattern pattern : patterns) {
102                 Matcher matcher = pattern.matcher(data);
103                 if (matcher.find()) {
104                     return true;
105                 }
106             }
107         }
108         return false;
109     }
110 }