NodeModels from namespaced DOM Elements

Tom Fennelly <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Hi guys.

Do you need to do something special to use NodeModels build from a 
namespaced DOM Element?  Attached please find a test that works when the 
XML is not namespaced, but fails when it is namespaced.

The FreeMarker error I get is....

18:09:27,170 ERROR [main][runtime]
Error on line 1, column 3 in free-marker-template
Expecting a string, date or number here, Expression item.nachname is 
instead a freemarker.ext.dom.NodeListModel
The problematic instruction:
----------
==> ${item.nachname} [on line 1, column 1 in free-marker-template]
----------

Java backtrace for programmers:
----------
freemarker.core.NonStringException: Error on line 1, column 3 in 
free-marker-template
Expecting a string, date or number here, Expression item.nachname is 
instead a freemarker.ext.dom.NodeListModel
     at freemarker.core.Expression.getStringValue(Expression.java:126)
     at freemarker.core.Expression.getStringValue(Expression.java:93)
     at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
     at freemarker.core.Environment.visit(Environment.java:209)
     at freemarker.core.MixedContent.accept(MixedContent.java:92)
     at freemarker.core.Environment.visit(Environment.java:209)
     at freemarker.core.Environment.process(Environment.java:189)
     at freemarker.template.Template.process(Template.java:237)
     at 
org.milyn.templating.freemarker.FreeMarkerNodeModelTest.applyTemplate(FreeMarkerNodeModelTest.java:53)
     at 
org.milyn.templating.freemarker.FreeMarkerNodeModelTest.test_with_namespace(FreeMarkerNodeModelTest.java:41)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at junit.framework.TestCase.runTest(TestCase.java:168)
     at junit.framework.TestCase.runBare(TestCase.java:134)
     at junit.framework.TestResult$1.protect(TestResult.java:110)
     at junit.framework.TestResult.runProtected(TestResult.java:128)
     at junit.framework.TestResult.run(TestResult.java:113)
     at junit.framework.TestCase.run(TestCase.java:124)
     at junit.textui.TestRunner.doRun(TestRunner.java:116)
     at 
com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:139)
     at junit.textui.TestRunner.doRun(TestRunner.java:109)
     at 
com.intellij.junit3.JUnit3IdeaTestRunner.startRunnerWithArgs(JUnit3IdeaTestRunner.java:52)
     at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
     at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

------------------------------------------------------------------------------
Doing More with Less: The Next Generation Virtual Desktop 
What are the key obstacles that have prevented many mid-market businesses
from deploying virtual desktops?   How do next-generation virtual desktops
provide companies an easier-to-deploy, easier-to-manage and more affordable
virtual desktop model.http://www.accelacomm.com/jaw/sfnl/114/51426474/

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
FreeMarkerNodeModelTest.java (text/plain, 3.3 KB)
package org.milyn.templating.freemarker;

import freemarker.ext.dom.NodeModel;
import freemarker.template.Configuration;
import freemarker.template.Template;
import junit.framework.TestCase;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

public class FreeMarkerNodeModelTest extends TestCase {

    /**
     * Works...
     */
    public void test_no_namespace() throws Exception {
        applyTemplate(NO_NS_XML);
    }

    /**
     * Throws exception saying ...
     *
     * 18:07:19,748 ERROR [main][runtime]
     * Error on line 1, column 3 in free-marker-template
     * Expecting a string, date or number here, Expression item.nachname is instead a freemarker.ext.dom.NodeListModel
     * The problematic instruction:
     * ----------
     * ==> ${item.nachname} [on line 1, column 1 in free-marker-template]
     * ----------
     */
    public void test_with_namespace() throws Exception {
        applyTemplate(NS_XML);
    }

    public void applyTemplate(String xml) throws Exception {
        Template template = createTemplate();
        Map model = new HashMap();
        Element xmlElement = parseXML(xml);
        NodeModel nodeModel = NodeModel.wrap(xmlElement);

        model.put("item", nodeModel);

        StringWriter outputWriter = new StringWriter();
        template.process(model, outputWriter);

        System.out.println(outputWriter);
    }

    private Element parseXML(String xml) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
    }

    private Template createTemplate() throws IOException {
        Reader templateReader = new StringReader(freemarkerTemplate);

        try {
            return new Template("free-marker-template", templateReader, new Configuration());
        } finally {
            templateReader.close();
        }

    }

    private static final String freemarkerTemplate = "${item.nachname}|${item.eintrittsdatum}|Erstellungszeit hhmmss|U|PersNr|${item.anrede}|Name|${item.vorname}|Geb.Datum|Funktion||Verteilstelle|Eintritt|Austritt|";

    private static final String NO_NS_XML = "<item>\n" +
            "         <titel>Prof. Dr. med.</titel>\n" +
            "         <anrede>Herr</anrede>\n" +
            "         <vorname>Beat</vorname>\n" +
            "         <nachname>schlater</nachname>\n" +
            "         <eintrittsdatum>2010-08-01+02:00</eintrittsdatum>\n" +
            "       </item>";

    private static final String NS_XML = "<item xmlns=\"http://acme.com/ns\">\n" +
            "         <titel>Prof. Dr. med.</titel>\n" +
            "         <anrede>Herr</anrede>\n" +
            "         <vorname>Beat</vorname>\n" +
            "         <nachname>schlater</nachname>\n" +
            "         <eintrittsdatum>2010-08-01+02:00</eintrittsdatum>\n" +
            "       </item>";
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.