Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

In this page you can find the example usage for org.dom4j Element addElement.

Prototype

Element addElement(String qualifiedName, String namespaceURI);

Source Link

Document

Adds a new Element node with the given qualified name and namespace URI to this branch and returns a reference to the new node.

Usage

From source file:com.haulmont.cuba.core.sys.persistence.MappingFileCreator.java

License:Apache License

private Document createDocument(Map<Class<?>, List<Attr>> mappings) {
    Document doc = DocumentHelper.createDocument();
    Element rootEl = doc.addElement("entity-mappings", XMLNS);
    Namespace xsi = new Namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    rootEl.add(xsi);/*from w w w  .  j  a va 2 s. co  m*/
    rootEl.addAttribute(new QName("schemaLocation", xsi), SCHEMA_LOCATION);
    rootEl.addAttribute("version", PERSISTENCE_VER);

    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(MappedSuperclass.class) != null) {
            Element entityEl = rootEl.addElement("mapped-superclass", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            createAttributes(entry, entityEl);
        }
    }
    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(Entity.class) != null) {
            Element entityEl = rootEl.addElement("entity", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            entityEl.addAttribute("name", entry.getKey().getAnnotation(Entity.class).name());
            createAttributes(entry, entityEl);
        }
    }
    for (Map.Entry<Class<?>, List<Attr>> entry : mappings.entrySet()) {
        if (entry.getKey().getAnnotation(Embeddable.class) != null) {
            Element entityEl = rootEl.addElement("embeddable", XMLNS);
            entityEl.addAttribute("class", entry.getKey().getName());
            createAttributes(entry, entityEl);
        }
    }

    return doc;
}

From source file:com.haulmont.cuba.core.sys.persistence.MappingFileCreator.java

License:Apache License

private void createAttributes(Map.Entry<Class<?>, List<Attr>> entry, Element entityEl) {
    Element attributesEl = entityEl.addElement("attributes", XMLNS);
    Collections.sort(entry.getValue(), new Comparator<Attr>() {
        @Override// w w  w.  j a  v  a 2  s . c o  m
        public int compare(Attr a1, Attr a2) {
            return a1.type.order - a2.type.order;
        }
    });
    for (Attr attr : entry.getValue()) {
        attr.toXml(attributesEl);
    }
}