1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.builder.xml;
17
18 import com.ibatis.common.resources.Resources;
19 import com.ibatis.sqlmap.engine.config.CacheModelConfig;
20 import com.ibatis.sqlmap.engine.config.ParameterMapConfig;
21 import com.ibatis.sqlmap.engine.config.ResultMapConfig;
22 import com.ibatis.sqlmap.engine.config.SqlMapConfiguration;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.StringTokenizer;
30
31 import javax.sql.DataSource;
32
33
34
35
36 public class XmlParserState {
37
38
39 private SqlMapConfiguration config = new SqlMapConfiguration();
40
41
42 private Properties globalProps = new Properties();
43
44
45 private Properties txProps = new Properties();
46
47
48 private Properties dsProps = new Properties();
49
50
51 private Properties cacheProps = new Properties();
52
53
54 private boolean useStatementNamespaces = false;
55
56
57 private Map sqlIncludes = new HashMap<>();
58
59
60 private ParameterMapConfig paramConfig;
61
62
63 private ResultMapConfig resultConfig;
64
65
66 private CacheModelConfig cacheConfig;
67
68
69 private String namespace;
70
71
72 private DataSource dataSource;
73
74
75
76
77
78
79 public SqlMapConfiguration getConfig() {
80 return config;
81 }
82
83
84
85
86
87
88
89 public void setGlobalProps(Properties props) {
90 globalProps = props;
91 }
92
93
94
95
96
97
98 public Properties getGlobalProps() {
99 return globalProps;
100 }
101
102
103
104
105
106
107 public Properties getTxProps() {
108 return txProps;
109 }
110
111
112
113
114
115
116 public Properties getDsProps() {
117 return dsProps;
118 }
119
120
121
122
123
124
125 public Properties getCacheProps() {
126 return cacheProps;
127 }
128
129
130
131
132
133
134
135 public void setUseStatementNamespaces(boolean useStatementNamespaces) {
136 this.useStatementNamespaces = useStatementNamespaces;
137 }
138
139
140
141
142
143
144 public boolean isUseStatementNamespaces() {
145 return useStatementNamespaces;
146 }
147
148
149
150
151
152
153 public Map getSqlIncludes() {
154 return sqlIncludes;
155 }
156
157
158
159
160
161
162
163 public void setNamespace(String namespace) {
164 this.namespace = namespace;
165 }
166
167
168
169
170
171
172
173
174
175 public String applyNamespace(String id) {
176 String newId = id;
177 if (namespace != null && !namespace.isEmpty() && id != null && id.indexOf('.') < 0) {
178 newId = namespace + "." + id;
179 }
180 return newId;
181 }
182
183
184
185
186
187
188 public CacheModelConfig getCacheConfig() {
189 return cacheConfig;
190 }
191
192
193
194
195
196
197
198 public void setCacheConfig(CacheModelConfig cacheConfig) {
199 this.cacheConfig = cacheConfig;
200 }
201
202
203
204
205
206
207 public ParameterMapConfig getParamConfig() {
208 return paramConfig;
209 }
210
211
212
213
214
215
216
217 public void setParamConfig(ParameterMapConfig paramConfig) {
218 this.paramConfig = paramConfig;
219 }
220
221
222
223
224
225
226 public ResultMapConfig getResultConfig() {
227 return resultConfig;
228 }
229
230
231
232
233
234
235
236 public void setResultConfig(ResultMapConfig resultConfig) {
237 this.resultConfig = resultConfig;
238 }
239
240
241
242
243
244
245
246
247
248 public String getFirstToken(String s) {
249 return new StringTokenizer(s, ", ", false).nextToken();
250 }
251
252
253
254
255
256
257
258
259
260 public String[] getAllButFirstToken(String s) {
261 List strings = new ArrayList<>();
262 StringTokenizer parser = new StringTokenizer(s, ", ", false);
263 parser.nextToken();
264 while (parser.hasMoreTokens()) {
265 strings.add(parser.nextToken());
266 }
267 return (String[]) strings.toArray(new String[strings.size()]);
268 }
269
270
271
272
273
274
275
276
277
278 public void setGlobalProperties(String resource, String url) {
279 config.getErrorContext().setActivity("loading global properties");
280 try {
281 Properties props;
282 if (resource != null) {
283 config.getErrorContext().setResource(resource);
284 props = Resources.getResourceAsProperties(resource);
285 } else if (url != null) {
286 config.getErrorContext().setResource(url);
287 props = Resources.getUrlAsProperties(url);
288 } else {
289 throw new RuntimeException("The " + "properties" + " element requires either a resource or a url attribute.");
290 }
291
292
293 if (props != null) {
294 props.putAll(globalProps);
295 globalProps = props;
296 }
297
298
299 String customizedSQLExecutor = globalProps.getProperty("sql_executor_class");
300 config.getErrorContext().setActivity("Loading SQLExecutor.");
301 if (customizedSQLExecutor != null) {
302 try {
303 config.getClient().getDelegate().setCustomExecutor(customizedSQLExecutor);
304 config.getClient().getDelegate().getSqlExecutor().init(config, globalProps);
305 } catch (Exception e) {
306 config.getErrorContext().setCause(e);
307 config.getErrorContext()
308 .setMoreInfo("Loading of customizedSQLExecutor failed. Please check Properties file.");
309 }
310 }
311
312 } catch (Exception e) {
313 throw new RuntimeException("Error loading properties. Cause: " + e, e);
314 }
315 }
316
317
318
319
320
321
322 public DataSource getDataSource() {
323 return dataSource;
324 }
325
326
327
328
329
330
331
332 public void setDataSource(DataSource dataSource) {
333 this.dataSource = dataSource;
334 }
335 }