Skip to content

Commit c3e1543

Browse files
author
ct
committed
lower the code hierarchy to make it more elegant
1 parent 6e84c85 commit c3e1543

File tree

1 file changed

+120
-115
lines changed

1 file changed

+120
-115
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 120 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ private Properties settingsAsProperties(XNode context) {
152152

153153
private void loadCustomVfs(Properties props) throws ClassNotFoundException {
154154
String value = props.getProperty("vfsImpl");
155-
if (value != null) {
156-
String[] clazzes = value.split(",");
157-
for (String clazz : clazzes) {
158-
if (!clazz.isEmpty()) {
159-
@SuppressWarnings("unchecked")
160-
Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
161-
configuration.setVfsImpl(vfsImpl);
162-
}
155+
if(value == null) {
156+
return;
157+
}
158+
String[] clazzes = value.split(",");
159+
for (String clazz : clazzes) {
160+
if (!clazz.isEmpty()) {
161+
@SuppressWarnings("unchecked")
162+
Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
163+
configuration.setVfsImpl(vfsImpl);
163164
}
164165
}
165166
}
@@ -169,37 +170,37 @@ private void loadCustomLogImpl(Properties props) {
169170
configuration.setLogImpl(logImpl);
170171
}
171172

172-
private void typeAliasesElement(XNode parent) {
173-
if (parent != null) {
174-
for (XNode child : parent.getChildren()) {
175-
if ("package".equals(child.getName())) {
176-
String typeAliasPackage = child.getStringAttribute("name");
177-
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
178-
} else {
179-
String alias = child.getStringAttribute("alias");
180-
String type = child.getStringAttribute("type");
181-
try {
182-
Class<?> clazz = Resources.classForName(type);
183-
if (alias == null) {
184-
typeAliasRegistry.registerAlias(clazz);
185-
} else {
186-
typeAliasRegistry.registerAlias(alias, clazz);
187-
}
188-
} catch (ClassNotFoundException e) {
189-
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
173+
private void typeAliasesElement(XNode context) {
174+
if(context == null) {
175+
return;
176+
}
177+
for (XNode child : context.getChildren()) {
178+
if ("package".equals(child.getName())) {
179+
String typeAliasPackage = child.getStringAttribute("name");
180+
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
181+
} else {
182+
String alias = child.getStringAttribute("alias");
183+
String type = child.getStringAttribute("type");
184+
try {
185+
Class<?> clazz = Resources.classForName(type);
186+
if (alias == null) {
187+
typeAliasRegistry.registerAlias(clazz);
188+
} else {
189+
typeAliasRegistry.registerAlias(alias, clazz);
190190
}
191+
} catch (ClassNotFoundException e) {
192+
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
191193
}
192194
}
193195
}
194196
}
195197

196-
private void pluginElement(XNode parent) throws Exception {
197-
if (parent != null) {
198-
for (XNode child : parent.getChildren()) {
198+
private void pluginElement(XNode context) throws Exception {
199+
if (context != null) {
200+
for (XNode child : context.getChildren()) {
199201
String interceptor = child.getStringAttribute("interceptor");
200202
Properties properties = child.getChildrenAsProperties();
201-
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor()
202-
.newInstance();
203+
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor().newInstance();
203204
interceptorInstance.setProperties(properties);
204205
configuration.addInterceptor(interceptorInstance);
205206
}
@@ -233,26 +234,27 @@ private void reflectorFactoryElement(XNode context) throws Exception {
233234
}
234235

235236
private void propertiesElement(XNode context) throws Exception {
236-
if (context != null) {
237-
Properties defaults = context.getChildrenAsProperties();
238-
String resource = context.getStringAttribute("resource");
239-
String url = context.getStringAttribute("url");
240-
if (resource != null && url != null) {
241-
throw new BuilderException(
242-
"The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
243-
}
244-
if (resource != null) {
245-
defaults.putAll(Resources.getResourceAsProperties(resource));
246-
} else if (url != null) {
247-
defaults.putAll(Resources.getUrlAsProperties(url));
248-
}
249-
Properties vars = configuration.getVariables();
250-
if (vars != null) {
251-
defaults.putAll(vars);
252-
}
253-
parser.setVariables(defaults);
254-
configuration.setVariables(defaults);
237+
if(context == null) {
238+
return;
239+
}
240+
Properties defaults = context.getChildrenAsProperties();
241+
String resource = context.getStringAttribute("resource");
242+
String url = context.getStringAttribute("url");
243+
if (resource != null && url != null) {
244+
throw new BuilderException(
245+
"The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
255246
}
247+
if (resource != null) {
248+
defaults.putAll(Resources.getResourceAsProperties(resource));
249+
} else if (url != null) {
250+
defaults.putAll(Resources.getUrlAsProperties(url));
251+
}
252+
Properties vars = configuration.getVariables();
253+
if (vars != null) {
254+
defaults.putAll(vars);
255+
}
256+
parser.setVariables(defaults);
257+
configuration.setVariables(defaults);
256258
}
257259

258260
private void settingsElement(Properties props) {
@@ -293,21 +295,22 @@ private void settingsElement(Properties props) {
293295
}
294296

295297
private void environmentsElement(XNode context) throws Exception {
296-
if (context != null) {
297-
if (environment == null) {
298-
environment = context.getStringAttribute("default");
299-
}
300-
for (XNode child : context.getChildren()) {
301-
String id = child.getStringAttribute("id");
302-
if (isSpecifiedEnvironment(id)) {
303-
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
304-
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
305-
DataSource dataSource = dsFactory.getDataSource();
306-
Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
307-
.dataSource(dataSource);
308-
configuration.setEnvironment(environmentBuilder.build());
309-
break;
310-
}
298+
if (context == null) {
299+
return;
300+
}
301+
if (environment == null) {
302+
environment = context.getStringAttribute("default");
303+
}
304+
for (XNode child : context.getChildren()) {
305+
String id = child.getStringAttribute("id");
306+
if (isSpecifiedEnvironment(id)) {
307+
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
308+
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
309+
DataSource dataSource = dsFactory.getDataSource();
310+
Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
311+
.dataSource(dataSource);
312+
configuration.setEnvironment(environmentBuilder.build());
313+
break;
311314
}
312315
}
313316
}
@@ -353,64 +356,66 @@ private DataSourceFactory dataSourceElement(XNode context) throws Exception {
353356
throw new BuilderException("Environment declaration requires a DataSourceFactory.");
354357
}
355358

356-
private void typeHandlerElement(XNode parent) {
357-
if (parent != null) {
358-
for (XNode child : parent.getChildren()) {
359-
if ("package".equals(child.getName())) {
360-
String typeHandlerPackage = child.getStringAttribute("name");
361-
typeHandlerRegistry.register(typeHandlerPackage);
362-
} else {
363-
String javaTypeName = child.getStringAttribute("javaType");
364-
String jdbcTypeName = child.getStringAttribute("jdbcType");
365-
String handlerTypeName = child.getStringAttribute("handler");
366-
Class<?> javaTypeClass = resolveClass(javaTypeName);
367-
JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
368-
Class<?> typeHandlerClass = resolveClass(handlerTypeName);
369-
if (javaTypeClass != null) {
370-
if (jdbcType == null) {
371-
typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
372-
} else {
373-
typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
374-
}
359+
private void typeHandlerElement(XNode context) {
360+
if (context == null) {
361+
return;
362+
}
363+
for (XNode child : context.getChildren()) {
364+
if ("package".equals(child.getName())) {
365+
String typeHandlerPackage = child.getStringAttribute("name");
366+
typeHandlerRegistry.register(typeHandlerPackage);
367+
} else {
368+
String javaTypeName = child.getStringAttribute("javaType");
369+
String jdbcTypeName = child.getStringAttribute("jdbcType");
370+
String handlerTypeName = child.getStringAttribute("handler");
371+
Class<?> javaTypeClass = resolveClass(javaTypeName);
372+
JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
373+
Class<?> typeHandlerClass = resolveClass(handlerTypeName);
374+
if (javaTypeClass != null) {
375+
if (jdbcType == null) {
376+
typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
375377
} else {
376-
typeHandlerRegistry.register(typeHandlerClass);
378+
typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
377379
}
380+
} else {
381+
typeHandlerRegistry.register(typeHandlerClass);
378382
}
379383
}
380384
}
381385
}
382386

383-
private void mapperElement(XNode parent) throws Exception {
384-
if (parent != null) {
385-
for (XNode child : parent.getChildren()) {
386-
if ("package".equals(child.getName())) {
387-
String mapperPackage = child.getStringAttribute("name");
388-
configuration.addMappers(mapperPackage);
389-
} else {
390-
String resource = child.getStringAttribute("resource");
391-
String url = child.getStringAttribute("url");
392-
String mapperClass = child.getStringAttribute("class");
393-
if (resource != null && url == null && mapperClass == null) {
394-
ErrorContext.instance().resource(resource);
395-
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
396-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
397-
configuration.getSqlFragments());
398-
mapperParser.parse();
399-
}
400-
} else if (resource == null && url != null && mapperClass == null) {
401-
ErrorContext.instance().resource(url);
402-
try (InputStream inputStream = Resources.getUrlAsStream(url)) {
403-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
404-
configuration.getSqlFragments());
405-
mapperParser.parse();
406-
}
407-
} else if (resource == null && url == null && mapperClass != null) {
408-
Class<?> mapperInterface = Resources.classForName(mapperClass);
409-
configuration.addMapper(mapperInterface);
410-
} else {
411-
throw new BuilderException(
412-
"A mapper element may only specify a url, resource or class, but not more than one.");
387+
private void mapperElement(XNode context) throws Exception {
388+
if (context == null) {
389+
return;
390+
}
391+
for (XNode child : context.getChildren()) {
392+
if ("package".equals(child.getName())) {
393+
String mapperPackage = child.getStringAttribute("name");
394+
configuration.addMappers(mapperPackage);
395+
} else {
396+
String resource = child.getStringAttribute("resource");
397+
String url = child.getStringAttribute("url");
398+
String mapperClass = child.getStringAttribute("class");
399+
if (resource != null && url == null && mapperClass == null) {
400+
ErrorContext.instance().resource(resource);
401+
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
402+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
403+
configuration.getSqlFragments());
404+
mapperParser.parse();
413405
}
406+
} else if (resource == null && url != null && mapperClass == null) {
407+
ErrorContext.instance().resource(url);
408+
try (InputStream inputStream = Resources.getUrlAsStream(url)) {
409+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
410+
configuration.getSqlFragments());
411+
mapperParser.parse();
412+
}
413+
} else if (resource == null && url == null && mapperClass != null) {
414+
Class<?> mapperInterface = Resources.classForName(mapperClass);
415+
configuration.addMapper(mapperInterface);
416+
} else {
417+
throw new BuilderException(
418+
"A mapper element may only specify a url, resource or class, but not more than one.");
414419
}
415420
}
416421
}

0 commit comments

Comments
 (0)