Search in sources :

Example 6 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project cxf by apache.

the class WrapperClassGenerator method addJAXBAnnotations.

private boolean addJAXBAnnotations(ASMHelper.FieldVisitor fv, List<Annotation> jaxbAnnos, String name) {
    ASMHelper.AnnotationVisitor av0;
    boolean addedEl = false;
    for (Annotation ann : jaxbAnnos) {
        if (ann instanceof XmlMimeType) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true);
            av0.visit("value", ((XmlMimeType) ann).value());
            av0.visitEnd();
        } else if (ann instanceof XmlJavaTypeAdapter) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
            generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
            av0.visitEnd();
        } else if (ann instanceof XmlAttachmentRef) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlList) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlElement) {
            addedEl = true;
            XmlElement el = (XmlElement) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
            if ("##default".equals(el.name())) {
                av0.visit("name", name);
            } else {
                av0.visit("name", el.name());
            }
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visit("defaultValue", el.defaultValue());
            if (el.type() != XmlElement.DEFAULT.class) {
                av0.visit("type", el.type());
            }
            av0.visitEnd();
        } else if (ann instanceof XmlElementWrapper) {
            XmlElementWrapper el = (XmlElementWrapper) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElementWrapper;", true);
            av0.visit("name", el.name());
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visitEnd();
        }
    }
    return addedEl;
}
Also used : XmlMimeType(javax.xml.bind.annotation.XmlMimeType) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ASMHelper(org.apache.cxf.common.util.ASMHelper) XmlElement(javax.xml.bind.annotation.XmlElement) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) XmlList(javax.xml.bind.annotation.XmlList)

Example 7 with XmlElementWrapper

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

the class ModelXmlWriter method writeTag.

protected void writeTag(String name, Object value, Consumer<Object> body) throws Exception {
    int s = name.lastIndexOf("/");
    int h = name.lastIndexOf("#");
    if (s != -1 || h != -1) {
        int l = s > h ? s : h;
        name = getTagName(name.substring(0, l + 1), name.substring(l + 1));
    }
    delimit();
    writer.write('<');
    writer.write(name);
    List<String> removeNamespaces = null;
    Object xmlValue = null;
    boolean foundXmlValue = false;
    if (value != null) {
        MetaProperty[] props = MetaPropertyLookup.getOrderedGettableProperties(value);
        AtomicInteger pos = new AtomicInteger(0);
        positions.push(pos);
        for (int i = 0; i < props.length; i++) {
            MetaProperty mp = props[i];
            XmlAttribute xa = getAnnotation(mp, XmlAttribute.class);
            if (xa != null) {
                String attName = xa.name();
                Object attValue = transformField(mp, mp.getProperty(value));
                if ("##default".equals(attName)) {
                    attName = mp.getName();
                }
                attName = getTagName(xa.namespace(), attName);
                inAttribute = true;
                handleField(attName, attValue);
                inAttribute = false;
            }
            XmlValue xv = getAnnotation(mp, XmlValue.class);
            if (xv != null) {
                if (foundXmlValue) {
                    log.warning("Found more than one @XmlValue on " + value.getClass().getName());
                } else {
                    foundXmlValue = true;
                    xmlValue = transformField(mp, mp.getProperty(value));
                }
            }
            XmlElement xe = getAnnotation(mp, XmlElement.class);
            if (xe != null && !"##default".equals(xe.namespace())) {
                getNamespacePrefix(xe.namespace());
            }
            XmlElementWrapper xew = getAnnotation(mp, XmlElementWrapper.class);
            if (xew != null && !"##default".equals(xew.namespace())) {
                getNamespacePrefix(xew.namespace());
            }
        }
        positions.pop();
    }
    if (declareNamespaces != null && !declareNamespaces.isEmpty()) {
        removeNamespaces = new ArrayList<>();
        for (Iterator<Entry<String, String>> iter = declareNamespaces.entrySet().iterator(); iter.hasNext(); ) {
            Entry<String, String> ns = iter.next();
            writer.write(" xmlns:");
            writer.write(ns.getValue());
            writer.write("=\"");
            escape.write(ns.getKey());
            writer.write("\"");
            iter.remove();
            removeNamespaces.add(ns.getKey());
        }
    }
    writer.write('>');
    if (foundXmlValue) {
        value = xmlValue;
    }
    if (indent >= 0) {
        indent++;
    }
    body.accept(value);
    if (indent >= 0) {
        indent--;
    }
    delimit();
    writer.write("</");
    writer.write(name);
    writer.write('>');
    doDelimit = true;
    if (removeNamespaces != null) {
        for (int i = 0; i < removeNamespaces.size(); i++) {
            namespacePrefixes.remove(removeNamespaces.get(i));
        }
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) Entry(java.util.Map.Entry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XmlValue(javax.xml.bind.annotation.XmlValue) XmlElement(javax.xml.bind.annotation.XmlElement) MetaProperty(groovy.lang.MetaProperty) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 8 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project swagger-core by swagger-api.

the class JAXBAnnotationsHelper method applyElement.

/**
     * Puts definitions for XML element.
     *
     * @param member   annotations provider
     * @param property property instance to be updated
     */
private static void applyElement(AnnotatedMember member, Property property) {
    final XmlElementWrapper wrapper = member.getAnnotation(XmlElementWrapper.class);
    if (wrapper != null) {
        final Xml xml = getXml(property);
        xml.setWrapped(true);
        // No need to set the xml name if the name provided by xmlelementwrapper annotation is ##default or equal to the property name | https://github.com/swagger-api/swagger-core/pull/2050
        if (!"##default".equals(wrapper.name()) && !wrapper.name().isEmpty() && !wrapper.name().equals(property.getName())) {
            xml.setName(wrapper.name());
        }
    } else {
        final XmlElement element = member.getAnnotation(XmlElement.class);
        if (element != null) {
            setName(element.namespace(), element.name(), property);
        }
    }
}
Also used : Xml(io.swagger.models.Xml) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 9 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project cxf by apache.

the class WrapperClassGenerator method addJAXBAnnotations.

private boolean addJAXBAnnotations(FieldVisitor fv, List<Annotation> jaxbAnnos, String name) {
    AnnotationVisitor av0;
    boolean addedEl = false;
    for (Annotation ann : jaxbAnnos) {
        if (ann instanceof XmlMimeType) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlMimeType;", true);
            av0.visit("value", ((XmlMimeType) ann).value());
            av0.visitEnd();
        } else if (ann instanceof XmlJavaTypeAdapter) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/adapters/XmlJavaTypeAdapter;", true);
            generateXmlJavaTypeAdapter(av0, (XmlJavaTypeAdapter) ann);
            av0.visitEnd();
        } else if (ann instanceof XmlAttachmentRef) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlAttachmentRef;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlList) {
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlList;", true);
            av0.visitEnd();
        } else if (ann instanceof XmlElement) {
            addedEl = true;
            XmlElement el = (XmlElement) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
            if ("##default".equals(el.name())) {
                av0.visit("name", name);
            } else {
                av0.visit("name", el.name());
            }
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visit("defaultValue", el.defaultValue());
            if (el.type() != XmlElement.DEFAULT.class) {
                av0.visit("type", el.type());
            }
            av0.visitEnd();
        } else if (ann instanceof XmlElementWrapper) {
            XmlElementWrapper el = (XmlElementWrapper) ann;
            av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElementWrapper;", true);
            av0.visit("name", el.name());
            av0.visit("nillable", el.nillable());
            av0.visit("required", el.required());
            av0.visit("namespace", el.namespace());
            av0.visitEnd();
        }
    }
    return addedEl;
}
Also used : XmlMimeType(javax.xml.bind.annotation.XmlMimeType) XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElement(javax.xml.bind.annotation.XmlElement) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) XmlList(javax.xml.bind.annotation.XmlList)

Example 10 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project ibas-framework by color-coding.

the class Serializer method getSerializedElements.

private List<SchemaElement> getSerializedElements(Field[] fields) {
    List<SchemaElement> elements = new ArrayList<>();
    for (Field field : fields) {
        Class<?> elementType = field.getType();
        String elementName = field.getName();
        String wrapperName = null;
        XmlElementWrapper xmlWrapper = field.getAnnotation(XmlElementWrapper.class);
        if (xmlWrapper != null) {
            // 首先判断是否为数组元素
            wrapperName = xmlWrapper.name();
        }
        XmlElement xmlElement = field.getAnnotation(XmlElement.class);
        if (xmlElement != null) {
            if (!xmlElement.name().equals("##default")) {
                elementName = xmlElement.name();
            }
            if (xmlElement.type() != null && !xmlElement.type().getName().startsWith(XmlElement.class.getName())) {
                elementType = xmlElement.type();
            }
        } else {
            continue;
        }
        if (elementName == null) {
            continue;
        }
        if (elementType == null) {
            continue;
        }
        elements.add(new SchemaElement(elementName, wrapperName, elementType));
    }
    return elements;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Aggregations

XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)15 XmlElement (javax.xml.bind.annotation.XmlElement)13 Annotation (java.lang.annotation.Annotation)4 ArrayList (java.util.ArrayList)4 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)4 XmlList (javax.xml.bind.annotation.XmlList)4 XmlAttachmentRef (javax.xml.bind.annotation.XmlAttachmentRef)3 XmlJavaTypeAdapter (javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter)3 MetaProperty (groovy.lang.MetaProperty)2 Xml (io.swagger.models.Xml)2 Field (java.lang.reflect.Field)2 XmlElements (javax.xml.bind.annotation.XmlElements)2 XmlMimeType (javax.xml.bind.annotation.XmlMimeType)2 XmlValue (javax.xml.bind.annotation.XmlValue)2 Closure (groovy.lang.Closure)1 MetaClass (groovy.lang.MetaClass)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1