Custom directive for XML model

Viktor Levine <[email protected]> Thu, 12 Sep 2013 07:31:30 +0000 (UTC)
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
In the test below the test for map model works fine, but for XML model
breaks. The custom directive is to wire in Spring's SPEL into FM templates,
but I'm certain that the same would fail on any other directive. The reason
IMHO is this construct in DocumentModel
if (StringUtil.isXMLID(key)) {
            ElementModel em = (ElementModel) NodeModel.wrap(((Document)
node).getDocumentElement());
            if (em.matchesName(key, Environment.getCurrentEnvironment())) {
                return em;
            } else {
//HERE
                return new NodeListModel(this);
            }
}
which should return null where it is commented HERE. Then the caller
(Environment.getGlobalVariable(String)) would correctly pull the directive
from the shared variables.

The template  is :
[#ftl]
We say [@spel]@greetingBean.printGreeting(name)[/@spel] from Freemarker in
[@spel expression="T(java.util.Locale).getDefault()"/]


The test (pardon me for the length):

public class SpelFreemarkerTemplateDirectiveTest {
    private static final String XML = "<name>Felix</name>";
    @Mock
    private BeanFactory beanFactory;
    private GreetingBean greetingBean = new GreetingBean("Hello");

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testFreemarkerSpel()
        throws IOException, TemplateException {
        Map<String, String> model = new HashMap<String, String>();

        model.put("name", "Felix");

        Configuration cfg = new Configuration();
        SpelFreemarkerTemplateDirective directive = new
SpelFreemarkerTemplateDirective();

        when(beanFactory.getBean("greetingBean")).thenReturn(greetingBean);
        directive.setBeanFactory(beanFactory);
        cfg.setSharedVariable("spel", directive);

        Template tpl = cfg.getTemplate("greeting-template.ftl");

        StringWriter writer = new StringWriter();

        tpl.process(model, writer);

        System.out.println(writer.toString());

        String expected = "We say " +
greetingBean.printGreeting(model.get("name")) +
            " from Freemarker in " + Locale.getDefault();

        assertEquals(expected, writer.toString());
    }

    /**
     * JAVADOC Method Level Comments
     *
     * @throws Exception JAVADOC.
     */
    @Test
    public void testXmlSpel()
        throws Exception {
        NodeModel model = NodeModel.parse(new InputSource(new
StringReader(XML)));
        Configuration cfg = new Configuration();

        SpelFreemarkerTemplateDirective directive = new
SpelFreemarkerTemplateDirective();

        when(beanFactory.getBean("greetingBean")).thenReturn(greetingBean);
        directive.setBeanFactory(beanFactory);
        cfg.setSharedVariable("spel", directive);

        Template tpl = cfg.getTemplate("greeting-template.ftl");

        StringWriter writer = new StringWriter();

        tpl.process(model, writer);

        System.out.println(writer.toString());

        String expected = "We say " + greetingBean.printGreeting("Felix") +
" from Freemarker in " +
            Locale.getDefault();

        assertEquals(expected, writer.toString());
    }

    public static class GreetingBean {
        private String greeting;

        public GreetingBean(String greeting) {
            this.greeting = greeting;
        }

        public String printGreeting(String name) {
            return greeting + " " + name;
        }
    }
}



------------------------------------------------------------------------------
How ServiceNow helps IT people transform IT departments:
1. Consolidate legacy IT systems to a single system of record for IT
2. Standardize and globalize service processes across IT
3. Implement zero-touch automation to replace manual, redundant tasks
http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk