View Javadoc
1   /*
2    *    Copyright 2006-2023 the original author or authors.
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    *       https://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 org.mybatis.generator.config;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.regex.Pattern;
24  
25  public class IgnoredColumnPattern {
26  
27      private final String patternRegex;
28      private final Pattern pattern;
29      private final List<IgnoredColumnException> exceptions = new ArrayList<>();
30  
31      public IgnoredColumnPattern(String patternRegex) {
32          this.patternRegex = patternRegex;
33          pattern = Pattern.compile(patternRegex);
34      }
35  
36      public void addException(IgnoredColumnException exception) {
37          exceptions.add(exception);
38      }
39  
40      public boolean matches(String columnName) {
41          boolean matches = pattern.matcher(columnName).matches();
42  
43          if (matches) {
44              for (IgnoredColumnException exception : exceptions) {
45                  if (exception.matches(columnName)) {
46                      matches = false;
47                      break;
48                  }
49              }
50          }
51  
52          return matches;
53      }
54  
55      public void validate(List<String> errors, String tableName) {
56          if (!stringHasValue(patternRegex)) {
57              errors.add(getString("ValidationError.27", //$NON-NLS-1$
58                      tableName));
59          }
60      }
61  }