WISHLIST: Empty Element existence as an property setter for boolean fields
--------------------------------------------------------------------------
Key: JIBX-72
URL: http://jira.codehaus.org/browse/JIBX-72
Project: JiBX
Type: Wish
Components: core
Versions: CVS
Reporter: Chris Chen
As discussed in the mailing list a while back (I have attached the email
below), it would be nice to allow a way to use the presence of an empty element
to indicate boolean value.
For instance, the existence of <required/> will automatically set an
arbitrarily designated boolean field to be set to true. During marshalling, if
the boolean field is false, then such an element will not be marshalled.
>From our previous discussion, Dennis indicated that it might be a bit
>complicated to add support for such behavior. I am thinking of something that
>might be easy to implement. Perhaps by adding another option to the style
>attribute will help. For instance:
<value name="required" field="required" style="booleanElement"/>
With the indicated style, jibx will know that during unmarshalling, the
existence of the element will automatically set the field to true. With this
style, the <value> is implicitly declared optional (usage="optional") by
default.
During marshalling, if false, then this element is not written out. If true,
then the element is written out as an empty element.
I believe this might help to ease the implementation of such a feature without
too much code changes.
=================================================================================
You might be able to do part of what you want by playing games with
deserializers, if you"re only unmarshalling XML. Try using a custom
deserializer that always returns "true", like:
public static boolean presenceDeserializer(String value) {
return true;
}
and set up the binding to use this deserializer method for the
<required/> element, binding the element to feature1Required. If the
<required/> element is present the deserializer will get called; if the
element isn"t present the boolean will just have the default "false"
value. You could also do something similar for the <feature1> element -
say that it contains a <value type="text" .../> which you bind to the
feature1Supported flag, using the same presenceDeserializer.
This gets more complicated if you need to marshal as well as unmarshal.
In theory you should be able to do this with test-methods for the
values, but it might get messy. This would also mean more changes to
your code, while for unmarshalling you can do essentially everything in
the binding definition.
I suppose for 2.0 I could generalize the test-method to also support a
test-field, or just define some way of associating an element directly
with a boolean. That gets ugly, though, in that you want it both for
simple elements (corresponding to <value> elements in the binding
definition) and those with substructure (corresponding to <structure>
elements). I think this has come up a few times, though, so some easier
way of handling the situation would be nice.
- Dennis
Mocky Habeeb wrote:
>I don"t believe there is a better way to do that than with your own
>marshaller since you"re not directly mapping a value in the xml to your
>java. I think the custom marshaller is the way to go, it seems like you
>want to extend what JiBX can do.
>
>Mocky
>
>
>
>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] On Behalf Of Chris Chen
>Sent: Saturday, April 16, 2005 3:28 AM
>To: [EMAIL PROTECTED]
>Subject: [jibx-users] Binding File question
>
>
>Hi,
>
>I am wondering if anyone can tell me if it"s possible to map the
>following XML to a class:
>
>The XML is something like this:
>
><features>
> <feature1>
> <required/>
> </feature1>
></features>
>
>The class is something like this:
>
>class Features {
> boolean feature1Supported;
> boolean feature1Required;
>
> //additional set/get methods
>}
>
>Is it possible with JiBX to define the binding file such that I can set
>a boolean value when a certain element exists (ie. <required/> in this
>case)? If so, how do I go about handling subelements and mapping them
>to the Features class as shown above? I"ve been pondering this for a
>while. The only way I can currently deal with this is through custom
>marshallers and unmarshallers.
>
>I am wondering if there is a better way other than writing custom code.
>
>Thank you for your time,
>Chris
>
>
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
jibx-devs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-devs