freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [04/36] incubator-freemarker git commit: FREEMARKER-55: splitting abstract class to keep common things
Date Wed, 13 Sep 2017 09:49:05 GMT
FREEMARKER-55: splitting abstract class to keep common things


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

Branch: refs/heads/3
Commit: aeaf0307736008b2921c7efa42cf7d31b2df6dbc
Parents: b656f1e
Author: Woonsan Ko <woonsan@apache.org>
Authored: Tue Aug 29 23:02:21 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Tue Aug 29 23:02:21 2017 -0400

----------------------------------------------------------------------
 .../AbstractSpringTemplateDirectiveModel.java   | 97 ++++++++++++++++++++
 .../freemarker/spring/model/BindDirective.java  | 62 +++----------
 2 files changed, 110 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/aeaf0307/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/AbstractSpringTemplateDirectiveModel.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/AbstractSpringTemplateDirectiveModel.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/AbstractSpringTemplateDirectiveModel.java
new file mode 100644
index 0000000..9baf3f0
--- /dev/null
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/AbstractSpringTemplateDirectiveModel.java
@@ -0,0 +1,97 @@
+/*
+ * 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.spring.model;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.freemarker.core.CallPlace;
+import org.apache.freemarker.core.Environment;
+import org.apache.freemarker.core.TemplateException;
+import org.apache.freemarker.core.model.ObjectWrapper;
+import org.apache.freemarker.core.model.TemplateDirectiveModel;
+import org.apache.freemarker.core.model.TemplateModel;
+import org.apache.freemarker.core.model.impl.DefaultObjectWrapper;
+import org.springframework.web.servlet.support.RequestContext;
+import org.springframework.web.servlet.view.AbstractTemplateView;
+
+public abstract class AbstractSpringTemplateDirectiveModel implements TemplateDirectiveModel
{
+
+    /**
+     * @see <code>org.springframework.web.servlet.tags.NestedPathTag#NESTED_PATH_VARIABLE_NAME</code>
+     */
+    private static final String NESTED_PATH_VARIABLE_NAME = "nestedPath";
+
+    private final HttpServletRequest request;
+    private final HttpServletResponse response;
+
+    public AbstractSpringTemplateDirectiveModel(HttpServletRequest request, HttpServletResponse
response) {
+        this.request = request;
+        this.response = response;
+    }
+
+    @Override
+    public final void execute(TemplateModel[] args, CallPlace callPlace, Writer out, Environment
env)
+            throws TemplateException, IOException {
+        final ObjectWrapper objectWrapper = env.getObjectWrapper();
+
+        if (!(objectWrapper instanceof DefaultObjectWrapper)) {
+            throw new TemplateException(
+                    "The ObjectWrapper of environment wasn't instance of " + DefaultObjectWrapper.class.getName());
+        }
+
+        TemplateModel rcModel = env.getVariable(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
+
+        if (rcModel == null) {
+            throw new TemplateException(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE
+ " not found.");
+        }
+
+        RequestContext requestContext = (RequestContext) ((DefaultObjectWrapper) objectWrapper).unwrap(rcModel);
+
+        executeInternal(args, callPlace, out, env, (DefaultObjectWrapper) objectWrapper,
requestContext);
+    }
+
+    protected abstract void executeInternal(TemplateModel[] args, CallPlace callPlace, Writer
out, Environment env,
+            DefaultObjectWrapper objectWrapper, RequestContext requestContext) throws TemplateException,
IOException;
+
+    protected final HttpServletRequest getRequest() {
+        return request;
+    }
+
+    protected final HttpServletResponse getResponse() {
+        return response;
+    }
+
+    protected final String resolveNestedPath(final Environment env, final String path) {
+        // TODO: should read it from request or env??
+        //       or read spring.nestedPath first and read request attribute next??
+        String nestedPath = (String) request.getAttribute(NESTED_PATH_VARIABLE_NAME);
+
+        if (nestedPath != null && !path.startsWith(nestedPath)
+                && !path.equals(nestedPath.substring(0, nestedPath.length() - 1)))
{
+            return nestedPath + path;
+        }
+
+        return path;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/aeaf0307/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindDirective.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindDirective.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindDirective.java
index fb68384..18ba1dd 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindDirective.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/model/BindDirective.java
@@ -29,8 +29,6 @@ import org.apache.freemarker.core.CallPlace;
 import org.apache.freemarker.core.Environment;
 import org.apache.freemarker.core.TemplateException;
 import org.apache.freemarker.core.model.ArgumentArrayLayout;
-import org.apache.freemarker.core.model.ObjectWrapper;
-import org.apache.freemarker.core.model.TemplateDirectiveModel;
 import org.apache.freemarker.core.model.TemplateModel;
 import org.apache.freemarker.core.model.impl.BeanModel;
 import org.apache.freemarker.core.model.impl.DefaultObjectWrapper;
@@ -38,58 +36,35 @@ import org.apache.freemarker.core.util.CallableUtils;
 import org.apache.freemarker.core.util.StringToIndexMap;
 import org.springframework.web.servlet.support.BindStatus;
 import org.springframework.web.servlet.support.RequestContext;
-import org.springframework.web.servlet.view.AbstractTemplateView;
 
-public class BindDirective implements TemplateDirectiveModel {
-
-    public static final String STATUS_VARIABLE_NAME = "status";
-
-    /**
-     * @see <code>org.springframework.web.servlet.tags.NestedPathTag#NESTED_PATH_VARIABLE_NAME</code>
-     */
-    private static final String NESTED_PATH_VARIABLE_NAME = "nestedPath";
+public class BindDirective extends AbstractSpringTemplateDirectiveModel {
 
     private static final int PATH_PARAM_IDX = 0;
     private static final int IGNORE_NESTED_PATH_PARAM_IDX = 1;
 
     private static final String IGNORE_NESTED_PATH_PARAM_NAME = "ignoreNestedPath";
 
-    private static final ArgumentArrayLayout ARGS_LAYOUT = ArgumentArrayLayout.create(
-            1,
-            true,
-            StringToIndexMap.of(
-                    IGNORE_NESTED_PATH_PARAM_NAME, IGNORE_NESTED_PATH_PARAM_IDX
-            ),
-            false
-    );
-
-    private final HttpServletRequest request;
-    private final HttpServletResponse response;
+    private static final ArgumentArrayLayout ARGS_LAYOUT =
+            ArgumentArrayLayout.create(
+                    1,
+                    true,
+                    StringToIndexMap.of(IGNORE_NESTED_PATH_PARAM_NAME, IGNORE_NESTED_PATH_PARAM_IDX),
+                    false
+                    );
 
     public BindDirective(HttpServletRequest request, HttpServletResponse response) {
-        this.request = request;
-        this.response = response;
+        super(request, response);
     }
 
     @Override
-    public void execute(TemplateModel[] args, CallPlace callPlace, Writer out, Environment
env)
-            throws TemplateException, IOException {
-        final ObjectWrapper objectWrapper = env.getObjectWrapper();
-
-        if (!(objectWrapper instanceof DefaultObjectWrapper)) {
-            throw new TemplateException("The ObjectWrapper of environment wasn't instance
of " + DefaultObjectWrapper.class.getName());
-        }
-
-        TemplateModel model = env.getDataModel().get(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
-        RequestContext requestContext = (RequestContext) ((DefaultObjectWrapper) objectWrapper).unwrap(model);
+    protected void executeInternal(TemplateModel[] args, CallPlace callPlace, Writer out,
Environment env,
+            DefaultObjectWrapper objectWrapper, RequestContext requestContext) throws TemplateException,
IOException {
 
-        String resolvedPath = CallableUtils.getStringArgument(args, PATH_PARAM_IDX, this);
+        final String path = CallableUtils.getStringArgument(args, PATH_PARAM_IDX, this);
         boolean ignoreNestedPath = CallableUtils.getOptionalBooleanArgument(args, IGNORE_NESTED_PATH_PARAM_IDX,
this,
                 false);
 
-        if (!ignoreNestedPath) {
-            resolvedPath = resolveNestedPath(resolvedPath);
-        }
+        final String resolvedPath = (ignoreNestedPath) ? path : resolveNestedPath(env, path);
 
         //TODO: how to deal with htmlEscape when invoking #getBindStatus()?
         BindStatus status = requestContext.getBindStatus(resolvedPath);
@@ -107,15 +82,4 @@ public class BindDirective implements TemplateDirectiveModel {
     public ArgumentArrayLayout getDirectiveArgumentArrayLayout() {
         return ARGS_LAYOUT;
     }
-
-    private String resolveNestedPath(final String path) {
-        String nestedPath = (String) request.getAttribute(NESTED_PATH_VARIABLE_NAME);
-
-        if (nestedPath != null && !path.startsWith(nestedPath)
-                && !path.equals(nestedPath.substring(0, nestedPath.length() - 1)))
{
-            return nestedPath + path;
-        }
-
-        return path;
-    }
 }


Mime
View raw message