use of freemarker.template.TemplateModel in project freemarker by apache.
the class TaglibFactory method resolveRelativeUri.
private static String resolveRelativeUri(String uri) throws TaglibGettingException {
TemplateModel reqHash;
try {
reqHash = Environment.getCurrentEnvironment().getVariable(FreemarkerServlet.KEY_REQUEST_PRIVATE);
} catch (TemplateModelException e) {
throw new TaglibGettingException("Failed to get FreemarkerServlet request information", e);
}
if (reqHash instanceof HttpRequestHashModel) {
HttpServletRequest req = ((HttpRequestHashModel) reqHash).getRequest();
String pi = req.getPathInfo();
String reqPath = req.getServletPath();
if (reqPath == null) {
reqPath = "";
}
reqPath += (pi == null ? "" : pi);
// We don't care about paths with ".." in them. If the container
// wishes to resolve them on its own, let it be.
int lastSlash = reqPath.lastIndexOf('/');
if (lastSlash != -1) {
return reqPath.substring(0, lastSlash + 1) + uri;
} else {
return '/' + uri;
}
}
throw new TaglibGettingException("Can't resolve relative URI " + uri + " as request URL information is unavailable.");
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class NumberFormatTest method testStringBIDoesSnapshot.
/**
* ?string formats lazily (at least in 2.3.x), so it must make a snapshot of the format inputs when it's called.
*/
@Test
public void testStringBIDoesSnapshot() throws Exception {
// TemplateNumberModel-s shouldn't change, but we have to keep BC when that still happens.
final MutableTemplateNumberModel nm = new MutableTemplateNumberModel();
nm.setNumber(123);
addToDataModel("n", nm);
addToDataModel("incN", new TemplateDirectiveModel() {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
nm.setNumber(nm.getAsNumber().intValue() + 1);
}
});
assertOutput("<#assign s1 = n?string>" + "<#setting numberFormat='@loc'>" + "<#assign s2 = n?string>" + "<#setting numberFormat='@hex'>" + "<#assign s3 = n?string>" + "${s1} ${s2} ${s3}", "123 123_en_US 7b");
assertOutput("<#assign s1 = n?string>" + "<@incN />" + "<#assign s2 = n?string>" + "${s1} ${s2}", "123 124");
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class SequenceBuiltInTest method testWithCollectionEx.
@Test
public void testWithCollectionEx() throws TemplateException, IOException {
ObjectWrapperWithAPISupport ow = (ObjectWrapperWithAPISupport) getConfiguration().getObjectWrapper();
TemplateModel xs = DefaultNonListCollectionAdapter.adapt(ImmutableSet.of("a", "b"), ow);
assertThat(xs, not(instanceOf(TemplateSequenceModel.class)));
assertThat(xs, instanceOf(TemplateCollectionModelEx.class));
addToDataModel("xs", xs);
try {
assertOutput("${xs[1]}", "b");
fail();
} catch (TemplateException e) {
// Contains tip to use ?sequence
assertThat(e.getMessage(), containsString("?sequence"));
}
assertOutput("${xs?sequence[1]}", "b");
// No need for ?sequence
assertOutput("${xs?size}", "2");
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class BeansWrapperMiscTest method java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest.
@Test
public void java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest() throws TemplateModelException {
assertTrue("This test case must be ran on Java 8 or later", _JavaVersions.JAVA_8 != null);
assertFalse(Modifier.isPublic(BeanWithInaccessibleIndexedProperty.class.getModifiers()));
for (Version ici : new Version[] { Configuration.VERSION_2_3_26, Configuration.VERSION_2_3_27 }) {
BeansWrapper bw = new BeansWrapper(ici);
TemplateHashModel beanTM = (TemplateHashModel) bw.wrap(new BeanWithInaccessibleIndexedProperty());
TemplateModel fooTM = beanTM.get("foo");
assertThat(fooTM, instanceOf(TemplateSequenceModel.class));
assertEquals("b", ((TemplateScalarModel) ((TemplateSequenceModel) fooTM).get(1)).getAsString());
// Even with 2.3.26, where the indexed reader was preferred, as it's inaccessible, we use the normal reader:
assertEquals(2, ((TemplateSequenceModel) fooTM).size());
TemplateModel barTM = beanTM.get("bar");
// all read methods inaccessible
assertNull(barTM);
}
}
use of freemarker.template.TemplateModel in project freemarker by apache.
the class EnumModelsTest method modelCaching.
@Test
public void modelCaching() throws Exception {
BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
TemplateHashModel enums = bw.getEnumModels();
TemplateHashModel e = (TemplateHashModel) enums.get(E.class.getName());
assertNotNull(e);
assertNotNull(e.get("A"));
assertNotNull(e.get("B"));
assertNull(e.get("C"));
try {
enums.get("no.such.ClassExists");
fail();
} catch (TemplateModelException ex) {
assertTrue(ex.getCause() instanceof ClassNotFoundException);
}
TemplateModel a = e.get("A");
assertTrue(a instanceof TemplateScalarModel);
assertTrue(a instanceof TemplateHashModel);
assertEquals(((TemplateScalarModel) a).getAsString(), "ts:A");
TemplateMethodModelEx nameMethod = (TemplateMethodModelEx) ((TemplateHashModel) a).get("name");
assertEquals(((TemplateScalarModel) nameMethod.exec(new ArrayList())).getAsString(), "A");
assertSame(e, enums.get(E.class.getName()));
bw.clearClassIntrospecitonCache();
TemplateHashModel eAfterClean = (TemplateHashModel) enums.get(E.class.getName());
assertNotSame(e, eAfterClean);
assertSame(eAfterClean, enums.get(E.class.getName()));
assertNotNull(eAfterClean.get("A"));
assertNotNull(eAfterClean.get("B"));
assertNull(eAfterClean.get("C"));
}
Aggregations