1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.config.xml;
17
18 import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19
20 import java.util.Properties;
21
22 import org.mybatis.generator.config.Configuration;
23 import org.mybatis.generator.config.Context;
24 import org.mybatis.generator.config.JavaClientGeneratorConfiguration;
25 import org.mybatis.generator.config.ModelType;
26 import org.mybatis.generator.config.PluginConfiguration;
27 import org.mybatis.generator.exception.XMLParserException;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31
32
33
34
35
36
37
38 public class IbatorConfigurationParser extends MyBatisGeneratorConfigurationParser {
39
40 public IbatorConfigurationParser(Properties properties) {
41 super(properties);
42 }
43
44 public Configuration parseIbatorConfiguration(Element rootNode)
45 throws XMLParserException {
46
47 Configuration configuration = new Configuration();
48
49 NodeList nodeList = rootNode.getChildNodes();
50 for (int i = 0; i < nodeList.getLength(); i++) {
51 Node childNode = nodeList.item(i);
52
53 if (childNode.getNodeType() != Node.ELEMENT_NODE) {
54 continue;
55 }
56
57 if ("properties".equals(childNode.getNodeName())) {
58 parseProperties(configuration, childNode);
59 } else if ("classPathEntry".equals(childNode.getNodeName())) {
60 parseClassPathEntry(configuration, childNode);
61 } else if ("ibatorContext".equals(childNode.getNodeName())) {
62 parseIbatorContext(configuration, childNode);
63 }
64 }
65
66 return configuration;
67 }
68
69 private void parseIbatorContext(Configuration configuration, Node node) {
70
71 Properties attributes = parseAttributes(node);
72 String defaultModelType = attributes.getProperty("defaultModelType");
73 String targetRuntime = attributes.getProperty("targetRuntime");
74 String introspectedColumnImpl = attributes
75 .getProperty("introspectedColumnImpl");
76 String id = attributes.getProperty("id");
77
78 ModelType mt = defaultModelType == null ? null : ModelType
79 .getModelType(defaultModelType);
80
81 Context context = new Context(mt);
82 context.setId(id);
83 if (stringHasValue(introspectedColumnImpl)) {
84 context.setIntrospectedColumnImpl(introspectedColumnImpl);
85 }
86 if (stringHasValue(targetRuntime)) {
87 context.setTargetRuntime(targetRuntime);
88 }
89
90 configuration.addContext(context);
91
92 NodeList nodeList = node.getChildNodes();
93 for (int i = 0; i < nodeList.getLength(); i++) {
94 Node childNode = nodeList.item(i);
95
96 if (childNode.getNodeType() != Node.ELEMENT_NODE) {
97 continue;
98 }
99
100 if ("property".equals(childNode.getNodeName())) {
101 parseProperty(context, childNode);
102 } else if ("ibatorPlugin".equals(childNode.getNodeName())) {
103 parseIbatorPlugin(context, childNode);
104 } else if ("commentGenerator".equals(childNode.getNodeName())) {
105 parseCommentGenerator(context, childNode);
106 } else if ("jdbcConnection".equals(childNode.getNodeName())) {
107 parseJdbcConnection(context, childNode);
108 } else if ("javaModelGenerator".equals(childNode.getNodeName())) {
109 parseJavaModelGenerator(context, childNode);
110 } else if ("javaTypeResolver".equals(childNode.getNodeName())) {
111 parseJavaTypeResolver(context, childNode);
112 } else if ("sqlMapGenerator".equals(childNode.getNodeName())) {
113 parseSqlMapGenerator(context, childNode);
114 } else if ("daoGenerator".equals(childNode.getNodeName())) {
115 parseDaoGenerator(context, childNode);
116 } else if ("table".equals(childNode.getNodeName())) {
117 parseTable(context, childNode);
118 }
119 }
120 }
121
122 private void parseIbatorPlugin(Context context, Node node) {
123 PluginConfiguration pluginConfiguration = new PluginConfiguration();
124
125 context.addPluginConfiguration(pluginConfiguration);
126
127 Properties attributes = parseAttributes(node);
128 String type = attributes.getProperty("type");
129
130 pluginConfiguration.setConfigurationType(type);
131
132 NodeList nodeList = node.getChildNodes();
133 for (int i = 0; i < nodeList.getLength(); i++) {
134 Node childNode = nodeList.item(i);
135
136 if (childNode.getNodeType() != Node.ELEMENT_NODE) {
137 continue;
138 }
139
140 if ("property".equals(childNode.getNodeName())) {
141 parseProperty(pluginConfiguration, childNode);
142 }
143 }
144 }
145
146 private void parseDaoGenerator(Context context, Node node) {
147 JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
148
149 context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
150
151 Properties attributes = parseAttributes(node);
152 String type = attributes.getProperty("type");
153 String targetPackage = attributes.getProperty("targetPackage");
154 String targetProject = attributes.getProperty("targetProject");
155 String implementationPackage = attributes
156 .getProperty("implementationPackage");
157
158 javaClientGeneratorConfiguration.setConfigurationType(type);
159 javaClientGeneratorConfiguration.setTargetPackage(targetPackage);
160 javaClientGeneratorConfiguration.setTargetProject(targetProject);
161 javaClientGeneratorConfiguration
162 .setImplementationPackage(implementationPackage);
163
164 NodeList nodeList = node.getChildNodes();
165 for (int i = 0; i < nodeList.getLength(); i++) {
166 Node childNode = nodeList.item(i);
167
168 if (childNode.getNodeType() != Node.ELEMENT_NODE) {
169 continue;
170 }
171
172 if ("property".equals(childNode.getNodeName())) {
173 parseProperty(javaClientGeneratorConfiguration, childNode);
174 }
175 }
176 }
177 }