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 a MapperConfig file containing mapper entries for SQL
36   * maps generated for MyBatis3. 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 MapperConfigPlugin extends PluginAdapter {
57  
58      private List<String> mapperFiles;
59  
60      public MapperConfigPlugin() {
61          mapperFiles = 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                      "MapperConfigPlugin", //$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                      "MapperConfigPlugin", //$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.MYBATIS3_MAPPER_CONFIG_PUBLIC_ID,
90                  XmlConstants.MYBATIS3_MAPPER_CONFIG_SYSTEM_ID);
91  
92          XmlElement root = new XmlElement("configuration"); //$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 a Mapper Config 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 MyBatis.")); //$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 mappers = new XmlElement("mappers"); //$NON-NLS-1$
113         root.addElement(mappers);
114 
115         XmlElement mapper;
116         for (String mapperFile : mapperFiles) {
117             mapper = new XmlElement("mapper"); //$NON-NLS-1$
118             mapper.addAttribute(new Attribute("resource", mapperFile)); //$NON-NLS-1$
119             mappers.addElement(mapper);
120         }
121 
122         GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
123                 .getProperty("fileName", "MapperConfig.xml"), //$NON-NLS-1$ //$NON-NLS-2$
124                 properties.getProperty("targetPackage"), //$NON-NLS-1$
125                 properties.getProperty("targetProject"), //$NON-NLS-1$
126                 false, context.getXmlFormatter());
127 
128         List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
129         answer.add(gxf);
130 
131         return answer;
132     }
133 
134     /*
135      * This method collects the name of every SqlMap file generated in
136      * this context.
137      */
138     @Override
139     public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
140             IntrospectedTable introspectedTable) {
141         StringBuilder sb = new StringBuilder();
142         sb.append(sqlMap.getTargetPackage());
143         sb.append('.');
144         String temp = sb.toString();
145         sb.setLength(0);
146         sb.append(temp.replace('.', '/'));
147         sb.append(sqlMap.getFileName());
148         mapperFiles.add(sb.toString());
149 
150         return true;
151     }
152 }