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.ArrayList;
22  import java.util.Date;
23  import java.util.List;
24  
25  import org.mybatis.generator.api.GeneratedXmlFile;
26  import org.mybatis.generator.api.PluginAdapter;
27  import org.mybatis.generator.api.IntrospectedTable;
28  import org.mybatis.generator.api.dom.xml.Attribute;
29  import org.mybatis.generator.api.dom.xml.Document;
30  import org.mybatis.generator.api.dom.xml.TextElement;
31  import org.mybatis.generator.api.dom.xml.XmlElement;
32  import org.mybatis.generator.codegen.XmlConstants;
33  
34  /**
35   * This plugin generates an SqlMapConfig file containing sqlMap entries for 
36   * all generated SQL maps. This demonstrates hooking into the code
37   * generation lifecycle and generating additional XML files.
38   * <p>
39   * This plugin accepts three properties:
40   * <ul>
41   * <li><tt>fileName</tt> (optional) the name of the generated file. this
42   * defaults to "SqlMapConfig.xml" if not specified.</li>
43   * <li><tt>targetPackage</tt> (required) the name of the package where the file
44   * should be placed. Specified like "com.mycompany.sql".</li>
45   * <li><tt>targetProject</tt> (required) the name of the project where the file
46   * should be placed.</li>
47   * </ul>
48   * 
49   * Note: targetPackage and targetProject follow the same rules as the
50   * targetPackage and targetProject values on the sqlMapGenerator configuration
51   * element.
52   * 
53   * @author Jeff Butler
54   * 
55   */
56  public class SqlMapConfigPlugin extends PluginAdapter {
57  
58      private List<String> sqlMapFiles;
59  
60      public SqlMapConfigPlugin() {
61          sqlMapFiles = new ArrayList<String>();
62      }
63  
64      public boolean validate(List<String> warnings) {
65          boolean valid = true;
66  
67          if (!stringHasValue(properties
68                  .getProperty("targetProject"))) { //$NON-NLS-1$
69              warnings.add(getString("ValidationError.18", //$NON-NLS-1$
70                      "SqlMapConfigPlugin", //$NON-NLS-1$
71                      "targetProject")); //$NON-NLS-1$
72              valid = false;
73          }
74  
75          if (!stringHasValue(properties
76                  .getProperty("targetPackage"))) { //$NON-NLS-1$
77              warnings.add(getString("ValidationError.18", //$NON-NLS-1$
78                      "SqlMapConfigPlugin", //$NON-NLS-1$
79                      "targetPackage")); //$NON-NLS-1$
80              valid = false;
81          }
82  
83          return valid;
84      }
85  
86      @Override
87      public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
88          Document document = new Document(
89                  XmlConstants.IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID,
90                  XmlConstants.IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID);
91  
92          XmlElement root = new XmlElement("sqlMapConfig"); //$NON-NLS-1$
93          document.setRootElement(root);
94  
95          root.addElement(new TextElement("<!--")); //$NON-NLS-1$
96          root.addElement(new TextElement(
97                  "  This file is generated by MyBatis Generator.")); //$NON-NLS-1$
98          root
99                  .addElement(new TextElement(
100                         "  This file is the shell of an SqlMapConfig file - in many cases you will need to add")); //$NON-NLS-1$
101         root.addElement(new TextElement(
102                 "    to this file before it is usable by iBATIS.")); //$NON-NLS-1$
103 
104         StringBuilder sb = new StringBuilder();
105         sb.append("  This file was generated on "); //$NON-NLS-1$
106         sb.append(new Date());
107         sb.append('.');
108         root.addElement(new TextElement(sb.toString()));
109 
110         root.addElement(new TextElement("-->")); //$NON-NLS-1$
111 
112         XmlElement settings = new XmlElement("settings"); //$NON-NLS-1$
113         settings.addAttribute(new Attribute("useStatementNamespaces", "true")); //$NON-NLS-1$ //$NON-NLS-2$
114         root.addElement(settings);
115 
116         XmlElement sqlMap;
117         for (String sqlMapFile : sqlMapFiles) {
118             sqlMap = new XmlElement("sqlMap"); //$NON-NLS-1$
119             sqlMap.addAttribute(new Attribute("resource", sqlMapFile)); //$NON-NLS-1$
120             root.addElement(sqlMap);
121         }
122 
123         GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
124                 .getProperty("fileName", "SqlMapConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$
125                 properties.getProperty("targetPackage"), //$NON-NLS-1$
126                 properties.getProperty("targetProject"), //$NON-NLS-1$
127                 false, context.getXmlFormatter());
128 
129         List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
130         answer.add(gxf);
131 
132         return answer;
133     }
134 
135     /*
136      * This method collects the name of every SqlMap file generated in
137      * this context.
138      */
139     @Override
140     public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
141             IntrospectedTable introspectedTable) {
142         StringBuilder sb = new StringBuilder();
143         sb.append(sqlMap.getTargetPackage());
144         sb.append('.');
145         String temp = sb.toString();
146         sb.setLength(0);
147         sb.append(temp.replace('.', '/'));
148         sb.append(sqlMap.getFileName());
149         sqlMapFiles.add(sb.toString());
150 
151         return true;
152     }
153 }