Discussion:
[FreeMarker-user] FreeMarker for newbies
Eyal
2013-07-09 14:00:06 UTC
Permalink
I am looking for a sample for a project that use Freemarker to inject new
elements to an existing XML file

How do you code this?



--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-07-10 10:35:11 UTC
Permalink
Post by Eyal
I am looking for a sample for a project that use Freemarker to inject new
elements to an existing XML file
How do you code this?
Can that existing XML file be a template instead of a plain XML file?
If not, maybe FreeMarker is not the right tool for the task, or it's
not the only tool that you need. I don't know much about what you are
trying to do.
--
Thanks,
Daniel Dekany
Eyal
2013-07-10 10:47:23 UTC
Permalink
Hi,

I'll explain in details.

I want to generate an XML file, that I have some of its general structure,
but some elements should be added in runtime.

The way I understood, I can create a template for the XML elements that I
need to calculate during runtime.

The missing information for me is- How do I process the template- that the
result will be added to the correct location in the XML file



--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654622.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Angelo zerr
2013-07-10 13:38:33 UTC
Permalink
Hi Eyal,
Post by Eyal
Hi,
I'll explain in details.
I want to generate an XML file, that I have some of its general structure,
but some elements should be added in runtime.
You have an existing XML file.
Post by Eyal
The way I understood, I can create a template for the XML elements that I
need to calculate during runtime.
You wish to use Freemarker to generate an XML element (in a string).
Post by Eyal
The missing information for me is- How do I process the template- that the
result will be added to the correct location in the XML file
You wish insert the XML element string in the existing XML file in a
special location.

If it that I had worked about this topic in the Akrogen Eclipse Plugin
http://akrogen.sourceforge.net/ several years ago.
My idea to insert (generated) XML element in special location is :

1) load XML file in a DOM document
2) generate XML element in a string by using Freemarker
3) load the generfated XML element in a DOM element
4) Insert the DOM element in the DOM document (you can retrieve node
location with XPath for instance or getElementsByTagName)
5) write the new DOM document in the file.

The important question is : do you wish keep the indentation, blank etc in
the XML file after inserting the generated XML element).
If you wish to do that, I had worked about this topic, but I don't remember
how I have done that.

Regards Angelo

You load your
Post by Eyal
--
http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654622.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Eyal
2013-07-10 13:50:59 UTC
Permalink
Hi Angelo,

Thanks for the algorithm!!

But I have to start with a simpler and technical question, when I want to
process the template, I need to direct it to a stream (In the samples it is
the System.out), how do I tell it to process the template to an XML element?

Thanks
Eyal



--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654624.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Angelo zerr
2013-07-10 14:00:30 UTC
Permalink
Post by Eyal
Hi Angelo,
Thanks for the algorithm!!
You are welcome.
Post by Eyal
But I have to start with a simpler and technical question, when I want to
process the template, I need to direct it to a stream (In the samples it is
the System.out), how do I tell it to process the template to an XML element?
1) Use StringWriter or ByteArrayOuptStream to stores the generated XML
element (generated with FM).
2) Load DOM document by using the StringWriter or ByteArrayOuptStream.
3) Get the DOM element (root element) of this DOM.

Perhaps using SAX Parser could be improved the memory, but I have never
done that.

Regards Angelo
Post by Eyal
Thanks
Eyal
--
http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654624.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Eyal
2013-07-10 14:08:26 UTC
Permalink
Thanks, will try to implement it :-)





--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654626.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-07-10 14:58:48 UTC
Permalink
Post by Eyal
Hi,
I'll explain in details.
I want to generate an XML file, that I have some of its general structure,
but some elements should be added in runtime.
The way I understood, I can create a template for the XML elements that I
need to calculate during runtime.
The missing information for me is- How do I process the template- that the
result will be added to the correct location in the XML file
How do you know where will those dynamic pieces inserted? If
it's know ahead of time, or at least you know where the potential
insertion points are (only they are conditional), you can create a
template from the whole XML file:

<myXml>
<blah />
${insertThoseElementsHere} or <@doSomeMoreFancyInsertingHere />
<blah />
${insertSomeMoreElementsHere}
</myXml>

So that's a typical templating task. I guess if that's what you need
you figure this out.

So, do you have to insert into a non-template XML? Or do you have to
decide where to insert what by walking the tree in Java? Then you can
invoke FreeMarker to generate a string at those places. Except... in
already parsed XML (DOM or SAX events) you can insert text. So maybe
you can get the source location of those places, and insert them into
the XML. Assuming you only need to generate XML "files" here, that's
probably the most efficient you can do, I think.
--
Thanks,
Daniel Dekany
Eyal
2013-07-11 07:56:12 UTC
Permalink
Hi,

I am using maven with nar plugin to compile also C++ code.

And now I was asked to create from the maven the Visual Stuido vcprojx files
(SInce the maven have all the information).

So- I am using a maven profile to compile every "Configuration" of Visual
Studio, I want to take an empty VCProjx file, and fill it with the relevant
elements of each profile.

Test2.xml <http://freemarker.624813.n4.nabble.com/file/n4654628/Test2.xml>

If you look at the attached file, you can see for example-

<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>


What I want to do- is create a template-

<ProjectConfiguration Include="${configuration}|${platform}">
<Configuration>${configuration}</Configuration>
<Platform>${platform}</Platform>
</ProjectConfiguration>

That will be populate during running on one of the maven profiles, and it
will create the correct XML element, that will be later injected to the -

<ItemGroup Label="ProjectConfigurations">


[ of course- I will need several templates to support all the entries in the
vcprojx file, but the idea is the same- process the templates and inject
them to the correct place in the file]

Eyal





--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654628.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Eyal
2013-07-11 07:58:26 UTC
Permalink
However- I will need to add several of the profiles/Configrations to the main
vcprojx, so I can not create one template for it (Unless there is a way that
a newbie like me does nto know yet :-))




--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654629.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Angelo zerr
2013-07-11 08:34:01 UTC
Permalink
Hi Eyel,

Just one question. Do you need to keep original indentation, whitespace in
your generated XML file?
If you need that (and if I find time), I could to try to create a github
project which provides this feature.

Regards Angelo
Post by Eyal
However- I will need to add several of the profiles/Configrations to the main
vcprojx, so I can not create one template for it (Unless there is a way that
a newbie like me does nto know yet :-))
--
http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654629.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Eyal
2013-07-11 08:37:20 UTC
Permalink
Thanks!!! Currently I am not sure that I need it, but after I'll start
working- who knows what I'll find.



--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654631.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-07-11 14:45:17 UTC
Permalink
Post by Eyal
Hi,
I am using maven with nar plugin to compile also C++ code.
And now I was asked to create from the maven the Visual Stuido vcprojx files
(SInce the maven have all the information).
So- I am using a maven profile to compile every "Configuration" of Visual
Studio, I want to take an empty VCProjx file, and fill it with the relevant
elements of each profile.
Test2.xml
<http://freemarker.624813.n4.nabble.com/file/n4654628/Test2.xml>
If you look at the attached file, you can see for example-
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
What I want to do- is create a template-
<ProjectConfiguration Include="${configuration}|${platform}">
<Configuration>${configuration}</Configuration>
<Platform>${platform}</Platform>
</ProjectConfiguration>
That will be populate during running on one of the maven profiles, and it
will create the correct XML element, that will be later injected to the -
<ItemGroup Label="ProjectConfigurations">
[ of course- I will need several templates to support all the entries in the
vcprojx file, but the idea is the same- process the templates and inject
them to the correct place in the file]
Can't you just do something like:

<Project DefaultTargets="Build" ToolsVersion="4.0">
<ItemGroup Label="ProjectConfigurations">
<#list profiles as p>
<ProjectConfiguration Include="${p.configuration}|${p.platform}">
<Configuration>${p.configuration}</Configuration>
<Platform>${p.platform}</Platform>
</ProjectConfiguration>
</#list>
</ItemGroup>
...
</Project>

If you need to generate those pieces in multiple rounds (calling Maven
again and again), and only after that insert them, then it sounds like
you need those little templates, save their output with a known name
somewhere, and then for the insertion you do something like:

<Project DefaultTargets="Build" ToolsVersion="4.0">
<ItemGroup Label="ProjectConfigurations">
<#list profiles as p>
<#include p + "_ProjectConfiguration.ftl">
</#list>
</ItemGroup>
...
</Project>
Post by Eyal
Eyal
--
Thanks,
Daniel Dekany
Eyal
2013-07-14 04:49:23 UTC
Permalink
Hi Daniel,

The problem is- I don't know how many profiles I will have, so I need the
maven to update the file on each run

Eyal



--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654640.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-07-14 08:53:48 UTC
Permalink
Post by Eyal
Hi Daniel,
The problem is- I don't know how many profiles I will have, so I need the
maven to update the file on each run
You mean, the input of a run is the output of the previous run? Then
you could render the template that generates a snippet of XML into a
String, and then find the tag in XML, and insert that String there. Of
course the role of FreeMarker here ends when that String was done. For
inserting into the XML you could just treat it as plain text, although
to find where to insert (character position), you may want to use DOM
or SAX (assuming implementations report that positions precisely).
Post by Eyal
Eyal
--
Thanks,
Daniel Dekany
Eyal
2013-07-14 14:16:52 UTC
Permalink
What I mean is- that after every maven run- the VC file should be a valid
file.

I cna not add it FreeMarker tags.

ALso- the issue with Maven it that every run as no relevance to the
previous- so I can not actually "keep" the previous data (Unless I will
start maintaining al kind of storage files for every XML elements, and will
need a special call to group them to the one VC file, a solution that I
don't like :-))

Thanks
Eyal





--
View this message in context: http://freemarker.624813.n4.nabble.com/FreeMarker-for-newbies-tp4654620p4654642.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-07-14 20:42:41 UTC
Permalink
Post by Eyal
What I mean is- that after every maven run- the VC file should be a valid
file.
I cna not add it FreeMarker tags.
Right, that was what I was talking about too. So then, inserting the
snippet is not the concern of FreeMarker, only generating it is.
Post by Eyal
ALso- the issue with Maven it that every run as no relevance to the
previous- so I can not actually "keep" the previous data (Unless I will
start maintaining al kind of storage files for every XML elements, and will
need a special call to group them to the one VC file, a solution that I
don't like :-))
Thanks
Eyal
--
Thanks,
Daniel Dekany
Loading...