freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [1/3] incubator-freemarker git commit: FM2 to FM3 converter properties cleanup
Date Thu, 20 Jul 2017 15:23:11 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 23341885a -> 4306b7d73


FM2 to FM3 converter properties cleanup


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/557b5edf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/557b5edf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/557b5edf

Branch: refs/heads/3
Commit: 557b5edf6643cb2805c652577e0cca9b9bf70cf7
Parents: 2334188
Author: ddekany <ddekany@apache.org>
Authored: Thu Jul 20 14:23:46 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Thu Jul 20 14:23:46 2017 +0200

----------------------------------------------------------------------
 .../freemarker/converter/FM2ToFM3Converter.java | 48 ++++++++++++++++----
 .../converter/FM2ToFM3ConverterCLI.java         | 15 +++---
 .../converter/FM2ToFM3ConverterCLITest.java     |  1 -
 3 files changed, 45 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/557b5edf/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3Converter.java
----------------------------------------------------------------------
diff --git a/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3Converter.java
b/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3Converter.java
index ea9d9c2..3ba7a02 100644
--- a/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3Converter.java
+++ b/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3Converter.java
@@ -72,7 +72,7 @@ public class FM2ToFM3Converter extends Converter {
 
     public static final Pattern DEFAULT_INCLUDE = Pattern.compile("(?i).*\\.(fm|ftl(x|h)?)");
 
-    public static final Map<String, String> DEFAULT_FILE_EXTENSION_SUBSTITUTIONS
+    public static final Map<String, String> PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS
             = new ImmutableMap.Builder<String,String>()
                     .put("ftl", "fm3")
                     .put("ftlh", "fm3h")
@@ -80,7 +80,8 @@ public class FM2ToFM3Converter extends Converter {
                     .put("fm", "fm3")
                     .build();
 
-    private Map<String, String> fileExtensionSubtitutions = DEFAULT_FILE_EXTENSION_SUBSTITUTIONS;
+    private boolean predefinedFileExtensionSubstitutionsEnabled;
+    private Map<String, String> fileExtensionSubstitutions = PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS;
     private Properties freeMarker2Settings;
     private Configuration fm2Cfg;
     private StringTemplateLoader stringTemplateLoader;
@@ -138,9 +139,12 @@ public class FM2ToFM3Converter extends Converter {
 
         String ext = srcFileName.substring(lastDotIdx + 1);
 
-        String replacementExt = getFileExtensionSubtitutions().get(ext);
+        String replacementExt = getFileExtensionSubstitutions().get(ext);
         if (replacementExt == null) {
-            replacementExt = getFileExtensionSubtitutions().get(ext.toLowerCase());
+            replacementExt = getFileExtensionSubstitutions().get(ext.toLowerCase());
+        }
+        if (replacementExt == null && getPredefinedFileExtensionSubstitutionsEnabled())
{
+            replacementExt = PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS.get(ext.toLowerCase());
         }
         if (replacementExt == null) {
             return srcFileName;
@@ -220,13 +224,39 @@ public class FM2ToFM3Converter extends Converter {
         return encoding != null ? encoding : fm2Cfg.getEncoding(template.getLocale());
     }
 
-    public Map<String, String> getFileExtensionSubtitutions() {
-        return fileExtensionSubtitutions;
+    /**
+     * Getter pair of {@link #setPredefinedFileExtensionSubstitutionsEnabled(boolean)}
+     */
+    public boolean getPredefinedFileExtensionSubstitutionsEnabled() {
+        return predefinedFileExtensionSubstitutionsEnabled;
+    }
+
+    /**
+     * Whether to use {@link #PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS} when {@link #getFileExtensionSubstitutions()}
+     * contains no mapping for the source file extension; defaults to {@code true}.
+     */
+    public void setPredefinedFileExtensionSubstitutionsEnabled(boolean predefinedFileExtensionSubstitutionsEnabled)
{
+        this.predefinedFileExtensionSubstitutionsEnabled = predefinedFileExtensionSubstitutionsEnabled;
+    }
+
+    /**
+     * Getter pair of {@link #setFileExtensionSubstitutions(Map)} .
+     */
+    public Map<String, String> getFileExtensionSubstitutions() {
+        return fileExtensionSubstitutions;
     }
 
-    public void setFileExtensionSubtitutions(Map<String, String> fileExtensionSubtitutions)
{
-        _NullArgumentException.check("fileExtensionSubtitutions", fileExtensionSubtitutions);
-        this.fileExtensionSubtitutions = fileExtensionSubtitutions;
+    /**
+     * Defines source file file extensions to destination file extensions mappings, in additionally
to the
+     * {@linkplain #setPredefinedFileExtensionSubstitutionsEnabled(boolean) predefined file
extension substitutions}.
+     * It's recommended to use lower case file extensions as keys, as if there's no hit with
case sensitive lookup,
+     * it will be retried with the source file extension converted to lower case (so it will
be a case insensitive
+     * lookup in effect). Mappings given here has higher priority than those coming from
the
+     * {@linkplain #setPredefinedFileExtensionSubstitutionsEnabled(boolean) predefined file
extension substitutions}.
+     */
+    public void setFileExtensionSubstitutions(Map<String, String> fileExtensionSubstitutions)
{
+        _NullArgumentException.check("fileExtensionSubstitutions", fileExtensionSubstitutions);
+        this.fileExtensionSubstitutions = fileExtensionSubstitutions;
     }
 
     public Properties getFreeMarker2Settings() {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/557b5edf/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3ConverterCLI.java
----------------------------------------------------------------------
diff --git a/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3ConverterCLI.java
b/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3ConverterCLI.java
index 3d26ad8..b4b5dbd 100644
--- a/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3ConverterCLI.java
+++ b/freemarker-converter/src/main/java/org/apache/freemarker/converter/FM2ToFM3ConverterCLI.java
@@ -23,7 +23,6 @@ import java.io.File;
 import java.io.PrintWriter;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Pattern;
@@ -115,6 +114,7 @@ public class FM2ToFM3ConverterCLI {
         return new FM2ToFM3ConverterCLI(out).executeInternal(args);
     }
 
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     private int executeInternal(String... args) {
         if (args.length == 0) {
             printHelp(true);
@@ -155,14 +155,11 @@ public class FM2ToFM3ConverterCLI {
                     converter.setExclude(getRegexpOption(cl, EXCLUDE_OPTION));
                 }
 
-                Map<String, String> fileExtensionSubtitutions = cl.hasOption(NO_PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS)
-                        ? new HashMap<String, String>()
-                        : new HashMap<String, String>(FM2ToFM3Converter.DEFAULT_FILE_EXTENSION_SUBSTITUTIONS);
-                for (Map.Entry<Object, Object> entry
-                        : cl.getOptionProperties(FILE_EXTENSION_SUBSTITUTION).entrySet())
{
-                    fileExtensionSubtitutions.put((String) entry.getKey(), (String) entry.getValue());
-                }
-                converter.setFileExtensionSubtitutions(Collections.unmodifiableMap(fileExtensionSubtitutions));
+                converter.setPredefinedFileExtensionSubstitutionsEnabled(
+                        !cl.hasOption(NO_PREDEFINED_FILE_EXTENSION_SUBSTITUTIONS));
+
+                converter.setFileExtensionSubstitutions((Map) Collections.unmodifiableMap(
+                        cl.getOptionProperties(FILE_EXTENSION_SUBSTITUTION)));
 
                 converter.setFreeMarker2Settings(cl.getOptionProperties(FREEMARKER_2_SETTING_OPTION));
                 try {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/557b5edf/freemarker-converter/src/test/java/org/freemarker/converter/FM2ToFM3ConverterCLITest.java
----------------------------------------------------------------------
diff --git a/freemarker-converter/src/test/java/org/freemarker/converter/FM2ToFM3ConverterCLITest.java
b/freemarker-converter/src/test/java/org/freemarker/converter/FM2ToFM3ConverterCLITest.java
index 866c8b6..f9d3319 100644
--- a/freemarker-converter/src/test/java/org/freemarker/converter/FM2ToFM3ConverterCLITest.java
+++ b/freemarker-converter/src/test/java/org/freemarker/converter/FM2ToFM3ConverterCLITest.java
@@ -104,7 +104,6 @@ public class FM2ToFM3ConverterCLITest extends ConverterTest {
         assertCLIResult(SUCCESS_EXIT_STATUS, null, null,
                 srcDir.toString(), "-d", dstDir.toString(),
                 "--include", ".*",
-                "--no-predef-file-ext-substs",
                 "--file-ext-subst", "txt=txt3",
                 "--no-predef-file-ext-substs"
         );


Mime
View raw message