All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mybatis.generator.plugins.RenameExampleClassPlugin Maven / Gradle / Ivy

The newest version!
/*
 *  Copyright 2008 The Apache Software Foundation
 *
 *  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:
 * 
    *
  • searchString (required) the regular expression of the name * search.
  • *
  • replaceString (required) the replacement String.
  • *
* * For example, to change the name of the generated Example classes from * xxxExample to xxxCriteria, specify the following: * *
*
searchString
*
Example$
*
replaceString
*
Criteria
*
* * * @author Jeff Butler * */ public class RenameExampleClassPlugin extends PluginAdapter { private String searchString; private String replaceString; private Pattern pattern; /** * */ public RenameExampleClassPlugin() { } public boolean validate(List 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); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy