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

mybatis / ibatis-2 / #341

08 Sep 2023 11:16PM UTC coverage: 64.938% (+0.03%) from 64.913%
#341

push

github

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

fixes #174, update GHA, maven wrapper, fix EOL markers, do not use star imports

5047 of 7772 relevant lines covered (64.94%)

0.65 hits per line

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

79.37
/src/main/java/com/ibatis/sqlmap/engine/builder/xml/SqlMapConfigParser.java
1
/*
2
 * Copyright 2004-2023 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
    return parse(reader);
1✔
82
  }
83

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

237
        javaType = state.getConfig().getTypeHandlerFactory().resolveAlias(javaType);
1✔
238
        callback = state.getConfig().getTypeHandlerFactory().resolveAlias(callback);
1✔
239

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

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

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

299
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
300

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

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

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

332
        Properties attributes = NodeletUtils.parseAttributes(node, state.getGlobalProps());
1✔
333

334
        String resource = attributes.getProperty("resource");
1✔
335
        String url = attributes.getProperty("url");
1✔
336

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

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

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

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

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

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

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