Package freemarker.template

Examples of freemarker.template.TemplateHashModel


        TemplateHashModel thm = (TemplateHashModel) bw.wrap(new C());
        return thm.get("getBar") != null;
    }

    private boolean exposesUnsafe(BeansWrapper bw) throws TemplateModelException {
        TemplateHashModel thm = (TemplateHashModel) bw.wrap(new C());
        return thm.get("wait") != null;
    }
View Full Code Here


                }
            } catch (Exception e) {
                throw new _ObjectBuilderSettingEvaluationException("Failed to inspect " + cl.getName() + " class", e);
            }

            TemplateHashModel beanTM = null;
            for (int i = 0; i < namedParamNames.size(); i++) {
                String name = (String) namedParamNames.get(i);
                if (!beanPropSetters.containsKey(name)) {
                    throw new _ObjectBuilderSettingEvaluationException(
                            "The " + cl.getName() + " class has no writeable JavaBeans property called "
                            + StringUtil.jQuote(name) + ".");
                }
               
                Method beanPropSetter = (Method) beanPropSetters.put(name, null);
                if (beanPropSetter == null) {
                        throw new _ObjectBuilderSettingEvaluationException(
                                "JavaBeans property " + StringUtil.jQuote(name) + " is set twice.");
                }
               
                try {
                    if (beanTM == null) {
                        TemplateModel wrappedObj = env.getObjectWrapper().wrap(bean);
                        if (!(wrappedObj instanceof TemplateHashModel)) {
                            throw new _ObjectBuilderSettingEvaluationException(
                                    "The " + cl.getName() + " class is not a wrapped as TemplateHashModel.");
                        }
                        beanTM = (TemplateHashModel) wrappedObj;
                    }
                   
                    TemplateModel m = beanTM.get(beanPropSetter.getName());
                    if (m == null) {
                        throw new _ObjectBuilderSettingEvaluationException(
                                "Can't find " + beanPropSetter + " as FreeMarker method.");
                    }
                    if (!(m instanceof TemplateMethodModelEx)) {
View Full Code Here

public class StaticModelsTest {

    @Test
    public void modelCaching() throws Exception {
        BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
        TemplateHashModel statics = bw.getStaticModels();
        TemplateHashModel s = (TemplateHashModel) statics.get(S.class.getName());
        assertNotNull(s);
        assertNotNull(s.get("F"));
        assertNotNull(s.get("m"));
        try {
            s.get("x");
            fail();
        } catch (TemplateModelException e) {
            assertThat(e.getMessage(), containsString("No such key"));
        }
       
        try {
            statics.get("no.such.ClassExists");
            fail();
        } catch (TemplateModelException e) {
            assertTrue(e.getCause() instanceof ClassNotFoundException);
        }
       
        TemplateModel f = s.get("F");
        assertTrue(f instanceof TemplateScalarModel);
        assertEquals(((TemplateScalarModel) f).getAsString(), "F OK");
       
        TemplateModel m = s.get("m");
        assertTrue(m instanceof TemplateMethodModelEx);
        assertEquals(((TemplateScalarModel) ((TemplateMethodModelEx) m).exec(new ArrayList())).getAsString(), "m OK");
       
        assertSame(s, statics.get(S.class.getName()));
       
        bw.clearClassIntrospecitonCache();
        TemplateHashModel sAfterClean = (TemplateHashModel) statics.get(S.class.getName());
        assertNotSame(s, sAfterClean);
        assertSame(sAfterClean, statics.get(S.class.getName()));
        assertNotNull(sAfterClean.get("F"));
        assertNotNull(sAfterClean.get("m"));
    }
View Full Code Here

public class EnumModelsTest {
   
    @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"));
    }
View Full Code Here

                for (int i = 0; i < iterations; i++) {
                    if (Math.random() < CACHE_CLEARING_CHANCE) {
                        beansWrapper.clearClassIntrospecitonCache();
                    }
                    int objIdx = (int) (Math.random() * NUM_ENTITYES);
                    TemplateHashModel h = getWrappedEntity(objIdx);
                    int mIdx = (int) (Math.random() * NUM_MEMBERS);
                    testProperty(h, objIdx, mIdx);
                    testMethod(h, objIdx, mIdx);
                }
            } catch (Throwable e) {
View Full Code Here

        ow.setExposeFields(true);
        checkIfProperlyWrapped(ow.wrap(new C()));
    }
   
    private void checkIfProperlyWrapped(TemplateModel tm) throws TemplateModelException {
        TemplateHashModel thm = (TemplateHashModel) tm;
        assertEquals("v1", ((TemplateScalarModel) thm.get("v1")).getAsString());
        assertEquals("v2()", ((TemplateScalarModel) thm.get("v2")).getAsString());
        assertEquals("getV3()", ((TemplateScalarModel) thm.get("v3")).getAsString());
        assertTrue(thm.get("getV3") instanceof TemplateMethodModelEx);
    }
View Full Code Here

        return globalNamespace;
    }
   
   
    public TemplateHashModel getDataModel() {
      final TemplateHashModel result = new TemplateHashModel() {
            public boolean isEmpty() {
                return false;
            }

            public TemplateModel get(String key) throws TemplateModelException {
                TemplateModel value = rootDataModel.get(key);
                if (value == null) {
                    value = getConfiguration().getSharedVariable(key);
                }
                return value;
            }
        };
       
        if (rootDataModel instanceof TemplateHashModelEx) {
          return new TemplateHashModelEx() {
            public boolean isEmpty() throws TemplateModelException {
              return result.isEmpty();
            }
            public TemplateModel get(String key) throws TemplateModelException {
              return result.get(key);
            }
           
            //NB: The methods below do not take into account
            // configuration shared variables even though
            // the hash will return them, if only for BWC reasons
View Full Code Here

     * That is, you see the variables created with
     * <code>&lt;#global ...&gt;</code>, and the variables of the data-model.
     * To create new global variables, use {@link #setGlobalVariable setGlobalVariable}.
     */
    public TemplateHashModel getGlobalVariables() {
        return new TemplateHashModel() {
            public boolean isEmpty() {
                return false;
            }
            public TemplateModel get(String key) throws TemplateModelException {
                TemplateModel result = globalNamespace.get(key);
View Full Code Here

    static private Logger logger = LoggerFactory.getLogger(FreemarkerUtil.class);
   
    static public <C> C getDataModel(String mapName, Class<C> c) {
        Environment env = Environment.getCurrentEnvironment();

        TemplateHashModel rootModel = env.getDataModel();
        Object tmpObj = rootModel;
        try {
            for (String key : mapName.split("\\.")) {
                if (tmpObj instanceof TemplateHashModel) {
                    tmpObj = ((TemplateHashModel) tmpObj).get(key);
View Full Code Here

                return new Float("1.1");
            }
        });

        // TemplateHashModel
        params.put("property7", new TemplateHashModel() {
            public TemplateModel get(String arg0) throws TemplateModelException {
                return null;
            }

            public boolean isEmpty() throws TemplateModelException {
View Full Code Here

TOP

Related Classes of freemarker.template.TemplateHashModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.