View Javadoc
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.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   * The Class XmlParserState.
35   */
36  public class XmlParserState {
37  
38    /** The config. */
39    private SqlMapConfiguration config = new SqlMapConfiguration();
40  
41    /** The global props. */
42    private Properties globalProps = new Properties();
43  
44    /** The tx props. */
45    private Properties txProps = new Properties();
46  
47    /** The ds props. */
48    private Properties dsProps = new Properties();
49  
50    /** The cache props. */
51    private Properties cacheProps = new Properties();
52  
53    /** The use statement namespaces. */
54    private boolean useStatementNamespaces = false;
55  
56    /** The sql includes. */
57    private Map sqlIncludes = new HashMap<>();
58  
59    /** The param config. */
60    private ParameterMapConfig paramConfig;
61  
62    /** The result config. */
63    private ResultMapConfig resultConfig;
64  
65    /** The cache config. */
66    private CacheModelConfig cacheConfig;
67  
68    /** The namespace. */
69    private String namespace;
70  
71    /** The data source. */
72    private DataSource dataSource;
73  
74    /**
75     * Gets the config.
76     *
77     * @return the config
78     */
79    public SqlMapConfiguration getConfig() {
80      return config;
81    }
82  
83    /**
84     * Sets the global props.
85     *
86     * @param props
87     *          the new global props
88     */
89    public void setGlobalProps(Properties props) {
90      globalProps = props;
91    }
92  
93    /**
94     * Gets the global props.
95     *
96     * @return the global props
97     */
98    public Properties getGlobalProps() {
99      return globalProps;
100   }
101 
102   /**
103    * Gets the tx props.
104    *
105    * @return the tx props
106    */
107   public Properties getTxProps() {
108     return txProps;
109   }
110 
111   /**
112    * Gets the ds props.
113    *
114    * @return the ds props
115    */
116   public Properties getDsProps() {
117     return dsProps;
118   }
119 
120   /**
121    * Gets the cache props.
122    *
123    * @return the cache props
124    */
125   public Properties getCacheProps() {
126     return cacheProps;
127   }
128 
129   /**
130    * Sets the use statement namespaces.
131    *
132    * @param useStatementNamespaces
133    *          the new use statement namespaces
134    */
135   public void setUseStatementNamespaces(boolean useStatementNamespaces) {
136     this.useStatementNamespaces = useStatementNamespaces;
137   }
138 
139   /**
140    * Checks if is use statement namespaces.
141    *
142    * @return true, if is use statement namespaces
143    */
144   public boolean isUseStatementNamespaces() {
145     return useStatementNamespaces;
146   }
147 
148   /**
149    * Gets the sql includes.
150    *
151    * @return the sql includes
152    */
153   public Map getSqlIncludes() {
154     return sqlIncludes;
155   }
156 
157   /**
158    * Sets the namespace.
159    *
160    * @param namespace
161    *          the new namespace
162    */
163   public void setNamespace(String namespace) {
164     this.namespace = namespace;
165   }
166 
167   /**
168    * Apply namespace.
169    *
170    * @param id
171    *          the id
172    *
173    * @return the string
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    * Gets the cache config.
185    *
186    * @return the cache config
187    */
188   public CacheModelConfig getCacheConfig() {
189     return cacheConfig;
190   }
191 
192   /**
193    * Sets the cache config.
194    *
195    * @param cacheConfig
196    *          the new cache config
197    */
198   public void setCacheConfig(CacheModelConfig cacheConfig) {
199     this.cacheConfig = cacheConfig;
200   }
201 
202   /**
203    * Gets the param config.
204    *
205    * @return the param config
206    */
207   public ParameterMapConfig getParamConfig() {
208     return paramConfig;
209   }
210 
211   /**
212    * Sets the param config.
213    *
214    * @param paramConfig
215    *          the new param config
216    */
217   public void setParamConfig(ParameterMapConfig paramConfig) {
218     this.paramConfig = paramConfig;
219   }
220 
221   /**
222    * Gets the result config.
223    *
224    * @return the result config
225    */
226   public ResultMapConfig getResultConfig() {
227     return resultConfig;
228   }
229 
230   /**
231    * Sets the result config.
232    *
233    * @param resultConfig
234    *          the new result config
235    */
236   public void setResultConfig(ResultMapConfig resultConfig) {
237     this.resultConfig = resultConfig;
238   }
239 
240   /**
241    * Gets the first token.
242    *
243    * @param s
244    *          the s
245    *
246    * @return the first token
247    */
248   public String getFirstToken(String s) {
249     return new StringTokenizer(s, ", ", false).nextToken();
250   }
251 
252   /**
253    * Gets the all but first token.
254    *
255    * @param s
256    *          the s
257    *
258    * @return the all but first token
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    * Sets the global properties.
272    *
273    * @param resource
274    *          the resource
275    * @param url
276    *          the url
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       // Merge properties with those passed in programmatically
293       if (props != null) {
294         props.putAll(globalProps);
295         globalProps = props;
296       }
297 
298       // Check for custom executors
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    * Gets the data source.
319    *
320    * @return the data source
321    */
322   public DataSource getDataSource() {
323     return dataSource;
324   }
325 
326   /**
327    * Sets the data source.
328    *
329    * @param dataSource
330    *          the new data source
331    */
332   public void setDataSource(DataSource dataSource) {
333     this.dataSource = dataSource;
334   }
335 }