[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    Re: [groovy-user] MarkupBuilder question
From:       John Wilson <tug () wilson ! co ! uk>
Date:       2004-09-15 16:19:51
Message-ID: 12341107-0733-11D9-8958-000A95B9441C () wilson ! co ! uk
[Download RAW message or body]


On 15 Sep 2004, at 16:44, Babelex wrote:

> Excellent, thanks john this explains a lot of stuff in one simple 
> example.
>
> I like the way that gpath is being used like xpath would be in xslt,
> except with the power of groovy under the hood, backed up by a java
> infrastructure..
>
> I'm gonna have a good think about how I can employ this to do it
> justice in our stuff. The only real difference for us is use of
> SAXStreambuilders in and xml/html environment rather than
> SwingBuilders within swing, but the principles are very similar.
>

I think you have got the idea!

One thing that may not be obvious:

paneBuilder = new StreamingMarkupBuilder().bind {
.....
}

this produces a Writable Closure. At this point we have not read 
anything from the network nor have we generated any HTML. It's only at 
the point where we try to write the value of paneBulder to an output 
stream:

System.out << paneBuilder

or turn it into a String

paneBuilder.toString()

that the data is fetched and the HTML is created.

It's called a Streaming Builder because System.out << paneBuilder 
writes the HTML out as it is generated. It doesn't create a String in 
memory and write it out at the end. If you are building big documents 
or serving chunked content this behaviour is important.

Also every time you write paneBuilder out the closure is executed the 
data is fetched from the net and the HTML is built again. So if the 
source data changed you write out the latest version.

The rather odd usage of "${paneBuilder}".toString() instead of 
paneBuilder.toString() is a workaround for a Groovy runtime bug ;)

I'll send you the original announcement of StreamingMarkupBuilder off 
list.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic