-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathPropertiesReplacer.java
More file actions
199 lines (188 loc) · 7.45 KB
/
PropertiesReplacer.java
File metadata and controls
199 lines (188 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-maven-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.git;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.regex.Pattern;
import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
import pl.project13.core.log.LogInterface;
/**
* This class encapsulates logic to perform property replacements. For a use-case refer to
* https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/317.
*/
public class PropertiesReplacer {
private final LogInterface log;
private final PluginParameterExpressionEvaluator expressionEvaluator;
/**
* Constructor to encapsulates all references required to perform property replacements.
*
* @param log The logger to log any messages
* @param expressionEvaluator Maven's PluginParameterExpressionEvaluator (see
* https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/413 why it's needed)
*/
public PropertiesReplacer(
LogInterface log, PluginParameterExpressionEvaluator expressionEvaluator) {
this.log = log;
this.expressionEvaluator = expressionEvaluator;
}
/**
* Method that performs the actual property replacement.
*
* @param properties all properties that are being generated by the plugin
* @param replacementProperties list of all replacement actions to perform
*/
public void performReplacement(
Properties properties, List<ReplacementProperty> replacementProperties) {
if ((replacementProperties != null) && (properties != null)) {
for (ReplacementProperty replacementProperty : replacementProperties) {
String propertyKey = replacementProperty.getProperty();
if (propertyKey == null) {
performReplacementOnAllGeneratedProperties(properties, replacementProperty);
} else {
performReplacementOnSingleProperty(properties, replacementProperty, propertyKey);
}
}
}
}
private void performReplacementOnAllGeneratedProperties(
Properties properties, ReplacementProperty replacementProperty) {
for (String propertyName : properties.stringPropertyNames()) {
String content = properties.getProperty(propertyName);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null)
&& (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyName + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info(
"apply replace on property "
+ propertyName
+ " and save to "
+ newPropertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
} else {
properties.setProperty(propertyName, result);
log.info(
"apply replace on property "
+ propertyName
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
}
}
}
private void performReplacementOnSingleProperty(
Properties properties, ReplacementProperty replacementProperty, String propertyKey) {
String content = properties.getProperty(propertyKey);
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null)
&& (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = propertyKey + "." + replacementProperty.getPropertyOutputSuffix();
properties.setProperty(newPropertyKey, result);
log.info(
"apply replace on property "
+ propertyKey
+ " and save to "
+ newPropertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
} else {
properties.setProperty(propertyKey, result);
log.info(
"apply replace on property "
+ propertyKey
+ ": original value '"
+ content
+ "' with '"
+ result
+ "'");
}
}
private String performReplacement(ReplacementProperty replacementProperty, String content) {
String evaluationContent = content;
if (evaluationContent == null
|| evaluationContent.isEmpty()
|| replacementProperty.isForceValueEvaluation()) {
evaluationContent = replacementProperty.getValue();
}
String result = "";
try {
result =
Optional.ofNullable(expressionEvaluator.evaluate(evaluationContent))
.map(x -> x.toString())
.orElse(evaluationContent);
} catch (Exception e) {
log.error("Something went wrong performing the replacement.", e);
}
if (replacementProperty != null) {
result =
performTransformationRules(
replacementProperty, result, TransformationRule.ApplyEnum.BEFORE);
if (replacementProperty.isRegex()) {
result =
replaceRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
} else {
result =
replaceNonRegex(result, replacementProperty.getToken(), replacementProperty.getValue());
}
result =
performTransformationRules(
replacementProperty, result, TransformationRule.ApplyEnum.AFTER);
}
return result;
}
private String performTransformationRules(
ReplacementProperty replacementProperty,
String content,
TransformationRule.ApplyEnum forRule) {
String result = content;
if ((replacementProperty.getTransformationRules() != null)
&& (!replacementProperty.getTransformationRules().isEmpty())) {
for (TransformationRule transformationRule : replacementProperty.getTransformationRules()) {
if (transformationRule.getApplyRule().equals(forRule)) {
result = transformationRule.getActionRule().perform(result);
}
}
}
return result;
}
private String replaceRegex(String content, String token, String value) {
if (token == null) {
log.error("found replacementProperty without required token.");
return content;
}
final Pattern compiledPattern = Pattern.compile(token);
return compiledPattern.matcher(content).replaceAll(value == null ? "" : value);
}
private String replaceNonRegex(String content, String token, String value) {
if (token == null) {
log.error("found replacementProperty without required token.");
return content;
}
return content.replace(token, value == null ? "" : value);
}
}