Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PatternMatcher.java
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.util;
16 
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 
22 import com.google.common.collect.Lists;
23 
29 public class PatternMatcher {
30  // Patterns to match against. A string is considered to match if it matches
31  // any of the patterns.
32  private List<Pattern> patterns_;
33 
34  // Returns true if the candidate matches.
35  public boolean matches(String candidate) {
36  if (patterns_ == null) return true;
37  if (patterns_.isEmpty()) return false;
38  for (Pattern pattern: patterns_) {
39  if (pattern.matcher(candidate).matches()) return true;
40  }
41  return false;
42  }
43 
51  public static PatternMatcher createHivePatternMatcher(String hivePattern) {
52  PatternMatcher result = new PatternMatcher();
53  if (hivePattern != null) {
54  result.patterns_ = Lists.newArrayList();
55  // Hive ignores pretty much all metacharacters, so we have to escape them.
56  final String metaCharacters = "+?.^()]\\/{}";
57  final Pattern regex = Pattern.compile("([" + Pattern.quote(metaCharacters) + "])");
58 
59  for (String pattern: Arrays.asList(hivePattern.split("\\|"))) {
60  Matcher matcher = regex.matcher(pattern);
61  pattern = matcher.replaceAll("\\\\$1").replace("*", ".*");
62  result.patterns_.add(Pattern.compile(pattern));
63  }
64  }
65  return result;
66  }
67 
71  public static PatternMatcher createJdbcPatternMatcher(String pattern) {
72  String wildcardPattern = ".*";
73  String workPattern = pattern;
74  if (workPattern == null || pattern.isEmpty()) {
75  workPattern = "%";
76  }
77  String result = workPattern
78  .replaceAll("([^\\\\])%", "$1" + wildcardPattern)
79  .replaceAll("\\\\%", "%")
80  .replaceAll("^%", wildcardPattern)
81  .replaceAll("([^\\\\])_", "$1.")
82  .replaceAll("\\\\_", "_")
83  .replaceAll("^_", ".");
84  PatternMatcher matcher = new PatternMatcher();
85  matcher.patterns_ = Lists.newArrayList();
86  matcher.patterns_.add(Pattern.compile(result));
87  return matcher;
88  }
89 }
static PatternMatcher createJdbcPatternMatcher(String pattern)
static PatternMatcher createHivePatternMatcher(String hivePattern)