You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by dd...@apache.org on 2015/09/24 00:55:08 UTC

incubator-freemarker git commit: Added XHTMLOutputFormat and TemplateXHTMLOutputModel.

Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3-gae b6bd3c2cc -> 6b2840c6c


Added XHTMLOutputFormat and TemplateXHTMLOutputModel.


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

Branch: refs/heads/2.3-gae
Commit: 6b2840c6cf767ed6f7adf86c917ea0d04af4a1f8
Parents: b6bd3c2
Author: ddekany <dd...@apache.org>
Authored: Thu Sep 24 00:54:43 2015 +0200
Committer: ddekany <dd...@apache.org>
Committed: Thu Sep 24 00:54:43 2015 +0200

----------------------------------------------------------------------
 .../core/BuiltInsForStringsEncoding.java        |  2 +-
 .../core/TemplateXHTMLOutputModel.java          | 40 +++++++++++
 .../java/freemarker/core/XHTMLOutputFormat.java | 73 ++++++++++++++++++++
 src/manual/book.xml                             | 20 ++++++
 .../java/freemarker/core/NumberFormatTest.java  |  6 ++
 .../PrintfGTemplateNumberFormatFactory.java     |  3 +-
 .../freemarker/core/XHTMLOutputFormatTest.java  | 60 ++++++++++++++++
 7 files changed, 201 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/main/java/freemarker/core/BuiltInsForStringsEncoding.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/BuiltInsForStringsEncoding.java b/src/main/java/freemarker/core/BuiltInsForStringsEncoding.java
index f182430..4f02805 100644
--- a/src/main/java/freemarker/core/BuiltInsForStringsEncoding.java
+++ b/src/main/java/freemarker/core/BuiltInsForStringsEncoding.java
@@ -152,7 +152,7 @@ class BuiltInsForStringsEncoding {
 
         @Override
         MarkupOutputFormat getMarkupOutputFormat() {
-            return XMLOutputFormat.INSTANCE; // TODO XHTMLOutputFormat
+            return XHTMLOutputFormat.INSTANCE;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/main/java/freemarker/core/TemplateXHTMLOutputModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/TemplateXHTMLOutputModel.java b/src/main/java/freemarker/core/TemplateXHTMLOutputModel.java
new file mode 100644
index 0000000..b7564e2
--- /dev/null
+++ b/src/main/java/freemarker/core/TemplateXHTMLOutputModel.java
@@ -0,0 +1,40 @@
+/*
+ * 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 freemarker.core;
+
+/**
+ * Stores HTML markup to be printed; used with {@link HTMLOutputFormat}.
+ * 
+ * @since 2.3.24
+ */
+public final class TemplateXHTMLOutputModel extends CommonTemplateMarkupOutputModel<TemplateXHTMLOutputModel> {
+    
+    /**
+     * See {@link CommonTemplateMarkupOutputModel#CommonTemplateMarkupOutputModel(String, String)}.
+     */
+    TemplateXHTMLOutputModel(String plainTextContent, String markupContent) {
+        super(plainTextContent, markupContent);
+    }
+
+    @Override
+    public XHTMLOutputFormat getOutputFormat() {
+        return XHTMLOutputFormat.INSTANCE;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/main/java/freemarker/core/XHTMLOutputFormat.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/XHTMLOutputFormat.java b/src/main/java/freemarker/core/XHTMLOutputFormat.java
new file mode 100644
index 0000000..e14f597
--- /dev/null
+++ b/src/main/java/freemarker/core/XHTMLOutputFormat.java
@@ -0,0 +1,73 @@
+/*
+ * 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 freemarker.core;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import freemarker.template.TemplateModelException;
+import freemarker.template.utility.StringUtil;
+
+/**
+ * Represents the XHTML output format.
+ * 
+ * @since 2.3.24
+ */
+public final class XHTMLOutputFormat extends CommonMarkupOutputFormat<TemplateXHTMLOutputModel> {
+
+    /**
+     * The only instance (singleton) of this {@link OutputFormat}.
+     */
+    public static final XHTMLOutputFormat INSTANCE = new XHTMLOutputFormat();
+    
+    private XHTMLOutputFormat() {
+        // Only to decrease visibility
+    }
+    
+    @Override
+    public String getName() {
+        return "XHTML";
+    }
+
+    @Override
+    public String getMimeType() {
+        return "application/xhtml+xml";
+    }
+
+    @Override
+    public void output(String textToEsc, Writer out) throws IOException, TemplateModelException {
+        StringUtil.XHTMLEnc(textToEsc, out);
+    }
+
+    @Override
+    public String escapePlainText(String plainTextContent) {
+        return StringUtil.XHTMLEnc(plainTextContent);
+    }
+
+    @Override
+    public boolean isLegacyBuiltInBypassed(String builtInName) {
+        return builtInName.equals("html") || builtInName.equals("xml") || builtInName.equals("xhtml");
+    }
+
+    @Override
+    protected TemplateXHTMLOutputModel newTemplateMarkupOutputModel(String plainTextContent, String markupContent) {
+        return new TemplateXHTMLOutputModel(plainTextContent, markupContent);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/manual/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/book.xml b/src/manual/book.xml
index a2f415f..d0679f6 100644
--- a/src/manual/book.xml
+++ b/src/manual/book.xml
@@ -26221,6 +26221,26 @@ TemplateModel x = env.getVariable("x");  // get variable x</programlisting>
               parse method now, though it's just a <literal>final</literal>
               placeholder now.</para>
             </listitem>
+
+            <listitem>
+              <para><literal>TemplateNumberFormat.format</literal> and
+              <literal>TemplateDateFormat.format</literal> overload that
+              returns markup output is now actually called, so for example now
+              it's possible to use HTML in formatting results. (See
+              <literal>freemarker.core.PrintfGTemplateNumberFormatFactory</literal>
+              under <literal>src/test/java</literal> as an example.) Note that
+              formatting to markup only happens if FTL thinks that the
+              expected result is not just a string, such as for
+              <literal>${<replaceable>myNumber</replaceable>}</literal> if
+              that file has markup <link
+              linkend="dgui_misc_autoescaping_outputformat">output
+              format</link>.</para>
+            </listitem>
+
+            <listitem>
+              <para>Added <literal>XHTMLOutputFormat</literal> and
+              <literal>TemplateXHTMLOutputModel</literal>.</para>
+            </listitem>
           </itemizedlist>
         </section>
       </section>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/test/java/freemarker/core/NumberFormatTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/NumberFormatTest.java b/src/test/java/freemarker/core/NumberFormatTest.java
index 8db57dc..46f4d07 100644
--- a/src/test/java/freemarker/core/NumberFormatTest.java
+++ b/src/test/java/freemarker/core/NumberFormatTest.java
@@ -297,6 +297,12 @@ public class NumberFormatTest extends TemplateTest {
                 "1.23*10<sup>6</sup> cat:1.23*10<sup>6</sup> 1.23*10<sup>-5</sup>");
         assertOutput("<#ftl outputFormat='HTML'>${\"" + commonFTL + "\"}",
                 "1.23E+06 cat:1.23E+06 1.23E-05");
+        assertOutput("<#escape x as x?html>" + commonFTL + "</#escape>",
+                "1.23*10<sup>6</sup> cat:1.23E+06 1.23*10<sup>-5</sup>");
+        assertOutput("<#escape x as x?xhtml>" + commonFTL + "</#escape>",
+                "1.23*10<sup>6</sup> cat:1.23E+06 1.23*10<sup>-5</sup>");
+        assertOutput("<#escape x as x?xml>" + commonFTL + "</#escape>",
+                "1.23E+06 cat:1.23E+06 1.23E-05");
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/test/java/freemarker/core/PrintfGTemplateNumberFormatFactory.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/PrintfGTemplateNumberFormatFactory.java b/src/test/java/freemarker/core/PrintfGTemplateNumberFormatFactory.java
index a44dac3..dd3c2be 100644
--- a/src/test/java/freemarker/core/PrintfGTemplateNumberFormatFactory.java
+++ b/src/test/java/freemarker/core/PrintfGTemplateNumberFormatFactory.java
@@ -92,8 +92,7 @@ public class PrintfGTemplateNumberFormatFactory extends TemplateNumberFormatFact
         @Override
         public <MO extends TemplateMarkupOutputModel> MO format(TemplateNumberModel numberModel,
                 MarkupOutputFormat<MO> outputFormat) throws UnformattableValueException, TemplateModelException {
-            // TODO XHTMLOutputFormat
-            if (!(outputFormat instanceof HTMLOutputFormat)) {
+            if (!(outputFormat instanceof HTMLOutputFormat || outputFormat instanceof XHTMLOutputFormat)) {
                 return null;
             }
             

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/6b2840c6/src/test/java/freemarker/core/XHTMLOutputFormatTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/XHTMLOutputFormatTest.java b/src/test/java/freemarker/core/XHTMLOutputFormatTest.java
new file mode 100644
index 0000000..81466ef
--- /dev/null
+++ b/src/test/java/freemarker/core/XHTMLOutputFormatTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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 freemarker.core;
+
+import static freemarker.core.XHTMLOutputFormat.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import org.junit.Test;
+
+import freemarker.template.TemplateModelException; 
+
+public class XHTMLOutputFormatTest {
+    
+    @Test
+    public void testOutputMO() throws TemplateModelException, IOException {
+       StringWriter out = new StringWriter();
+       INSTANCE.output(INSTANCE.fromPlainTextByEscaping("a'b"), out);
+       assertEquals("a&#39;b", out.toString());
+    }
+    
+    @Test
+    public void testOutputString() throws TemplateModelException, IOException {
+        StringWriter out = new StringWriter();
+        INSTANCE.output("a'b", out);
+        assertEquals("a&#39;b", out.toString());
+    }
+    
+    @Test
+    public void testEscaplePlainText() {
+        assertEquals("", INSTANCE.escapePlainText(""));
+        assertEquals("a", INSTANCE.escapePlainText("a"));
+        assertEquals("&lt;a&amp;b&#39;c&quot;d&gt;", INSTANCE.escapePlainText("<a&b'c\"d>"));
+        assertEquals("&lt;&gt;", INSTANCE.escapePlainText("<>"));
+    }
+    
+    @Test
+    public void testGetMimeType() {
+        assertEquals("application/xhtml+xml", INSTANCE.getMimeType());
+    }
+    
+}