PropertyParser.java

  1. /*
  2.  *    Copyright 2009-2024 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 org.apache.ibatis.parsing;

  17. import java.util.Properties;

  18. /**
  19.  * @author Clinton Begin
  20.  * @author Kazuki Shimizu
  21.  */
  22. public class PropertyParser {

  23.   private static final String KEY_PREFIX = "org.apache.ibatis.parsing.PropertyParser.";
  24.   /**
  25.    * The special property key that indicate whether enable a default value on placeholder.
  26.    * <p>
  27.    * The default value is {@code false} (indicate disable a default value on placeholder) If you specify the
  28.    * {@code true}, you can specify key and default value on placeholder (e.g. {@code ${db.username:postgres}}).
  29.    *
  30.    * @since 3.4.2
  31.    */
  32.   public static final String KEY_ENABLE_DEFAULT_VALUE = KEY_PREFIX + "enable-default-value";

  33.   /**
  34.    * The special property key that specify a separator for key and default value on placeholder.
  35.    * <p>
  36.    * The default separator is {@code ":"}.
  37.    *
  38.    * @since 3.4.2
  39.    */
  40.   public static final String KEY_DEFAULT_VALUE_SEPARATOR = KEY_PREFIX + "default-value-separator";

  41.   private static final String ENABLE_DEFAULT_VALUE = "false";
  42.   private static final String DEFAULT_VALUE_SEPARATOR = ":";

  43.   private PropertyParser() {
  44.     // Prevent Instantiation
  45.   }

  46.   public static String parse(String string, Properties variables) {
  47.     VariableTokenHandler handler = new VariableTokenHandler(variables);
  48.     GenericTokenParser parser = new GenericTokenParser("${", "}", handler);
  49.     return parser.parse(string);
  50.   }

  51.   private static class VariableTokenHandler implements TokenHandler {
  52.     private final Properties variables;
  53.     private final boolean enableDefaultValue;
  54.     private final String defaultValueSeparator;

  55.     private VariableTokenHandler(Properties variables) {
  56.       this.variables = variables;
  57.       this.enableDefaultValue = Boolean.parseBoolean(getPropertyValue(KEY_ENABLE_DEFAULT_VALUE, ENABLE_DEFAULT_VALUE));
  58.       this.defaultValueSeparator = getPropertyValue(KEY_DEFAULT_VALUE_SEPARATOR, DEFAULT_VALUE_SEPARATOR);
  59.     }

  60.     private String getPropertyValue(String key, String defaultValue) {
  61.       return variables == null ? defaultValue : variables.getProperty(key, defaultValue);
  62.     }

  63.     @Override
  64.     public String handleToken(String content) {
  65.       if (variables != null) {
  66.         String key = content;
  67.         if (enableDefaultValue) {
  68.           final int separatorIndex = content.indexOf(defaultValueSeparator);
  69.           String defaultValue = null;
  70.           if (separatorIndex >= 0) {
  71.             key = content.substring(0, separatorIndex);
  72.             defaultValue = content.substring(separatorIndex + defaultValueSeparator.length());
  73.           }
  74.           if (defaultValue != null) {
  75.             return variables.getProperty(key, defaultValue);
  76.           }
  77.         }
  78.         if (variables.containsKey(key)) {
  79.           return variables.getProperty(key);
  80.         }
  81.       }
  82.       return "${" + content + "}";
  83.     }
  84.   }

  85. }