Terry Brick wrote:
Hello,
This request is carried over from the users list regarding introducing XML from a
variable/component into the JXT output stream.
For example, let's say I have Java bean with a String property that holds XML text.  
If, in my
template, I do something like...
${MyBean.XML}
... Then all of the XML characters are escaped.
For example,
<para>
..becomes..
&lt;para&gt;

I would think that for most Cocoon users, this is undesirable behavior most of the time.
The current workaround is to have the bean property return a DOM Document instead of a String. However, in many cases, other than to accomodate this workaround, I have no other need to create a
DOM document within my Java component.... so it seems like there could be some unecessary overhead
associated with doing that. I guess that depends on how Cocoon handles the doc... maybe it's a
wash.
Anyway, an escapeXml="true/false" attribute on the JX output tags (similar to JSTL) would
certainly be handy IMO.


Thanks!
Solution:

function stringToSAX( str, consumer, ignoreRootElement ) {
        var is = new Packages.org.xml.sax.InputSource( new java.io.StringReader( str ) 
);
        var ignore = ( ignoreRootElement == "true" );
        var parser = null;
        var includeConsumer = new org.apache.cocoon.xml.IncludeXMLConsumer( consumer, 
consumer );
        includeConsumer.setIgnoreRootElement( true );
        try {   
                parser = cocoon.getComponent( 
Packages.org.apache.excalibur.xml.sax.SAXParser.ROLE );
                parser.parse( is, includeConsumer );    
        } finally {
                if ( parser != null ) cocoon.releaseComponent( parser );
        }
}
put it into session by
cocoon.session.setAttribute( "saxer", stringToSAX  );

implement a jx:macro:
        <jx:macro name="xmlize">
                <jx:parameter name="value"/>
                <jx:parameter name="ignoreRoot" default="false"/>
                <jx:set var="ignored" value="${cocoon.session.stringToSAX( value, 
cocoon.consumer, ignoreRoot )}"/>
        </jx:macro>


use it like this:

<xmlize value="${xmlVal}" ignoreRoot="true"/>

and you're done.
        LG

Reply via email to