freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [03/17] incubator-freemarker git commit: FREEMARKER-55: moving builder into TaglibFactory
Date Thu, 06 Jul 2017 08:18:54 GMT
FREEMARKER-55: moving builder into TaglibFactory


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

Branch: refs/heads/3
Commit: dcbebe9b19e85dff067a46c4394e107916fb7845
Parents: 3c91ecb
Author: Woonsan Ko <woonsan@apache.org>
Authored: Wed Jul 5 16:30:09 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Wed Jul 5 16:30:09 2017 -0400

----------------------------------------------------------------------
 .../freemarker/servlet/FreemarkerServlet.java   |   7 +-
 .../freemarker/servlet/jsp/TaglibFactory.java   | 132 +++++++++++++++
 .../servlet/jsp/TaglibFactoryBuilder.java       | 167 -------------------
 .../spring/web/view/FreemarkerViewResolver.java |   3 +-
 .../spring/web/view/FreemarkerViewTest.java     |   3 +-
 5 files changed, 137 insertions(+), 175 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/dcbebe9b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java
----------------------------------------------------------------------
diff --git a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java
b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java
index 9b5008f..c56f6d7 100644
--- a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java
+++ b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/FreemarkerServlet.java
@@ -65,7 +65,6 @@ import org.apache.freemarker.core.util._SecurityUtil;
 import org.apache.freemarker.core.util._StringUtil;
 import org.apache.freemarker.servlet.jsp.TaglibFactory;
 import org.apache.freemarker.servlet.jsp.TaglibFactory.MetaInfTldSource;
-import org.apache.freemarker.servlet.jsp.TaglibFactoryBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -637,7 +636,7 @@ public class FreemarkerServlet extends HttpServlet {
                 } else if (name.equals(INIT_PARAM_EXCEPTION_ON_MISSING_TEMPLATE)) {
                     exceptionOnMissingTemplate = _StringUtil.getYesNo(value);
                 } else if (name.equals(INIT_PARAM_META_INF_TLD_LOCATIONS)) {
-                    metaInfTldSources = TaglibFactoryBuilder.parseMetaInfTldLocations(InitParamParser.parseCommaSeparatedList(value));
+                    metaInfTldSources = TaglibFactory.Builder.parseMetaInfTldLocations(InitParamParser.parseCommaSeparatedList(value));
                 } else if (name.equals(INIT_PARAM_CLASSPATH_TLDS)) {
                     List newClasspathTlds = new ArrayList();
                     if (classpathTlds != null) {
@@ -1001,7 +1000,7 @@ public class FreemarkerServlet extends HttpServlet {
         try {
             final String prop = _SecurityUtil.getSystemProperty(SYSTEM_PROPERTY_META_INF_TLD_SOURCES,
null);
             metaInfTldSourcesFromSysProp = (List<MetaInfTldSource>) ((prop != null)
-                    ? TaglibFactoryBuilder.parseMetaInfTldLocations(InitParamParser.parseCommaSeparatedList(prop))
+                    ? TaglibFactory.Builder.parseMetaInfTldLocations(InitParamParser.parseCommaSeparatedList(prop))
                     : Collections.emptyList());
         } catch (ParseException e) {
             throw new TemplateModelException(
@@ -1028,7 +1027,7 @@ public class FreemarkerServlet extends HttpServlet {
                     "Failed to parse system property \"" + SYSTEM_PROPERTY_CLASSPATH_TLDS
+ "\"", e);
         }
 
-        return new TaglibFactoryBuilder(servletContext, objectWrapper)
+        return new TaglibFactory.Builder(servletContext, objectWrapper)
                 .addAllMetaInfTldSources(metaInfTldSources)
                 .addAllMetaInfTldSources(metaInfTldSourcesFromSysProp)
                 .addAllJettyMetaInfTldJarPatterns(jettyTaglibJarPatterns)

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/dcbebe9b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactory.java
----------------------------------------------------------------------
diff --git a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactory.java
b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactory.java
index 0fbba1b..9da15e4 100644
--- a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactory.java
+++ b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactory.java
@@ -38,6 +38,7 @@ import java.net.URLConnection;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Enumeration;
@@ -61,6 +62,7 @@ import javax.servlet.jsp.tagext.Tag;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParserFactory;
 
+import org.apache.freemarker.core.ConfigurationException;
 import org.apache.freemarker.core.Environment;
 import org.apache.freemarker.core.model.ObjectWrapper;
 import org.apache.freemarker.core.model.TemplateHashModel;
@@ -70,6 +72,7 @@ import org.apache.freemarker.core.model.TemplateModelException;
 import org.apache.freemarker.core.model.TemplateTransformModel;
 import org.apache.freemarker.core.model.impl.DefaultObjectWrapper;
 import org.apache.freemarker.core.util.BugException;
+import org.apache.freemarker.core.util.CommonBuilder;
 import org.apache.freemarker.core.util._ClassUtil;
 import org.apache.freemarker.core.util._NullArgumentException;
 import org.apache.freemarker.core.util._StringUtil;
@@ -1990,4 +1993,133 @@ public class TaglibFactory implements TemplateHashModel {
         
     }
 
+    public static class Builder implements CommonBuilder<TaglibFactory> {
+
+        /**
+         * Servlet context.
+         */
+        private final ServletContext servletContext;
+
+        /**
+         * Object wrapper to be used in model building.
+         */
+        private final ObjectWrapper objectWrapper;
+
+        /**
+         * TLD locations to look for when finding available JSP tag libraries.
+         */
+        private List<MetaInfTldSource> metaInfTldSources = new ArrayList<>();
+
+        /**
+         * TLD classpath locations to look for when finding available JSP tag libraries.
+         */
+        private List<String> classPathTlds = new ArrayList<>();
+
+        public Builder(ServletContext servletContext, ObjectWrapper objectWrapper) {
+            this.servletContext = servletContext;
+            this.objectWrapper = objectWrapper;
+        }
+
+        public Builder addMetaInfTldSource(MetaInfTldSource metaInfTldSource) {
+            metaInfTldSources.add(metaInfTldSource);
+            return this;
+        }
+
+        public Builder addAllMetaInfTldSources(List<MetaInfTldSource> metaInfTldSources)
{
+            this.metaInfTldSources.addAll(metaInfTldSources);
+            return this;
+        }
+
+        public Builder addMetaInfTldLocation(String metaInfTldLocation) throws ParseException
{
+            return addMetaInfTldSource(parseMetaInfTldLocation(metaInfTldLocation));
+        }
+
+        public Builder addMetaInfTldLocations(List<String> metaInfTldLocations) throws
ParseException {
+            return addAllMetaInfTldSources(parseMetaInfTldLocations(metaInfTldLocations));
+        }
+
+        public Builder addJettyMetaInfTldJarPattern(Pattern pattern) {
+            return addMetaInfTldSource(new ClasspathMetaInfTldSource(pattern));
+        }
+
+        public Builder addAllJettyMetaInfTldJarPatterns(List<Pattern> patterns) {
+            for (Pattern pattern : patterns) {
+                addJettyMetaInfTldJarPattern(pattern);
+            }
+
+            return this;
+        }
+
+        public Builder addClasspathTld(String classpathTld) {
+            classPathTlds.add(classpathTld);
+            return this;
+        }
+
+        public Builder addAllClasspathTlds(List<String> classpathTlds) {
+            classPathTlds.addAll(classpathTlds);
+            return this;
+        }
+
+        public TaglibFactory build() throws ConfigurationException {
+            TaglibFactory taglibFactory = new TaglibFactory(servletContext);
+            taglibFactory.setObjectWrapper(objectWrapper);
+            taglibFactory.setMetaInfTldSources(metaInfTldSources);
+            taglibFactory.setClasspathTlds(classPathTlds);
+            return taglibFactory;
+        }
+
+        public static MetaInfTldSource parseMetaInfTldLocation(String value) throws ParseException
{
+            MetaInfTldSource metaInfTldSource;
+
+            if (value.equals(FreemarkerServlet.META_INF_TLD_LOCATION_WEB_INF_PER_LIB_JARS))
{
+                metaInfTldSource = WebInfPerLibJarMetaInfTldSource.INSTANCE;
+            } else if (value.startsWith(FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH))
{
+                String itemRightSide = value.substring(FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH.length())
+                        .trim();
+
+                if (itemRightSide.length() == 0) {
+                    metaInfTldSource = new ClasspathMetaInfTldSource(Pattern.compile(".*",
Pattern.DOTALL));
+                } else if (itemRightSide.startsWith(":")) {
+                    final String regexpStr = itemRightSide.substring(1).trim();
+                    if (regexpStr.length() == 0) {
+                        throw new ParseException("Empty regular expression after \""
+                                + FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH + ":\"",
-1);
+                    }
+                    metaInfTldSource = new ClasspathMetaInfTldSource(Pattern.compile(regexpStr));
+                } else {
+                    throw new ParseException("Invalid \"" + FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH
+                            + "\" value syntax: " + value, -1);
+                }
+            } else if (value.startsWith(FreemarkerServlet.META_INF_TLD_LOCATION_CLEAR)) {
+                metaInfTldSource = ClearMetaInfTldSource.INSTANCE;
+            } else {
+                throw new ParseException("Item has no recognized source type prefix: " +
value, -1);
+            }
+
+            return metaInfTldSource;
+        }
+
+        public static List<MetaInfTldSource> parseMetaInfTldLocations(List<String>
values) throws ParseException {
+            List<MetaInfTldSource> metaInfTldSources = null;
+
+            if (values != null) {
+                for (String value : values) {
+                    final MetaInfTldSource metaInfTldSource = parseMetaInfTldLocation(value);
+
+                    if (metaInfTldSources == null) {
+                        metaInfTldSources = new ArrayList();
+                    }
+
+                    metaInfTldSources.add(metaInfTldSource);
+                }
+            }
+
+            if (metaInfTldSources == null) {
+                metaInfTldSources = Collections.emptyList();
+            }
+
+            return metaInfTldSources;
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/dcbebe9b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactoryBuilder.java
----------------------------------------------------------------------
diff --git a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactoryBuilder.java
b/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactoryBuilder.java
deleted file mode 100644
index 34daec3..0000000
--- a/freemarker-servlet/src/main/java/org/apache/freemarker/servlet/jsp/TaglibFactoryBuilder.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * 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.servlet.jsp;
-
-import java.text.ParseException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.regex.Pattern;
-
-import javax.servlet.ServletContext;
-
-import org.apache.freemarker.core.model.ObjectWrapper;
-import org.apache.freemarker.servlet.FreemarkerServlet;
-import org.apache.freemarker.servlet.jsp.TaglibFactory.ClasspathMetaInfTldSource;
-import org.apache.freemarker.servlet.jsp.TaglibFactory.ClearMetaInfTldSource;
-import org.apache.freemarker.servlet.jsp.TaglibFactory.MetaInfTldSource;
-import org.apache.freemarker.servlet.jsp.TaglibFactory.WebInfPerLibJarMetaInfTldSource;
-
-/**
- * Builder for {@link TaglibFactory}.
- */
-public class TaglibFactoryBuilder {
-
-    /**
-     * Servlet context.
-     */
-    private final ServletContext servletContext;
-
-    /**
-     * Object wrapper to be used in model building.
-     */
-    private final ObjectWrapper objectWrapper;
-
-    /**
-     * TLD locations to look for when finding available JSP tag libraries.
-     */
-    private List<MetaInfTldSource> metaInfTldSources = new ArrayList<>();
-
-    /**
-     * TLD classpath locations to look for when finding available JSP tag libraries.
-     */
-    private List<String> classPathTlds = new ArrayList<>();
-
-    public TaglibFactoryBuilder(ServletContext servletContext, ObjectWrapper objectWrapper)
{
-        this.servletContext = servletContext;
-        this.objectWrapper = objectWrapper;
-    }
-
-    public TaglibFactoryBuilder addMetaInfTldSource(MetaInfTldSource metaInfTldSource) {
-        metaInfTldSources.add(metaInfTldSource);
-        return this;
-    }
-
-    public TaglibFactoryBuilder addAllMetaInfTldSources(List<MetaInfTldSource> metaInfTldSources)
{
-        this.metaInfTldSources.addAll(metaInfTldSources);
-        return this;
-    }
-
-    public TaglibFactoryBuilder addMetaInfTldLocation(String metaInfTldLocation) throws ParseException
{
-        return addMetaInfTldSource(parseMetaInfTldLocation(metaInfTldLocation));
-    }
-
-    public TaglibFactoryBuilder addMetaInfTldLocations(List<String> metaInfTldLocations)
throws ParseException {
-        return addAllMetaInfTldSources(parseMetaInfTldLocations(metaInfTldLocations));
-    }
-
-    public TaglibFactoryBuilder addJettyMetaInfTldJarPattern(Pattern pattern) {
-        return addMetaInfTldSource(new ClasspathMetaInfTldSource(pattern));
-    }
-
-    public TaglibFactoryBuilder addAllJettyMetaInfTldJarPatterns(List<Pattern> patterns)
{
-        for (Pattern pattern : patterns) {
-            addJettyMetaInfTldJarPattern(pattern);
-        }
-
-        return this;
-    }
-
-    public TaglibFactoryBuilder addClasspathTld(String classpathTld) {
-        classPathTlds.add(classpathTld);
-        return this;
-    }
-
-    public TaglibFactoryBuilder addAllClasspathTlds(List<String> classpathTlds) {
-        classPathTlds.addAll(classpathTlds);
-        return this;
-    }
-
-    public TaglibFactory build() {
-        TaglibFactory taglibFactory = new TaglibFactory(servletContext);
-        taglibFactory.setObjectWrapper(objectWrapper);
-        taglibFactory.setMetaInfTldSources(metaInfTldSources);
-        taglibFactory.setClasspathTlds(classPathTlds);
-        return taglibFactory;
-    }
-
-    public static MetaInfTldSource parseMetaInfTldLocation(String value) throws ParseException
{
-        MetaInfTldSource metaInfTldSource;
-
-        if (value.equals(FreemarkerServlet.META_INF_TLD_LOCATION_WEB_INF_PER_LIB_JARS)) {
-            metaInfTldSource = WebInfPerLibJarMetaInfTldSource.INSTANCE;
-        } else if (value.startsWith(FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH)) {
-            String itemRightSide = value.substring(FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH.length())
-                    .trim();
-
-            if (itemRightSide.length() == 0) {
-                metaInfTldSource = new ClasspathMetaInfTldSource(Pattern.compile(".*", Pattern.DOTALL));
-            } else if (itemRightSide.startsWith(":")) {
-                final String regexpStr = itemRightSide.substring(1).trim();
-                if (regexpStr.length() == 0) {
-                    throw new ParseException("Empty regular expression after \""
-                            + FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH + ":\"",
-1);
-                }
-                metaInfTldSource = new ClasspathMetaInfTldSource(Pattern.compile(regexpStr));
-            } else {
-                throw new ParseException("Invalid \"" + FreemarkerServlet.META_INF_TLD_LOCATION_CLASSPATH
-                        + "\" value syntax: " + value, -1);
-            }
-        } else if (value.startsWith(FreemarkerServlet.META_INF_TLD_LOCATION_CLEAR)) {
-            metaInfTldSource = ClearMetaInfTldSource.INSTANCE;
-        } else {
-            throw new ParseException("Item has no recognized source type prefix: " + value,
-1);
-        }
-
-        return metaInfTldSource;
-    }
-
-    public static List<MetaInfTldSource> parseMetaInfTldLocations(List<String>
values) throws ParseException {
-        List<MetaInfTldSource> metaInfTldSources = null;
-
-        if (values != null) {
-            for (String value : values) {
-                final MetaInfTldSource metaInfTldSource = parseMetaInfTldLocation(value);
-
-                if (metaInfTldSources == null) {
-                    metaInfTldSources = new ArrayList();
-                }
-
-                metaInfTldSources.add(metaInfTldSource);
-            }
-        }
-
-        if (metaInfTldSources == null) {
-            metaInfTldSources = Collections.emptyList();
-        }
-
-        return metaInfTldSources;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/dcbebe9b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
index c3f87a9..7969b56 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/web/view/FreemarkerViewResolver.java
@@ -26,7 +26,6 @@ import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
 import org.apache.freemarker.core.model.impl.DefaultObjectWrapper;
 import org.apache.freemarker.servlet.ServletContextHashModel;
 import org.apache.freemarker.servlet.jsp.TaglibFactory;
-import org.apache.freemarker.servlet.jsp.TaglibFactoryBuilder;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
 import org.springframework.web.servlet.view.AbstractUrlBasedView;
@@ -128,7 +127,7 @@ public class FreemarkerViewResolver extends AbstractTemplateViewResolver
impleme
 
         servletContextModel = new ServletContextHashModel(pageContextServlet, objectWrapper);
 
-        taglibFactory = new TaglibFactoryBuilder(getServletContext(), objectWrapper).build();
+        taglibFactory = new TaglibFactory.Builder(getServletContext(), objectWrapper).build();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/dcbebe9b/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 093472c..1ed2831 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
@@ -32,7 +32,6 @@ import org.apache.freemarker.core.model.ObjectWrapperAndUnwrapper;
 import org.apache.freemarker.core.templateresolver.impl.StringTemplateLoader;
 import org.apache.freemarker.servlet.ServletContextHashModel;
 import org.apache.freemarker.servlet.jsp.TaglibFactory;
-import org.apache.freemarker.servlet.jsp.TaglibFactoryBuilder;
 import org.junit.Before;
 import org.junit.Test;
 import org.springframework.mock.web.MockHttpServletRequest;
@@ -73,7 +72,7 @@ public class FreemarkerViewTest {
 
         pageContextServlet = new PageContextServlet();
         pageContextServlet.init(new PageContextServletConfig(servletContext, PageContextServlet.class.getSimpleName()));
-        taglibFactory = new TaglibFactoryBuilder(servletContext, objectWrapper).build();
+        taglibFactory = new TaglibFactory.Builder(servletContext, objectWrapper).build();
 
         viewResolver = new FreemarkerViewResolver();
         viewResolver.setServletContext(servletContext);


Mime
View raw message