use of freemarker.core.Environment.Namespace in project freemarker by apache.
the class MistakenlyPublicImportAPIsTest method testImportCopying.
@Test
public void testImportCopying() throws IOException, TemplateException {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
StringTemplateLoader tl = new StringTemplateLoader();
tl.putTemplate("imp1", "<#macro m>1</#macro>");
tl.putTemplate("imp2", "<#assign x = 2><#macro m>${x}</#macro>");
cfg.setTemplateLoader(tl);
Template t1 = new Template(null, "<#import 'imp1' as i1><#import 'imp2' as i2>", cfg);
List<LibraryLoad> imports = t1.getImports();
assertEquals(2, imports.size());
{
Template t2 = new Template(null, "<@i1.m/><@i2.m/>", cfg);
for (LibraryLoad libLoad : imports) {
t2.addImport(libLoad);
}
try {
t2.process(null, NullWriter.INSTANCE);
fail();
} catch (InvalidReferenceException e) {
// Apparenly, it has never worked like this...
assertEquals("i1", e.getBlamedExpressionString());
}
}
// It works this way, though it has nothing to do with the problematic API-s:
Environment env = t1.createProcessingEnvironment(null, NullWriter.INSTANCE);
env.process();
TemplateModel i1 = env.getVariable("i1");
assertThat(i1, instanceOf(Namespace.class));
TemplateModel i2 = env.getVariable("i2");
assertThat(i2, instanceOf(Namespace.class));
{
Template t2 = new Template(null, "<@i1.m/>", cfg);
StringWriter sw = new StringWriter();
env = t2.createProcessingEnvironment(null, sw);
env.setVariable("i1", i1);
env.process();
assertEquals("1", sw.toString());
}
{
Template t2 = new Template(null, "<@i2.m/>", cfg);
StringWriter sw = new StringWriter();
env = t2.createProcessingEnvironment(null, sw);
env.setVariable("i2", i2);
try {
env.process();
assertEquals("2", sw.toString());
} catch (NullPointerException e) {
// Expected on 2.3.x, because it won't find the namespace for the macro
// [2.4] Fix this "bug"
}
}
}
use of freemarker.core.Environment.Namespace in project PublicCMS-preview by sanluan.
the class TemplateDirectiveHandler method reduce.
private void reduce(Map<String, TemplateModel> reduceMap) {
if (CommonUtils.notEmpty(reduceMap)) {
Namespace namespace = environment.getCurrentNamespace();
namespace.putAll(reduceMap);
}
}
use of freemarker.core.Environment.Namespace in project PublicCMS-preview by sanluan.
the class TemplateDirectiveHandler method reduce.
private Map<String, TemplateModel> reduce() throws TemplateModelException {
Map<String, TemplateModel> reduceMap = new LinkedHashMap<>();
ObjectWrapper objectWrapper = environment.getObjectWrapper();
Namespace namespace = environment.getCurrentNamespace();
Iterator<Entry<String, Object>> iterator = map.entrySet().iterator();
for (int i = 0; iterator.hasNext(); i++) {
Entry<String, Object> entry = iterator.next();
if (i < loopVars.length) {
loopVars[i] = objectWrapper.wrap(entry.getValue());
} else {
String key = entry.getKey();
reduceMap.put(key, namespace.get(key));
namespace.put(key, objectWrapper.wrap(entry.getValue()));
}
}
return reduceMap;
}
Aggregations