View Javadoc
1   /*
2    *    Copyright 2010-2026 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.migration;
17  
18  import java.util.Arrays;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Objects;
22  import java.util.stream.Collectors;
23  
24  /**
25   * The Class VariableReplacer.
26   */
27  public class VariableReplacer {
28  
29    private static final String OPEN_TOKEN = "${";
30    private static final String CLOSE_TOKEN = "}";
31    private final List<Map<? extends Object, ? extends Object>> variablesList;
32  
33    public VariableReplacer(Map<? extends Object, ? extends Object> variablesList) {
34      this(Arrays.asList(variablesList));
35    }
36  
37    public VariableReplacer(List<Map<? extends Object, ? extends Object>> variablesList) {
38      this.variablesList = variablesList == null ? List.of()
39          : variablesList.stream().filter(Objects::nonNull).collect(Collectors.toList());
40    }
41  
42    public String replace(String text) {
43      if (text == null || text.isEmpty()) {
44        return "";
45      }
46      // search open token
47      int start = text.indexOf(OPEN_TOKEN);
48      if (start == -1) {
49        return text;
50      }
51      char[] src = text.toCharArray();
52      int offset = 0;
53      final StringBuilder builder = new StringBuilder();
54      StringBuilder expression = null;
55      while (start > -1) {
56        if (start > 0 && src[start - 1] == '\\') {
57          // this open token is escaped. remove the backslash and continue.
58          builder.append(src, offset, start - offset - 1).append(OPEN_TOKEN);
59          offset = start + OPEN_TOKEN.length();
60        } else {
61          // found open token. let's search close token.
62          if (expression == null) {
63            expression = new StringBuilder();
64          } else {
65            expression.setLength(0);
66          }
67          builder.append(src, offset, start - offset);
68          offset = start + OPEN_TOKEN.length();
69          int end = text.indexOf(CLOSE_TOKEN, offset);
70          while (end > -1) {
71            if (end <= offset || src[end - 1] != '\\') {
72              expression.append(src, offset, end - offset);
73              break;
74            }
75            // this close token is escaped. remove the backslash and continue.
76            expression.append(src, offset, end - offset - 1).append(CLOSE_TOKEN);
77            offset = end + CLOSE_TOKEN.length();
78            end = text.indexOf(CLOSE_TOKEN, offset);
79          }
80          if (end == -1) {
81            // close token was not found.
82            builder.append(src, start, src.length - start);
83            offset = src.length;
84          } else {
85            appendWithReplace(builder, expression.toString());
86            offset = end + CLOSE_TOKEN.length();
87          }
88        }
89        start = text.indexOf(OPEN_TOKEN, offset);
90      }
91      if (offset < src.length) {
92        builder.append(src, offset, src.length - offset);
93      }
94      return builder.toString();
95    }
96  
97    private StringBuilder appendWithReplace(StringBuilder builder, String key) {
98      String value = null;
99      for (Map<? extends Object, ? extends Object> variables : variablesList) {
100       value = (String) variables.get(key);
101       if (value != null) {
102         builder.append(value);
103         break;
104       }
105     }
106     if (value == null) {
107       builder.append(OPEN_TOKEN).append(key).append(CLOSE_TOKEN);
108     }
109     return builder;
110   }
111 }