You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Reinhard Poetz <re...@apache.org> on 2005/05/12 12:42:52 UTC

Streaming strings in JXTemplate

Can it be that difficult to stream a string as SAX events in JXTemplate, or did 
I overlook a simpler solution?

  - implement your own streamer object with a toSAX method
  - pass a streamer object to JXTemplate
  - assign this object to a variable
  - call the toSAX method and pass the content handler and the
    string that should be streamed to it

----
<jx:set var="streamer" value="${request.getAttribute('streamer')}"/>		
<p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', $cocoon/consumer)}"/></p>
----
the toSAX method of the streamer
public void toSAX(String content, ContentHandler handler) throws SAXException {
   (new StringXMLizable(content)).toSAX(new IncludeXMLConsumer(handler));
}
----

Can't we do this better?

-- 
Reinhard Pötz           Independent Consultant, Trainer & (IT)-Coach 

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------


Re: Streaming strings in JXTemplate

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Leszek Gawron wrote:
> Vadim Gritsenko wrote:
> 
>> Leszek Gawron wrote:
>>
>>> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>
>>
>>
>> xinclude has parse attribute:
>>
>>   <jx:out value="${str}" parse="xml"/>
>>
>> where parse is either "text" or "xml".
> 
> what about @strip-root attribute? Is there an equivalent?

No :-)

Vadim

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Vadim Gritsenko wrote:
> Leszek Gawron wrote:
> 
>> Leszek Gawron wrote:
>>
>>> Daniel Fagerstrom wrote:
>>>
>>>> If you want to make it even simpler we could either have another 
>>>> instruction that parses strings to SAX, or have some attribute on 
>>>> jx:out for parsing the argument.
>>>
>>>
>>>
>>> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some 
>>> users also asked about it long time ago.
>>>
>>
>> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>
> 
> 
> xinclude has parse attribute:
> 
>   <jx:out value="${str}" parse="xml"/>
> 
> where parse is either "text" or "xml".
what about @strip-root attribute? Is there an equivalent?

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Leszek Gawron wrote:
> Leszek Gawron wrote:
> 
>> Daniel Fagerstrom wrote:
>>
>>> If you want to make it even simpler we could either have another 
>>> instruction that parses strings to SAX, or have some attribute on 
>>> jx:out for parsing the argument.
>>
>>
>> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some users 
>> also asked about it long time ago.
>>
>
> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>

xinclude has parse attribute:

   <jx:out value="${str}" parse="xml"/>

where parse is either "text" or "xml".

Vadim

Re: Streaming strings in JXTemplate

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Sylvain Wallez wrote:
>
>> Ahem... back to another hot thread: would be good to update the docs 
>> to mention these two little attributes I wasn't aware of and can be 
>> soooo useful.
>
> not implemented yet :)) I will commit that ASAP.


Oooops, sorry!! I though it was already there!!!

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://apache.org/~sylvain            http://anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:
> Leszek Gawron wrote:
> 
>> Leszek Gawron wrote:
>>
>>> Daniel Fagerstrom wrote:
>>>
>>>> Reinhard Poetz wrote:
>>>>
>>>>>
>>>>> Can it be that difficult to stream a string as SAX events in 
>>>>> JXTemplate, or did I overlook a simpler solution?
>>>>>
>>>>>  - implement your own streamer object with a toSAX method
>>>>>  - pass a streamer object to JXTemplate
>>>>>  - assign this object to a variable
>>>>>  - call the toSAX method and pass the content handler and the
>>>>>    string that should be streamed to it
>>>>>
>>>>> ----
>>>>> <jx:set var="streamer" 
>>>>> value="${request.getAttribute('streamer')}"/>       
>>>>> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', 
>>>>> $cocoon/consumer)}"/></p>
>>>>> ----
>>>>> the toSAX method of the streamer
>>>>> public void toSAX(String content, ContentHandler handler) throws 
>>>>> SAXException {
>>>>>   (new StringXMLizable(content)).toSAX(new 
>>>>> IncludeXMLConsumer(handler));
>>>>> }
>>>>> ----
>>>>>
>>>>> Can't we do this better?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>  From a quick check of the code it seem like jx:out is XMLizable 
>>>> aware, so you should be able to use jx:out on your XMLizable object. 
>>>> If it doesn't work, it is a bug that we should fix.
>>>>
>>>> If you want to make it even simpler we could either have another 
>>>> instruction that parses strings to SAX, or have some attribute on 
>>>> jx:out for parsing the argument.
>>>
>>>
>>>
>>> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some 
>>> users also asked about it long time ago.
>>>
>>>
>> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>
> 
> 
> 
> Ahem... back to another hot thread: would be good to update the docs to 
> mention these two little attributes I wasn't aware of and can be soooo 
> useful.
not implemented yet :)) I will commit that ASAP.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Reinhard Poetz <re...@apache.org>.
Sylvain Wallez wrote:

>> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>
> 
> Ahem... back to another hot thread: would be good to update the docs to 
> mention these two little attributes I wasn't aware of and can be soooo 
> useful.

I think they haven't been implemented yet, at least if I interprete the code of 
the JXTemplateGenerator correctly ;-)

-- 
Reinhard Pötz           Independent Consultant, Trainer & (IT)-Coach 

{Software Engineering, Open Source, Web Applications, Apache Cocoon}

                                        web(log): http://www.poetz.cc
--------------------------------------------------------------------


Re: Streaming strings in JXTemplate

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Leszek Gawron wrote:
> Vadim Gritsenko wrote:
> 
>>> The main reason for using DOM in some cases (chosen by user at 
>>> runtime) is the ability to avoid SAXException. SaxBuffer does not 
>>> offer such functionality as it just stores sax bits and does not 
>>> check if xml is well formed.
>>
>> And neither does DOM. So can you explain your point or give an example?
> 
> I'd like to implement something like:
> <jx:out value="${str}" xmlize="true" lenient="true"/>
> 
> If @lenient="true":
> 
> 1. cache SAX events from parsed ${str}
> 2. validate SAX stream somehow for well formedness (that is why I 
> proposed DOM)
> 3. play SAX events into the main stream only if no SAXException occured 
> previously.
> 
> Right now parsing a not well formed xml fragment into view's SAX stream 
> will destroy the view completely.

So you can use SAXBuffer. Parse into it, parser will check wellformedness (see 
samples/errorhandling/error-giving-page.xml).

Vadim

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Vadim Gritsenko wrote:
>> The main reason for using DOM in some cases (chosen by user at 
>> runtime) is the ability to avoid SAXException. SaxBuffer does not 
>> offer such functionality as it just stores sax bits and does not check 
>> if xml is well formed.
> 
> 
> And neither does DOM. So can you explain your point or give an example?
I'd like to implement something like:
<jx:out value="${str}" xmlize="true" lenient="true"/>

If @lenient="true":

1. cache SAX events from parsed ${str}
2. validate SAX stream somehow for well formedness (that is why I 
proposed DOM)
3. play SAX events into the main stream only if no SAXException occured 
previously.

Right now parsing a not well formed xml fragment into view's SAX stream 
will destroy the view completely.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Leszek Gawron wrote:
> Vadim Gritsenko wrote:
> 
>> Ugo Cei wrote:
>>
>>> Il giorno 12/mag/05, alle 14:08, Leszek Gawron ha scritto:
>>>
>>>> what do we do with
>>>> <jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>
>>>>
>>>> 1. generate SAX events - if value is invalid this breaks the whole view
>>>> 2. parse to DOM, catch exceptions, use @lenient to throw or silently 
>>>> omit.
>>>
>>>
>>> Either 1 or give users a choice. Defaulting to DOM might be too 
>>> resource-intensive.
>>
>>
>> ... especially since we have compressed xml serializer *and* sax 
>> buffer as alternatives.
> 
> 
> The main reason for using DOM in some cases (chosen by user at runtime) 
> is the ability to avoid SAXException. SaxBuffer does not offer such 
> functionality as it just stores sax bits and does not check if xml is 
> well formed.

And neither does DOM. So can you explain your point or give an example?

Vadim

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Vadim Gritsenko wrote:
> Ugo Cei wrote:
> 
>> Il giorno 12/mag/05, alle 14:08, Leszek Gawron ha scritto:
>>
>>> what do we do with
>>> <jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>
>>>
>>> 1. generate SAX events - if value is invalid this breaks the whole view
>>> 2. parse to DOM, catch exceptions, use @lenient to throw or silently 
>>> omit.
>>
>>
>>
>> Either 1 or give users a choice. Defaulting to DOM might be too 
>> resource-intensive.
> 
> 
> ... especially since we have compressed xml serializer *and* sax buffer 
> as alternatives.

The main reason for using DOM in some cases (chosen by user at runtime) 
is the ability to avoid SAXException. SaxBuffer does not offer such 
functionality as it just stores sax bits and does not check if xml is 
well formed.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Ugo Cei wrote:
> Il giorno 12/mag/05, alle 14:08, Leszek Gawron ha scritto:
> 
>> what do we do with
>> <jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>
>>
>> 1. generate SAX events - if value is invalid this breaks the whole view
>> 2. parse to DOM, catch exceptions, use @lenient to throw or silently 
>> omit.
> 
> 
> Either 1 or give users a choice. Defaulting to DOM might be too 
> resource-intensive.

... especially since we have compressed xml serializer *and* sax buffer as 
alternatives.

Vadim

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Ugo Cei wrote:
> Il giorno 12/mag/05, alle 14:08, Leszek Gawron ha scritto:
> 
>> what do we do with
>> <jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>
>>
>> 1. generate SAX events - if value is invalid this breaks the whole view
>> 2. parse to DOM, catch exceptions, use @lenient to throw or silently 
>> omit.
> 
> 
> Either 1 or give users a choice. Defaulting to DOM might be too 
> resource-intensive.
What if @lenient="true" then and only then parse to dom to avoid view 
exceptions?

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Ugo Cei <ug...@apache.org>.
Il giorno 12/mag/05, alle 14:08, Leszek Gawron ha scritto:

> what do we do with
> <jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>
>
> 1. generate SAX events - if value is invalid this breaks the whole view
> 2. parse to DOM, catch exceptions, use @lenient to throw or silently 
> omit.

Either 1 or give users a choice. Defaulting to DOM might be too 
resource-intensive.

	Ugo

-- 
Ugo Cei
Tech Blog: http://agylen.com/
Source.zone: http://sourcezone.info/
Wine & Food Blog: http://www.divinocibo.it/

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Daniel Fagerstrom wrote:
> 
> 
> Agree, but we should implement them first ;)
one question only:

what do we do with
<jx:out value="<root><tag>broken</mispelledtag></root>" parse="xml"/>

1. generate SAX events - if value is invalid this breaks the whole view
2. parse to DOM, catch exceptions, use @lenient to throw or silently omit.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Sylvain Wallez wrote:

> Leszek Gawron wrote:
>
>> Leszek Gawron wrote:
>>
>>> Daniel Fagerstrom wrote:
>>>
>>>> Reinhard Poetz wrote:
>>>>
>>>>>
>>>>> Can it be that difficult to stream a string as SAX events in 
>>>>> JXTemplate, or did I overlook a simpler solution?
>>>>>
>>>>>  - implement your own streamer object with a toSAX method
>>>>>  - pass a streamer object to JXTemplate
>>>>>  - assign this object to a variable
>>>>>  - call the toSAX method and pass the content handler and the
>>>>>    string that should be streamed to it
>>>>>
>>>>> ----
>>>>> <jx:set var="streamer" 
>>>>> value="${request.getAttribute('streamer')}"/>       
>>>>> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', 
>>>>> $cocoon/consumer)}"/></p>
>>>>> ----
>>>>> the toSAX method of the streamer
>>>>> public void toSAX(String content, ContentHandler handler) throws 
>>>>> SAXException {
>>>>>   (new StringXMLizable(content)).toSAX(new 
>>>>> IncludeXMLConsumer(handler));
>>>>> }
>>>>> ----
>>>>>
>>>>> Can't we do this better?
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>  From a quick check of the code it seem like jx:out is XMLizable 
>>>> aware, so you should be able to use jx:out on your XMLizable 
>>>> object. If it doesn't work, it is a bug that we should fix.
>>>>
>>>> If you want to make it even simpler we could either have another 
>>>> instruction that parses strings to SAX, or have some attribute on 
>>>> jx:out for parsing the argument.
>>>
>>>
>>>
>>> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some 
>>> users also asked about it long time ago.
>>>
>>>
>> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>
>
>
>
> Ahem... back to another hot thread: would be good to update the docs 
> to mention these two little attributes I wasn't aware of and can be 
> soooo useful.

Agree, but we should implement them first ;)

/Daniel


Re: Streaming strings in JXTemplate

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Leszek Gawron wrote:
>
>> Daniel Fagerstrom wrote:
>>
>>> Reinhard Poetz wrote:
>>>
>>>>
>>>> Can it be that difficult to stream a string as SAX events in 
>>>> JXTemplate, or did I overlook a simpler solution?
>>>>
>>>>  - implement your own streamer object with a toSAX method
>>>>  - pass a streamer object to JXTemplate
>>>>  - assign this object to a variable
>>>>  - call the toSAX method and pass the content handler and the
>>>>    string that should be streamed to it
>>>>
>>>> ----
>>>> <jx:set var="streamer" 
>>>> value="${request.getAttribute('streamer')}"/>       
>>>> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', 
>>>> $cocoon/consumer)}"/></p>
>>>> ----
>>>> the toSAX method of the streamer
>>>> public void toSAX(String content, ContentHandler handler) throws 
>>>> SAXException {
>>>>   (new StringXMLizable(content)).toSAX(new 
>>>> IncludeXMLConsumer(handler));
>>>> }
>>>> ----
>>>>
>>>> Can't we do this better?
>>>
>>>
>>>
>>>
>>>  From a quick check of the code it seem like jx:out is XMLizable 
>>> aware, so you should be able to use jx:out on your XMLizable object. 
>>> If it doesn't work, it is a bug that we should fix.
>>>
>>> If you want to make it even simpler we could either have another 
>>> instruction that parses strings to SAX, or have some attribute on 
>>> jx:out for parsing the argument.
>>
>>
>> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some 
>> users also asked about it long time ago.
>>
>>
> or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>


Ahem... back to another hot thread: would be good to update the docs to 
mention these two little attributes I wasn't aware of and can be soooo 
useful.

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://apache.org/~sylvain            http://anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Leszek Gawron wrote:
> Daniel Fagerstrom wrote:
> 
>> Reinhard Poetz wrote:
>>
>>>
>>> Can it be that difficult to stream a string as SAX events in 
>>> JXTemplate, or did I overlook a simpler solution?
>>>
>>>  - implement your own streamer object with a toSAX method
>>>  - pass a streamer object to JXTemplate
>>>  - assign this object to a variable
>>>  - call the toSAX method and pass the content handler and the
>>>    string that should be streamed to it
>>>
>>> ----
>>> <jx:set var="streamer" 
>>> value="${request.getAttribute('streamer')}"/>       
>>> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', 
>>> $cocoon/consumer)}"/></p>
>>> ----
>>> the toSAX method of the streamer
>>> public void toSAX(String content, ContentHandler handler) throws 
>>> SAXException {
>>>   (new StringXMLizable(content)).toSAX(new IncludeXMLConsumer(handler));
>>> }
>>> ----
>>>
>>> Can't we do this better?
>>
>>
>>
>>  From a quick check of the code it seem like jx:out is XMLizable 
>> aware, so you should be able to use jx:out on your XMLizable object. 
>> If it doesn't work, it is a bug that we should fix.
>>
>> If you want to make it even simpler we could either have another 
>> instruction that parses strings to SAX, or have some attribute on 
>> jx:out for parsing the argument.
> 
> <jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some users 
> also asked about it long time ago.
> 
> 
or even better: <jx:out value="${str}" xmlize="true" strip-root="true"/>

-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Daniel Fagerstrom wrote:
> Reinhard Poetz wrote:
> 
>>
>> Can it be that difficult to stream a string as SAX events in 
>> JXTemplate, or did I overlook a simpler solution?
>>
>>  - implement your own streamer object with a toSAX method
>>  - pass a streamer object to JXTemplate
>>  - assign this object to a variable
>>  - call the toSAX method and pass the content handler and the
>>    string that should be streamed to it
>>
>> ----
>> <jx:set var="streamer" 
>> value="${request.getAttribute('streamer')}"/>       
>> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', 
>> $cocoon/consumer)}"/></p>
>> ----
>> the toSAX method of the streamer
>> public void toSAX(String content, ContentHandler handler) throws 
>> SAXException {
>>   (new StringXMLizable(content)).toSAX(new IncludeXMLConsumer(handler));
>> }
>> ----
>>
>> Can't we do this better?
> 
> 
>  From a quick check of the code it seem like jx:out is XMLizable aware, 
> so you should be able to use jx:out on your XMLizable object. If it 
> doesn't work, it is a bug that we should fix.
> 
> If you want to make it even simpler we could either have another 
> instruction that parses strings to SAX, or have some attribute on jx:out 
> for parsing the argument.
<jx:out value="${str}" xmlize="true"/> ? Almost one liner.. some users 
also asked about it long time ago.


-- 
Leszek Gawron                                      lgawron@mobilebox.pl
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

Re: Streaming strings in JXTemplate

Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Reinhard Poetz wrote:
> 
> Can it be that difficult to stream a string as SAX events in JXTemplate, 
> or did I overlook a simpler solution?
> 
>  - implement your own streamer object with a toSAX method
>  - pass a streamer object to JXTemplate
>  - assign this object to a variable
>  - call the toSAX method and pass the content handler and the
>    string that should be streamed to it
> 
> ----
> <jx:set var="streamer" value="${request.getAttribute('streamer')}"/>       
> <p>#{toSAX($streamer, '&lt;b&gt;blah&lt;/b&gt;', $cocoon/consumer)}"/></p>
> ----
> the toSAX method of the streamer
> public void toSAX(String content, ContentHandler handler) throws 
> SAXException {
>   (new StringXMLizable(content)).toSAX(new IncludeXMLConsumer(handler));
> }
> ----
> 
> Can't we do this better?

 From a quick check of the code it seem like jx:out is XMLizable aware, 
so you should be able to use jx:out on your XMLizable object. If it 
doesn't work, it is a bug that we should fix.

If you want to make it even simpler we could either have another 
instruction that parses strings to SAX, or have some attribute on jx:out 
for parsing the argument.

/Daniel