Re: Transform POJO/XML into another POJO/XML using Freemarker ????
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Tuesday, July 1, 2008, 12:19:50 PM, cmoulliard wrote: > Hi, > > I would like to know if it makes sense to use Freemarker to transform/map a > POJO into another POJO (idem but for XML to XML) ? FreeMarker's input can be anything (POJO-s), but the output is designed to be text. So in principle you can't do what you want. But, you could write some custom directives (see TemplateTransformModel and #macro) that instead of printing to the output, build a tree of POJO-s inside a special variable (that only you know is special) of the data-model. Of course, then directives like #escape and #attempt/#recover will not do anything... nor plain static-text nor interpolation-in-static-text will (unless you use a special Writer that somehow contributes to the POJO building). Also the object-wrapping feature will become rather an annoyance than a useful feature in this case, since you don't really have a separate "presentation world" and "business logic world" here (so you will have to un-wrap in the TemplateTransformModel-s). So it's a bit tricky, but doable. I'm not sure if it's practical to do it though. If you will need only a dozen of TemplateTransformModel-s but a lot of templates (and/or #macros), then it can be practical. Then it will look like a terser form of XML describing the POJO (plus ifs, loops, etc), only it directly outputs the resulting POJO, so no need for JAXB and like. > If this is the case, How can I achieve this ? > > Reason : We have a solution able to process different messages in entry but > created according to different format (csv, fixed-length, XML, ...). The > existing solution has been designed around a commercial solution but my boss > would like to avoid to use it in the future. So, two scenario exist today to > process the messages and tranform them to fit the information into our > internal data model which is generic for all the messages > > 1) XML to XML > Transform (or encapsulate) all the incoming messages into a XML stream. Next > map/transform the XML message into an internal XML (= Data Model) stream > (this is why I have to map/transform various XML format into a XML one). Use > JAXB to map the XML towards our entities beans used by hibernate to commit > the information into the database > > 2) POJO to POJO > Transform all the incoming messages into POJO objects. Map/Transform the > POJOs into another POJOs (= entities beans used by hibernate) representing > our datamodel. Apparently Dozer is an interesting tool to map/transform > object into another object but paint full to use/debug. > > Regards, > > Charles -- Best regards, Daniel Dekany ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php