MarkupBuilder and comment in header

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

MarkupBuilder and comment in header

Cédric Champeau-2
Hi !

I'm trying to use the markup builder to generate a comment in the header. I'm using this :

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.mkp.comment ' header ' // not generated in the output

xml.html {
   mkp.comment 'toto' // fine
}

writer.toString()


And the comment is not generated. Looking at the source code, the reason is that a comment write is delegated to yield() which checks the state of the builder and only writes if the current state is 2 or 3. Hacking this way :

xml.state = 2
xml.mkp.comment ' header ' // fine

xml.html {
   mkp.comment 'toto' // fine
}

writer.toString()


It just works. Shouldn't yield() be allowed on the initial state too (state=0) ? Or is there any other way to add a comment at the start without using xml.out.println ?

Thanks

Reply | Threaded
Open this post in threaded view
|

Re: MarkupBuilder and comment in header

paulk_asert
We probably need to tweak things to make your use case work without
making it to easy to write totally non-conforming XML-like things.
Does StreamingMarkupBuilder do any better?

Paul.

On Wed, Sep 28, 2011 at 9:24 PM, Cédric Champeau
<[hidden email]> wrote:

> Hi !
>
> I'm trying to use the markup builder to generate a comment in the header.
> I'm using this :
>
> import groovy.xml.MarkupBuilder
>
> def writer = new StringWriter()
> def xml = new MarkupBuilder(writer)
>
> xml.mkp.comment ' header ' // not generated in the output
>
> xml.html {
>    mkp.comment 'toto' // fine
> }
>
> writer.toString()
>
> And the comment is not generated. Looking at the source code, the reason is
> that a comment write is delegated to yield() which checks the state of the
> builder and only writes if the current state is 2 or 3. Hacking this way :
>
> xml.state = 2
> xml.mkp.comment ' header ' // fine
>
> xml.html {
>    mkp.comment 'toto' // fine
> }
>
> writer.toString()
>
> It just works. Shouldn't yield() be allowed on the initial state too
> (state=0) ? Or is there any other way to add a comment at the start without
> using xml.out.println ?
>
> Thanks
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: MarkupBuilder and comment in header

Cédric Champeau-2
Le 28/09/2011 13:57, Paul King a écrit :
We probably need to tweak things to make your use case work without
making it to easy to write totally non-conforming XML-like things.
Does StreamingMarkupBuilder do any better?

Yes, it's better as this works out of the box :

import groovy.xml.StreamingMarkupBuilder
import org.custommonkey.xmlunit.*

def xml = new StreamingMarkupBuilder().bind {
  comment << 'header'
  html { }
}

println xml.toString()


<!--header--><html></html>

Paul.

On Wed, Sep 28, 2011 at 9:24 PM, Cédric Champeau
[hidden email] wrote:
Hi !

I'm trying to use the markup builder to generate a comment in the header.
I'm using this :

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)

xml.mkp.comment ' header ' // not generated in the output

xml.html {
   mkp.comment 'toto' // fine
}

writer.toString()

And the comment is not generated. Looking at the source code, the reason is
that a comment write is delegated to yield() which checks the state of the
builder and only writes if the current state is 2 or 3. Hacking this way :

xml.state = 2
xml.mkp.comment ' header ' // fine

xml.html {
   mkp.comment 'toto' // fine
}

writer.toString()

It just works. Shouldn't yield() be allowed on the initial state too
(state=0) ? Or is there any other way to add a comment at the start without
using xml.out.println ?

Thanks