View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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   *****************************************************************************/
17  package org.settings4j.settings;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.regex.Pattern;
22  
23  import org.settings4j.Filter;
24  
25  /**
26   * The Default Implementation uses the RegEx-{@link Pattern} Expressions to evaluate the includes and excludes.
27   * 
28   * @author Harald.Brabenetz
29   */
30  public class DefaultFilter implements Filter {
31  
32      /** General Logger for this Class. */
33      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(DefaultFilter.class);
34  
35      private final List<Pattern> includePatterns = new ArrayList<Pattern>();
36      private final List<Pattern> excludePatterns = new ArrayList<Pattern>();
37  
38      /** {@inheritDoc} */
39      public void addExclude(final String pattern) {
40          try {
41              final Pattern p = Pattern.compile(pattern);
42              this.excludePatterns.add(p);
43          } catch (final Exception e) {
44              LOG.warn("cannnot compile Pattern-String '" + pattern + "'" + e.getMessage(), e);
45          }
46      }
47  
48      /** {@inheritDoc} */
49      public void addInclude(final String pattern) {
50          try {
51              final Pattern p = Pattern.compile(pattern);
52              this.includePatterns.add(p);
53          } catch (final Exception e) {
54              LOG.warn("cannnot compile Pattern-String '" + pattern + "'" + e.getMessage(), e);
55          }
56      }
57  
58      /** {@inheritDoc} */
59      public boolean isValid(final String key) {
60          // if exclude match, return always false.
61          for (final Pattern pattern : this.excludePatterns) {
62              if (pattern.matcher(key).matches()) {
63                  return false;
64              }
65          }
66  
67          // if no include is defined, return always true
68          if (this.includePatterns.isEmpty()) {
69              return true;
70          }
71  
72          // if include match, return true.
73          for (final Pattern pattern : this.includePatterns) {
74              if (pattern.matcher(key).matches()) {
75                  return true;
76              }
77          }
78  
79          // no includePattern matched.
80          return false;
81      }
82  
83  }