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.plugins;
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  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  import org.mybatis.generator.api.PluginAdapter;
26  import org.mybatis.generator.api.IntrospectedTable;
27  
28  /**
29   * This plugin demonstrates overriding the initialized() method to rename the
30   * generated example classes. Instead of xxxExample, the classes will be named
31   * xxxCriteria
32   * 
33   * This plugin accepts two properties:
34   * <ul>
35   * <li><tt>searchString</tt> (required) the regular expression of the name
36   * search.</li>
37   * <li><tt>replaceString</tt> (required) the replacement String.</li>
38   * </ul>
39   * 
40   * For example, to change the name of the generated Example classes from
41   * xxxExample to xxxCriteria, specify the following:
42   * 
43   * <dl>
44   * <dt>searchString</dt>
45   * <dd>Example$</dd>
46   * <dt>replaceString</dt>
47   * <dd>Criteria</dd>
48   * </dl>
49   * 
50   * 
51   * @author Jeff Butler
52   * 
53   */
54  public class RenameExampleClassPlugin extends PluginAdapter {
55      private String searchString;
56      private String replaceString;
57      private Pattern pattern;
58  
59      /**
60       * 
61       */
62      public RenameExampleClassPlugin() {
63      }
64  
65      public boolean validate(List<String> warnings) {
66  
67          searchString = properties.getProperty("searchString"); //$NON-NLS-1$
68          replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$
69  
70          boolean valid = stringHasValue(searchString)
71                  && stringHasValue(replaceString);
72  
73          if (valid) {
74              pattern = Pattern.compile(searchString);
75          } else {
76              if (!stringHasValue(searchString)) {
77                  warnings.add(getString("ValidationError.18", //$NON-NLS-1$
78                          "RenameExampleClassPlugin", //$NON-NLS-1$
79                          "searchString")); //$NON-NLS-1$
80              }
81              if (!stringHasValue(replaceString)) {
82                  warnings.add(getString("ValidationError.18", //$NON-NLS-1$
83                          "RenameExampleClassPlugin", //$NON-NLS-1$
84                          "replaceString")); //$NON-NLS-1$
85              }
86          }
87  
88          return valid;
89      }
90  
91      @Override
92      public void initialized(IntrospectedTable introspectedTable) {
93          String oldType = introspectedTable.getExampleType();
94          Matcher matcher = pattern.matcher(oldType);
95          oldType = matcher.replaceAll(replaceString);
96  
97          introspectedTable.setExampleType(oldType);
98      }
99  }