[Simple-support] more trouble with convert...
Brought to you by:
niallg
|
From: -=}\\*/{=- <rui...@gm...> - 2011-12-15 23:31:01
|
i'm trying to read and write an interface using a Converter that uses the
element name as the simple class name and an attribute "package" for the
package name, and then
it works fine when reading, but not when writing,...
at first i had no "type" in the annotation and was not working but thought
that by adding it would solve it... wrong me.
since the handler is the interface for other people to implement, i would
like to have this style working for class resolving (i know there is the
default "class" attribute, but i rather avoid repeating tags and
attributes), but so that the other people only have to declare annotations
and not have to write some kind of converter.
bellow is code... cheers,
rui
annotation...
@ElementList(name = "handler", type = Handler.class, required = false)
private ArrayList<Handler> handlers;
input xml... WORKS fine :)
<handler>
<Downloader package="abc.kis.handler"/>
<KISRemote package="abc.kis.simple"/>
</handler>
output xml... hoops...
<handler>
<handler class="abc.kis.handler.Downloader"/>
<handler class="abc.kis.simple.KISRemote"/>
</handler>
converter...
@Override
public Handler read(InputNode in) throws Exception {
return (Handler)Class.forName(
in.getAttribute("package").getValue().concat(".").concat(in.getName())).
newInstance();
}
@Override
public void write(OutputNode on, Handler h) throws Exception {
KeepItSimple.PERSISTER.write(h, on);
on.setName(h.getClass().getSimpleName());
on.setAttribute("package", h.getClass().getPackage().getName());
on.commit();
}
example extending class...
@Root
public class KISRemote implements Handler {
@Override
public boolean accepts(Quest quest) {
return quest.getPathInfo().startsWith(Server.class.getName(), 1);
}
@Override
public boolean handle(Quest quest, Sponse sponse) {
String path = quest.getPathInfo().substring(
1 + Server.class.getName().length() +1);
if ("stop".equals(path))
System.err.println("GET THE SERVER HERE AND THIS MESSAGE OUT");
else return false;
return true;
}
}
|