From ef5cdbfe4eea3f285c1a18c1441d4fb81dd6af04 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Fri, 15 Mar 2019 22:52:30 +0100 Subject: [PATCH] Bugfix: propyerty key is value of enum constant. --- .../de/kreth/property2java/Generator.java | 64 ++++++++++++------- src/main/resources/template/enum_template.tpl | 11 ++-- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index ccaee0a..dc20df3 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -3,12 +3,13 @@ package de.kreth.property2java; import java.io.IOException; import java.io.Reader; import java.io.Writer; +import java.text.DateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import de.kreth.property2java.cli.ArgumentConfiguration; @@ -17,9 +18,11 @@ import freemarker.template.TemplateException; public class Generator { - private Configuration config; + private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); - private Template template; + private final Configuration config; + + private final Template template; public Generator(Configuration config) { this.config = config; @@ -27,14 +30,13 @@ public class Generator { template = FreemarkerConfig.INSTANCE.getTemplate(); } catch (IOException e) { - throw new IllegalStateException("Unable to load freemarker template", e); } } public void start() throws IOException, TemplateException { - for (Entry entry : config.getInput().entrySet()) { + for (Map.Entry entry : config.getInput().entrySet()) { String fileName = entry.getKey(); Writer out = config.outWriter(fileName); Properties properties = new Properties(); @@ -47,11 +49,13 @@ public class Generator { throws IOException, TemplateException { Map root = new HashMap<>(); + root.put("generator_name", getClass().getName()); + root.put("generation_date", dateTimeInstance.format(new Date())); root.put("package", config.getPackage()); root.put("fileName", fileName); root.put("classname", config.mapFilenameToClassName(fileName)); - List> entries = new ArrayList<>(); + List entries = new ArrayList<>(); root.put("entries", entries); @@ -62,24 +66,8 @@ public class Generator { final String propertyKeyString = propertyNames.nextElement(); final String propertyValue = properties.getProperty(propertyKeyString); - Entry entry = new Entry() { - - @Override - public String getKey() { - return propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"); - } - - @Override - public String getValue() { - return propertyValue; - } - - @Override - public String setValue(String value) { - throw new UnsupportedOperationException("Set Value not supported!"); - } - }; - entries.add(entry); + entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, + propertyValue)); } template.process(root, out); } @@ -89,4 +77,32 @@ public class Generator { generator.start(); } + public class Entry { + + public final String constant; + + public final String key; + + public final String value; + + public Entry(String constant, String key, String value) { + super(); + this.constant = constant; + this.key = key; + this.value = value; + } + + public String getConstant() { + return constant; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } + + } } diff --git a/src/main/resources/template/enum_template.tpl b/src/main/resources/template/enum_template.tpl index 19b5379..9ee662d 100644 --- a/src/main/resources/template/enum_template.tpl +++ b/src/main/resources/template/enum_template.tpl @@ -4,16 +4,19 @@ import java.util.ResourceBundle; import java.util.function.UnaryOperator; +import javax.annotation.processing.Generated; + /** - * Property key from ${fileName} + * Property keys from ${fileName} */ +@Generated(date = "${generation_date}", value = "${generator_name}") public enum ${classname} { -<#list entries as entry> +<#list entries as e> /** - * "${entry.value}" + * ${e.key} = "${e.value}" */ - ${entry.key} ("${entry.value}")<#sep>, + ${e.constant} ("${e.key}")<#sep>, ;