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