-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement placeholder replacement via mapping
- Loading branch information
1 parent
60ffb4f
commit 9ea11cf
Showing
8 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.docutools.jocument; | ||
|
||
public interface PlaceholderMapper { | ||
String map(String placeholder); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/docutools/jocument/impl/PlaceholderMapperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.docutools.jocument.impl; | ||
|
||
import com.docutools.jocument.PlaceholderMapper; | ||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class PlaceholderMapperImpl implements PlaceholderMapper { | ||
private static final Logger logger = LogManager.getLogger(); | ||
private static Map<String, String> placeholderMappings = new HashMap<>(); | ||
|
||
static { | ||
var pathString = System.getenv("DT_JT_RR_PLACEHOLDER_MAPPINGS"); | ||
if (pathString != null) { | ||
var path = Path.of(pathString); | ||
var file = path.toFile(); | ||
if (file.exists() && file.canRead()) { | ||
try { | ||
var reader = new BufferedReader(new FileReader(file)); | ||
placeholderMappings = reader.lines() | ||
.filter(line -> !line.isEmpty()) | ||
.filter(line -> !line.startsWith("//")) | ||
.map(line -> line.split(":")) | ||
.collect(Collectors.toMap(strings -> strings[0], strings -> strings[1])); | ||
} catch (IOException e) { | ||
logger.error(e); | ||
} | ||
} else if (!file.exists()) { | ||
logger.error("Mappings file {} does not exist", pathString); | ||
} else if (!file.canRead()) { | ||
logger.error("Mappings file {} can not be read", pathString); | ||
} | ||
} else { | ||
logger.info("No mapper found"); | ||
} | ||
} | ||
|
||
@Override | ||
public String map(String placeholder) { | ||
return placeholderMappings.getOrDefault(placeholder, placeholder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
captain-name:name | ||
officer-name:name | ||
officer-rank:rank | ||
officer-uniform:uniform | ||
service-ship-name:shipName |
Binary file not shown.