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

mybatis / ibatis-2 / 730

28 Dec 2025 10:16PM UTC coverage: 65.615% (+0.5%) from 65.146%
730

push

github

web-flow
Update README.md

1602 of 2802 branches covered (57.17%)

5053 of 7701 relevant lines covered (65.61%)

0.66 hits per line

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

84.13
/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.Nodelet;
20
import com.ibatis.common.xml.NodeletParser;
21
import com.ibatis.common.xml.NodeletUtils;
22
import com.ibatis.sqlmap.client.SqlMapClient;
23
import com.ibatis.sqlmap.client.SqlMapException;
24
import com.ibatis.sqlmap.engine.config.SqlMapConfiguration;
25
import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;
26
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
27
import com.ibatis.sqlmap.engine.transaction.TransactionConfig;
28
import com.ibatis.sqlmap.engine.transaction.TransactionManager;
29

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

34
import org.w3c.dom.Node;
35

36
/**
37
 * The Class SqlMapConfigParser.
38
 */
39
public class SqlMapConfigParser {
40

41
  /** The parser. */
42
  protected final NodeletParser parser = new NodeletParser();
1✔
43

44
  /** The state. */
45
  private XmlParserState state = new XmlParserState();
1✔
46

47
  /** The using streams. */
48
  private boolean usingStreams = false;
1✔
49

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

57
    addSqlMapConfigNodelets();
1✔
58
    addGlobalPropNodelets();
1✔
59
    addSettingsNodelets();
1✔
60
    addTypeAliasNodelets();
1✔
61
    addTypeHandlerNodelets();
1✔
62
    addTransactionManagerNodelets();
1✔
63
    addSqlMapNodelets();
1✔
64
    addResultObjectFactoryNodelets();
1✔
65

66
  }
1✔
67

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

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

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

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

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

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

140
  /**
141
   * Adds the sql map config nodelets.
142
   */
143
  private void addSqlMapConfigNodelets() {
144
    parser.addNodelet("/sqlMapConfig/end()", new Nodelet() {
1✔
145
      public void process(Node node) throws Exception {
146
        state.getConfig().finalizeSqlMapConfig();
1✔
147
      }
1✔
148
    });
149
  }
1✔
150

151
  /**
152
   * Adds the global prop nodelets.
153
   */
154
  private void addGlobalPropNodelets() {
155
    parser.addNodelet("/sqlMapConfig/properties", new Nodelet() {
1✔
156
      public void process(Node node) throws Exception {
157
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
158
        String resource = attributes.getProperty("resource");
1✔
159
        String url = attributes.getProperty("url");
1✔
160
        state.setGlobalProperties(resource, url);
1✔
161
      }
1✔
162
    });
163
  }
1✔
164

165
  /**
166
   * Adds the settings nodelets.
167
   */
168
  private void addSettingsNodelets() {
169
    parser.addNodelet("/sqlMapConfig/settings", new Nodelet() {
1✔
170
      public void process(Node node) throws Exception {
171
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
172
        SqlMapConfiguration config = state.getConfig();
1✔
173

174
        String classInfoCacheEnabledAttr = attributes.getProperty("classInfoCacheEnabled");
1✔
175
        boolean classInfoCacheEnabled = (classInfoCacheEnabledAttr == null || "true".equals(classInfoCacheEnabledAttr));
1!
176
        config.setClassInfoCacheEnabled(classInfoCacheEnabled);
1✔
177

178
        String lazyLoadingEnabledAttr = attributes.getProperty("lazyLoadingEnabled");
1✔
179
        boolean lazyLoadingEnabled = (lazyLoadingEnabledAttr == null || "true".equals(lazyLoadingEnabledAttr));
1!
180
        config.setLazyLoadingEnabled(lazyLoadingEnabled);
1✔
181

182
        String statementCachingEnabledAttr = attributes.getProperty("statementCachingEnabled");
1✔
183
        boolean statementCachingEnabled = (statementCachingEnabledAttr == null
1✔
184
            || "true".equals(statementCachingEnabledAttr));
1!
185
        config.setStatementCachingEnabled(statementCachingEnabled);
1✔
186

187
        String cacheModelsEnabledAttr = attributes.getProperty("cacheModelsEnabled");
1✔
188
        boolean cacheModelsEnabled = (cacheModelsEnabledAttr == null || "true".equals(cacheModelsEnabledAttr));
1!
189
        config.setCacheModelsEnabled(cacheModelsEnabled);
1✔
190

191
        String enhancementEnabledAttr = attributes.getProperty("enhancementEnabled");
1✔
192
        boolean enhancementEnabled = (enhancementEnabledAttr == null || "true".equals(enhancementEnabledAttr));
1!
193
        config.setEnhancementEnabled(enhancementEnabled);
1✔
194

195
        String useColumnLabelAttr = attributes.getProperty("useColumnLabel");
1✔
196
        boolean useColumnLabel = (useColumnLabelAttr == null || "true".equals(useColumnLabelAttr));
1!
197
        config.setUseColumnLabel(useColumnLabel);
1✔
198

199
        String forceMultipleResultSetSupportAttr = attributes.getProperty("forceMultipleResultSetSupport");
1✔
200
        boolean forceMultipleResultSetSupport = "true".equals(forceMultipleResultSetSupportAttr);
1✔
201
        config.setForceMultipleResultSetSupport(forceMultipleResultSetSupport);
1✔
202

203
        String defaultTimeoutAttr = attributes.getProperty("defaultStatementTimeout");
1✔
204
        Integer defaultTimeout = defaultTimeoutAttr == null ? null : Integer.valueOf(defaultTimeoutAttr);
1!
205
        config.setDefaultStatementTimeout(defaultTimeout);
1✔
206

207
        String useStatementNamespacesAttr = attributes.getProperty("useStatementNamespaces");
1✔
208
        boolean useStatementNamespaces = "true".equals(useStatementNamespacesAttr);
1✔
209
        state.setUseStatementNamespaces(useStatementNamespaces);
1✔
210
      }
1✔
211
    });
212
  }
1✔
213

214
  /**
215
   * Adds the type alias nodelets.
216
   */
217
  private void addTypeAliasNodelets() {
218
    parser.addNodelet("/sqlMapConfig/typeAlias", new Nodelet() {
1✔
219
      public void process(Node node) throws Exception {
220
        Properties prop = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
221
        String alias = prop.getProperty("alias");
1✔
222
        String type = prop.getProperty("type");
1✔
223
        state.getConfig().getTypeHandlerFactory().putTypeAlias(alias, type);
1✔
224
      }
1✔
225
    });
226
  }
1✔
227

228
  /**
229
   * Adds the type handler nodelets.
230
   */
231
  private void addTypeHandlerNodelets() {
232
    parser.addNodelet("/sqlMapConfig/typeHandler", new Nodelet() {
1✔
233
      public void process(Node node) throws Exception {
234
        Properties prop = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
235
        String jdbcType = prop.getProperty("jdbcType");
1✔
236
        String javaType = prop.getProperty("javaType");
1✔
237
        String callback = prop.getProperty("callback");
1✔
238

239
        javaType = state.getConfig().getTypeHandlerFactory().resolveAlias(javaType);
1✔
240
        callback = state.getConfig().getTypeHandlerFactory().resolveAlias(callback);
1✔
241

242
        state.getConfig().newTypeHandler(Resources.classForName(javaType), jdbcType, Resources.instantiate(callback));
1✔
243
      }
1✔
244
    });
245
  }
1✔
246

247
  /**
248
   * Adds the transaction manager nodelets.
249
   */
250
  private void addTransactionManagerNodelets() {
251
    parser.addNodelet("/sqlMapConfig/transactionManager/property", new Nodelet() {
1✔
252
      public void process(Node node) throws Exception {
253
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
×
254
        String name = attributes.getProperty("name");
×
255
        String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
×
256
        state.getTxProps().setProperty(name, value);
×
257
      }
×
258
    });
259
    parser.addNodelet("/sqlMapConfig/transactionManager/end()", new Nodelet() {
1✔
260
      public void process(Node node) throws Exception {
261
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
262
        String type = attributes.getProperty("type");
1✔
263
        boolean commitRequired = "true".equals(attributes.getProperty("commitRequired"));
1✔
264

265
        state.getConfig().getErrorContext().setActivity("configuring the transaction manager");
1✔
266
        type = state.getConfig().getTypeHandlerFactory().resolveAlias(type);
1✔
267
        TransactionManager txManager;
268
        try {
269
          state.getConfig().getErrorContext().setMoreInfo("Check the transaction manager type or class.");
1✔
270
          TransactionConfig config = (TransactionConfig) Resources.instantiate(type);
1✔
271
          config.setDataSource(state.getDataSource());
1✔
272
          state.getConfig().getErrorContext().setMoreInfo("Check the transaction manager properties or configuration.");
1✔
273
          config.setProperties(state.getTxProps());
1✔
274
          config.setForceCommit(commitRequired);
1✔
275
          config.setDataSource(state.getDataSource());
1✔
276
          state.getConfig().getErrorContext().setMoreInfo(null);
1✔
277
          txManager = new TransactionManager(config);
1✔
278
        } catch (Exception e) {
×
279
          if (e instanceof SqlMapException) {
×
280
            throw (SqlMapException) e;
×
281
          } else {
282
            throw new SqlMapException(
×
283
                "Error initializing TransactionManager.  Could not instantiate TransactionConfig.  Cause: " + e, e);
284
          }
285
        }
1✔
286
        state.getConfig().setTransactionManager(txManager);
1✔
287
      }
1✔
288
    });
289
    parser.addNodelet("/sqlMapConfig/transactionManager/dataSource/property", new Nodelet() {
1✔
290
      public void process(Node node) throws Exception {
291
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
292
        String name = attributes.getProperty("name");
1✔
293
        String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
1✔
294
        state.getDsProps().setProperty(name, value);
1✔
295
      }
1✔
296
    });
297
    parser.addNodelet("/sqlMapConfig/transactionManager/dataSource/end()", new Nodelet() {
1✔
298
      public void process(Node node) throws Exception {
299
        state.getConfig().getErrorContext().setActivity("configuring the data source");
1✔
300

301
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
302

303
        String type = attributes.getProperty("type");
1✔
304
        Properties props = state.getDsProps();
1✔
305

306
        type = state.getConfig().getTypeHandlerFactory().resolveAlias(type);
1✔
307
        try {
308
          state.getConfig().getErrorContext().setMoreInfo("Check the data source type or class.");
1✔
309
          DataSourceFactory dsFactory = (DataSourceFactory) Resources.instantiate(type);
1✔
310
          state.getConfig().getErrorContext().setMoreInfo("Check the data source properties or configuration.");
1✔
311
          dsFactory.initialize(props);
1✔
312
          state.setDataSource(dsFactory.getDataSource());
1✔
313
          state.getConfig().getErrorContext().setMoreInfo(null);
1✔
314
        } catch (Exception e) {
×
315
          if (e instanceof SqlMapException) {
×
316
            throw (SqlMapException) e;
×
317
          } else {
318
            throw new SqlMapException(
×
319
                "Error initializing DataSource.  Could not instantiate DataSourceFactory.  Cause: " + e, e);
320
          }
321
        }
1✔
322
      }
1✔
323
    });
324
  }
1✔
325

326
  /**
327
   * Adds the sql map nodelets.
328
   */
329
  protected void addSqlMapNodelets() {
330
    parser.addNodelet("/sqlMapConfig/sqlMap", new Nodelet() {
1✔
331
      public void process(Node node) throws Exception {
332
        state.getConfig().getErrorContext().setActivity("loading the SQL Map resource");
1✔
333

334
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
335

336
        String resource = attributes.getProperty("resource");
1✔
337
        String url = attributes.getProperty("url");
1✔
338

339
        if (usingStreams) {
1✔
340
          InputStream inputStream = null;
1✔
341
          if (resource != null) {
1!
342
            state.getConfig().getErrorContext().setResource(resource);
1✔
343
            inputStream = Resources.getResourceAsStream(resource);
1✔
344
          } else if (url != null) {
×
345
            state.getConfig().getErrorContext().setResource(url);
×
346
            inputStream = Resources.getUrlAsStream(url);
×
347
          } else {
348
            throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
×
349
          }
350

351
          new SqlMapParser(state).parse(inputStream);
1✔
352
        } else {
1✔
353
          Reader reader = null;
1✔
354
          if (resource != null) {
1!
355
            state.getConfig().getErrorContext().setResource(resource);
1✔
356
            reader = Resources.getResourceAsReader(resource);
1✔
357
          } else if (url != null) {
×
358
            state.getConfig().getErrorContext().setResource(url);
×
359
            reader = Resources.getUrlAsReader(url);
×
360
          } else {
361
            throw new SqlMapException("The <sqlMap> element requires either a resource or a url attribute.");
×
362
          }
363

364
          new SqlMapParser(state).parse(reader);
1✔
365
        }
366
      }
1✔
367
    });
368
  }
1✔
369

370
  /**
371
   * Adds the result object factory nodelets.
372
   */
373
  private void addResultObjectFactoryNodelets() {
374
    parser.addNodelet("/sqlMapConfig/resultObjectFactory", new Nodelet() {
1✔
375
      public void process(Node node) throws Exception {
376
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
377
        String type = attributes.getProperty("type");
1✔
378

379
        state.getConfig().getErrorContext().setActivity("configuring the Result Object Factory");
1✔
380
        ResultObjectFactory rof;
381
        try {
382
          rof = (ResultObjectFactory) Resources.instantiate(type);
1✔
383
          state.getConfig().setResultObjectFactory(rof);
1✔
384
        } catch (Exception e) {
×
385
          throw new SqlMapException("Error instantiating resultObjectFactory: " + type, e);
×
386
        }
1✔
387

388
      }
1✔
389
    });
390
    parser.addNodelet("/sqlMapConfig/resultObjectFactory/property", new Nodelet() {
1✔
391
      public void process(Node node) throws Exception {
392
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
393
        String name = attributes.getProperty("name");
1✔
394
        String value = NodeletUtils.parsePropertyTokens(attributes.getProperty("value"), state.getGlobalProps());
1✔
395
        state.getConfig().getDelegate().getResultObjectFactory().setProperty(name, value);
1✔
396
      }
1✔
397
    });
398
  }
1✔
399

400
}
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

© 2025 Coveralls, Inc