Re: [Simple-support] @ElementList
Brought to you by:
niallg
|
From: Jochen v. W. <Jo...@va...> - 2012-12-06 08:34:28
|
Hi Niall,
I need PLObject to be an interface.
How would I use the implementations and annotate them as normal?
I would use ElementUnion as mentioned below and annote the e.g.
PLNumber.class?
How would that look like?
import java.math.BigDecimal;
import java.math.BigInteger;
import org.simpleframework.xml.Root;
@Root
public class PLNumber implements PLObject {
public static final int INTEGER = 0;
public static final int REAL = 1;
private int type = -1;
private BigDecimal decimalValue = null;
private BigInteger integerValue = null;
public PLNumber(String value){
super();
try{
BigInteger integerValue
= new BigInteger(value);
this.integerValue =
integerValue;
this.type = INTEGER;
}catch(Exception longException){
try {
BigDecimal decimalValue = new
BigDecimal(value);
this.decimalValue = decimalValue;
this.type = REAL;
}catch(Exception doubleException) {
throw new
IllegalArgumentException("Given input " + value + " neither represents a
double or int value.");
}
}
}
public int intValue() {
return this.integerValue.intValue();
}
public long longValue() {
return this.integerValue.longValue();
}
public float floatValue() {
return this.decimalValue.floatValue();
}
public double doubleValue() {
return this.decimalValue.doubleValue();
}
public boolean isInteger(){
return this.type == INTEGER;
}
public boolean isReal(){
return this.type == REAL;
}
}
Thanks for you support
Jochen
On 05.12.2012 22:49 , Niall Gallagher - Yieldbroker wrote:
> With a converter you would not use an @ElementUnion, one Converter should be used to handle all the different types for PLObject. Why do you not just use implementations and annotate them as normal? At any rate a converter should work for PLObject.
>
> -----Original Message-----
> From: Jochen van Waasen [mailto:Jo...@va...]
> Sent: Thursday, 6 December 2012 7:22 AM
> To: Niall Gallagher; sim...@li...
> Subject: Re: [Simple-support] @ElementList
>
> Hi Niall,
>
> I still don’t understand how to solve my issue:
>
> My new PropertyList class looks like this. It now reflects the DTD as the objects are mutual exclusive.
>
> import org.simpleframework.xml.Attribute;
> import org.simpleframework.xml.Element;
> import org.simpleframework.xml.ElementUnion;
> import org.simpleframework.xml.Root;
>
> @Root(name = "plist")
> public class PropertyList{
>
> @ElementUnion({
> @Element(name = "dict", required = false, type = PLDict.class),
> @Element(name = "array", required = false, type = PLArray.class),
> @Element(name = "real", required = false, type = PLNumber.class),
> @Element(name = "integer", required = false, type = PLNumber.class),
> @Element(name = "string", required = false, type = PLString.class),
> @Element(name = "data", required = false, type = PLData.class),
> @Element(name = "date", required = false, type = PLDate.class),
> @Element(name = "true", required = false, type = PLBoolean.class),
> @Element(name = "false", required = false, type =
> PLBoolean.class)
> })
> private PLObject plObject;
>
> @Attribute(required = true)
> private String version;
>
> public PropertyList(){
>
> super();
> }
>
> public String getVersion(){
>
> return this.version;
> }
>
> public void setVersion(String version){
>
> this.version = version;
> }
>
> public PLObject getPLObject() {
>
> return plObject;
> }
>
>
>
> public void setPLObject(PLObject plObject) {
>
> this.plObject = plObject;
> }
> }
>
> How can I define Converters for my classes?
>
> This does not work
> @ElementUnion({
> @Element(name = "dict", required = false, type = PLDict.class),
> @Element(name = "array", required = false, type = PLArray.class),
> @Element(name = "real", required = false, type = PLNumber.class),
> @Element(name = "integer", required = false, type = PLNumber.class),
> @Element(name = "string", required = false, type = PLString.class),
> @Element(name = "data", required = false, type = PLData.class),
> @Element(name = "date", required = false, type = PLDate.class),
> @Element(name = "true", required = false, type = PLBoolean.class),
> @Element(name = "false", required = false, type =
> PLBoolean.class)
> })
> @Convert(PLObjectConverter.class)
> private PLObject plObject;
>
> and gives me the following error:
>
> Element annotation required for field 'plObject'
>
> Where do I have to annotate/define the converter when using ElementUnion?
>
> Thanks
> Jochen
>
> On 05.12.2012 09:31 , Niall Gallagher wrote:
>> Yes an @ElementUnion can match two different classes, PlKey and PlObject need to be objects not interfaces. If there are many implementations use @ElementMapUnion to specify them.
>>
>> --- On Wed, 5/12/12, jo...@va...<jo...@va...> wrote:
>>
>>> From: jo...@va...<jo...@va...>
>>> Subject: [Simple-support] @ElementList
>>> To: sim...@li...
>>> Received: Wednesday, 5 December, 2012, 12:21 AM Hi,
>>>
>>> I have this set of custom classes:
>>> PLBigDecimal, PLBoolean, PLData, PLDate, PLNumber, PLString:
>>> these all implement the interface PLObject PLKey (does not implement
>>> interface PLObject).
>>>
>>> @ElementMap(name = "dict",
>>> key = "key", keyType = PLKey.class,
>>> required = false, inline = false) private Map<PLKey,
>>> PLObject> plDictionary;
>>>
>>> The XML looks like this:
>>> <?xml version="1.0"
>>> encoding="UTF-8"?>
>>> <plist version="1.0">
>>> <date>2012-11-30T10:14:00Z</date>
>>> <dict>
>>>
>>> <key>key01</key><string>value01</string>
>>>
>>> <key>key02</key><string>value02</string>
>>>
>>> <key>key03</key><string>value03</string>
>>> </dict>
>>> <real>150.0</real>
>>> <integer>500</integer>
>>> <string>myPLString</string>
>>> <true/>
>>> <false/>
>>> </plist>
>>>
>>> How can I parse the<dict> structure.
>>> My current (see above) annotation gives me this error:
>>> Cannot instantiate interface PLObject for interface PLObject
>>>
>>> How can I use this Map<PLKey, PLObject> so that it can hold any
>>> combination of PLKey and my PLObject interface (PLString,
>>> PLBigDecimal, …)?
>>> Can this be done at all with simpleXML?
>>>
>>> Any help on the map topic is appreciated!
>>>
>>> The second thing:
>>> @Element(name = "true", required = false)
>>> @Convert(PLBooleanConverter.class)
>>> private PLBoolean plTrue;
>>>
>>> @Element(name = "false", required =
>>> false)
>>> @Convert(PLBooleanConverter.class)
>>> private PLBoolean plFalse;
>>>
>>> Is it possible to bind two xml elements to one object?
>>> The above works but I am looking for something like (map two xml tags
>>> to one object):
>>> @ElementUnion({
>>> @Element(name = "false", required = false),
>>> @Element(name = "true", required =
>>> false)
>>> })
>>> @Convert(PLBooleanConverter.class)
>>> private PLBoolean plBoolean;
>>>
>>> Thanks
>>> Jochen
>>>
>>> PS: Implementation for my root xml element, The ElementList is not
>>> working for the same reason (interface cannot be
>>> instantiated)
>>>
>>> @Root(name = "plist")
>>> public class PropertyList{
>>>
>>> @ElementMap(name = "dict",
>>> key =
>>> "key", keyType = PLKey.class,
>>> required = false, inline = false)
>>> private Map<PLKey, PLObject> plDictionary;
>>>
>>> @ElementList(name = "array", required =
>>> false)
>>> private List<PLObject> plArray;
>>>
>>> @Element(name = "real", required = false)
>>> @Convert(PLNumberConverter.class)
>>> private PLNumber plReal;
>>>
>>> @Element(name = "integer", required =
>>> false)
>>> @Convert(PLNumberConverter.class)
>>> private PLNumber plInteger;
>>>
>>> @Element(name = "string", required =
>>> false)
>>> @Convert(PLStringConverter.class)
>>> private PLString plString;
>>>
>>> @Element(name = "data", required = false)
>>> @Convert(PLDataConverter.class)
>>> private PLData plData;
>>>
>>> @Element(name = "date", required = false)
>>> @Convert(PLDateConverter.class)
>>> private PLDate plDate;
>>>
>>> @Element(name = "true", required = false)
>>> @Convert(PLBooleanConverter.class)
>>> private PLBoolean plTrue;
>>>
>>> @Element(name = "false", required =
>>> false)
>>> @Convert(PLBooleanConverter.class)
>>> private PLBoolean plFalse;
>>>
>>> @Attribute(required = true)
>>> private String version;
>>>
>>> public PropertyList(){
>>>
>>> super();
>>> }
>>> }
>>>
>>>
>>> -----Inline Attachment Follows-----
>>>
>>> ---------------------------------------------------------------------
>>> --------- LogMeIn Rescue: Anywhere, Anytime Remote support for IT.
>>> Free Trial
>>> Remotely access PCs and mobile devices and provide instant support
>>> Improve your efficiency, and focus on delivering more value-add
>>> services Discover what IT Professionals Know. Rescue delivers
>>> http://p.sf.net/sfu/logmein_12329d2d
>>> -----Inline Attachment Follows-----
>>>
>>> _______________________________________________
>>> Simple-support mailing list
>>> Sim...@li...
>>> https://lists.sourceforge.net/lists/listinfo/simple-support
>>>
> ------------------------------------------------------------------------------
> LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial Remotely access PCs and mobile devices and provide instant support Improve your efficiency, and focus on delivering more value-add services Discover what IT Professionals Know. Rescue delivers http://p.sf.net/sfu/logmein_12329d2d
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
|