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.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   * This class parses old Ibator configuration files into the new Configuration
34   * API
35   * 
36   * @author Jeff Butler
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())) { //$NON-NLS-1$
58                  parseProperties(configuration, childNode);
59              } else if ("classPathEntry".equals(childNode.getNodeName())) { //$NON-NLS-1$
60                  parseClassPathEntry(configuration, childNode);
61              } else if ("ibatorContext".equals(childNode.getNodeName())) { //$NON-NLS-1$
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"); //$NON-NLS-1$
73          String targetRuntime = attributes.getProperty("targetRuntime"); //$NON-NLS-1$
74          String introspectedColumnImpl = attributes
75                  .getProperty("introspectedColumnImpl"); //$NON-NLS-1$
76          String id = attributes.getProperty("id"); //$NON-NLS-1$
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())) { //$NON-NLS-1$
101                 parseProperty(context, childNode);
102             } else if ("ibatorPlugin".equals(childNode.getNodeName())) { //$NON-NLS-1$
103                 parseIbatorPlugin(context, childNode);
104             } else if ("commentGenerator".equals(childNode.getNodeName())) { //$NON-NLS-1$
105                 parseCommentGenerator(context, childNode);
106             } else if ("jdbcConnection".equals(childNode.getNodeName())) { //$NON-NLS-1$
107                 parseJdbcConnection(context, childNode);
108             } else if ("javaModelGenerator".equals(childNode.getNodeName())) { //$NON-NLS-1$
109                 parseJavaModelGenerator(context, childNode);
110             } else if ("javaTypeResolver".equals(childNode.getNodeName())) { //$NON-NLS-1$
111                 parseJavaTypeResolver(context, childNode);
112             } else if ("sqlMapGenerator".equals(childNode.getNodeName())) { //$NON-NLS-1$
113                 parseSqlMapGenerator(context, childNode);
114             } else if ("daoGenerator".equals(childNode.getNodeName())) { //$NON-NLS-1$
115                 parseDaoGenerator(context, childNode);
116             } else if ("table".equals(childNode.getNodeName())) { //$NON-NLS-1$
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"); //$NON-NLS-1$
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())) { //$NON-NLS-1$
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"); //$NON-NLS-1$
153         String targetPackage = attributes.getProperty("targetPackage"); //$NON-NLS-1$
154         String targetProject = attributes.getProperty("targetProject"); //$NON-NLS-1$
155         String implementationPackage = attributes
156                 .getProperty("implementationPackage"); //$NON-NLS-1$
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())) { //$NON-NLS-1$
173                 parseProperty(javaClientGeneratorConfiguration, childNode);
174             }
175         }
176     }
177 }