Re: [Simple-support] Element/Attribute Default Values
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2007-06-13 19:44:20
|
Hi,
Simple 1.3.1 has been release with support for null @Attribute and @Text values. This means that within the XML document the Java null value can be represented in a textual manner within the XML document. For example take the following:
@Root
public class Example {
@Attribute(empty="NULL")
private String name;
@Text(empty="NO TEXT HERE")
private String value;
public Example() {
super();
}
public Example(String name, String value) {
this.name = name;
this.value = value;
}
}
So if Persister.write was used on various instances of the Example class the output would look like follows:
1) new Example(null, null)
<example name="NULL>NO TEXT HERE</example>
2) new Example("Some Name", null)
<example name="Some Name">NO TEXT HERE</example>
3) new Example("Some Name", "Some Description")
<example name="Some Name">Some Description</example>
Deserialization will ensure that fields with the null text value will be assigned null. I have not yet had time to add support for null @Element, @ElementArray, or @ElementList.
Niall
----- Original Message ----
From: Niall Gallagher <gal...@ya...>
To: Niall Gallagher <gal...@ya...>; Keith Byrne <ka...@gm...>; sim...@li...
Sent: Tuesday, June 12, 2007 10:36:56 AM
Subject: Re: [Simple-support] Element/Attribute Default Values
Hi,
In future I hope to add an "empty" attribute to all of the annotations such that empty values can be inserted without having to bend over backwards to cope with null values. Ill be implementing something like
@Root
public class MyClass {
@Attribute(empty="NULL")
private String example;
}
Such that if the field "example" had a null value it would be written as follows:
<myClass example="NULL"/>
The deserialization process will know to substitute the specified value with null when reconstituting the object. For the @Element, @ElementList, and @ElementArray annotations the field will be a boolean like so:
@Root
public class MyOtherClass
{
@ElementList(empty=true)
private Collection<String> list;
}
The above annotation tells the persister to serialize the empty value such that the result of having a null list would be:
<myOtherClass>
<list/>
</myOtherClass>
Where as if the annotation was set not to write an empty value the resulting XML would be:
<myOtherClass/>
This requires only very minor changes, so will probably make it in soon enough.
Niall
----- Original Message ----
From: Niall Gallagher <gal...@ya...>
To: Keith Byrne <ka...@gm...>; sim...@li...
Sent: Monday, June 11, 2007 10:38:52 PM
Subject: Re: [Simple-support] Element/Attribute Default Values
Hi,
Yes there is a way to intercept, for example:
public class MyExample {
private static final DEFAULT_VALUE = "[[NULL]]";
@Element
private String webAddress
@Commit
private void resolveMyDefault() {
if(webAddress.equals(DEFAULT_VALUE)) {
webAddress = null;
}
}
@Persist
private void fixMyObject() {
if(webAddress == null){
webAddress = DEFAULT_VALUE:
}
}
@Complete
private void backToNormal() {
resolveMyDefault();
}
}
This should do the trick, as @Persist gets called just before the object is persisted, and @Complete gets called when its been serialized to the XML document. The @Commit gets called after deserialization so the object values will be reverted to a proper working runtime value.
Hope this helps.
Niall
----- Original Message ----
From: Keith Byrne <ka...@gm...>
To: sim...@li...
Sent: Monday, June 11, 2007 10:33:16 AM
Subject: [Simple-support] Element/Attribute Default Values
I have a situation where a field isn't required, but when it's null and is serialized, the tag still needs to show up empty. Specifically, there is a String field (webAddress) that has an Attribute annotation, that went it's null
should be serialized to <webAddress/>, instead of not showing up. Is there a way to intercept the serializing of an Element/Attribute, and based upon it's type, serialize a default value?
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
Shape Yahoo! in your own image.
Join our Network Research Panel today!
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
Got a little couch potato?
Check out fun summer activities for kids.
____________________________________________________________________________________
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
http://farechase.yahoo.com/ |