Access Array item by index in template

This commit is contained in:
Mickael Jeanroy
2013-10-05 22:26:14 +02:00
parent 3af55c2660
commit 98c7a5d38f
2 changed files with 74 additions and 1 deletions
@@ -7,8 +7,12 @@ package com.samskivert.mustache;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
@@ -139,7 +143,18 @@ public class Template
}
// once we step into a composite key, we drop the ability to query our parent
// contexts; that would be weird and confusing
data = getValueIn(data, comps[ii].intern(), line);
String next = comps[ii].intern();
Integer idx = toNumber(next);
if (idx != null) {
Object nextData = null;
List<Object> c = toList(data);
if (c != null && idx >= 0 && idx < c.size()) {
nextData = c.get(idx);
}
data = nextData;
} else {
data = getValueIn(data, next, line);
}
}
return checkForMissing(name, line, missingIsNull, data);
}
@@ -169,6 +184,30 @@ public class Template
return checkForMissing(name, line, missingIsNull, NO_FETCHER_FOUND);
}
protected static List<Object> toList(Object obj) {
if (obj instanceof Collection) {
return new ArrayList<Object>((Collection) obj);
} else if (obj instanceof Iterator) {
Iterator<Object> it = (Iterator<Object>) obj;
List<Object> lst = new ArrayList<Object>();
while (it.hasNext()) {
lst.add(it.next());
}
return lst;
} else if (obj instanceof Object[]) {
return Arrays.asList((Object[]) obj);
}
return null;
}
protected static Integer toNumber(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException ex) {
return null;
}
}
/**
* Returns the value of the specified variable, noting that it is intended to be used as the
* contents for a segment. Presently this does not do anything special, but eventually this