Hi,
On 10.03.2010 11:48, [email protected] wrote:
Werner,
Thanks for your reply.
a binding file makes sense to me.
Great.
Although I do not quite understand why it is not possible with a mapping file.
Is it not possible to mix generated descriptors and a mapping?
Well, it is, but probably not at this level. It is possible, to my
understanding, to override a complete class mapping, but in this case
you are trying something very fine-granular.
Anyway, I tried it with a binding file.
Which imho is the far better approach.
But that leads to a problem which I will try to explain by showing my
relevant configuration and code.
Can I address this a little bit later ?
my binding file:
<binding ...
<elementBinding
name="/complexType:DocumentIdentificationType/CreationDateAndTime"
<member name="CreationDateAndTime" handler="some.package.SBDDateHandler"/
</elementBinding
</binding
relevant part of my xsd:
<xs:complexType name="DocumentIdentificationType"
<xs:sequence
...
<xs:element name="CreationDateAndTime" type="xs:dateTime"/
</xs:sequence
</xs:complexType
The relevant generated code of DocumentIdentificationTypeDescriptor:
//-- _creationDateAndTime
desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl(java.util.Date.class,
"_creationDateAndTime", "CreationDateAndTime",
org.exolab.castor.xml.NodeType.Element);
desc.setImmutable(true);
handler = new some.package.SBDDateHandler();
//-- test for generalized field handler
if (handler instanceof
org.exolab.castor.mapping.GeneralizedFieldHandler)
{
//-- save reference to user-specified handler
org.exolab.castor.mapping.GeneralizedFieldHandler gfh =
(org.exolab.castor.mapping.GeneralizedFieldHandler) handler;
handler = new org.exolab.castor.xml.XMLFieldHandler() {
@Override
public java.lang.Object getValue( java.lang.Object object )
throws IllegalStateException
{
DocumentIdentificationType target =
(DocumentIdentificationType) object;
return target.getCreationDateAndTime();
}
@Override
public void setValue( java.lang.Object object,
java.lang.Object value)
throws IllegalStateException, IllegalArgumentException
{
try {
DocumentIdentificationType target =
(DocumentIdentificationType) object;
target.setCreationDateAndTime( (java.util.Date)
value);
} catch (java.lang.Exception ex) {
throw new IllegalStateException(ex.toString());
}
}
@Override
@SuppressWarnings("unused")
public java.lang.Object newInstance(java.lang.Object parent)
{
return null;
}
};
gfh.setFieldHandler(handler);
handler = gfh;
}
<<<<<<<<<<<<<<<<<<<<<
When I unmarshall an XML document which contains
...
<CreationDateAndTime2010-03-1T09:57:01+01:00</CreationDateAndTime
...
An exception is thrown before my SBDDateHandler is called.
Debugging some castor code leads to the following:
At line 872 of UnmarshalHandler.java (castor-1.3)
code: if (addObject) handler.setValue(state.object, value);
At that point the handler is a DateFieldHandler which seems to handle the
content of the element for a non primitiveorimmutable type?
It calls:
code: Date temp = Date.parseDate(value.toString()) ; (line 182 of
DateDescriptor.java)
This will throw an exception as my Date has an invalid value (day must
have 2 digits).
So the problem is that the Date field is parsed before my Date Handler gets
a chance to parse it!!!?
Now when I change the binding:
<elementBinding
name="/complexType:DocumentIdentificationType/CreationDateAndTime"
<member name="CreationDateAndTime" java-type="java.lang.String"
handler="some.package.SBDDateHandler"/
</elementBinding
It changes the generated descriptor:
The relevant generated code of DocumentIdentificationTypeDescriptor:
//-- _creationDateAndTime
desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl(java.lang.String.class,
"_creationDateAndTime", "CreationDateAndTime",
org.exolab.castor.xml.NodeType.Element);
desc.setImmutable(true);
handler = new some.package.SBDDateHandler();
//-- test for generalized field handler
if (handler instanceof
org.exolab.castor.mapping.GeneralizedFieldHandler)
{
//-- save reference to user-specified handler
org.exolab.castor.mapping.GeneralizedFieldHandler gfh =
(org.exolab.castor.mapping.GeneralizedFieldHandler) handler;
handler = new org.exolab.castor.xml.XMLFieldHandler() {
@Override
public java.lang.Object getValue( java.lang.Object object )
throws IllegalStateException
{
DocumentIdentificationType target =
(DocumentIdentificationType) object;
return target.getCreationDateAndTime();
}
@Override
public void setValue( java.lang.Object object,
java.lang.Object value)
throws IllegalStateException, IllegalArgumentException
{
try {
DocumentIdentificationType target =
(DocumentIdentificationType) object;
target.setCreationDateAndTime( (java.lang.String)
value);
} catch (java.lang.Exception ex) {
throw new IllegalStateException(ex.toString());
}
}
@Override
@SuppressWarnings("unused")
public java.lang.Object newInstance(java.lang.Object parent)
{
return null;
}
};
gfh.setFieldHandler(handler);
handler = gfh;
}
<<<<<<<<<<<<<<<<<<<<<
The problem in this case is that my DocumentIdentificationType does not
contain a member creationDateAndTime of type String but of type Date.
So how do I get a String into my SBDDateHandler and keep my
creationDateAndTime a Date in DocumentIdentificationType ??
Greetings,
Huub
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email