1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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"))) {
69 warnings.add(getString("ValidationError.18",
70 "SqlMapConfigPlugin",
71 "targetProject"));
72 valid = false;
73 }
74
75 if (!stringHasValue(properties
76 .getProperty("targetPackage"))) {
77 warnings.add(getString("ValidationError.18",
78 "SqlMapConfigPlugin",
79 "targetPackage"));
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");
93 document.setRootElement(root);
94
95 root.addElement(new TextElement("<!--"));
96 root.addElement(new TextElement(
97 " This file is generated by MyBatis Generator."));
98 root
99 .addElement(new TextElement(
100 " This file is the shell of an SqlMapConfig file - in many cases you will need to add"));
101 root.addElement(new TextElement(
102 " to this file before it is usable by iBATIS."));
103
104 StringBuilder sb = new StringBuilder();
105 sb.append(" This file was generated on ");
106 sb.append(new Date());
107 sb.append('.');
108 root.addElement(new TextElement(sb.toString()));
109
110 root.addElement(new TextElement("-->"));
111
112 XmlElement settings = new XmlElement("settings");
113 settings.addAttribute(new Attribute("useStatementNamespaces", "true"));
114 root.addElement(settings);
115
116 XmlElement sqlMap;
117 for (String sqlMapFile : sqlMapFiles) {
118 sqlMap = new XmlElement("sqlMap");
119 sqlMap.addAttribute(new Attribute("resource", sqlMapFile));
120 root.addElement(sqlMap);
121 }
122
123 GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties
124 .getProperty("fileName", "SqlMapConfig.xml"),
125 properties.getProperty("targetPackage"),
126 properties.getProperty("targetProject"),
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
137
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 }