|
|
@ -2,28 +2,33 @@ |
|
|
|
package de.srsoftware; |
|
|
|
package de.srsoftware; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
import org.stringtemplate.v4.*; |
|
|
|
import org.slf4j.Logger; |
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
public class Templates extends STRawGroupDir { |
|
|
|
public class Templates { |
|
|
|
private static Templates singleton = null; |
|
|
|
private static Templates singleton = null; |
|
|
|
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(Templates.class); |
|
|
|
|
|
|
|
private Path dir = searchTemplates(); |
|
|
|
|
|
|
|
|
|
|
|
Templates() { |
|
|
|
public Templates() throws FileNotFoundException {} |
|
|
|
super(templateDir(), '«', '»'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String templateDir() { |
|
|
|
private static Path searchTemplates() throws FileNotFoundException { |
|
|
|
return templateDir(new File(System.getProperty("user.dir"))).get().getAbsolutePath(); |
|
|
|
return searchTemplates(new File(System.getProperty("user.dir"))).map(File::toPath).orElseThrow(() -> new FileNotFoundException("Missing template directory")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static Optional<File> templateDir(File dir) { |
|
|
|
private static Optional<File> searchTemplates(File dir) { |
|
|
|
if (dir.isDirectory()) { |
|
|
|
if (dir.isDirectory()) { |
|
|
|
var children = dir.listFiles(); |
|
|
|
var children = dir.listFiles(); |
|
|
|
for (File child : children) { |
|
|
|
for (File child : children) { |
|
|
|
if (child.isDirectory()) { |
|
|
|
if (child.isDirectory()) { |
|
|
|
if (child.getName().equals("templates")) return Optional.of(child); |
|
|
|
if (child.getName().equals("templates")) return Optional.of(child); |
|
|
|
var inner = templateDir(child); |
|
|
|
var inner = searchTemplates(child); |
|
|
|
if (inner.isPresent()) return inner; |
|
|
|
if (inner.isPresent()) return inner; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -35,4 +40,21 @@ public class Templates extends STRawGroupDir { |
|
|
|
if (singleton == null) singleton = new Templates(); |
|
|
|
if (singleton == null) singleton = new Templates(); |
|
|
|
return singleton; |
|
|
|
return singleton; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Optional<String> get(String path, Map<String, String> replacements) { |
|
|
|
|
|
|
|
var file = dir.resolve(path); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return Optional.of(Files.readString(file)).map(s -> replaceKeys(s,replacements)); |
|
|
|
|
|
|
|
// TODO: replacements
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
LOG.warn("Failed to read {}", path, e); |
|
|
|
|
|
|
|
return Optional.empty(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String replaceKeys(String text, Map<String, String> replacements) { |
|
|
|
|
|
|
|
for (Map.Entry<String, String> replacement : replacements.entrySet()) text = text.replace("{"+replacement.getKey()+"}",replacement.getValue()); |
|
|
|
|
|
|
|
return text; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|