Search in sources :

Example 11 with XmlElementWrapper

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

the class ReflectionServiceFactoryBean method getJaxbAnnoMap.

private Map<Class<?>, Boolean> getJaxbAnnoMap(MessagePartInfo mpi) {
    Map<Class<?>, Boolean> map = new ConcurrentHashMap<>(4, 0.75f, 1);
    Annotation[] anns = getMethodParameterAnnotations(mpi);
    if (anns != null) {
        for (Annotation anno : anns) {
            if (anno instanceof XmlList) {
                map.put(XmlList.class, true);
            }
            if (anno instanceof XmlAttachmentRef) {
                map.put(XmlAttachmentRef.class, true);
            }
            if (anno instanceof XmlJavaTypeAdapter) {
                map.put(XmlJavaTypeAdapter.class, true);
            }
            if (anno instanceof XmlElementWrapper) {
                map.put(XmlElementWrapper.class, true);
            }
        }
    }
    return map;
}
Also used : XmlAttachmentRef(javax.xml.bind.annotation.XmlAttachmentRef) XmlJavaTypeAdapter(javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) XmlList(javax.xml.bind.annotation.XmlList)

Example 12 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project zm-mailbox by Zimbra.

the class JaxbInfo method processFieldRelatedAnnotations.

private void processFieldRelatedAnnotations(Annotation[] annots, String fieldName, Type defaultGenericType) {
    WrappedElementInfo wrappedInfo = null;
    for (Annotation annot : annots) {
        if (annot instanceof XmlElementWrapper) {
            XmlElementWrapper wrapper = (XmlElementWrapper) annot;
            wrappedInfo = new WrappedElementInfo(wrapper, fieldName);
            jaxbElemNodeInfo.add(wrappedInfo);
            break;
        }
    }
    for (Annotation annot : annots) {
        if (annot instanceof XmlValue) {
            elementValue = new JaxbValueInfo((XmlValue) annot, fieldName, defaultGenericType);
        } else if (annot instanceof XmlAttribute) {
            XmlAttribute attr = (XmlAttribute) annot;
            String attrName = attr.name();
            if ((attrName == null) || DEFAULT_MARKER.equals(attrName)) {
                attrName = fieldName;
            }
            this.setXmlAttributeInfo(attr, fieldName, defaultGenericType);
            this.attributeNames.add(attrName);
        } else if (annot instanceof XmlElement) {
            XmlElement xmlElem = (XmlElement) annot;
            if (wrappedInfo == null) {
                setXmlElementInfo(xmlElem, fieldName, defaultGenericType);
            } else {
                wrappedInfo.add(xmlElem, fieldName, defaultGenericType);
            }
        } else if (annot instanceof XmlElementRef) {
            XmlElementRef xmlElemR = (XmlElementRef) annot;
            if (wrappedInfo == null) {
                setXmlElementInfo(xmlElemR, null, null);
            } else {
                wrappedInfo.add(xmlElemR, null, null);
            }
        } else if (annot instanceof XmlElements) {
            JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
            if (wrappedInfo == null) {
                jaxbElemNodeInfo.add(choiceNode);
            } else {
                wrappedInfo.add(choiceNode);
            }
            XmlElements xmlElemsAnnot = (XmlElements) annot;
            for (XmlElement xmlE : xmlElemsAnnot.value()) {
                choiceNode.add(xmlE);
            }
        } else if (annot instanceof XmlElementRefs) {
            JaxbPseudoNodeChoiceInfo choiceNode = new JaxbPseudoNodeChoiceInfo(fieldName, defaultGenericType);
            if (wrappedInfo == null) {
                jaxbElemNodeInfo.add(choiceNode);
            } else {
                wrappedInfo.add(choiceNode);
            }
            XmlElementRefs elemRefs = (XmlElementRefs) annot;
            for (XmlElementRef xmlE : elemRefs.value()) {
                choiceNode.add(xmlE);
            }
        }
    }
}
Also used : XmlElementRef(javax.xml.bind.annotation.XmlElementRef) XmlElements(javax.xml.bind.annotation.XmlElements) XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlValue(javax.xml.bind.annotation.XmlValue) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementRefs(javax.xml.bind.annotation.XmlElementRefs) Annotation(java.lang.annotation.Annotation) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 13 with XmlElementWrapper

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

the class JAXBAnnotationsHelper method apply.

/**
 * Applies annotations to property's {@link XML} definition.
 *
 * @param member   annotations provider
 * @param property property instance to be updated
 */
public static void apply(Annotated member, Annotation[] annotations, Schema property) {
    XmlElementWrapper wrapper = member.getAnnotation(XmlElementWrapper.class);
    if (wrapper == null) {
        wrapper = AnnotationsUtils.getAnnotation(XmlElementWrapper.class, annotations);
    }
    XmlAttribute attr = member.getAnnotation(XmlAttribute.class);
    if (attr == null) {
        attr = AnnotationsUtils.getAnnotation(XmlAttribute.class, annotations);
    }
    XmlElement elem = member.getAnnotation(XmlElement.class);
    if (elem == null) {
        elem = AnnotationsUtils.getAnnotation(XmlElement.class, annotations);
    }
    if (wrapper != null) {
        applyElement(wrapper, property);
    } else if (elem != null) {
        applyElement(elem, property);
    } else if (attr != null && isAttributeAllowed(property)) {
        applyAttribute(attr, property);
    }
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 14 with XmlElementWrapper

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

the class ServiceModel method getSamples.

@XmlElement(name = "sample")
@XmlElementWrapper(name = "samples")
public List<Sample> getSamples() {
    final List<Sample> samples = new ArrayList<>();
    if (serviceMetadata != null && serviceMetadata.getSamples() != null) {
        serviceMetadata.getSamples().forEach(sample -> {
            final Sample resolvedSample = new Sample();
            resolvedSample.setDescription(sample.getDescription());
            if (StringUtils.isNotBlank(sample.getValue())) {
                resolvedSample.setValue(sample.getValue());
            } else {
                final String method = StringUtils.isBlank(sample.getMethod()) ? "GET" : sample.getMethod();
                resolvedSample.setValue(String.format(Locale.ROOT, CURL_SAMPLE_TEMPLATE, method, getServiceUrl(), sample.getPath()));
            }
            samples.add(resolvedSample);
        });
    }
    return samples;
}
Also used : Sample(org.apache.knox.gateway.service.definition.Sample) ArrayList(java.util.ArrayList) XmlElement(javax.xml.bind.annotation.XmlElement) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 15 with XmlElementWrapper

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

the class ModelXmlWriter method visitObjectField.

@SuppressWarnings("rawtypes")
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;
    // aggressively resolve closures and futures so we can determine type
    if (value instanceof Closure) {
        value = ((Closure) value).call();
    } else if (value instanceof Future) {
        value = ((Future) value).get();
    }
    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(Optional.of(listElementName));
        listTypedElementNames.push(Optional.ofNullable(listTypedNames));
    } else {
        XmlElement xe = getAnnotation(mp, XmlElement.class);
        if (xe != null) {
            if (!"##default".equals(xe.name())) {
                name = xe.name();
            }
            name = getTagName(xe.namespace(), name);
        }
        listElementNames.push(Optional.empty());
        listTypedElementNames.push(Optional.empty());
    }
    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);
    }
    listElementNames.pop();
    listTypedElementNames.pop();
}
Also used : XmlAttribute(javax.xml.bind.annotation.XmlAttribute) Closure(groovy.lang.Closure) 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) Future(java.util.concurrent.Future) 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

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