View Javadoc
1   /*
2    * Copyright 2004-2022 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.client;
17  
18  import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser;
19  
20  import java.io.InputStream;
21  import java.io.Reader;
22  import java.util.Properties;
23  
24  /**
25   * Builds SqlMapClient instances from a supplied resource (e.g. XML configuration file)
26   * <p>
27   * The SqlMapClientBuilder class is responsible for parsing configuration documents and building the SqlMapClient
28   * instance. Its current implementation works with XML configuration files (e.g. sql-map-config.xml).
29   * <p>
30   * Example:
31   *
32   * <pre>
33   * Reader reader = Resources.getResourceAsReader(&quot;properties/sql-map-config.xml&quot;);
34   * SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient(reader);
35   * </pre>
36   * <p>
37   * Examples of the XML document structure used by SqlMapClientBuilder can be found at the links below.
38   * <p>
39   * Note: They might look big, but they're mostly comments!
40   * <ul>
41   * <li><a href="sql-map-config.txt">The SQL Map Config File</a>
42   * <li><a href="sql-map.txt">An SQL Map File</a>
43   * </ul>
44   */
45  public class SqlMapClientBuilder {
46  
47    /**
48     * No instantiation allowed.
49     */
50    protected SqlMapClientBuilder() {
51    }
52  
53    /**
54     * Builds an SqlMapClient using the specified reader.
55     *
56     * @param reader
57     *          A Reader instance that reads an sql-map-config.xml file. The reader should read an well formed
58     *          sql-map-config.xml file.
59     *
60     * @return An SqlMapClient instance.
61     */
62    public static SqlMapClient buildSqlMapClient(Reader reader) {
63      // return new XmlSqlMapClientBuilder().buildSqlMap(reader);
64      return new SqlMapConfigParser().parse(reader);
65    }
66  
67    /**
68     * Builds an SqlMapClient using the specified reader and properties file.
69     * <p>
70     *
71     * @param reader
72     *          A Reader instance that reads an sql-map-config.xml file. The reader should read an well formed
73     *          sql-map-config.xml file.
74     * @param props
75     *          Properties to be used to provide values to dynamic property tokens in the sql-map-config.xml configuration
76     *          file. This provides an easy way to achieve some level of programmatic configuration.
77     *
78     * @return An SqlMapClient instance.
79     */
80    public static SqlMapClient buildSqlMapClient(Reader reader, Properties props) {
81      // return new XmlSqlMapClientBuilder().buildSqlMap(reader, props);
82      return new SqlMapConfigParser().parse(reader, props);
83    }
84  
85    /**
86     * Builds an SqlMapClient using the specified input stream.
87     *
88     * @param inputStream
89     *          An InputStream instance that reads an sql-map-config.xml file. The stream should read a well formed
90     *          sql-map-config.xml file.
91     *
92     * @return An SqlMapClient instance.
93     */
94    public static SqlMapClient buildSqlMapClient(InputStream inputStream) {
95      return new SqlMapConfigParser().parse(inputStream);
96    }
97  
98    /**
99     * Builds an SqlMapClient using the specified input stream and properties file.
100    * <p>
101    *
102    * @param inputStream
103    *          An InputStream instance that reads an sql-map-config.xml file. The stream should read an well formed
104    *          sql-map-config.xml file.
105    * @param props
106    *          Properties to be used to provide values to dynamic property tokens in the sql-map-config.xml configuration
107    *          file. This provides an easy way to achieve some level of programmatic configuration.
108    *
109    * @return An SqlMapClient instance.
110    */
111   public static SqlMapClient buildSqlMapClient(InputStream inputStream, Properties props) {
112     return new SqlMapConfigParser().parse(inputStream, props);
113   }
114 }