You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by sg...@apache.org on 2020/02/16 16:58:07 UTC

[freemarker-generator] branch FREEMARKER-129 updated: FREEMARKER-129 Use `freemarker.configuration.setting` in `freemarker-cli.properties` to configure FreeMarker

This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a commit to branch FREEMARKER-129
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git


The following commit(s) were added to refs/heads/FREEMARKER-129 by this push:
     new 13f6f72  FREEMARKER-129 Use `freemarker.configuration.setting` in `freemarker-cli.properties` to configure FreeMarker
13f6f72 is described below

commit 13f6f72a66ce9ded162c90df86201bbdee04d311
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sun Feb 16 17:57:53 2020 +0100

    FREEMARKER-129 Use `freemarker.configuration.setting` in `freemarker-cli.properties` to configure FreeMarker
---
 CHANGELOG.md                                       |  1 +
 .../generator/base/util/LocaleUtils.java           |  4 ++-
 .../generator/base/util/PropertiesTransformer.java | 20 +++++++++--
 .../generator/util/PropertiesTransformerTest.java  | 16 +++++++--
 .../src/main/config/freemarker-cli.properties      |  5 +--
 .../generator/cli/impl/ConfigurationSupplier.java  | 41 ++++++++++++++++------
 .../generator/cli/impl/ToolsSupplier.java          |  3 +-
 .../freemarker/generator/cli/model/Settings.java   |  2 +-
 .../src/main/resources/freemarker-cli.properties   |  5 +--
 9 files changed, 75 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 40b5f55..3b6a840 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. We try to a
 * [FREEMARKER-129] Migrate `freemarker-cli` into `freemarker-generator` project (see [https://github.com/sgoeschl/freemarker-cli](https://github.com/sgoeschl/freemarker-cli))
 
 ### Changed
+* [FREEMARKER-129] Use `freemarker.configuration.setting` in `freemarker-cli.properties` to configure FreeMarker
 * [FREEMARKER-129] Provide a `toString()` metheod for all tools
 * [FREEMARKER-129] Use version "0.X.Y" to cater for API changes according to [Sematik Versioning](https://semver.org)
 * [FREEMARKER-129] Change artifact `freemarker-maven-plugin` to `freemarker-generator-maven-plugin`
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/LocaleUtils.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/LocaleUtils.java
index 0131568..5edd405 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/LocaleUtils.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/LocaleUtils.java
@@ -18,10 +18,12 @@ package org.apache.freemarker.generator.base.util;
 
 import java.util.Locale;
 
+import static org.apache.freemarker.generator.base.util.StringUtils.isEmpty;
+
 public class LocaleUtils {
 
     public static Locale parseLocale(String value) {
-        if (StringUtils.isEmpty(value)) {
+        if (isEmpty(value) || value.equalsIgnoreCase("JVM default") || value.equalsIgnoreCase("default")) {
             return Locale.getDefault();
         }
 
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java
index 6da5ecf..bfd97fd 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/util/PropertiesTransformer.java
@@ -24,17 +24,31 @@ import java.util.Properties;
 public class PropertiesTransformer {
 
     /**
-     * Create a new <code>java.util.Properties</code> instance having the matching key prefix removed
+     * Create a new <code>java.util.Properties</code> instance having only key with the prefix.
      *
      * @param properties the properties
-     * @param prefix     prefix to be removed from the matching keys
+     * @param prefix     prefix
      * @return Properties
      */
-    public static Properties removeKeyPrefix(Properties properties, String prefix) {
+    public static Properties filterKeyPrefix(Properties properties, String prefix) {
         final Properties result = new Properties();
         properties.entrySet()
                 .stream()
                 .filter(entry -> entry.getKey().toString().startsWith(prefix))
+                .forEach(entry -> result.put(entry.getKey().toString(), entry.getValue()));
+        return result;
+    }
+
+    /**
+     * Create a new <code>java.util.Properties</code> instance having the key prefix removed.
+     *
+     * @param properties the properties
+     * @param prefix     prefix to be removed from the matching keys
+     * @return Properties
+     */
+    public static Properties removeKeyPrefix(Properties properties, String prefix) {
+        final Properties result = new Properties();
+        properties.entrySet()
                 .forEach(entry -> result.put(entry.getKey().toString().substring(prefix.length()), entry.getValue()));
         return result;
     }
diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java
index 710307a..aa7dcac 100644
--- a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java
+++ b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/util/PropertiesTransformerTest.java
@@ -23,15 +23,27 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.apache.freemarker.generator.base.util.PropertiesTransformer.filterKeyPrefix;
+import static org.apache.freemarker.generator.base.util.PropertiesTransformer.removeKeyPrefix;
 import static org.junit.Assert.assertEquals;
 
 public class PropertiesTransformerTest {
 
+    private final String PREFIX = "freemarker.tools.";
     private final Map<String, Object> settings = new HashMap<>();
 
     @Test
-    public void shouldCreateToolWithDefaultConstructor() {
-        final Properties properties = PropertiesTransformer.removeKeyPrefix(properties(), "freemarker.tools.");
+    public void shouldFilterKeyPrefix() {
+        final Properties properties = filterKeyPrefix(properties(), PREFIX);
+
+        assertEquals(2, properties.size());
+        assertEquals("o.a.f.g.t.commonscsv.CommonsCSVTool", properties.getProperty("freemarker.tools.CSVTool"));
+        assertEquals("o.a.f.g.t.commonscsv.ExecTool", properties.getProperty("freemarker.tools.ExecTool"));
+    }
+
+    @Test
+    public void shouldRemoveKeyPrefix() {
+        final Properties properties = removeKeyPrefix(filterKeyPrefix(properties(), PREFIX), PREFIX);
 
         assertEquals(2, properties.size());
         assertEquals("o.a.f.g.t.commonscsv.CommonsCSVTool", properties.getProperty("CSVTool"));
diff --git a/freemarker-generator-cli/src/main/config/freemarker-cli.properties b/freemarker-generator-cli/src/main/config/freemarker-cli.properties
index 129015f..af23b05 100644
--- a/freemarker-generator-cli/src/main/config/freemarker-cli.properties
+++ b/freemarker-generator-cli/src/main/config/freemarker-cli.properties
@@ -16,9 +16,10 @@
 ## ---------------------------------------------------------------------------
 
 #############################################################################
-# General configuration
+# General FreeMarker Configuration
+# See https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
 #############################################################################
-freemarker.locale=en_US
+# freemarker.configuration.setting.locale=JVM default
 
 #############################################################################
 # Configure FreeMarker Tools (name -> implementation class)
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ConfigurationSupplier.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ConfigurationSupplier.java
index a5b5c9a..f7ae651 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ConfigurationSupplier.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ConfigurationSupplier.java
@@ -24,14 +24,18 @@ import freemarker.template.TemplateExceptionHandler;
 import freemarker.template.Version;
 import org.apache.freemarker.generator.cli.model.Settings;
 
+import java.util.Properties;
 import java.util.function.Supplier;
 
 import static freemarker.core.TemplateClassResolver.ALLOWS_NOTHING_RESOLVER;
 import static freemarker.template.Configuration.VERSION_2_3_29;
 import static java.util.Objects.requireNonNull;
+import static org.apache.freemarker.generator.base.util.PropertiesTransformer.filterKeyPrefix;
+import static org.apache.freemarker.generator.base.util.PropertiesTransformer.removeKeyPrefix;
 
 public class ConfigurationSupplier implements Supplier<Configuration> {
 
+    private static final String FREEMARGER_CONFIGURATION_SETTING_PREFIX = "freemarker.configuration.setting.";
     private static final Version FREEMARKER_VERSION = VERSION_2_3_29;
 
     private final Settings settings;
@@ -44,16 +48,27 @@ public class ConfigurationSupplier implements Supplier<Configuration> {
 
     @Override
     public Configuration get() {
-        final Configuration configuration = new Configuration(FREEMARKER_VERSION);
-        configuration.setAPIBuiltinEnabled(false);
-        configuration.setDefaultEncoding(settings.getTemplateEncoding().name());
-        configuration.setLocale(settings.getLocale());
-        configuration.setObjectWrapper(objectWrapper());
-        configuration.setOutputEncoding(settings.getOutputEncoding().name());
-        configuration.setTemplateLoader(templateLoader.get());
-        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
-        configuration.setNewBuiltinClassResolver(ALLOWS_NOTHING_RESOLVER);
-        return configuration;
+        try {
+            final Configuration configuration = new Configuration(FREEMARKER_VERSION);
+
+            // safe default configuration
+            configuration.setAPIBuiltinEnabled(false);
+            configuration.setNewBuiltinClassResolver(ALLOWS_NOTHING_RESOLVER);
+
+            // apply all "freemarker.configuration.setting" values
+            configuration.setSettings(freeMarkerConfigurationSettings());
+
+            // override current configuration with caller-provided settings
+            configuration.setDefaultEncoding(settings.getTemplateEncoding().name());
+            configuration.setLocale(settings.getLocale());
+            configuration.setObjectWrapper(objectWrapper());
+            configuration.setOutputEncoding(settings.getOutputEncoding().name());
+            configuration.setTemplateLoader(templateLoader.get());
+
+            return configuration;
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to create FreeMarker configuration", e);
+        }
     }
 
     private static DefaultObjectWrapper objectWrapper() {
@@ -61,4 +76,10 @@ public class ConfigurationSupplier implements Supplier<Configuration> {
         builder.setIterableSupport(false);
         return builder.build();
     }
+
+    private Properties freeMarkerConfigurationSettings() {
+        return removeKeyPrefix(
+                filterKeyPrefix(settings.getConfiguration(), FREEMARGER_CONFIGURATION_SETTING_PREFIX),
+                FREEMARGER_CONFIGURATION_SETTING_PREFIX);
+    }
 }
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java
index 977cd1c..14bc836 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/impl/ToolsSupplier.java
@@ -24,6 +24,7 @@ import java.util.function.Supplier;
 
 import static java.util.Objects.requireNonNull;
 import static java.util.stream.Collectors.toMap;
+import static org.apache.freemarker.generator.base.util.PropertiesTransformer.filterKeyPrefix;
 import static org.apache.freemarker.generator.base.util.PropertiesTransformer.removeKeyPrefix;
 
 public class ToolsSupplier implements Supplier<Map<String, Object>> {
@@ -53,7 +54,7 @@ public class ToolsSupplier implements Supplier<Map<String, Object>> {
     }
 
     private Properties toolsProperties() {
-        return removeKeyPrefix(this.configuration, FREEMARKER_TOOLS_PREFIX);
+        return removeKeyPrefix(filterKeyPrefix(this.configuration, FREEMARKER_TOOLS_PREFIX), FREEMARKER_TOOLS_PREFIX);
     }
 
     private static boolean toolExists(String clazzName) {
diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/model/Settings.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/model/Settings.java
index 84b99be..c3c08d7 100644
--- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/model/Settings.java
+++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/model/Settings.java
@@ -42,7 +42,7 @@ public class Settings {
     private static final Locale DEFAULT_LOCALE = US;
     private static final Charset DEFAULT_CHARSET = UTF_8;
 
-    private static final String FREEMARKER_CLI_LOCALE_KEY = "freemarker.locale";
+    private static final String FREEMARKER_CLI_LOCALE_KEY = "freemarker.configuration.setting.locale";
 
     /** FreeMarker CLI configuration containing tools */
     private final Properties configuration;
diff --git a/freemarker-generator-cli/src/main/resources/freemarker-cli.properties b/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
index d6337c6..8dd5f59 100644
--- a/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
+++ b/freemarker-generator-cli/src/main/resources/freemarker-cli.properties
@@ -16,9 +16,10 @@
 ## ---------------------------------------------------------------------------
 
 #############################################################################
-# General configuration
+# General FreeMarker Configuration
+# See https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-
 #############################################################################
-freemarker.locale=en_US
+# freemarker.configuration.setting.locale=JVM default
 
 #############################################################################
 # Configure FreeMarker Tools (name -> implementation class)