I'm using an odt template and want to merge it with a generated XML
File. All Variables are replaced fine, except the one where I'm having
a #list element in my template.
So here is the example:
Template:
XML:
<Properties>
<Name>SomeName</Name>
</Properties>
will be replaced just fine.
List:
<#list doc.Properties.Require.Category>
${doc.Properties.Require.Category.Requirements_category}
${doc.Properties.Require.Category.Explanation}
${doc.Properties.Require.Category.Requirement_text}
</#list>
XML:
<Properties>
<Require>
<Category>
<Requirements_category>Req1</Requirements_category>
<Explanation/>
<Requirement_text>reqText</Requirement_text>
</Category>
<Category>
<Requirements_category>Req2</Requirements_category>
<Explanation/>
<Requirement_text>reqText2</Requirement_text>
</Category>
</Require>
</Properties>
With one Category Element the #list Tag works fine (but the <#list>
tag is still visible in the final document), but when I'm having two
or more Category Elements I'm getting the following exception:
Error on line 2, column 17 in content.xml
Expecting a string, date or number here, Expression any is instead a
freemarker.ext.dom.NodeListModel
The problematic instruction:
----------
==> ${doc.Properties.Require.Category.Requirements_category} escaped $
{doc.Properties.Require.Category.Requirements_category?xml?replace("
","<text:line-break />")} [on line 4, column 37936 in content.xml]
----------
Java backtrace for programmers:
----------
freemarker.core.NonStringException: Error on line 2, column 17 in
content.xml
Expecting a string, date or number here, Expression any is instead a
freemarker.ext.dom.NodeListModel
at freemarker.core.Expression.getStringValue(Expression.java:126)
at freemarker.core.Expression.getStringValue(Expression.java:93)
If I look at the content.xml
<text:list-header><text:p text:style-name="P19"><#list
doc.Properties.Require.Category></text:p><text:p text:style-
name="P20"><text:span text:style-name="T2">$
{doc.Properties.Require.Category.Requirements_category}</text:span></
text:p>
Could the < be a problem?
I don't know what to do anymore. I think #list should work fine in
JODReports, otherwise someone else would have found that problem.
Last info: I'm using jodreports-cli-2.4.0-jar-with-dependencies.jar.
Thanks for any help.
René
You probably forgot the 'as item' part of the Freemarker list syntax.
See [1] for reference and example.
Though I never worked with JODReports and XML, I guess replacing the
above code with the following will at least get you some further:
<#list doc.Properties.Require.Category as category>
${category.Requirements_category}
${category.Explanation}
${category.Requirement_text}
</#list>
Many features of JODReports are heavily based on Freemarker. For XML processing, you might want to consider [2] to learn the basic concepts.
Best regards
Ansgar
[1] http://fmpp.sourceforge.net/freemarker/ref_directive_list.html
[2] http://freemarker.sourceforge.net/docs/xgui_imperative_learn.html
thanks for your answer. But I already tried that and I'm nearly
getting the same exception:
09:32:02,648 ERROR [runtime] Expression category is undefined on line
4, column 37409 in content.xml.
Expression category is undefined on line 4, column 37409 in
content.xml.
The problematic instruction:
----------
==> ${category.Requirements_category} escaped $
{category.Requirements_category?xml?replace("
","<text:line-break />")} [on line 4, column 37407 in content.xml]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression category is
undefined on line 4, column 37409 in content.xml.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:
124)
at
freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:
134)
at freemarker.core.Dot._getAsTemplateModel(Dot.java:78)
I checked the freemarker tutorial before and tried that tutorial:
http://fmpp.sourceforge.net/freemarker/xgui_imperative_learn.html
If I'm setting as output a plain html document and just use freemarker
code, everything works fine with the list element. But if I'm using an
ODT Template I'm getting the same exception as in my project. I don't
think there is something wrong with my code, because it's just copied
from the tutorial:
ClassLoader classL = Thread.currentThread().getContextClassLoader();
URL uri = classL.getResource("template.odt");
String temp = uri.getFile();
template = new File(temp);
File outputFile = new File("test.odt");
OutputFormat format = new OutputFormat(dom, "UTF-8", true);
XMLSerializer serializer;
try {
serializer = new XMLSerializer(new FileOutputStream(new File("test-
data.xml")), format);
serializer.serialize(dom);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e2) {
e2.printStackTrace();
}
File dataFile = new File("test-data.xml");
HashMap root = new HashMap();
try {
root.put("doc", NodeModel.parse(dataFile));
} catch (SAXException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
} catch (ParserConfigurationException e2) {
e2.printStackTrace();
}
DocumentTemplateFactory tempFactory = new DocumentTemplateFactory();
DocumentTemplate templateDoc = null;
try {
templateDoc = tempFactory.getTemplate(template);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
templateDoc.createDocument(root, new FileOutputStream(outputFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentTemplateException e) {
e.printStackTrace();
}
If it's running with a html file and freemarker I guess there is a
problem with jodreports.
Regards,
René
On 1 Dez., 21:12, Ansgar Konermann <ansgar.konerm...@googlemail.com>
wrote:
Aaahm... now that you're mentioning it... YES!
Use [ instead.
JODReports uses the "alternative" Freemarker syntax, using [ and ]
instead of < and >.
This is because ODT (the template format) is XML-based and it should be
easy to differentiate Freemarker directives from XML tags. You'd have to
escape the Freemarker < and > otherwise -- this is inconvenient.
In your case:
[#list doc.Properties.Require.Category as category ]
${category.Requirements_category}
${category.Explanation}
${category.Requirement_text}
[/#list]
HTH
Best regards
Ansgar
thank you very much. That fixed the problem :)!!!
Regards,
René
2011/12/2 Ansgar Konermann <ansgar.k...@googlemail.com>: