Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
small refactoring to understand the code better
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSnoozer committed Oct 12, 2019
1 parent 963a0ad commit a556257
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions maven/src/main/java/pl/project13/maven/git/PropertiesReplacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,54 @@ public PropertiesReplacer(LoggerBridge log) {
this.log = log;
}

/**
* @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) {
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String)entry.getKey();
String content = (String)entry.getValue();
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
propertiesToBeAdded.put(newPropertyKey, result);
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
entry.setValue(result);
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
}
}
properties.putAll(propertiesToBeAdded);
performReplacementOnAllGeneratedProperties(properties, replacementProperty);
} else {
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 + "'");
}
performReplacementOnSingleProperty(properties, replacementProperty, propertyKey);
}
}
}
}

private void performReplacementOnAllGeneratedProperties(Properties properties, ReplacementProperty replacementProperty) {
Map<Object, Object> propertiesToBeAdded = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String)entry.getKey();
String content = (String)entry.getValue();
String result = performReplacement(replacementProperty, content);
if ((replacementProperty.getPropertyOutputSuffix() != null) && (!replacementProperty.getPropertyOutputSuffix().isEmpty())) {
String newPropertyKey = key + "." + replacementProperty.getPropertyOutputSuffix();
propertiesToBeAdded.put(newPropertyKey, result);
log.info("apply replace on property " + key + " and save to " + newPropertyKey + ": original value '" + content + "' with '" + result + "'");
} else {
entry.setValue(result);
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
}
}
properties.putAll(propertiesToBeAdded);
}

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 result = content;
if (replacementProperty != null) {
Expand Down

0 comments on commit a556257

Please sign in to comment.