View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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 org.apache.ibatis.parsing;
17  
18  import java.util.Properties;
19  
20  /**
21   * @author Clinton Begin
22   * @author Kazuki Shimizu
23   */
24  public class PropertyParser {
25  
26    private static final String KEY_PREFIX = "org.apache.ibatis.parsing.PropertyParser.";
27    /**
28     * The special property key that indicate whether enable a default value on placeholder.
29     * <p>
30     *   The default value is {@code false} (indicate disable a default value on placeholder)
31     *   If you specify the {@code true}, you can specify key and default value on placeholder (e.g. {@code ${db.username:postgres}}).
32     * </p>
33     * @since 3.4.2
34     */
35    public static final String KEY_ENABLE_DEFAULT_VALUE = KEY_PREFIX + "enable-default-value";
36  
37    /**
38     * The special property key that specify a separator for key and default value on placeholder.
39     * <p>
40     *   The default separator is {@code ":"}.
41     * </p>
42     * @since 3.4.2
43     */
44    public static final String KEY_DEFAULT_VALUE_SEPARATOR = KEY_PREFIX + "default-value-separator";
45  
46    private static final String ENABLE_DEFAULT_VALUE = "false";
47    private static final String DEFAULT_VALUE_SEPARATOR = ":";
48  
49    private PropertyParser() {
50      // Prevent Instantiation
51    }
52  
53    public static String parse(String string, Properties variables) {
54      VariableTokenHandler handler = new VariableTokenHandler(variables);
55      GenericTokenParser parser = new GenericTokenParser("${", "}", handler);
56      return parser.parse(string);
57    }
58  
59    private static class VariableTokenHandler implements TokenHandler {
60      private final Properties variables;
61      private final boolean enableDefaultValue;
62      private final String defaultValueSeparator;
63  
64      private VariableTokenHandler(Properties variables) {
65        this.variables = variables;
66        this.enableDefaultValue = Boolean.parseBoolean(getPropertyValue(KEY_ENABLE_DEFAULT_VALUE, ENABLE_DEFAULT_VALUE));
67        this.defaultValueSeparator = getPropertyValue(KEY_DEFAULT_VALUE_SEPARATOR, DEFAULT_VALUE_SEPARATOR);
68      }
69  
70      private String getPropertyValue(String key, String defaultValue) {
71        return (variables == null) ? defaultValue : variables.getProperty(key, defaultValue);
72      }
73  
74      @Override
75      public String handleToken(String content) {
76        if (variables != null) {
77          String key = content;
78          if (enableDefaultValue) {
79            final int separatorIndex = content.indexOf(defaultValueSeparator);
80            String defaultValue = null;
81            if (separatorIndex >= 0) {
82              key = content.substring(0, separatorIndex);
83              defaultValue = content.substring(separatorIndex + defaultValueSeparator.length());
84            }
85            if (defaultValue != null) {
86              return variables.getProperty(key, defaultValue);
87            }
88          }
89          if (variables.containsKey(key)) {
90            return variables.getProperty(key);
91          }
92        }
93        return "${" + content + "}";
94      }
95    }
96  
97  }