FreeMarker for newbies

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

FreeMarker for newbies

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?
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Daniel Dekany
Tuesday, July 9, 2013, 4:00:06 PM, Eyal wrote:

> 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


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

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
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

angelozerr
Hi Eyal,

If I understand your need : 


2013/7/10 Eyal <[hidden email]>
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.
 

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). 

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 




--
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.

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Eyal
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
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

angelozerr



2013/7/10 Eyal <[hidden email]>
Hi Angelo,

Thanks for the algorithm!!
You are welcome. 

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

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.

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Eyal
Thanks, will try to implement it :-)

Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Daniel Dekany
In reply to this post by Eyal
Wednesday, July 10, 2013, 12:47:23 PM, Eyal wrote:

> 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


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

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

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

Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

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 :-))
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

angelozerr
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


2013/7/11 Eyal <[hidden email]>
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.

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Eyal
Thanks!!! Currently I am not sure that I need it, but after I'll start working- who knows what I'll find.
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Daniel Dekany
In reply to this post by Eyal
Thursday, July 11, 2013, 9:56:12 AM, Eyal wrote:

> 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>

> Eyal

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

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

Eyal
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Daniel Dekany
Sunday, July 14, 2013, 6:49:23 AM, Eyal wrote:

> 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).

> Eyal

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

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.

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

Reply | Threaded
Open this post in threaded view
|

Re: FreeMarker for newbies

Daniel Dekany
Sunday, July 14, 2013, 4:16:52 PM, Eyal wrote:

> 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.

> 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


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user