Author: jgbutler Date: Thu Dec 18 15:11:17 2008 New Revision: 727852 URL: http://svn.apache.org/viewvc?rev=727852&view=rev Log: [ibator] further improvements and documentation of the plugin API
Added: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/RenameExampleClassPlugin.java Removed: ibatis/trunk/java/tools/ibator/core/buildsupport/org/apache/ibatis/ibator/build/test/ Modified: ibatis/trunk/java/tools/ibator/core/build/build.xml ibatis/trunk/java/tools/ibator/core/build/version.properties ibatis/trunk/java/tools/ibator/core/doc/ReleaseNotes.txt ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/ibatorPlugin.html ibatis/trunk/java/tools/ibator/core/htmldoc/reference/pluggingIn.html ibatis/trunk/java/tools/ibator/core/htmldoc/whatsNew.html ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPlugin.java ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPluginAdapter.java ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IntrospectedTable.java ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/config/IbatorContext.java ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/IbatorPluginAggregator.java ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/util/messages/messages.properties ibatis/trunk/java/tools/ibator/core/testJava5/ibatortest/ibatorConfig.xml Modified: ibatis/trunk/java/tools/ibator/core/build/build.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/build/build.xml?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/build/build.xml (original) +++ ibatis/trunk/java/tools/ibator/core/build/build.xml Thu Dec 18 15:11:17 2008 @@ -41,7 +41,6 @@ <path id="emma.runtime.path"> <pathelement location="${work.instrumented}"/> <pathelement location="${work.classes}"/> - <pathelement location="${work.buildsupport}"/> <path refid="emma.lib"/> </path> Modified: ibatis/trunk/java/tools/ibator/core/build/version.properties URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/build/version.properties?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/build/version.properties (original) +++ ibatis/trunk/java/tools/ibator/core/build/version.properties Thu Dec 18 15:11:17 2008 @@ -1,4 +1,4 @@ #Ibator build version info -#Thu Dec 18 11:42:11 CST 2008 +#Thu Dec 18 16:54:10 CST 2008 version=1.2.1 -buildNum=673 +buildNum=678 Modified: ibatis/trunk/java/tools/ibator/core/doc/ReleaseNotes.txt URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/doc/ReleaseNotes.txt?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/doc/ReleaseNotes.txt (original) +++ ibatis/trunk/java/tools/ibator/core/doc/ReleaseNotes.txt Thu Dec 18 15:11:17 2008 @@ -13,6 +13,10 @@ 2. Added methods to IntrospectedTable to get/set attributes. This allows plugin classes to maintain table based state between plugin calls. +3. Added "initialized" method to the plugin API. This + allows plugins to alter some of the fundamental code generation items + (like the name of a generated class, for example). +4. Added an example plugin to show usage of the "initialized" method. ------------------------------------------------------------------------------- Version 1.2.0: Renamed to Ibator Modified: ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/ibatorPlugin.html URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/ibatorPlugin.html?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/ibatorPlugin.html (original) +++ ibatis/trunk/java/tools/ibator/core/htmldoc/configreference/ibatorPlugin.html Thu Dec 18 15:11:17 2008 @@ -53,6 +53,21 @@ dependancy in this simple plugin.</p></td> </tr> <tr> + <td valign="top"><code>(package).RenameExampleClassPlugin</code></td> + <td>This plugin demonstrates usage of the <code>initialized</code> method + by renaming the generated example classes generated by Ibator. + <p>This plugin accepts two properties:</p> + <ul> + <li><tt>searchString</tt> (required) a regular expression used to search + the default generated name of the example class.</li> + <li><tt>replaceString</tt> (required) the string to be inserted on matches of the + <tt>searchString</tt>.</li> + </ul> + <p>For example, to rename the generated example classes from xxxExample to + xxxCriteria, specify <tt>Example$</tt> for <tt>searchString</tt> + and <tt>Criteria</tt> for <tt>replaceString</tt></p></td> + </tr> + <tr> <td valign="top"><code>(package).SerializablePlugin</code></td> <td>This plugin adds the marker interface <code>java.io.Serializable</code> to the Java model objects generated by Ibator. This plugin also adds the Modified: ibatis/trunk/java/tools/ibator/core/htmldoc/reference/pluggingIn.html URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/htmldoc/reference/pluggingIn.html?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/htmldoc/reference/pluggingIn.html (original) +++ ibatis/trunk/java/tools/ibator/core/htmldoc/reference/pluggingIn.html Thu Dec 18 15:11:17 2008 @@ -25,6 +25,14 @@ class provides basic plugin support, and implements no-operation methods for most of the interface methods (similar to Swing adapter classes).</p> +<p>Ibator supplies several plugins (all are in the package +<code>org.apache.ibatis.ibator.plugins</code>). The supplied plugins demonstrate +different types of tasks that can be accomplished with Ibator plugins. Source +code for the plugins is available with the Ibator downloads, or can be viewed +online +<a target="_blank" href="http://svn.apache.org/repos/asf/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/"> +here</a>.</p> + <h2>Plugin Lifecycle</h2> <p>Plugins have a lifecycle. Plugins are created during the initialization of the code generation process and are called, in order, in different phases of the process. The following @@ -37,7 +45,7 @@ then no further methods in the plugin will be called</li> <li>For each table in the configuration: <ol type="a"> - <li><code>attributesCalculated</code> method called</li> + <li><code>initialized</code> method called</li> <li>DAO Methods:<sup>1,2</sup> <ol type="i"> <li><code>daoXXXMethodGenerated(Method, TopLevelClass, IntrospectedTable)</code> - these methods Modified: ibatis/trunk/java/tools/ibator/core/htmldoc/whatsNew.html URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/htmldoc/whatsNew.html?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/htmldoc/whatsNew.html (original) +++ ibatis/trunk/java/tools/ibator/core/htmldoc/whatsNew.html Thu Dec 18 15:11:17 2008 @@ -31,9 +31,11 @@ <li>Added methods to IntrospectedTable to get/set attributes. This allows plugin classes to maintain table based state between plugin calls.</li> - <li>Added <code>attributesCalculated</code> method to the plugin API. This + <li>Added <code>initialized</code> method to the plugin API. This allows plugins to alter some of the fundamental code generation items (like the name of a generated class, for example).</li> + <li>Added an example plugin to show usage of the <code>initialized</code> + method.</li> </ul> <h2>Version 1.2.0</h2> Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPlugin.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPlugin.java?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPlugin.java (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPlugin.java Thu Dec 18 15:11:17 2008 @@ -37,7 +37,7 @@ * <ol> * <li>The setXXX methods are called one time</li> * <li>The validate method is called one time</li> - * <li>The attributesCalculated method is called for each introspected table</li> + * <li>The initialized method is called for each introspected table</li> * <li>The daoXXX methods are called for each introspected table</li> * <li>The modelXXX methods are called for each introspected table</li> * <li>The sqlMapXXX methods are called for each introspected table</li> @@ -88,11 +88,12 @@ void setProperties(Properties properties); /** - * This method is called after the IntrospectedTable calculates all - * the attributes for code generation. Attributes are listed as - * static Strings with the prefix ATTR_ in IntrospectedTable. Plugins - * can implement this method to override any of the default attributes, - * or to add additional attributes for use by other plugin methods. + * This method is called just before the getGeneratedXXXFiles methods + * are called on the introspected table. Plugins can implement this + * method to override any of the default attributes, or change + * the results of database introspection, before any code + * generation activities occur. Attributes are listed as + * static Strings with the prefix ATTR_ in IntrospectedTable. * <p> * A good example of overriding an attribute would be the case where * a user wanted to change the name of one of the generated classes, @@ -107,7 +108,7 @@ * * @param introspectedTable */ - void attributesCalculated(IntrospectedTable introspectedTable); + void initialized(IntrospectedTable introspectedTable); /** * This method is called after all the setXXX methods are called, but before any Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPluginAdapter.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPluginAdapter.java?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPluginAdapter.java (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IbatorPluginAdapter.java Thu Dec 18 15:11:17 2008 @@ -304,6 +304,6 @@ return true; } - public void attributesCalculated(IntrospectedTable introspectedTable) { + public void initialized(IntrospectedTable introspectedTable) { } } Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IntrospectedTable.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IntrospectedTable.java?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IntrospectedTable.java (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/api/IntrospectedTable.java Thu Dec 18 15:11:17 2008 @@ -39,7 +39,7 @@ /** * Base class for all code generator implementations. - * This class provides many of the ibator housekeeping methods needed + * This class provides many of the Ibator housekeeping methods needed * to implement a code generator, with only the actual code generation * methods left unimplemented. * @@ -111,16 +111,14 @@ protected TableConfiguration tableConfiguration; protected FullyQualifiedTable fullyQualifiedTable; protected IbatorContext ibatorContext; - protected IbatorRules rules; + private IbatorRules rules; protected List<IntrospectedColumn> primaryKeyColumns; protected List<IntrospectedColumn> baseColumns; protected List<IntrospectedColumn> blobColumns; - protected boolean hasJDBCDateColumns; - protected boolean hasJDBCTimeColumns; /** * Attributes may be used by plugins to capture table related state - * between the different plugin calls. Attibutes also are used + * between the different plugin calls. Attributes also are used * to store commonly accessed items by all code generators */ protected Map<String, Object> attributes; @@ -203,7 +201,25 @@ * @return true if the table contains DATE columns */ public boolean hasJDBCDateColumns() { - return hasJDBCDateColumns; + boolean rc = false; + + for (IntrospectedColumn introspectedColumn : primaryKeyColumns) { + if (introspectedColumn.isJDBCDateColumn()) { + rc = true; + break; + } + } + + if (!rc) { + for (IntrospectedColumn introspectedColumn : baseColumns) { + if (introspectedColumn.isJDBCDateColumn()) { + rc = true; + break; + } + } + } + + return rc; } /** @@ -213,7 +229,25 @@ * @return true if the table contains TIME columns */ public boolean hasJDBCTimeColumns() { - return hasJDBCTimeColumns; + boolean rc = false; + + for (IntrospectedColumn introspectedColumn : primaryKeyColumns) { + if (introspectedColumn.isJDBCTimeColumn()) { + rc = true; + break; + } + } + + if (!rc) { + for (IntrospectedColumn introspectedColumn : baseColumns) { + if (introspectedColumn.isJDBCTimeColumn()) { + rc = true; + break; + } + } + } + + return rc; } /** @@ -407,14 +441,6 @@ baseColumns.add(introspectedColumn); } - if (introspectedColumn.isJDBCDateColumn()) { - hasJDBCDateColumns = true; - } - - if (introspectedColumn.isJDBCTimeColumn()) { - hasJDBCTimeColumns = true; - } - introspectedColumn.setIntrospectedTable(this); } @@ -459,7 +485,7 @@ attributes.put(name, value); } - public void calculateAttributes() { + public void initialize() { calculateDAOImplementationPackage(); calculateDAOInterfacePackage(); calculateDAOImplementationType(); @@ -474,7 +500,7 @@ calculateSqlMapPackage(); calculateSqlMapFileName(); - ibatorContext.getPlugins().attributesCalculated(this); + ibatorContext.getPlugins().initialized(this); } private void calculateDAOImplementationPackage() { Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/config/IbatorContext.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/config/IbatorContext.java?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/config/IbatorContext.java (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/config/IbatorContext.java Thu Dec 18 15:11:17 2008 @@ -494,7 +494,7 @@ for (IntrospectedTable introspectedTable : introspectedTables) { callback.checkCancel(); - introspectedTable.calculateAttributes(); + introspectedTable.initialize(); generatedJavaFiles.addAll(introspectedTable.getGeneratedJavaFiles()); generatedXmlFiles.addAll(introspectedTable.getGeneratedXmlFiles()); Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/IbatorPluginAggregator.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/IbatorPluginAggregator.java?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/IbatorPluginAggregator.java (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/IbatorPluginAggregator.java Thu Dec 18 15:11:17 2008 @@ -945,9 +945,9 @@ return rc; } - public void attributesCalculated(IntrospectedTable introspectedTable) { + public void initialized(IntrospectedTable introspectedTable) { for (IbatorPlugin plugin : plugins) { - plugin.attributesCalculated(introspectedTable); + plugin.initialized(introspectedTable); } } } Modified: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/util/messages/messages.properties URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/util/messages/messages.properties?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/util/messages/messages.properties (original) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/internal/util/messages/messages.properties Thu Dec 18 15:11:17 2008 @@ -64,8 +64,10 @@ Warning.22=Property {0} exists in root class {1}, but does not have a getter. iBATOR will generate the property. Warning.23=Property {0} exists in root class {1}, but does not have a setter. iBATOR will generate the property. Warning.24=Plugin {0} in context {1} is invalid and will be ignored. -Warning.25=The SqlMapConfig plugin requires the targetProject property -Warning.26=The SqlMapConfig plugin requires the targetPackage property +Warning.25=SqlMapConfigPlugin requires the targetProject property +Warning.26=SqlMapConfigPlugin requires the targetPackage property +Warning.27=RenameExampleClassPlugin requires the searchString property +Warning.28=RenameExampleClassPlugin requires the replaceString property Progress.0=Connecting to the Database Progress.1=Introspecting table {0} Added: ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/RenameExampleClassPlugin.java URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/RenameExampleClassPlugin.java?rev=727852&view=auto ============================================================================== --- ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/RenameExampleClassPlugin.java (added) +++ ibatis/trunk/java/tools/ibator/core/src/org/apache/ibatis/ibator/plugins/RenameExampleClassPlugin.java Thu Dec 18 15:11:17 2008 @@ -0,0 +1,102 @@ +/* + * 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.apache.ibatis.ibator.plugins; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.ibatis.ibator.api.IbatorPluginAdapter; +import org.apache.ibatis.ibator.api.IntrospectedTable; +import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType; +import org.apache.ibatis.ibator.internal.util.StringUtility; +import org.apache.ibatis.ibator.internal.util.messages.Messages; + +/** + * 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 IbatorPluginAdapter { + private String searchString; + private String replaceString; + private Pattern pattern; + + /** + * + */ + public RenameExampleClassPlugin() { + } + + /* (non-Javadoc) + * @see org.apache.ibatis.ibator.api.IbatorPlugin#validate(java.util.List) + */ + public boolean validate(List<String> warnings) { + + searchString = properties.getProperty("searchString"); + replaceString = properties.getProperty("replaceString"); + + boolean valid = StringUtility.stringHasValue(searchString) + && StringUtility.stringHasValue(replaceString); + + if (valid) { + pattern = Pattern.compile(searchString); + } else { + if (!StringUtility.stringHasValue(searchString)) { + warnings.add(Messages.getString("Warning.27")); + } + if (!StringUtility.stringHasValue(replaceString)) { + warnings.add(Messages.getString("Warning.28")); + } + } + + return valid; + } + + @Override + public void initialized(IntrospectedTable introspectedTable) { + FullyQualifiedJavaType oldType = (FullyQualifiedJavaType) + introspectedTable.getAttribute(IntrospectedTable.ATTR_EXAMPLE_TYPE); + + String typeName = oldType.getFullyQualifiedName(); + Matcher matcher = pattern.matcher(typeName); + typeName = matcher.replaceAll(replaceString); + + introspectedTable.setAttribute(IntrospectedTable.ATTR_EXAMPLE_TYPE, + new FullyQualifiedJavaType(typeName)); + } +} Modified: ibatis/trunk/java/tools/ibator/core/testJava5/ibatortest/ibatorConfig.xml URL: http://svn.apache.org/viewvc/ibatis/trunk/java/tools/ibator/core/testJava5/ibatortest/ibatorConfig.xml?rev=727852&r1=727851&r2=727852&view=diff ============================================================================== --- ibatis/trunk/java/tools/ibator/core/testJava5/ibatortest/ibatorConfig.xml (original) +++ ibatis/trunk/java/tools/ibator/core/testJava5/ibatortest/ibatorConfig.xml Thu Dec 18 15:11:17 2008 @@ -261,7 +261,10 @@ <ibatorContext id="miscellaneousTests" targetRuntime="Ibatis2Java5"> <ibatorPlugin type="org.apache.ibatis.ibator.plugins.EqualsHashCodePlugin" /> - <ibatorPlugin type="org.apache.ibatis.ibator.build.test.TestPlugin" /> + <ibatorPlugin type="org.apache.ibatis.ibator.plugins.RenameExampleClassPlugin" > + <property name="searchString" value="Example$"/> + <property name="replaceString" value="Criteria"/> + </ibatorPlugin> <commentGenerator> <property name="suppressDate" value="true"/>