• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / ibatis-2 / 749

03 Jan 2026 10:40PM UTC coverage: 65.533% (-0.06%) from 65.595%
749

push

github

web-flow
Merge pull request #344 from hazendaz/master

Use lambda's

1598 of 2797 branches covered (57.13%)

281 of 327 new or added lines in 2 files covered. (85.93%)

3 existing lines in 2 files now uncovered.

5027 of 7671 relevant lines covered (65.53%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.96
/src/main/java/com/ibatis/sqlmap/engine/builder/xml/SqlMapConfigParser.java
1
/*
2
 * Copyright 2004-2025 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
 *    https://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 com.ibatis.sqlmap.engine.builder.xml;
17

18
import com.ibatis.common.resources.Resources;
19
import com.ibatis.common.xml.NodeletParser;
20
import com.ibatis.common.xml.NodeletUtils;
21
import com.ibatis.sqlmap.client.SqlMapClient;
22
import com.ibatis.sqlmap.client.SqlMapException;
23
import com.ibatis.sqlmap.engine.config.SqlMapConfiguration;
24
import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
25
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
26
import com.ibatis.sqlmap.engine.transaction.TransactionConfig;
27
import com.ibatis.sqlmap.engine.transaction.TransactionManager;
28

29
import java.io.InputStream;
30
import java.io.Reader;
31
import java.util.Properties;
32

33
/**
34
 * The Class SqlMapConfigParser.
35
 */
36
public class SqlMapConfigParser {
37

38
  /** The parser. */
39
  protected final NodeletParser parser = new NodeletParser();
1✔
40

41
  /** The state. */
42
  private XmlParserState state = new XmlParserState();
1✔
43

44
  /** The using streams. */
45
  private boolean usingStreams = false;
1✔
46

47
  /**
48
   * Instantiates a new sql map config parser.
49
   */
50
  public SqlMapConfigParser() {
1✔
51
    parser.setValidation(true);
1✔
52
    parser.setEntityResolver(new SqlMapClasspathEntityResolver());
1✔
53

54
    addSqlMapConfigNodelets();
1✔
55
    addGlobalPropNodelets();
1✔
56
    addSettingsNodelets();
1✔
57
    addTypeAliasNodelets();
1✔
58
    addTypeHandlerNodelets();
1✔
59
    addTransactionManagerNodelets();
1✔
60
    addSqlMapNodelets();
1✔
61
    addResultObjectFactoryNodelets();
1✔
62

63
  }
1✔
64

65
  /**
66
   * Parses the.
67
   *
68
   * @param reader
69
   *          the reader
70
   * @param props
71
   *          the props
72
   *
73
   * @return the sql map client
74
   */
75
  public SqlMapClient parse(Reader reader, Properties props) {
76
    if (props != null) {
1✔
77
      state.setGlobalProps(props);
1✔
78
    }
79
    return parse(reader);
1✔
80
  }
81

82
  /**
83
   * Parses the.
84
   *
85
   * @param reader
86
   *          the reader
87
   *
88
   * @return the sql map client
89
   */
90
  public SqlMapClient parse(Reader reader) {
91
    try {
92
      usingStreams = false;
1✔
93

94
      parser.parse(reader);
1✔
95
      return state.getConfig().getClient();
1✔
96
    } catch (Exception e) {
×
97
      throw new RuntimeException("Error occurred.  Cause: " + e, e);
×
98
    }
99
  }
100

101
  /**
102
   * Parses the.
103
   *
104
   * @param inputStream
105
   *          the input stream
106
   * @param props
107
   *          the props
108
   *
109
   * @return the sql map client
110
   */
111
  public SqlMapClient parse(InputStream inputStream, Properties props) {
112
    if (props != null) {
×
113
      state.setGlobalProps(props);
×
114
    }
115
    return parse(inputStream);
×
116
  }
117

118
  /**
119
   * Parses the.
120
   *
121
   * @param inputStream
122
   *          the input stream
123
   *
124
   * @return the sql map client
125
   */
126
  public SqlMapClient parse(InputStream inputStream) {
127
    try {
128
      usingStreams = true;
1✔
129

130
      parser.parse(inputStream);
1✔
131
      return state.getConfig().getClient();
1✔
132
    } catch (Exception e) {
×
133
      throw new RuntimeException("Error occurred.  Cause: " + e, e);
×
134
    }
135
  }
136

137
  /**
138
   * Adds the sql map config nodelets.
139
   */
140
  private void addSqlMapConfigNodelets() {
141
    parser.addNodelet("/sqlMapConfig/end()", node -> state.getConfig().finalizeSqlMapConfig());
1✔
142
  }
1✔
143

144
  /**
145
   * Adds the global prop nodelets.
146
   */
147
  private void addGlobalPropNodelets() {
148
    parser.addNodelet("/sqlMapConfig/properties", node -> {
1✔
149
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
150
      String resource = attributes.getProperty("resource");
1✔
151
      String url = attributes.getProperty("url");
1✔
152
      state.setGlobalProperties(resource, url);
1✔
153
    });
1✔
154
  }
1✔
155

156
  /**
157
   * Adds the settings nodelets.
158
   */
159
  private void addSettingsNodelets() {
160
    parser.addNodelet("/sqlMapConfig/settings", node -> {
1✔
161
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
162
      SqlMapConfiguration config = state.getConfig();
1✔
163

164
      String classInfoCacheEnabledAttr = attributes.getProperty("classInfoCacheEnabled");
1✔
165
      boolean classInfoCacheEnabled = classInfoCacheEnabledAttr == null || "true".equals(classInfoCacheEnabledAttr);
1!
166
      config.setClassInfoCacheEnabled(classInfoCacheEnabled);
1✔
167

168
      String lazyLoadingEnabledAttr = attributes.getProperty("lazyLoadingEnabled");
1✔
169
      boolean lazyLoadingEnabled = lazyLoadingEnabledAttr == null || "true".equals(lazyLoadingEnabledAttr);
1!
170
      config.setLazyLoadingEnabled(lazyLoadingEnabled);
1✔
171

172
      String statementCachingEnabledAttr = attributes.getProperty("statementCachingEnabled");
1✔
173
      boolean statementCachingEnabled = statementCachingEnabledAttr == null
1✔
174
          || "true".equals(statementCachingEnabledAttr);
1!
175
      config.setStatementCachingEnabled(statementCachingEnabled);
1✔
176

177
      String cacheModelsEnabledAttr = attributes.getProperty("cacheModelsEnabled");
1✔
178
      boolean cacheModelsEnabled = cacheModelsEnabledAttr == null || "true".equals(cacheModelsEnabledAttr);
1!
179
      config.setCacheModelsEnabled(cacheModelsEnabled);
1✔
180

181
      String enhancementEnabledAttr = attributes.getProperty("enhancementEnabled");
1✔
182
      boolean enhancementEnabled = enhancementEnabledAttr == null || "true".equals(enhancementEnabledAttr);
1!
183
      config.setEnhancementEnabled(enhancementEnabled);
1✔
184

185
      String useColumnLabelAttr = attributes.getProperty("useColumnLabel");
1✔
186
      boolean useColumnLabel = useColumnLabelAttr == null || "true".equals(useColumnLabelAttr);
1!
187
      config.setUseColumnLabel(useColumnLabel);
1✔
188

189
      String forceMultipleResultSetSupportAttr = attributes.getProperty("forceMultipleResultSetSupport");
1✔
190
      boolean forceMultipleResultSetSupport = "true".equals(forceMultipleResultSetSupportAttr);
1✔
191
      config.setForceMultipleResultSetSupport(forceMultipleResultSetSupport);
1✔
192

193
      String defaultTimeoutAttr = attributes.getProperty("defaultStatementTimeout");
1✔
194
      Integer defaultTimeout = defaultTimeoutAttr == null ? null : Integer.valueOf(defaultTimeoutAttr);
1!
195
      config.setDefaultStatementTimeout(defaultTimeout);
1✔
196

197
      String useStatementNamespacesAttr = attributes.getProperty("useStatementNamespaces");
1✔
198
      boolean useStatementNamespaces = "true".equals(useStatementNamespacesAttr);
1✔
199
      state.setUseStatementNamespaces(useStatementNamespaces);
1✔
200
    });
1✔
201
  }
1✔
202

203
  /**
204
   * Adds the type alias nodelets.
205
   */
206
  private void addTypeAliasNodelets() {
207
    parser.addNodelet("/sqlMapConfig/typeAlias", node -> {
1✔
208
      Properties prop = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
209
      String alias = prop.getProperty("alias");
1✔
210
      String type = prop.getProperty("type");
1✔
211
      state.getConfig().getTypeHandlerFactory().putTypeAlias(alias, type);
1✔
212
    });
1✔
213
  }
1✔
214

215
  /**
216
   * Adds the type handler nodelets.
217
   */
218
  private void addTypeHandlerNodelets() {
219
    parser.addNodelet("/sqlMapConfig/typeHandler", node -> {
1✔
220
      Properties prop = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
221
      String jdbcType = prop.getProperty("jdbcType");
1✔
222
      String javaType = prop.getProperty("javaType");
1✔
223
      String callback = prop.getProperty("callback");
1✔
224

225
      javaType = state.getConfig().getTypeHandlerFactory().resolveAlias(javaType);
1✔
226
      callback = state.getConfig().getTypeHandlerFactory().resolveAlias(callback);
1✔
227

228
      state.getConfig().newTypeHandler(Resources.classForName(javaType), jdbcType, Resources.instantiate(callback));
1✔
229
    });
1✔
230
  }
1✔
231

232
  /**
233
   * Adds the transaction manager nodelets.
234
   */
235
  private void addTransactionManagerNodelets() {
236
    parser.addNodelet("/sqlMapConfig/transactionManager/property", node -> {
1✔
NEW
237
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
×
NEW
238
      String name = attributes.getProperty("name");
×
NEW
239
      String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
×
NEW
240
      state.getTxProps().setProperty(name, value);
×
UNCOV
241
    });
×
242
    parser.addNodelet("/sqlMapConfig/transactionManager/end()", node -> {
1✔
243
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
244
      String type = attributes.getProperty("type");
1✔
245
      boolean commitRequired = "true".equals(attributes.getProperty("commitRequired"));
1✔
246

247
      state.getConfig().getErrorContext().setActivity("configuring the transaction manager");
1✔
248
      type = state.getConfig().getTypeHandlerFactory().resolveAlias(type);
1✔
249
      TransactionManager txManager;
250
      try {
251
        state.getConfig().getErrorContext().setMoreInfo("Check the transaction manager type or class.");
1✔
252
        TransactionConfig config = (TransactionConfig) Resources.instantiate(type);
1✔
253
        config.setDataSource(state.getDataSource());
1✔
254
        state.getConfig().getErrorContext().setMoreInfo("Check the transaction manager properties or configuration.");
1✔
255
        config.setProperties(state.getTxProps());
1✔
256
        config.setForceCommit(commitRequired);
1✔
257
        config.setDataSource(state.getDataSource());
1✔
258
        state.getConfig().getErrorContext().setMoreInfo(null);
1✔
259
        txManager = new TransactionManager(config);
1✔
NEW
260
      } catch (Exception e) {
×
NEW
261
        if (e instanceof SqlMapException) {
×
NEW
262
          throw (SqlMapException) e;
×
263
        }
NEW
264
        throw new SqlMapException(
×
265
            "Error initializing TransactionManager.  Could not instantiate TransactionConfig.  Cause: " + e, e);
266
      }
1✔
267
      state.getConfig().setTransactionManager(txManager);
1✔
268
    });
1✔
269
    parser.addNodelet("/sqlMapConfig/transactionManager/dataSource/property", node -> {
1✔
270
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
271
      String name = attributes.getProperty("name");
1✔
272
      String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
1✔
273
      state.getDsProps().setProperty(name, value);
1✔
274
    });
1✔
275
    parser.addNodelet("/sqlMapConfig/transactionManager/dataSource/end()", node -> {
1✔
276
      state.getConfig().getErrorContext().setActivity("configuring the data source");
1✔
277

278
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
279

280
      String type = attributes.getProperty("type");
1✔
281
      Properties props = state.getDsProps();
1✔
282

283
      type = state.getConfig().getTypeHandlerFactory().resolveAlias(type);
1✔
284
      try {
285
        state.getConfig().getErrorContext().setMoreInfo("Check the data source type or class.");
1✔
286
        DataSourceFactory dsFactory = (DataSourceFactory) Resources.instantiate(type);
1✔
287
        state.getConfig().getErrorContext().setMoreInfo("Check the data source properties or configuration.");
1✔
288
        dsFactory.initialize(props);
1✔
289
        state.setDataSource(dsFactory.getDataSource());
1✔
290
        state.getConfig().getErrorContext().setMoreInfo(null);
1✔
NEW
291
      } catch (Exception e) {
×
NEW
292
        if (e instanceof SqlMapException) {
×
NEW
293
          throw (SqlMapException) e;
×
294
        }
NEW
295
        throw new SqlMapException(
×
296
            "Error initializing DataSource.  Could not instantiate DataSourceFactory.  Cause: " + e, e);
297
      }
1✔
298
    });
1✔
299
  }
1✔
300

301
  /**
302
   * Adds the sql map nodelets.
303
   */
304
  protected void addSqlMapNodelets() {
305
    parser.addNodelet("/sqlMapConfig/sqlMap", node -> {
1✔
306
      state.getConfig().getErrorContext().setActivity("loading the SQL Map resource");
1✔
307

308
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
309

310
      String resource = attributes.getProperty("resource");
1✔
311
      String url = attributes.getProperty("url");
1✔
312

313
      if (usingStreams) {
1✔
314
        InputStream inputStream = null;
1✔
315
        if (resource != null) {
1!
316
          state.getConfig().getErrorContext().setResource(resource);
1✔
317
          inputStream = Resources.getResourceAsStream(resource);
1✔
NEW
318
        } else if (url != null) {
×
NEW
319
          state.getConfig().getErrorContext().setResource(url);
×
NEW
320
          inputStream = Resources.getUrlAsStream(url);
×
321
        } else {
NEW
322
          throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
×
323
        }
324

325
        new SqlMapParser(state).parse(inputStream);
1✔
326
      } else {
1✔
327
        Reader reader = null;
1✔
328
        if (resource != null) {
1!
329
          state.getConfig().getErrorContext().setResource(resource);
1✔
330
          reader = Resources.getResourceAsReader(resource);
1✔
NEW
331
        } else if (url != null) {
×
NEW
332
          state.getConfig().getErrorContext().setResource(url);
×
NEW
333
          reader = Resources.getUrlAsReader(url);
×
334
        } else {
NEW
335
          throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
×
336
        }
337

338
        new SqlMapParser(state).parse(reader);
1✔
339
      }
340
    });
1✔
341
  }
1✔
342

343
  /**
344
   * Adds the result object factory nodelets.
345
   */
346
  private void addResultObjectFactoryNodelets() {
347
    parser.addNodelet("/sqlMapConfig/resultObjectFactory", node -> {
1✔
348
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
349
      String type = attributes.getProperty("type");
1✔
350

351
      state.getConfig().getErrorContext().setActivity("configuring the Result Object Factory");
1✔
352
      ResultObjectFactory rof;
353
      try {
354
        rof = (ResultObjectFactory) Resources.instantiate(type);
1✔
355
        state.getConfig().setResultObjectFactory(rof);
1✔
NEW
356
      } catch (Exception e) {
×
NEW
357
        throw new SqlMapException("Error instantiating resultObjectFactory: " + type, e);
×
358
      }
1✔
359

360
    });
1✔
361
    parser.addNodelet("/sqlMapConfig/resultObjectFactory/property", node -> {
1✔
362
      Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
363
      String name = attributes.getProperty("name");
1✔
364
      String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
1✔
365
      state.getConfig().getDelegate().getResultObjectFactory().setProperty(name, value);
1✔
366
    });
1✔
367
  }
1✔
368

369
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc