castor-data-binding / castor

http://castor-data-binding.github.io/castor/
35 stars 29 forks source link

CDATA XML Mapping #62

Open kovax opened 7 years ago

kovax commented 7 years ago

I would like to be able marshall and unmarshall the following XML:

<cristalquery name="TestQuery" version="0">
    <query language="existdb:xquery"><![CDATA[
<TRList>{
    for $prop in collection('weighbridge')/Property[@name='Type']
    where $prop = "Weighbridge"
    return local:queryTransaction(util:collection-name($prop))
}</TRList>
    ]]></query>
</cristalquery>

As you can see it contains an XQuery, and to store that unescaped I am need to use CDATA. Currently I am planning to implemenet a custom FieldHandler, but I think it would be great if castor could support CDATA with a mapping like bellow, i.e. uses node="cdata" in the last field:

<mapping>
    <class name="org.cristalise.kernel.querying.Query">
        <map-to xml="cristalquery"/>
        <field name="name" type="string" direct="false">
            <bind-xml name="name" node="attribute" />
        </field>
        <field name="version" type="integer" direct="false">
            <bind-xml name="version" node="attribute" />
        </field>
        <field name="language" type="string" direct="false">
            <bind-xml name="language" location="query" node="attribute" />
        </field>
        <field name="query" type="string" direct="false">
            <bind-xml name="query" node="cdata" />
        </field>
    </class>
</mapping>

org.w3c.dom.CDATASection is a subclass of Text, so it should not be hard to handle the ![CDATA...]] wrapper around that text.

ramanujamsridhar commented 7 years ago

I agree. is this available with latest version of castor ?

kovax commented 7 years ago

I would appreciate any feedback on this. I could contribute this functionality, but I would need some guidance.