• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / migrations / #436

pending completion
#436

push

github

web-flow
Merge pull request #295 from hazendaz/develop

Run overall code cleanup using Eclipse

91 of 91 new or added lines in 19 files covered. (100.0%)

1777 of 2212 relevant lines covered (80.33%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.2
/src/main/java/org/apache/ibatis/migration/VariableReplacer.java
1
/*
2
 *    Copyright 2010-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 org.apache.ibatis.migration;
17

18
import java.util.Arrays;
19
import java.util.Collections;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Objects;
23
import java.util.stream.Collectors;
24

25
/**
26
 * @author Clinton Begin
27
 */
28
public class VariableReplacer {
29

30
  private static final String openToken = "${";
31
  private static final String closeToken = "}";
32
  private final List<Map<? extends Object, ? extends Object>> variablesList;
33

34
  public VariableReplacer(Map<? extends Object, ? extends Object> variablesList) {
35
    this(Arrays.asList(variablesList));
1✔
36
  }
1✔
37

38
  public VariableReplacer(List<Map<? extends Object, ? extends Object>> variablesList) {
1✔
39
    this.variablesList = variablesList == null ? Collections.emptyList()
1✔
40
        : variablesList.stream().filter(Objects::nonNull).collect(Collectors.toList());
1✔
41
  }
1✔
42

43
  public String replace(String text) {
44
    if (text == null || text.isEmpty()) {
1✔
45
      return "";
1✔
46
    }
47
    // search open token
48
    int start = text.indexOf(openToken);
1✔
49
    if (start == -1) {
1✔
50
      return text;
1✔
51
    }
52
    char[] src = text.toCharArray();
1✔
53
    int offset = 0;
1✔
54
    final StringBuilder builder = new StringBuilder();
1✔
55
    StringBuilder expression = null;
1✔
56
    while (start > -1) {
1✔
57
      if (start > 0 && src[start - 1] == '\\') {
1✔
58
        // this open token is escaped. remove the backslash and continue.
59
        builder.append(src, offset, start - offset - 1).append(openToken);
1✔
60
        offset = start + openToken.length();
1✔
61
      } else {
62
        // found open token. let's search close token.
63
        if (expression == null) {
1✔
64
          expression = new StringBuilder();
1✔
65
        } else {
66
          expression.setLength(0);
1✔
67
        }
68
        builder.append(src, offset, start - offset);
1✔
69
        offset = start + openToken.length();
1✔
70
        int end = text.indexOf(closeToken, offset);
1✔
71
        while (end > -1) {
1✔
72
          if (end <= offset || src[end - 1] != '\\') {
1✔
73
            expression.append(src, offset, end - offset);
1✔
74
            break;
1✔
75
          }
76
          // this close token is escaped. remove the backslash and continue.
77
          expression.append(src, offset, end - offset - 1).append(closeToken);
×
78
          offset = end + closeToken.length();
×
79
          end = text.indexOf(closeToken, offset);
×
80
        }
81
        if (end == -1) {
1✔
82
          // close token was not found.
83
          builder.append(src, start, src.length - start);
×
84
          offset = src.length;
×
85
        } else {
86
          appendWithReplace(builder, expression.toString());
1✔
87
          offset = end + closeToken.length();
1✔
88
        }
89
      }
90
      start = text.indexOf(openToken, offset);
1✔
91
    }
92
    if (offset < src.length) {
1✔
93
      builder.append(src, offset, src.length - offset);
1✔
94
    }
95
    return builder.toString();
1✔
96
  }
97

98
  private StringBuilder appendWithReplace(StringBuilder builder, String key) {
99
    String value = null;
1✔
100
    for (Map<? extends Object, ? extends Object> variables : variablesList) {
1✔
101
      value = (String) variables.get(key);
1✔
102
      if (value != null) {
1✔
103
        builder.append(value);
1✔
104
        break;
1✔
105
      }
106
    }
1✔
107
    if (value == null) {
1✔
108
      builder.append(openToken).append(key).append(closeToken);
1✔
109
    }
110
    return builder;
1✔
111
  }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc