Repository: freemarker
Updated Branches:
  refs/heads/3 7a3c48172 -> a89a67713


Continued work on TemplateLanguage configuration and new file extensions: 
Banned *.ftl* by default (with error message saying that FM2 templates aren't 
supported in FM3). Also improved UnparsedTemplateLanguage a bit.


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

Branch: refs/heads/3
Commit: 4b5422e0180db58a3dd52c49eccb531e3c6f6e20
Parents: 7a3c481
Author: ddekany <ddek...@apache.org>
Authored: Fri Apr 13 17:46:46 2018 +0200
Committer: ddekany <ddek...@apache.org>
Committed: Fri Apr 13 17:47:20 2018 +0200

----------------------------------------------------------------------
 FM3-CHANGE-LOG.txt                              |  3 +-
 .../core/TemplateConfigurationTest.java         |  4 +-
 .../core/TemplateGetEncodingTest.java           |  2 +-
 .../freemarker/core/TemplateLanguageTest.java   | 73 +++++++++++++++++++-
 .../apache/freemarker/core/Configuration.java   | 11 +--
 ...utableParsingAndProcessingConfiguration.java |  2 +-
 .../core/UnparsedTemplateLanguage.java          | 15 +++-
 .../core/UnsupportedFM2TemplateLanguage.java    | 63 +++++++++++++++++
 8 files changed, 160 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/FM3-CHANGE-LOG.txt
----------------------------------------------------------------------
diff --git a/FM3-CHANGE-LOG.txt b/FM3-CHANGE-LOG.txt
index b849d9a..c30b690 100644
--- a/FM3-CHANGE-LOG.txt
+++ b/FM3-CHANGE-LOG.txt
@@ -253,7 +253,8 @@ Major changes / features
      the `Configuration` or other kind of `ParsingConfiguration`). This is 
important for syntax highlighting of the
      static parts, hence it's encoded into the file extension too. It's also 
important for security reasons, 
   So a common file extensions will be "f3ah".
-  The FM2 file extensions, "ftl", "ftlh", and "ftlx" are banned to prevent 
confusion [TODO: not yet...].
+  The FM2 file extensions, "ftl", "ftlh", and "ftlx" by default give error (FM 
2 templates not supported) to
+  prevent confusion.
   
 
 Smaller Java API changes (categorized)

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java
index fdd699c..145e347 100644
--- 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java
@@ -190,7 +190,7 @@ public class TemplateConfigurationTest {
                 ImmutableMap.of("dummy", 
EpochMillisTemplateDateFormatFactory.INSTANCE));
 
         // Parser-only settings:
-        SETTING_ASSIGNMENTS.put("templateLanguage", 
UnparsedTemplateLanguage.INSTANCE);
+        SETTING_ASSIGNMENTS.put("templateLanguage", 
UnparsedTemplateLanguage.F3UU);
         SETTING_ASSIGNMENTS.put("tagSyntax", TagSyntax.SQUARE_BRACKET);
         SETTING_ASSIGNMENTS.put("interpolationSyntax", 
InterpolationSyntax.SQUARE_BRACKET);
         SETTING_ASSIGNMENTS.put("whitespaceStripping", false);
@@ -621,7 +621,7 @@ public class TemplateConfigurationTest {
             // assertOutput here, as that hard-coded to create an FTL Template.
 
             TemplateConfiguration.Builder tcb = new 
TemplateConfiguration.Builder();
-            tcb.setTemplateLanguage(UnparsedTemplateLanguage.INSTANCE);
+            tcb.setTemplateLanguage(UnparsedTemplateLanguage.F3UU);
 
             TestConfigurationBuilder cfgB = new TestConfigurationBuilder();
             cfgB.setTemplateConfigurations(

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java
index 0c16fe6..8e993b9 100644
--- 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java
@@ -44,7 +44,7 @@ public class TemplateGetEncodingTest {
             tl.putTextTemplate("text", "test");
             tl.putTextTemplate("text-static", "<#test>");
             TemplateConfiguration.Builder staticTextTCB = new 
TemplateConfiguration.Builder();
-            
staticTextTCB.setTemplateLanguage(UnparsedTemplateLanguage.INSTANCE);
+            staticTextTCB.setTemplateLanguage(UnparsedTemplateLanguage.F3UU);
             cfgB.setTemplateConfigurations(
                     new ConditionalTemplateConfigurationFactory(
                             new FileNameGlobMatcher("*-static*"), 
staticTextTCB.build()));

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateLanguageTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateLanguageTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateLanguageTest.java
index 211dec1..47026d3 100644
--- 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateLanguageTest.java
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/TemplateLanguageTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.freemarker.core;
 
+import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
 
 import java.io.IOException;
@@ -26,12 +27,17 @@ import java.io.InputStream;
 import java.io.Reader;
 
 import org.apache.freemarker.core.outputformat.OutputFormat;
+import org.apache.freemarker.core.outputformat.impl.HTMLOutputFormat;
+import org.apache.freemarker.core.outputformat.impl.RTFOutputFormat;
+import org.apache.freemarker.core.outputformat.impl.UndefinedOutputFormat;
+import org.apache.freemarker.core.outputformat.impl.XMLOutputFormat;
+import org.apache.freemarker.test.TestConfigurationBuilder;
 import org.junit.Test;
 
 public class TemplateLanguageTest {
     
     @Test
-    public void fileExtensionRestrictionsTest() {
+    public void testFileExtensionRestrictions() {
         new DummyTemplateLanguage("xfoo", null, null);
         
         new DummyTemplateLanguage("ffoo", true, null, null);
@@ -82,5 +88,70 @@ public class TemplateLanguageTest {
         }
         
     }
+    
+    @Test
+    public void testDefaultFileExtensionsRecognized() throws Exception {
+        Configuration cfg = new TestConfigurationBuilder()
+                .outputFormat(RTFOutputFormat.INSTANCE)
+                .build();
+        
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3ac", "", cfg),
+                DefaultTemplateLanguage.F3AC, RTFOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.notRecognized", "", cfg),
+                DefaultTemplateLanguage.F3AC, RTFOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3au", "", cfg),
+                DefaultTemplateLanguage.F3AU, UndefinedOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3ah", "", cfg),
+                DefaultTemplateLanguage.F3AH, HTMLOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3ax", "", cfg),
+                DefaultTemplateLanguage.F3AX, XMLOutputFormat.INSTANCE);
+        
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3sc", "", cfg),
+                DefaultTemplateLanguage.F3SC, RTFOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3su", "", cfg),
+                DefaultTemplateLanguage.F3SU, UndefinedOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3sh", "", cfg),
+                DefaultTemplateLanguage.F3SH, HTMLOutputFormat.INSTANCE);
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3sx", "", cfg),
+                DefaultTemplateLanguage.F3SX, XMLOutputFormat.INSTANCE);
+        
+        assertLanguageAndOutputFormat(
+                new Template("foo.f3uu", "", cfg),
+                UnparsedTemplateLanguage.F3UU, UndefinedOutputFormat.INSTANCE);
+        
+       try {
+           new Template("foo.ftl", "", cfg);
+           fail();
+       } catch (ParseException e) {
+           assertThat(e.getMessage(), allOf(containsString("FreeMarker 2"), 
containsString("*.ftl")));
+       }
+       try {
+           new Template("foo.ftlh", "", cfg);
+           fail();
+       } catch (ParseException e) {
+           assertThat(e.getMessage(), allOf(containsString("FreeMarker 2"), 
containsString("*.ftlh")));
+       }
+       try {
+           new Template("foo.ftlx", "", cfg);
+           fail();
+       } catch (ParseException e) {
+           assertThat(e.getMessage(), allOf(containsString("FreeMarker 2"), 
containsString("*.ftlx")));
+       }
+    }
+
+    private void assertLanguageAndOutputFormat(Template template, 
TemplateLanguage expectedLanguage,
+            OutputFormat expectedOutputFromat) {
+        assertEquals(expectedLanguage, 
template.getParsingConfiguration().getTemplateLanguage());
+        assertEquals(expectedOutputFromat, template.getOutputFormat());
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core/src/main/java/org/apache/freemarker/core/Configuration.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/Configuration.java 
b/freemarker-core/src/main/java/org/apache/freemarker/core/Configuration.java
index 60c372a..be9f802 100644
--- 
a/freemarker-core/src/main/java/org/apache/freemarker/core/Configuration.java
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/Configuration.java
@@ -762,13 +762,16 @@ public final class Configuration implements 
TopLevelConfiguration, CustomStateSc
     
     static final Map<String, TemplateLanguage> 
PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION;
     static {
-        PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION = new HashMap<String, 
TemplateLanguage>(
-                (DefaultTemplateLanguage.STANDARD_INSTANCES.length + 1) * 2, 
0.5f);
-        for (DefaultTemplateLanguage tl : 
DefaultTemplateLanguage.STANDARD_INSTANCES) {
+        PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION = new HashMap<String, 
TemplateLanguage>(32);
+        
+        for (TemplateLanguage tl : DefaultTemplateLanguage.STANDARD_INSTANCES) 
{
+            
PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION.put(tl.getFileExtension(), tl);      
      
+        }
+        for (TemplateLanguage tl : UnsupportedFM2TemplateLanguage.INSTANCES) {
             
PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION.put(tl.getFileExtension(), tl);      
      
         }
         PREDEFINED_TEMPLATE_LANGUAGES_BY_EXTENSION.put(
-                UnparsedTemplateLanguage.INSTANCE.getFileExtension(), 
UnparsedTemplateLanguage.INSTANCE);            
+                UnparsedTemplateLanguage.F3UU.getFileExtension(), 
UnparsedTemplateLanguage.F3UU);            
     }
     
     /**

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core/src/main/java/org/apache/freemarker/core/MutableParsingAndProcessingConfiguration.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/MutableParsingAndProcessingConfiguration.java
 
b/freemarker-core/src/main/java/org/apache/freemarker/core/MutableParsingAndProcessingConfiguration.java
index 5bcb652..0f6b83e 100644
--- 
a/freemarker-core/src/main/java/org/apache/freemarker/core/MutableParsingAndProcessingConfiguration.java
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/MutableParsingAndProcessingConfiguration.java
@@ -117,7 +117,7 @@ public abstract class 
MutableParsingAndProcessingConfiguration<
                         value.toLowerCase());
                 if (templateLanguage == null) {
                     if ("staticText".equals(value)) {
-                        templateLanguage = UnparsedTemplateLanguage.INSTANCE;
+                        templateLanguage = UnparsedTemplateLanguage.F3UU;
                     } else {
                         // TODO [FM3] Allow setting a custom template language 
by class name.
                         throw new InvalidSettingValueException(name, value, 
"Unsupported template language name");

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core/src/main/java/org/apache/freemarker/core/UnparsedTemplateLanguage.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/UnparsedTemplateLanguage.java
 
b/freemarker-core/src/main/java/org/apache/freemarker/core/UnparsedTemplateLanguage.java
index 430bda7..b2b9834 100644
--- 
a/freemarker-core/src/main/java/org/apache/freemarker/core/UnparsedTemplateLanguage.java
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/UnparsedTemplateLanguage.java
@@ -33,10 +33,13 @@ public final class UnparsedTemplateLanguage extends 
TemplateLanguage {
     
     private static final int READ_BUFFER_SIZE = 4096;
     
-    public static final TemplateLanguage INSTANCE = new 
UnparsedTemplateLanguage();
+    /**
+     * Instance with {@link UndefinedOutputFormat} output format. 
+     */
+    public static final UnparsedTemplateLanguage F3UU = new 
UnparsedTemplateLanguage(UndefinedOutputFormat.INSTANCE);
 
-    private UnparsedTemplateLanguage() {
-        super("f3uu", true, UndefinedOutputFormat.INSTANCE, 
AutoEscapingPolicy.ENABLE_IF_DEFAULT);
+    private UnparsedTemplateLanguage(OutputFormat outputFormat) {
+        super("f3uu", true, outputFormat, 
AutoEscapingPolicy.ENABLE_IF_DEFAULT);
     }
 
     /**
@@ -63,6 +66,12 @@ public final class UnparsedTemplateLanguage extends 
TemplateLanguage {
         ASTStaticText root = new ASTStaticText(sb.toString());
         root.setFieldsForRootElement();
         root.setLocation(template, -1, -1, -1, -1); // TODO [FM3] Positions 
are dummy
+        
+        template.setOutputFormat(contextOutputFormat != null ? 
contextOutputFormat
+                : getOutputFormat(template.getConfiguration()));
+        template.setAutoEscapingPolicy(contextAutoEscapingPolicy != null ? 
contextAutoEscapingPolicy
+                : getAutoEscapingPolicy());
+        
         return root;
     }
     

http://git-wip-us.apache.org/repos/asf/freemarker/blob/4b5422e0/freemarker-core/src/main/java/org/apache/freemarker/core/UnsupportedFM2TemplateLanguage.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/UnsupportedFM2TemplateLanguage.java
 
b/freemarker-core/src/main/java/org/apache/freemarker/core/UnsupportedFM2TemplateLanguage.java
new file mode 100644
index 0000000..0170f36
--- /dev/null
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/UnsupportedFM2TemplateLanguage.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.freemarker.core;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+import org.apache.freemarker.core.outputformat.OutputFormat;
+
+/**
+ * FreeMarker 2 template language, which we don't support. To avoid confusion 
we ban loading such templates. 
+ */
+class UnsupportedFM2TemplateLanguage extends TemplateLanguage {
+
+    private static final UnsupportedFM2TemplateLanguage FTL = new 
UnsupportedFM2TemplateLanguage(
+            "ftl", null, AutoEscapingPolicy.ENABLE_IF_DEFAULT);  
+    private static final UnsupportedFM2TemplateLanguage FTLH = new 
UnsupportedFM2TemplateLanguage(
+            "ftlh", null, AutoEscapingPolicy.ENABLE_IF_DEFAULT);  
+    private static final UnsupportedFM2TemplateLanguage FTLX = new 
UnsupportedFM2TemplateLanguage(
+            "ftlx", null, AutoEscapingPolicy.ENABLE_IF_DEFAULT);
+    
+    static final UnsupportedFM2TemplateLanguage[] INSTANCES = new 
UnsupportedFM2TemplateLanguage[] { FTL, FTLH, FTLX };
+    
+    private UnsupportedFM2TemplateLanguage(String fileExtension, OutputFormat 
outputFormat,
+            AutoEscapingPolicy autoEscapingPolicy) {
+        super(fileExtension, true, outputFormat, autoEscapingPolicy);
+    }
+
+    @Override
+    public boolean getCanSpecifyEncodingInContent() {
+        return false;
+    }
+
+    @Override
+    public ASTElement parse(Template template, Reader reader, 
ParsingConfiguration pCfg,
+            OutputFormat contextOutputFormat, AutoEscapingPolicy 
contextAutoEscapingPolicy,
+            InputStream streamToUnmarkWhenEncEstabd) throws IOException, 
ParseException {
+        throw new ParseException(
+                "FreeMarker 2 templates (*." + getFileExtension() + ") aren't 
supported in FreeMarker 3. "
+                + "(Note that FreeMarker 2 and 3 uses different Java packages, 
so you can use both in the same "
+                + "application.)",
+                template, 0, 0, 0, 0);
+    }
+
+}

Reply via email to