freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [03/16] incubator-freemarker git commit: FREEMARKER-55: adding model building code.
Date Tue, 04 Jul 2017 07:44:14 GMT
FREEMARKER-55: adding model building code.


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

Branch: refs/heads/3
Commit: 334d9cde950f06e480027d3064e53b6c1cfa54e2
Parents: ec2d37c
Author: Woonsan Ko <woonsan@apache.org>
Authored: Wed Jun 28 23:36:49 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Wed Jun 28 23:36:49 2017 -0400

----------------------------------------------------------------------
 freemarker-spring/build.gradle                  |  2 +
 .../spring/web/view/AbstractFreemarkerView.java | 67 ++++++++++++++++++--
 .../spring/web/view/FreemarkerView.java         | 60 ++++++++++++++++++
 .../spring/web/view/FreemarkerViewTest.java     |  2 +-
 4 files changed, 123 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/334d9cde/freemarker-spring/build.gradle
----------------------------------------------------------------------
diff --git a/freemarker-spring/build.gradle b/freemarker-spring/build.gradle
index 60e03b2..1865cc2 100644
--- a/freemarker-spring/build.gradle
+++ b/freemarker-spring/build.gradle
@@ -25,7 +25,9 @@ published = true
 inAggregateJavadoc = true
 
 dependencies {
+
     compile project(":freemarker-core")
+    compile project(":freemarker-servlet")
 
     // TODO: what's difference between compileOnly and compile??
     compile "org.apache.geronimo.specs:geronimo-servlet_3.0_spec:1.0"

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/334d9cde/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
index a2a2e8b..595ada2 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/AbstractFreemarkerView.java
@@ -19,6 +19,8 @@
 package org.apache.freemarker.spring.web.view;
 
 import java.io.IOException;
+import java.io.Serializable;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
@@ -28,39 +30,90 @@ import org.apache.freemarker.core.Configuration;
 import org.apache.freemarker.core.ParseException;
 import org.apache.freemarker.core.Template;
 import org.apache.freemarker.core.TemplateNotFoundException;
+import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
+import org.apache.freemarker.core.model.TemplateHashModel;
+import org.apache.freemarker.core.model.impl.DefaultObjectWrapper;
 import org.apache.freemarker.core.templateresolver.MalformedTemplateNameException;
 import org.springframework.web.servlet.view.AbstractView;
 
 public abstract class AbstractFreemarkerView extends AbstractView {
 
     private Configuration configuration;
-    private String templateName;
+    private String name;
+    private Locale locale;
+    private Serializable customLookupCondition;
+    private boolean ignoreMissing;
 
     public Configuration getConfiguration() {
         return configuration;
     }
 
     public void setConfiguration(Configuration configuration) {
+        if (!(configuration.getObjectWrapper() instanceof ObjectWrapperAndUnwrapper)) {
+            throw new RuntimeException(AbstractFreemarkerView.class.getSimpleName() + " requires
an ObjectWrapper that "
+                    + "implements " + ObjectWrapperAndUnwrapper.class.getName() + ", but
this class doesn't do that: "
+                    + configuration.getObjectWrapper().getClass().getName());
+        }
+
         this.configuration = configuration;
     }
 
-    public String getTemplateName() {
-        return templateName;
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Locale getLocale() {
+        return locale;
+    }
+
+    public void setLocale(Locale locale) {
+        this.locale = locale;
+    }
+
+    public Serializable getCustomLookupCondition() {
+        return customLookupCondition;
     }
 
-    public void setTemplateName(String templateName) {
-        this.templateName = templateName;
+    public void setCustomLookupCondition(Serializable customLookupCondition) {
+        this.customLookupCondition = customLookupCondition;
+    }
+
+    public boolean isIgnoreMissing() {
+        return ignoreMissing;
+    }
+
+    public void setIgnoreMissing(boolean ignoreMissing) {
+        this.ignoreMissing = ignoreMissing;
     }
 
     @Override
     protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest
request,
             HttpServletResponse response) throws Exception {
-        getTemplate().process(model, response.getWriter());
+        getTemplate().process(createModel(model, getObjectWrapperForModel(), request, response),
response.getWriter());
     }
 
     protected Template getTemplate()
             throws TemplateNotFoundException, MalformedTemplateNameException, ParseException,
IOException {
-        return getConfiguration().getTemplate(getTemplateName());
+        return getConfiguration().getTemplate(getName(), getLocale(), getCustomLookupCondition(),
isIgnoreMissing());
+    }
+
+    protected ObjectWrapperAndUnwrapper getObjectWrapperForModel() {
+        ObjectWrapperAndUnwrapper wrapper;
+
+        if (configuration.isObjectWrapperSet()) {
+            wrapper = (ObjectWrapperAndUnwrapper) configuration.getObjectWrapper();
+        } else {
+            // TODO: need to cache this?
+            wrapper = new DefaultObjectWrapper.Builder(configuration.getIncompatibleImprovements()).build();
+        }
+
+        return wrapper;
     }
 
+    protected abstract TemplateHashModel createModel(Map<String, Object> map,
+            ObjectWrapperAndUnwrapper objectWrapperForModel, HttpServletRequest request,
HttpServletResponse response);
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/334d9cde/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
index da3ad09..c13a49f 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerView.java
@@ -18,6 +18,66 @@
  */
 package org.apache.freemarker.spring.web.view;
 
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
+import org.apache.freemarker.core.model.TemplateHashModel;
+import org.apache.freemarker.servlet.AllHttpScopesHashModel;
+import org.apache.freemarker.servlet.FreemarkerServlet;
+import org.apache.freemarker.servlet.HttpRequestHashModel;
+import org.apache.freemarker.servlet.HttpRequestParametersHashModel;
+import org.apache.freemarker.servlet.HttpSessionHashModel;
+import org.apache.freemarker.servlet.ServletContextHashModel;
+import org.apache.freemarker.servlet.jsp.TaglibFactory;
+
 public class FreemarkerView extends AbstractFreemarkerView {
 
+    private ServletContextHashModel servletContextModel;
+
+    private TaglibFactory taglibFactory;
+
+    public ServletContextHashModel getServletContextModel() {
+        return servletContextModel;
+    }
+
+    public void setServletContextModel(ServletContextHashModel servletContextModel) {
+        this.servletContextModel = servletContextModel;
+    }
+
+    public TaglibFactory getTaglibFactory() {
+        // TODO
+        return taglibFactory;
+    }
+
+    public void setTaglibFactory(TaglibFactory taglibFactory) {
+        this.taglibFactory = taglibFactory;
+    }
+
+    @Override
+    protected TemplateHashModel createModel(Map<String, Object> map, ObjectWrapperAndUnwrapper
objectWrapperForModel,
+            HttpServletRequest request, HttpServletResponse response) {
+        AllHttpScopesHashModel model = new AllHttpScopesHashModel(objectWrapperForModel,
getServletContext(), request);
+        model.putUnlistedModel(FreemarkerServlet.KEY_APPLICATION, getServletContextModel());
+        HttpSession session = request.getSession(false);
+        if (session != null) {
+            model.putUnlistedModel(FreemarkerServlet.KEY_SESSION, getHttpSessionModel(objectWrapperForModel,
request, response));
+        }
+        model.putUnlistedModel(FreemarkerServlet.KEY_REQUEST, new HttpRequestHashModel(request,
response, objectWrapperForModel));
+        model.putUnlistedModel(FreemarkerServlet.KEY_REQUEST_PARAMETERS,
+                new HttpRequestParametersHashModel(request, objectWrapperForModel));
+        model.putUnlistedModel(FreemarkerServlet.KEY_JSP_TAGLIBS, getTaglibFactory());
+        model.putAll(map);
+        return model;
+    }
+
+    protected HttpSessionHashModel getHttpSessionModel(ObjectWrapperAndUnwrapper objectWrapperForModel,
+            HttpServletRequest request, HttpServletResponse response) {
+        // TODO
+        HttpSessionHashModel sessionModel = new HttpSessionHashModel(null, request, response,
objectWrapperForModel);
+        return sessionModel;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/334d9cde/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
index 64f50e0..4d24632 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/web/view/FreemarkerViewTest.java
@@ -60,7 +60,7 @@ public class FreemarkerViewTest {
         FreemarkerView view = new FreemarkerView();
         view.setServletContext(servletContext);
         view.setConfiguration(configuration);
-        view.setTemplateName("hello.ftl");
+        view.setName("hello.ftl");
 
         int visitCount = 0;
         Map<String, Object> model = new HashMap<String, Object>();


Mime
View raw message