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