Make MustacheCustomContext a Mustache inner interface.
This matches the style of all the other extension points.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -324,6 +324,21 @@ public class Mustache {
|
||||
<K,V> Map<K,V> 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 {
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user