]> git.basschouten.com Git - openhab-addons.git/blob
b4ba401e7049defc8e5846159c8b7722661f4281
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * This class implements logic for regular expression based searching.
26  *
27  * @author Pauli Anttila - Initial contribution
28  */
29 @NonNullByDefault
30 public class SearchEngine {
31
32     private List<Pattern> matchers;
33     private List<Pattern> blacklistingMatchers;
34
35     private long matchCount;
36
37     /**
38      * Initialize search patterns.
39      *
40      * @param patterns search patterns.
41      * @param blacklistingPatterns search patterns to bypass results which have found by the initial search patterns.
42      *
43      */
44     public SearchEngine(String patterns, @Nullable String blacklistingPatterns) throws PatternSyntaxException {
45         matchers = compilePatterns(patterns);
46         blacklistingMatchers = compilePatterns(blacklistingPatterns);
47     }
48
49     /**
50      * Check if data is matching to one of the provided search patterns.
51      *
52      * @param data data against search will be done.
53      * @return true if one of the search patterns found.
54      */
55     public boolean isMatching(String data) {
56         if (isMatching(matchers, data)) {
57             if (notBlacklisted(data)) {
58                 matchCount++;
59                 return true;
60             }
61         }
62         return false;
63     }
64
65     public long getMatchCount() {
66         return matchCount;
67     }
68
69     public void setMatchCount(long matchCount) {
70         this.matchCount = matchCount;
71     }
72
73     public void clearMatchCount() {
74         setMatchCount(0);
75     }
76
77     /**
78      * Split pattern string and precompile search patterns.
79      *
80      * @param patterns patterns which will handled.
81      * @return list of precompiled patterns. If pattern parameter is null, empty list is returned.
82      */
83     private List<Pattern> compilePatterns(@Nullable String patterns) throws PatternSyntaxException {
84         List<Pattern> patternsList = new ArrayList<>();
85         if (patterns != null && !patterns.isEmpty()) {
86             String[] list = patterns.split("\\|");
87             if (list.length > 0) {
88                 for (String patternStr : list) {
89                     patternsList.add(Pattern.compile(patternStr));
90                 }
91             }
92         }
93         return patternsList;
94     }
95
96     private boolean notBlacklisted(String data) {
97         return !isMatching(blacklistingMatchers, data);
98     }
99
100     private boolean isMatching(@Nullable List<Pattern> patterns, String data) {
101         if (patterns != null) {
102             for (Pattern pattern : patterns) {
103                 Matcher matcher = pattern.matcher(data);
104                 if (matcher.find()) {
105                     return true;
106                 }
107             }
108         }
109         return false;
110     }
111 }