public XmlDocument toXML(final String baseName) {
Assert.notEmpty(baseName, "baseName");
ClassFinder classFinder = new ClassFinderImpl(baseName, new AnnotationClassFilter(Entity.class));
XmlDocument document = new XmlDocument(AnnotationToXML.XML_NAMESPACE, AnnotationToXML.XML_LOCATION, AnnotationToXML.XML_ROOT);
XmlElement entityMappings = document.getRoot();
for (Class<?> clazz : classFinder.getClasses()) {
XmlElement entity = entityMappings.addElement("entity");
entity.setAttribute("class", clazz.getCanonicalName());
entity.setAttribute("access", "PROPERTY");
entity.setAttribute("metadata-complete", "true");
BeanDescriptor beanDescriptor = new BeanDescriptorBuilder(clazz).getBeanDescriptor();
Entity e = beanDescriptor.getAnnotation(Entity.class);
if (Conditions.isNotEmpty(e.name())) {
entity.setAttribute("name", e.name());
}
Table t = beanDescriptor.getAnnotation(Table.class);
if (t != null) {
XmlElement table = entity.addElement("table");
table.setAttribute("name", t.name());
if (Conditions.isNotEmpty(t.schema())) {
table.setAttribute("schema", t.schema());
}
}
XmlElement attributes = entity.addElement("attributes");
for (PropertyDescriptor propertyDescriptor : beanDescriptor.getProperties()) {
if (propertyDescriptor.isAnnotationPresent(ManyToMany.class)) {
ManyToMany mm = propertyDescriptor.getAnnotation(ManyToMany.class);
this.addManyToMany(attributes, propertyDescriptor, mm);
} else if (propertyDescriptor.isAnnotationPresent(ManyToOne.class)) {
ManyToOne mo = propertyDescriptor.getAnnotation(ManyToOne.class);
this.addManyToOne(attributes, propertyDescriptor, mo);
} else if (propertyDescriptor.isAnnotationPresent(OneToMany.class)) {
OneToMany om = propertyDescriptor.getAnnotation(OneToMany.class);
this.addOneToMany(attributes, propertyDescriptor, om);
} else if (propertyDescriptor.isAnnotationPresent(OneToOne.class)) {
OneToOne oo = propertyDescriptor.getAnnotation(OneToOne.class);
this.addOneToOne(attributes, propertyDescriptor, oo);
} else if (propertyDescriptor.isAnnotationPresent(Basic.class)) {
Basic b = propertyDescriptor.getAnnotation(Basic.class);
this.addBasic(attributes, propertyDescriptor, b);
} else if (propertyDescriptor.isAnnotationPresent(Id.class)) {
this.addId(attributes, propertyDescriptor);
} else if (propertyDescriptor.isAnnotationPresent(Column.class)) {
Column c = propertyDescriptor.getAnnotation(Column.class);
this.addBasic(attributes, propertyDescriptor, c);
} else if (propertyDescriptor.isAnnotationPresent(Transient.class)) {
this.addTransient(attributes, propertyDescriptor);
} else {
attributes.addComment("Field " + propertyDescriptor.getName() + " not correctly mapped");
}
}
}
return document;