Search in sources :

Example 1 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 {
    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 2 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project candlepin by candlepin.

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);
        if (!"##default".equals(wrapper.name()) && !wrapper.name().isEmpty()) {
            xml.setName(wrapper.name());
        }
    }
    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 3 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(Method[] methods) {
    List<SchemaElement> elements = new ArrayList<>();
    for (Method method : methods) {
        Class<?> elementType = method.getReturnType();
        if (elementType == null && method.getParameterTypes().length == 1) {
            // 没有返回类型时,取一个参数的设置类型
            elementType = method.getParameterTypes()[0];
        }
        String elementName = null;
        String wrapperName = null;
        XmlElementWrapper xmlWrapper = method.getAnnotation(XmlElementWrapper.class);
        if (xmlWrapper != null) {
            // 首先判断是否为数组元素
            wrapperName = xmlWrapper.name();
        }
        XmlElement xmlElement = method.getAnnotation(XmlElement.class);
        if (xmlElement != null) {
            if (elementName == null) {
                elementName = xmlElement.name();
            }
            if (xmlElement.type() != null && !xmlElement.type().getName().startsWith(XmlElement.class.getName())) {
                elementType = xmlElement.type();
            }
        }
        if (elementName == null) {
            continue;
        }
        if (elementType == null) {
            continue;
        }
        elements.add(new SchemaElement(elementName, wrapperName, elementType));
    }
    return elements;
}
Also used : ArrayList(java.util.ArrayList) XmlElement(javax.xml.bind.annotation.XmlElement) Method(java.lang.reflect.Method) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Example 4 with XmlElementWrapper

use of javax.xml.bind.annotation.XmlElementWrapper in project killbill by killbill.

the class CatalogSafetyInitializer method initializeNonRequiredFields.

// For each type of catalog object we keep the 'Field' associated to non required attribute fields
private static LinkedList<Field> initializeNonRequiredFields(final Class<?> aClass) {
    final LinkedList<Field> result = new LinkedList();
    final Field[] fields = aClass.getDeclaredFields();
    for (final Field f : fields) {
        if (f.getType().isArray()) {
            final XmlElementWrapper xmlElementWrapper = f.getAnnotation(XmlElementWrapper.class);
            if (xmlElementWrapper != null) {
                if (!xmlElementWrapper.required()) {
                    result.add(f);
                }
            } else {
                final XmlElement xmlElement = f.getAnnotation(XmlElement.class);
                if (xmlElement != null && !xmlElement.required()) {
                    result.add(f);
                }
            }
        } else if (!f.getType().isPrimitive()) {
            if (f.getType().isEnum()) {
                if (FixedType.class.equals(f.getType())) {
                    result.add(f);
                } else if (BlockType.class.equals(f.getType())) {
                    result.add(f);
                } else if (TierBlockPolicy.class.equals(f.getType())) {
                    result.add(f);
                }
            } else if (Integer.class.equals(f.getType())) {
                result.add(f);
            } else if (Double.class.equals(f.getType())) {
                result.add(f);
            }
        }
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) XmlElement(javax.xml.bind.annotation.XmlElement) LinkedList(java.util.LinkedList) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper) FixedType(org.killbill.billing.catalog.api.FixedType) TierBlockPolicy(org.killbill.billing.catalog.api.TierBlockPolicy)

Example 5 with XmlElementWrapper

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

the class NameInfo method setWrapperInfo.

private void setWrapperInfo(AnnotationIntrospector ai, AnnotatedMember prop) {
    XmlElementWrapper wrapper = prop.getAnnotation(XmlElementWrapper.class);
    if (wrapper == null) {
        return;
    }
    wrapperName = new QName(wrapper.namespace(), wrapper.name());
}
Also used : QName(javax.xml.namespace.QName) XmlElementWrapper(javax.xml.bind.annotation.XmlElementWrapper)

Aggregations

XmlElementWrapper (javax.xml.bind.annotation.XmlElementWrapper)14 XmlElement (javax.xml.bind.annotation.XmlElement)12 Annotation (java.lang.annotation.Annotation)4 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)4 XmlList (javax.xml.bind.annotation.XmlList)4 ArrayList (java.util.ArrayList)3 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 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 Entry (java.util.Map.Entry)1