Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 62 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

Using FreeMarker instead of Velocity
Yes, lets get sensible error messages 50%  50%  [ 2 ]
No, I am addicted to spend hours of leading-no-where debugging 25%  25%  [ 1 ]
I don't care 25%  25%  [ 1 ]
I have an alternative suggestion (place a comment) 0%  0%  [ 0 ]
Total votes : 4
Author Message
 Post subject:
PostPosted: Tue Jan 31, 2006 1:17 am 
Newbie

Joined: Tue Jan 31, 2006 12:24 am
Posts: 3
Location: Melbourne AU
I actually did this a few years ago mainly as a learning exercise in freemarker, but also because I was hacking the tools templates at the time and velocity was annoying me.

It was a few years ago so apologies for the ensuing vagueness.

Implementing the FreemarkerRenderer was pretty much a 30 minute exercise (due to the abstraction). I seem to recall that I had an issue with implementing the 'two-pass' method used by the velocity renderer. IIRC the two-pass was used to identify dependencies for the import block which was rendered on the second pass.

The hard part was of course the templates. The vm->ftl conversion tool actually did about 90% of work, a few regexps and maybe a bit of manual tinkering did the rest. I only ported the templates for the module I was needing to hack (might have been pojos or ejbs i cant recall) and my end 'solution' called both the velocity and freemarker renderers.

I was actually in the process of cleaning it up and sending it to you guys when I lost it all (including at least of month of production work) to a disk crash (IBM DeathStar GXP45). That reminds me I better go backup now...

I expect that tools is pretty dramatically different now so Im not sure how relevant my experience was. But I have to say hacking the freemarker templates was a joy compared to the drudgery of velocity. The power of freemarker could facilitate a total refactoring of the templates and an orders-of-magnitude increase in hackability/maintainability.

Personally I have piggybacked on tools to generate other boilerplate code specific to my project needs. If this process can enhanced/abstracted/formalised/documented etc I see that a big plus to the value of hibernate tools. (think spring/seam/tapestry)

BTW I found this thread from the team blog announcement.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 2:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
yes, tools has changed a great deal since the days of a FreemarkerRenderer was possible.

but i take you message as a sign of freemarker not being completly of the mark ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 9:13 am 
Newbie

Joined: Fri Mar 05, 2004 8:55 am
Posts: 19
JSP ?

OMELET (http://www.eclipse.org/technology/archived.php)
or MDDi (http://www.eclipse.org/mddi)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
yopiyop wrote:
JSP ?


are you crazy ? ;)

Quote:

[/quote]

looks waay too overengineered for my usage. and apparently highly dependent on eclipse tech which doesn't make it as lean and mean as i want it.

remember the goal of the hibernate tools is to utilize the hibernate metamodel to generate stuff...and this should be done as simple and easy as possible.

thanks for the pointers though...will monitor them in the future.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 9:26 am 
Newbie

Joined: Tue Jan 31, 2006 9:17 am
Posts: 4
I've used both freemarker and velocity and freemarker was a much better experience. Better messages, better template language (although remarkably similar to velocity).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 12:12 pm 
Newbie

Joined: Tue Jan 31, 2006 12:24 am
Posts: 3
Location: Melbourne AU
Quote:
yes, tools has changed a great deal since the days of a FreemarkerRenderer was possible.

So I see, just had a quick scan current tools cvs:

Code:
import org.apache.velocity.tools.generic.log.CommonsLogLogSystem;

You cant make that shit up.

Quote:
but i take you message as a sign of freemarker not being completly of the mark ;)

+1 freemarker, absolutely.

Quote:
JSP ?

I was thinking AJAX but wouldnt we have to port tools to Ruby first?
;o)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 12:50 pm 
Newbie

Joined: Fri Mar 05, 2004 8:55 am
Posts: 19
max wrote:
yopiyop wrote:
JSP ?


are you crazy ? ;)

[quote]

JSP syntax is simple.everybody can use it.
JET is a very powerful tools (JSP-like syntax):
http://www.eclipse.org/articles/Article ... rial1.html

It is for me the best alternative.I don't want spend hours on these tools (velocity, codegen... ).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 1:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
Hi,

The problem with velocity logging is that it thinks everything is a warning - which it definitly is not. Impossible to filter down to a sensible level (and some of the logging is usefull)

But again, I have done my work on making it less verbose in its logging but it does not solve my main problem namely that exceptions and spellingerrors are ignored.

FYI I started doing the freemarker conversion today (just to see if I could do it within reason and to uncover any hidden limits/annoyances within freemarker) and I must say that FreeMarker looks *very* sweet compared to velocity. It is sooo freaking simple to figure out what template is failing and why - in velocity I have to basically guess and use hrs to debug it (and each time I add in extra guards if possible into the tools code so futures users won't bump into them)....in FreeMarker it is all seem to be built in.

...and sure lets do a AJAX based tools ;) (actually semi-serious, since a ajax query console would be nice ;)

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 8:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
and if we wanted to use JSP where does one get a not-dependent on servlet etc. JSP engine ?

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Tools now have a TOOLS_FREEMARKER branch
PostPosted: Wed Feb 01, 2006 9:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
The tools now have a TOOLS_FREEMARKER branch.

It took 1,5 day to convert it from velocity to freemaker and it is now available in cvs in hibernateext/tools in the TOOLS_FREEMARKER branch.

The porting experience were a bliss compared to velocity development and I removed alot of "doublecheck-velocity" code, found bugs ignored by velocity and life were great (i'll blog about it when i get around to it), but i did find one thing that were pretty annoying with freemarker.

It apparently has to be hard to escape e.g. ${build.dir} and #{msg.${some.property}} and such strings occurs pretty often in build.xml's and jsp/jsf pages.

The best thing i've found until now is: ${'#'}{msg.${some.property}} but that actually get pretty ugly in a file with many $ or #'s.

But this is the *only* downside I have found ....the rest is just upsides.

If anyone have the courage and time then I would appreciate if you tried to make these new templates crash! The code passes all 145 unittests, but i'm sure you have some weird usecase that i did not catch ;)

REMEMBER: This is currently *only* for the hibernateext/tools part - meaning only Ant, not eclipse. (eclipse probably just work, but i haven't tested it yet)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 9:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Goodwork Max


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 12:19 am 
Newbie

Joined: Tue Jan 31, 2006 12:24 am
Posts: 3
Location: Melbourne AU
Quote:
The problem with velocity logging is that it thinks everything is a warning

Did find the verbosity of logging annoying yet helpful and necessary last time i used tools (reveng).

Discussion prompted me to dig up some of my own codegen codebase which elicited the usual (for me anyway..) cringe factor when you look at stuff you wrote more than a few years ago:

Eg. Logging from a template, assumes you inject a commons logger into the model as 'logger' (rarely ever used this, but as an example of the powerfully dangerous 'eval' built-in..):

Code:
<#macro log message level="info">${"logger.${level}"?eval("${message}")}</#macro>


Dont recall *ever* using this abomination but I must have written it!

Code:
<#macro logvars object members level="info">
  <#list members?word_list as member>
    ${"logger.${level}"?eval("${object}.${member} = ${object?eval[member]}")}
  </#list>
</#macro>

Can u detect the aroma of perl here? eval is your friend ;o)

The essential parameter list:
Code:
<#macro csv list postfix="">
<#compress>
<#list list as item>${item.name}${postfix}<#if item_has_next>, </#if></#list>
</#compress>
</#macro>



And la pièce de résistance, extracting the class name from a fqn:

Code:
<#macro shortType type>
   <#compress>
      <#assign
         lastdot = type?last_index_of('.')
         length = type?length
      >
      <#if lastdot != 0>
         ${type[(lastdot + 1) .. (length - 1)]}
      <#else>
         ${type}
      </#if>
   </#compress>
</#macro>



Never used freemarker for ant build files, but experience tells me that writing a macro will clean the escaping up for you. The macro might be as ugly as hell (see above) but your core templates (ie. what yours users see) will be cleaner.

Of course you can also apply DRY priciples and create 'components' using macros; they can call other macros, do callbacks to java code etc. The end result is your templates look a lot more like your target files. (I use freemarker to generate C/C++/Java/SQL code :o)

Will try and have a look at the branch this weekend to give you some feedback.

A.


Top
 Profile  
 
 Post subject: FTL tags break XML
PostPosted: Fri Feb 03, 2006 12:46 am 
Beginner
Beginner

Joined: Thu Aug 04, 2005 8:41 pm
Posts: 47
FreeMarker would have been much more useful if it had used tags
which are valid in XML.

Ex: instead of <#if

why don't they use

<f:if> (with f registered as namespace prefix) and so on.

Much closer to JSTL and EL. All XML tools would have still be applicable.

<% was a big flaw in JSP design. Why repeat it again?

--MG


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 2:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
andrewa wrote:
Quote:
The problem with velocity logging is that it thinks everything is a warning

Did find the verbosity of logging annoying yet helpful and necessary last time i used tools (reveng).



Yes, but the verbosity is horrible and you only needed it because velocity would not let me fail early so i could tell you exactly what was causing the problem....i guess that is why they bumped all the levels up to insane high levels so it always were visible in the velocity logs...but damn that is bad!

Quote:
Never used freemarker for ant build files, but experience tells me that writing a macro will clean the escaping up for you. The macro might be as ugly as hell (see above) but your core templates (ie. what yours users see) will be cleaner.

Of course you can also apply DRY priciples and create 'components' using macros; they can call other macros, do callbacks to java code etc. The end result is your templates look a lot more like your target files. (I use freemarker to generate C/C++/Java/SQL code :o)


yes, with freemarker we can definitly write more compact and modular templates.

[quote
Will try and have a look at the branch this weekend to give you some feedback.
[/quote]

be warned though - i haven't comed all the templates for visual "layout" the automatic conversion did not do everything beautifull. But the functionallity should be exactly the same.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: FTL tags break XML
PostPosted: Fri Feb 03, 2006 2:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8611
Location: Neuchatel, Switzerland (Danish)
mgrouch wrote:
FreeMarker would have been much more useful if it had used tags
which are valid in XML.

Ex: instead of <#if

why don't they use

<f:if> (with f registered as namespace prefix) and so on.

Much closer to JSTL and EL. All XML tools would have still be applicable.

<% was a big flaw in JSP design. Why repeat it again?

--MG


agreed. the latest freemarker has support for an alternative syntax which i might consider using. [#blah] instead of <#blah>.

The problem is (i assume) that to avoid using a xml parser for non-xml parser files they would need to force users to use the <f: syntax...

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 62 posts ]  Go to page Previous  1, 2, 3, 4, 5  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.