diff options
Diffstat (limited to 'node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java')
-rw-r--r-- | node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java deleted file mode 100644 index 553f2ffba36..00000000000 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/Templar.java +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.node.admin.task.util.file; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.Map; - -/** - * A very simple template engine when there's little complexity and lots of Velocity special characters $ and #, - * i.e. typically shell script. - * - * @author hakonhall - */ -public class Templar { - private final String template; - - private static final String prefix = "<%="; - private static final String suffix = "%>"; - - private final Map<String, String> settings = new HashMap<>(); - - public static Templar fromUtf8File(Path path) { - return new Templar(new UnixPath(path).readUtf8File()); - } - - public Templar(String template) { - this.template = template; - } - - public Templar set(String name, String value) { - settings.put(name, value); - return this; - } - - public String resolve() { - StringBuilder text = new StringBuilder(template.length() * 2); - - int start= 0; - int end; - - for (; start < template.length(); start = end) { - int prefixStart = template.indexOf(prefix, start); - - - if (prefixStart == -1) { - text.append(template, start, template.length()); - break; - } else { - text.append(template, start, prefixStart); - } - - int suffixStart = template.indexOf(suffix, prefixStart + prefix.length()); - if (suffixStart == -1) { - throw new IllegalArgumentException("Prefix at offset " + prefixStart + " is not terminated"); - } - - int prefixEnd = prefixStart + prefix.length(); - String name = template.substring(prefixEnd, suffixStart).trim(); - String value = settings.get(name); - if (value == null) { - throw new IllegalArgumentException("No value is set for name '" + name + "' at offset " + prefixEnd); - } - - text.append(value); - - end = suffixStart + suffix.length(); - } - - return text.toString(); - } - - public FileWriter getFileWriterTo(Path path) { - return new FileWriter(path, this::resolve); - } -} |