• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.loganalysis.util;
17 
18 import java.util.HashSet;
19 import java.util.Set;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22 
23 /**
24  * A utility class for matching a message against a set of patterns.
25  * <p>
26  * This is used to match a message against a set of patterns, and optionally, an extra object. If
27  * the message is matched, a category will be returned. This means that a single object can be used
28  * to match many different categories.
29  */
30 public class LogPatternUtil {
31 
32     /**
33      * A class used to store pattern, extras, and category.
34      */
35     private class PatternInfo {
36         public Pattern mPattern;
37         public Object mExtras;
38         public String mCategory;
39 
40         /**
41          * Constructor for {@link PatternInfo}
42          *
43          * @param pattern the {@link Pattern} to match against.
44          * @param extras the {@link Object} to additionally match against.  If extras is null, it
45          * will be treated as wildcard
46          * @param category the category to return if there is a match.
47          */
PatternInfo(Pattern pattern, Object extras, String category)48         public PatternInfo(Pattern pattern, Object extras, String category) {
49             mPattern = pattern;
50             mExtras = extras;
51             mCategory = category;
52         }
53     }
54 
55     private Set<PatternInfo> mPatterns = new HashSet<PatternInfo>();
56 
57     /**
58      * Add a pattern to this list of patterns to match against.
59      *
60      * @param pattern the {@link Pattern} object to match against.
61      * @param category the category to return if there is a match.
62      */
addPattern(Pattern pattern, String category)63     public void addPattern(Pattern pattern, String category) {
64         addPattern(pattern, null, category);
65     }
66 
67     /**
68      * Add a pattern to this list of patterns to match against.
69      *
70      * @param pattern the {@link Pattern} to match against.
71      * @param extras the {@link Object} to additionally match against.  If extras is null, it will
72      * be treated as wildcard
73      * @param category the category to return if there is a match.
74      */
addPattern(Pattern pattern, Object extras, String category)75     public void addPattern(Pattern pattern, Object extras, String category) {
76         mPatterns.add(new PatternInfo(pattern, extras, category));
77     }
78 
79     /**
80      * Checks to see if the message matches any patterns.
81      *
82      * @param message the message to match against
83      * @return The category of the match.
84      */
checkMessage(String message)85     public String checkMessage(String message) {
86         return checkMessage(message, null);
87     }
88 
89     /**
90      * Checks to see if the message matches any patterns.
91      *
92      * @param message the message to match against
93      * @param extras the extras to match against
94      * @return The category of the match.
95      */
checkMessage(String message, Object extras)96     public String checkMessage(String message, Object extras) {
97         for (PatternInfo patternInfo : mPatterns) {
98             Matcher m = patternInfo.mPattern.matcher(message);
99 
100             // Return the category if the pattern matches and the extras are equal. Treat a null
101             // patternInfo.mExtras as a wildcard.
102             if (m.matches() &&
103                     (patternInfo.mExtras == null || patternInfo.mExtras.equals(extras))) {
104                 return patternInfo.mCategory;
105             }
106         }
107         return null;
108     }
109 }
110