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