diff --git a/src/main/java/com/samskivert/mustache/BasicCollector.java b/src/main/java/com/samskivert/mustache/BasicCollector.java index 0765f32..09d167c 100644 --- a/src/main/java/com/samskivert/mustache/BasicCollector.java +++ b/src/main/java/com/samskivert/mustache/BasicCollector.java @@ -35,7 +35,7 @@ public abstract class BasicCollector implements Mustache.Collector } public Mustache.VariableFetcher createFetcher (Object ctx, String name) { - if (ctx instanceof MustacheCustomContext) return CUSTOM_FETCHER; + if (ctx instanceof Mustache.CustomContext) return CUSTOM_FETCHER; if (ctx instanceof Map) return MAP_FETCHER; // if the name looks like a number, potentially use one of our 'indexing' fetchers @@ -68,7 +68,7 @@ public abstract class BasicCollector implements Mustache.Collector protected static final Mustache.VariableFetcher CUSTOM_FETCHER = new Mustache.VariableFetcher() { public Object get (Object ctx, String name) throws Exception { - MustacheCustomContext custom = (MustacheCustomContext)ctx; + Mustache.CustomContext custom = (Mustache.CustomContext)ctx; Object val = custom.get(name); return val == null ? Template.NO_FETCHER_FOUND : val; } diff --git a/src/main/java/com/samskivert/mustache/DefaultCollector.java b/src/main/java/com/samskivert/mustache/DefaultCollector.java index ab4a641..234b7a3 100644 --- a/src/main/java/com/samskivert/mustache/DefaultCollector.java +++ b/src/main/java/com/samskivert/mustache/DefaultCollector.java @@ -4,12 +4,9 @@ package com.samskivert.mustache; -import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.AbstractList; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 7a173b4..073f612 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -324,6 +324,21 @@ public class Mustache { Map createFetcherCache (); } + /** + * Provides a means to implement custom logic for variable lookup. If a context object + * implements this interface, its {@code get} method will be used to look up variables instead + * of the usual methods. + * + * This is simpler than having a context implement {@link Map} which would require that it also + * support the {@link Map#entrySet} method for iteration. A {@code CustomContext} object cannot + * be used for a list section. + */ + public interface CustomContext { + + /** Fetches the value of a variable named {@code name}. */ + Object get (String name) throws Exception; + } + /** Used to visit the tags in a template without executing it. */ public interface Visitor { diff --git a/src/main/java/com/samskivert/mustache/MustacheCustomContext.java b/src/main/java/com/samskivert/mustache/MustacheCustomContext.java deleted file mode 100644 index 2f9d5cb..0000000 --- a/src/main/java/com/samskivert/mustache/MustacheCustomContext.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.samskivert.mustache; - -/** - * Provides a simple interface for callers to implement their own logic for contextual values. - */ -public interface MustacheCustomContext { - Object get(String name) throws Exception; -} diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index ce446ae..80e776d 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -45,8 +45,8 @@ public class MustacheTest extends SharedTests }); } - @Test public void testMustacheCustomContext() { - test("bar", "{{foo}}", new MustacheCustomContext() { + @Test public void testCustomContext() { + test("bar", "{{foo}}", new Mustache.CustomContext() { @Override public Object get(String name) { return "foo".equals(name) ? "bar" : null;