/** * Copyright 2006-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.mybatis.generator.plugins; import static org.mybatis.generator.internal.util.StringUtility.stringHasValue; import static org.mybatis.generator.internal.util.messages.Messages.getString; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.IntrospectedTable; /** * This plugin demonstrates overriding the initialized() method to rename the * generated example classes. Instead of xxxExample, the classes will be named * xxxCriteria * * This plugin accepts two properties: * <ul> * <li><tt>searchString</tt> (required) the regular expression of the name * search.</li> * <li><tt>replaceString</tt> (required) the replacement String.</li> * </ul> * * For example, to change the name of the generated Example classes from * xxxExample to xxxCriteria, specify the following: * * <dl> * <dt>searchString</dt> * <dd>Example$</dd> * <dt>replaceString</dt> * <dd>Criteria</dd> * </dl> * * * @author Jeff Butler * */ public class RenameExampleClassPlugin extends PluginAdapter { private String searchString; private String replaceString; private Pattern pattern; /** * */ public RenameExampleClassPlugin() { } public boolean validate(List<String> warnings) { searchString = properties.getProperty("searchString"); //$NON-NLS-1$ replaceString = properties.getProperty("replaceString"); //$NON-NLS-1$ boolean valid = stringHasValue(searchString) && stringHasValue(replaceString); if (valid) { pattern = Pattern.compile(searchString); } else { if (!stringHasValue(searchString)) { warnings.add(getString("ValidationError.18", //$NON-NLS-1$ "RenameExampleClassPlugin", //$NON-NLS-1$ "searchString")); //$NON-NLS-1$ } if (!stringHasValue(replaceString)) { warnings.add(getString("ValidationError.18", //$NON-NLS-1$ "RenameExampleClassPlugin", //$NON-NLS-1$ "replaceString")); //$NON-NLS-1$ } } return valid; } @Override public void initialized(IntrospectedTable introspectedTable) { String oldType = introspectedTable.getExampleType(); Matcher matcher = pattern.matcher(oldType); oldType = matcher.replaceAll(replaceString); introspectedTable.setExampleType(oldType); } }