Re: [Simple-support] Problem with subclass annotation
Brought to you by:
niallg
|
From: Onno H. <on...@aq...> - 2012-01-18 14:42:10
|
Hi,
I solved the problem the following way and would be happy for a comment if thats a good and performant way,
especially if it's OK to ignore all methods of the Value Interface except the getType():Class? If i'm right, default behavior is used for the rest.
Strategy strategy = new Strategy() {
@Override
public boolean write(Type arg0, Object arg1,
NodeMap<OutputNode> arg2, Map arg3) throws Exception {
return false;
}
@Override
public Value read(Type arg0, NodeMap<InputNode> arg1, Map arg2)
throws Exception {
if (arg0.getType().equals(Field.class)) {
InputNode typeNode = arg1.getNode().getAttribute("type");
String typeValue = typeNode.getValue();
// remove namespace prefix
int idx = typeValue.indexOf(':');
String typeV = typeValue.substring(idx + 1,
typeValue.length());
if (typeV.equals("FieldSubClass1")) {
return new Value() {
@Override
public void setValue(Object arg0) {
}
@Override
public boolean isReference() {
return false;
}
@Override
public Object getValue() {
return null;
}
@Override
public Class getType() {
return FieldSubClass1.class;
}
@Override
public int getLength() {
return 0;
}
};
} else if (typeV.equals("FieldSubClass2")) {
return new Value() {
@Override
public void setValue(Object arg0) {
}
@Override
public boolean isReference() {
return false;
}
@Override
public Object getValue() {
return null;
}
@Override
public Class getType() {
return FieldSubClass2.class;
}
@Override
public int getLength() {
return 0;
}
};
}
}
return null;
}
};
Serializer serializer = new Persister(strategy);
Wrapper wrapperItem = serializer.read(Wrapper.class, reader,
false);
Am 18.01.2012 um 13:21 schrieb Vincent Schlatter:
> Hello,
>
> I'm struggling with the following problem.
>
> Let's say we have the following XML:
>
> <?xml version="1.0"?>
> <wrapper xmlns="http://www.example.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <obj xsi:type="FieldSubClass1">
> <property1>some text</property1>
> <property2>some text</property2>
> </obj>
> <obj xsi:type="FieldSubClass2">
> <property1>some text</property1>
> <property2>some text</property2>
> <property3>some text</property3>
> </obj>
> <obj xsi:type="FieldSubClass3">
> <property1>some text</property1>
> <property2>some text</property2>
> </obj>
> </wrapper>
>
> The Java classes are:
>
> public class Wrapper {
> private List<Field> fieldList = null;
> }
>
> public class Field {
> private String property1 = null;
> }
>
> public class FieldSubClass1 extends Field {
> private String property2 = null;
> }
>
> public class FieldSubClass2 extends Field {
> private String property2 = null;
> private String property3 = null;
> }
>
> public class FieldSubClass3 extends Field {
> private String property2 = null;
> }
>
> Is it possible to use Simple for this problem? Simple would need to analyse the xsi:type attribute to find out which sub class to instantiate, as the sub classes cannot be distinguished by their properties.
>
> I tried a lot of different combinations of annotations but haven't been able to created the designated result.
>
> Any help would be much appreciated.
>
> Kind regards,
> Vincent
|