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