1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class CompositeDeElement<E extends Composant> {
private static final long serialVersionUID = 1L;
private final Class<E> type;
private ArrayList<E> childs;
public CompositeDeElement(Class<E> type) {
if (type==null) {
throw new NullPointerException("null type");
}
this.type = type;
}
private E newE(SimpleXML node) {
try {
return this.type.getConstructor(SimpleXML.class)
.newInstance(node);
} catch (Exception e) {
throw new RuntimeException("Instantiation error", e);
}
}
public void fromXML(SimpleXML xml) {
List<String> nodeNames = xml.getNodes();
if(nodeNames.size() != 0) {
String name = nodeNames.get(0);
List<SimpleXML> nodes = xml.getNodes(nodeNames.get(0));
for(SimpleXML node : nodes) {
E e = newE(node);
this.add(e);
}
}
}
// ...
} |