freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [5/6] incubator-freemarker git commit: FREEMARKER-54: Adding baseLocation property in SpringResourceTemplateLoader for convenience.
Date Mon, 19 Jun 2017 09:29:14 GMT
FREEMARKER-54: Adding baseLocation property in SpringResourceTemplateLoader for convenience.


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

Branch: refs/heads/3
Commit: 85c43214e743eca5b82123ebfb1e2ac4dec1c073
Parents: 2c5a4fd
Author: Woonsan Ko <woonsan@apache.org>
Authored: Mon Jun 19 00:52:25 2017 -0400
Committer: Woonsan Ko <woonsan@apache.org>
Committed: Mon Jun 19 00:52:25 2017 -0400

----------------------------------------------------------------------
 .../SpringResourceTemplateLoader.java           | 52 +++++++++++++++++++-
 .../SpringResourceTemplateLoaderTest.java       |  5 +-
 2 files changed, 53 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/85c43214/freemarker-spring/src/main/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoader.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/main/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoader.java
b/freemarker-spring/src/main/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoader.java
index 3f96228..3230841 100644
--- a/freemarker-spring/src/main/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoader.java
+++ b/freemarker-spring/src/main/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoader.java
@@ -39,18 +39,55 @@ public class SpringResourceTemplateLoader implements TemplateLoader, ResourceLoa
 
     private static final Logger LOG = _SpringLogs.TEMPLATE_RESOLVER;
 
+    /**
+     * Base template resource location.
+     * <P>
+     * If this property is a non-null string, this is prepended to the template name internally
when resolving
+     * a resource.
+     * </P>
+     */
+    private String baseLocation;
+
+    /**
+     * Spring Framework resource loader.
+     */
     private ResourceLoader resourceLoader;
 
+    /**
+     * Base resource location which can be prepended to the template name internally when
resolving a resource.
+     * @return
+     */
+    public String getBaseLocation() {
+        return baseLocation;
+    }
+
+    /**
+     * Set base resource location which can be prepended to the template name internally
when resolving a resource.
+     * @param baseLocation
+     */
+    public void setBaseLocation(String baseLocation) {
+        this.baseLocation = baseLocation;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void setResourceLoader(ResourceLoader resourceLoader) {
         this.resourceLoader = resourceLoader;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public TemplateLoaderSession createSession() {
         return null;
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public TemplateLoadingResult load(String name, TemplateLoadingSource ifSourceDiffersFrom,
             Serializable ifVersionDiffersFrom, TemplateLoaderSession session) throws IOException
{
@@ -58,7 +95,15 @@ public class SpringResourceTemplateLoader implements TemplateLoader, ResourceLoa
             throw new IllegalStateException("Spring Framework ResourceLoader was not set.");
         }
 
-        Resource resource = resourceLoader.getResource(name);
+        String resourceLocation;
+
+        if (baseLocation == null) {
+            resourceLocation = name;
+        } else {
+            resourceLocation = baseLocation + "/" + name;
+        }
+
+        Resource resource = resourceLoader.getResource(resourceLocation);
 
         if (!resource.exists()) {
             return TemplateLoadingResult.NOT_FOUND;
@@ -74,7 +119,7 @@ public class SpringResourceTemplateLoader implements TemplateLoader, ResourceLoa
                 version = lmd;
             }
         } catch (IOException e) {
-            LOG.debug("The last modified timestamp of the resource at '{}' may not be resolved.",
name, e);
+            LOG.debug("The last modified timestamp of the resource at '{}' may not be resolved.",
resourceLocation, e);
         }
 
         if (ifSourceDiffersFrom != null && ifSourceDiffersFrom.equals(source)
@@ -85,6 +130,9 @@ public class SpringResourceTemplateLoader implements TemplateLoader, ResourceLoa
         return new TemplateLoadingResult(source, version, resource.getInputStream(), null);
     }
 
+    /**
+     * {@inheritDoc}
+     */
     @Override
     public void resetState() {
         // Does nothing

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/85c43214/freemarker-spring/src/test/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoaderTest.java
----------------------------------------------------------------------
diff --git a/freemarker-spring/src/test/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoaderTest.java
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoaderTest.java
index 23f604e..bcf014a 100644
--- a/freemarker-spring/src/test/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoaderTest.java
+++ b/freemarker-spring/src/test/java/org/apache/freemarker/spring/templateresolver/SpringResourceTemplateLoaderTest.java
@@ -46,6 +46,7 @@ public class SpringResourceTemplateLoaderTest {
     public void setUp() throws IOException {
         appContext = new GenericApplicationContext();
         templateLoader = new SpringResourceTemplateLoader();
+        templateLoader.setBaseLocation(TEMPLATE_BASE_PATH);
         templateLoader.setResourceLoader(appContext);
         cfg = new TestConfigurationBuilder().templateLoader(templateLoader).build();
     }
@@ -62,7 +63,7 @@ public class SpringResourceTemplateLoaderTest {
     @Test
     public void testSuccessful() throws Exception {
         for (int i = 0; i < 2; i++) {
-            assertEquals("foo", cfg.getTemplate(TEMPLATE_BASE_PATH + "sub1/sub2/t.ftl").toString());
+            assertEquals("foo", cfg.getTemplate("sub1/sub2/t.ftl").toString());
         }
     }
 
@@ -70,7 +71,7 @@ public class SpringResourceTemplateLoaderTest {
     public void testNotFound() throws Exception {
         for (int i = 0; i < 2; i++) {
             try {
-                cfg.getTemplate(TEMPLATE_BASE_PATH + "sub1X/sub2/t.ftl");
+                cfg.getTemplate("sub1X/sub2/t.ftl");
                 fail();
             } catch (TemplateNotFoundException e) {
                 assertThat(e.getMessage(), containsString("sub1X"));


Mime
View raw message