java - How specify realization of interface in xStream? -
the xml description of entities "messages".
<message id="11600005" name="some_name"> <sourcepartitionid>11600</sourcepartitionid> <destpartitionid>11700</destpartitionid> <payloadid>1300005</payloadid> <sourceudp>1045</sourceudp> <destudp>1046</destudp> <sourceip>10.4.48.0</sourceip> <destip>10.4.49.0</destip> <sourceport id="1045" name="sp_q_1045_11600_11700_005"> <type>queuing</type> <maxmessagesize>8192</maxmessagesize> <characteristic>1</characteristic> </sourceport> <destport id="1046" name="dp_q_1045_1046_11600_11700_005"> <type>queuing</type> <maxmessagesize>8192</maxmessagesize> <characteristic>1</characteristic> </destport> </message>
in fields sourceport
, destport
described object implements interface comport
:
public interface comport { enum porttype {sampling, queuing} enum portdirection {rx,tx} public porttype getporttype(); public portdirection getportdirection(); public int getmaxmessagesize(); public int getportcharacteristic();
at interface there 2 implementations: samplingport
, queuingport
. both main difference - characteristic field. tell me how make xstream based on <type>
tag creates instance of corresponding implementation?
an important point: necessary consider when sourceport
tag - direction field tx
, , when destport
tag - direction field rx
i figured out issue myself. first, need create class-converter:
public class comportconverter implements converter { @override public void marshal(object o, hierarchicalstreamwriter out, marshallingcontext context) { comport comport = (comport)o; if (comport.getportdirection()== comport.portdirection.tx){ out.startnode("sourceport"); }else { out.startnode("destport"); } out.addattribute("id",integer.tostring(comport.getid())); out.addattribute("name", comport.getname()); out.startnode("type"); out.setvalue(comport.getporttype().name()); out.endnode(); out.startnode("maxmessagesize"); out.setvalue(integer.tostring(comport.getmaxmessagesize())); out.endnode(); out.startnode("characteristic"); out.setvalue(integer.tostring(comport.getportcharacteristic())); out.endnode(); out.close(); } @override public object unmarshal(hierarchicalstreamreader in, unmarshallingcontext context) { comport result; comport.portdirection direction=null; if (in.getnodename().equals("sourceport")){ direction = comport.portdirection.tx; }else if (in.getnodename().equals("destport")){ direction = comport.portdirection.rx; } int id = integer.parseint(in.getattribute("id")); string name = in.getattribute("name"); in.movedown(); if (in.getvalue().equals("sampling")) result = new samplingport(id,name); else if(in.getvalue().equals("queuing")) result = new queuingport(id,name); else throw new illegalargumentexception("illegal port type value"); result.setportdirection(direction); in.moveup(); in.movedown(); result.setmaxmessagesize(integer.parseint(in.getvalue())); in.moveup(); in.movedown(); result.setportcharacteristic(integer.parseint(in.getvalue())); in.moveup(); return result; } @override public boolean canconvert(class type) { return comport.class.isassignablefrom(type); } }
then, need register converter
public static messagesstorage unmarshallingmessages(file file){ xstream xstream = new xstream(); xstream.processannotations(new class[]{messagesstorage.class,message.class}); xstream.registerconverter(new comportconverter()); return (messagesstorage) xstream.fromxml(file); }
Comments
Post a Comment