Search in sources :

Example 1 with XmlMixed

use of javax.xml.bind.annotation.XmlMixed in project groovity by disney.

the class ModelXmlWriter method visitObjectField.

public void visitObjectField(String name, Object value) throws Exception {
    if (inAttribute) {
        writer.write(" ");
        writer.write(name);
        writer.write("=\"");
        super.visitObjectField(name, value);
        writer.write("\"");
        return;
    }
    boolean writeTag = true;
    Object currentObject = getCurrentObject();
    MetaClass mc = GroovySystem.getMetaClassRegistry().getMetaClass(currentObject.getClass());
    MetaProperty mp = mc.hasProperty(currentObject, name);
    XmlAttribute xa = getAnnotation(mp, XmlAttribute.class);
    if (xa != null) {
        // attributes are written with open tag
        return;
    }
    String listElementName = null;
    if (value instanceof List || (value != null && value.getClass().isArray()) || (mp != null && (mp.getType().isArray() || List.class.isAssignableFrom(mp.getType())))) {
        writeTag = false;
        listElementName = name;
        Map<Class<?>, String> listTypedNames = NO_NAMES;
        XmlElementWrapper xew = getAnnotation(mp, XmlElementWrapper.class);
        if (xew != null) {
            writeTag = true;
            if (!"##default".equals(xew.name())) {
                name = xew.name();
            }
            name = getTagName(xew.namespace(), name);
        }
        XmlElement xe = getAnnotation(mp, XmlElement.class);
        if (xe != null) {
            if (!"##default".equals(xe.name())) {
                listElementName = xe.name();
            }
            listElementName = getTagName(xe.namespace(), listElementName);
        }
        if (xe == null) {
            XmlElements xes = getAnnotation(mp, XmlElements.class);
            if (xes != null) {
                listTypedNames = new HashMap<>();
                for (int i = 0; i < xes.value().length; i++) {
                    XmlElement e = xes.value()[i];
                    listTypedNames.put(e.type(), e.name());
                }
            }
        }
        XmlList xel = getAnnotation(mp, XmlList.class);
        if (xel != null) {
            writeTag = true;
            name = listElementName;
            value = transformField(mp, value);
        }
        XmlMixed xm = getAnnotation(mp, XmlMixed.class);
        if (xm != null) {
            listTypedNames = SKIP_TAG;
        }
        listElementNames.push(listElementName);
        listTypedElementNames.push(listTypedNames);
    } else {
        XmlElement xe = getAnnotation(mp, XmlElement.class);
        if (xe != null) {
            if (!"##default".equals(xe.name())) {
                name = xe.name();
            }
            name = getTagName(xe.namespace(), name);
        }
    }
    doDelimit = true;
    if (writeTag) {
        final String n = name;
        writeTag(name, value, o -> {
            try {
                super.visitObjectField(n, o);
            } catch (RuntimeException e) {
                throw e;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    } else {
        super.visitObjectField(name, value);
    }
    if (listElementName != null) {
        listElementNames.pop();
        listTypedElementNames.pop();
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlMixed(javax.xml.bind.annotation.XmlMixed) IOException(java.io.IOException) XmlList(javax.xml.bind.annotation.XmlList) XmlElements(javax.xml.bind.annotation.XmlElements) MetaClass(groovy.lang.MetaClass) XmlElement(javax.xml.bind.annotation.XmlElement) ArrayList(java.util.ArrayList) List(java.util.List) XmlList(javax.xml.bind.annotation.XmlList) MetaClass(groovy.lang.MetaClass) MetaProperty(groovy.lang.MetaProperty) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Aggregations

MetaClass (groovy.lang.MetaClass)1 MetaProperty (groovy.lang.MetaProperty)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)1 XmlElements (javax.xml.bind.annotation.XmlElements)1 XmlList (javax.xml.bind.annotation.XmlList)1 XmlMixed (javax.xml.bind.annotation.XmlMixed)1