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.List;
22  
23  /**
24   * This class is used to specify a renaming rule for columns in a table. This
25   * renaming rule will be run against all column names before calculating the
26   * corresponding property name. The most common use case is when columns in a
27   * table are all prefixed by a certain value.
28   *
29   * <p>For example, if columns in a table are named:
30   *
31   * <ul>
32   * <li>CUST_NAME</li>
33   * <li>CUST_ADDRESS</li>
34   * <li>CUST_CITY</li>
35   * <li>CUST_STATE</li>
36   * </ul>
37   *
38   * <p>it might be annoying to have the generated properties all containing the CUST
39   * prefix. This class can be used to remove the prefix by specifying
40   *
41   * <ul>
42   * <li>searchString = "^CUST"</li>
43   * <li>replaceString=""</li>
44   * </ul>
45   *
46   * <p>Note that internally, the generator uses the
47   * <code>java.util.regex.Matcher.replaceAll</code> method for this function. See
48   * the documentation of that method for example of the regular expression
49   * language used in Java.
50   *
51   * @author Jeff Butler
52   *
53   */
54  public class ColumnRenamingRule {
55      private String searchString;
56      private String replaceString;
57  
58      public String getReplaceString() {
59          return replaceString;
60      }
61  
62      public void setReplaceString(String replaceString) {
63          this.replaceString = replaceString;
64      }
65  
66      public String getSearchString() {
67          return searchString;
68      }
69  
70      public void setSearchString(String searchString) {
71          this.searchString = searchString;
72      }
73  
74      public void validate(List<String> errors, String tableName) {
75          if (!stringHasValue(searchString)) {
76              errors.add(getString("ValidationError.14", tableName)); //$NON-NLS-1$
77          }
78      }
79  }