Castor uses the Xerces Serializer so the escaping problem would be coming from Xerces not Castor. However you can use an element with xml:space="preserve" as such:

<foo>
   <endofline xml:space="preserve">
</endofline>
</foo>

The following trick works for me:

<?xml version="1.0"?>
<mapping>

   <class name="Foo">
      <field name="value" type="string">
         <bind-xml node="text" location="endofline"/>
      </field>
      <field name="space" type="string"
          handler="com.acme.XMLSpaceHandler">
<bind-xml name="xml:space" node="attribute" location="endofline"/>
      </field>
   </class>

</mapping>

where XMLSpaceHandler is the following:

package com.acme;

import org.exolab.castor.mapping.FieldHandler;
import org.exolab.castor.mapping.ValidityException;

public class XMLSpaceHandler implements FieldHandler {

    public XMLSpaceHandler() {
        super();
    }

    /* (non-Javadoc)
* @see org.exolab.castor.mapping.FieldHandler#getValue(java.lang.Object)
     */
    public Object getValue(Object object) throws IllegalStateException {
        return "preserve";
    }

    /* (non-Javadoc)
* @see org.exolab.castor.mapping.FieldHandler#setValue(java.lang.Object, java.lang.Object)
     */
    public void setValue(Object object, Object value)
        throws IllegalStateException, IllegalArgumentException
    {
        /* empty */
    }

    /* (non-Javadoc)
* @see org.exolab.castor.mapping.FieldHandler#resetValue(java.lang.Object)
     */
    public void resetValue(Object object)
    {
        /* empty */
    }

    /* (non-Javadoc)
* @see org.exolab.castor.mapping.FieldHandler#checkValidity(java.lang.Object)
     */
    public void checkValidity(Object object)
        throws ValidityException
    {
        /* empty */
    }

    /* (non-Javadoc)
* @see org.exolab.castor.mapping.FieldHandler#newInstance(java.lang.Object)
     */
    public Object newInstance(Object parent) {
        return null;
    }

}

--Keith


Bedin, Stephane (GE Healthcare) wrote:
Hi,
I'm trying to marshall a class with a field named "endOfLine" of type String which 
typically contents "\r\n".
In the mapping file, I have:
<field name="endOfLine" type="string">
  <bind-xml name="endofline" node="attribute" />
</field>

The problem is that castor does not replace during the marshalling the "\r\n" by 
"&#xD;&#xA;"
The consequence is that if I write the XML file and read it, the value 
endOfLine is not the same.

If I read a XML file with "&#xD;&#xA;" as values, it works fine.

Is it a way to tell to escape the charater of this field during the marshalling 
?
Or is there another solution?



Stéphane BEDIN

-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------




-------------------------------------------------
If you wish to unsubscribe from this list, please send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------

Reply via email to