Search in sources :

Example 1 with XmlValue

use of jakarta.xml.bind.annotation.XmlValue in project glassfish-hk2 by eclipse-ee4j.

the class GeneratorUtilities method getXmlNameMap.

public static NameInformation getXmlNameMap(AltClass convertMe) {
    Map<String, XmlElementData> xmlNameMap = new LinkedHashMap<String, XmlElementData>();
    Set<String> unmappedNames = new LinkedHashSet<String>();
    Map<String, String> addMethodToVariableMap = new LinkedHashMap<String, String>();
    Map<String, String> removeMethodToVariableMap = new LinkedHashMap<String, String>();
    Map<String, String> lookupMethodToVariableMap = new LinkedHashMap<String, String>();
    Set<String> referenceSet = new LinkedHashSet<String>();
    Map<String, List<XmlElementData>> aliasMap = new LinkedHashMap<String, List<XmlElementData>>();
    XmlElementData valueData = null;
    XmlElementData xmlAnyAttributeData = null;
    boolean hasAnElement = false;
    for (AltMethod originalMethod : convertMe.getMethods()) {
        String originalMethodName = originalMethod.getName();
        String setterVariable = Utilities.isSetter(originalMethod);
        if (setterVariable == null) {
            setterVariable = Utilities.isGetter(originalMethod);
            if (setterVariable == null)
                continue;
        }
        if (isSpecifiedReference(originalMethod)) {
            referenceSet.add(setterVariable);
        }
        AltAnnotation pluralOf = null;
        AltAnnotation xmlElement = originalMethod.getAnnotation(XmlElement.class.getName());
        AltAnnotation xmlElements = originalMethod.getAnnotation(XmlElements.class.getName());
        AltAnnotation xmlElementWrapper = originalMethod.getAnnotation(XmlElementWrapper.class.getName());
        AltAnnotation xmlAttribute = originalMethod.getAnnotation(XmlAttribute.class.getName());
        AltAnnotation xmlValue = originalMethod.getAnnotation(XmlValue.class.getName());
        AltAnnotation xmlAnyAttribute = originalMethod.getAnnotation(XmlAnyAttribute.class.getName());
        String xmlElementWrapperName = (xmlElementWrapper == null) ? null : xmlElementWrapper.getStringValue("name");
        if (xmlElementWrapperName != null && xmlElementWrapperName.isEmpty()) {
            xmlElementWrapperName = setterVariable;
        }
        checkOnlyOne(convertMe, originalMethod, xmlElement, xmlElements);
        checkOnlyOne(convertMe, originalMethod, xmlElement, xmlAttribute);
        checkOnlyOne(convertMe, originalMethod, xmlElements, xmlAttribute);
        checkOnlyOne(convertMe, originalMethod, xmlElement, xmlValue);
        checkOnlyOne(convertMe, originalMethod, xmlElements, xmlValue);
        checkOnlyOne(convertMe, originalMethod, xmlAttribute, xmlValue);
        checkOnlyOne(convertMe, originalMethod, xmlElement, xmlAnyAttribute);
        checkOnlyOne(convertMe, originalMethod, xmlElements, xmlAnyAttribute);
        checkOnlyOne(convertMe, originalMethod, xmlAttribute, xmlAnyAttribute);
        checkOnlyOne(convertMe, originalMethod, xmlValue, xmlAnyAttribute);
        if (xmlElements != null) {
            hasAnElement = true;
            // First add the actual method so it is known to the system
            pluralOf = originalMethod.getAnnotation(PluralOf.class.getName());
            String defaultValue = Generator.JAXB_DEFAULT_DEFAULT;
            xmlNameMap.put(setterVariable, new XmlElementData("", setterVariable, setterVariable, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, false, originalMethodName));
            String aliasName = setterVariable;
            AltAnnotation[] allXmlElements = xmlElements.getAnnotationArrayValue("value");
            List<XmlElementData> aliases = new ArrayList<XmlElementData>(allXmlElements.length);
            aliasMap.put(setterVariable, aliases);
            for (AltAnnotation allXmlElement : allXmlElements) {
                defaultValue = allXmlElement.getStringValue("defaultValue");
                String allXmlElementNamespace = allXmlElement.getStringValue("namespace");
                String allXmlElementName = allXmlElement.getStringValue("name");
                boolean allXmlElementRequired = allXmlElement.getBooleanValue("required");
                AltClass allXmlElementType = (AltClass) allXmlElement.getAnnotationValues().get("type");
                String allXmlElementTypeName = (allXmlElementType == null) ? null : allXmlElementType.getName();
                boolean allXmlElementTypeInterface = (allXmlElementType == null) ? true : allXmlElementType.isInterface();
                if (Generator.JAXB_DEFAULT_STRING.equals(allXmlElementName)) {
                    throw new IllegalArgumentException("The name field of an XmlElement inside an XmlElements must have a specified name");
                } else {
                    aliases.add(new XmlElementData(allXmlElementNamespace, allXmlElementName, aliasName, defaultValue, Format.ELEMENT, allXmlElementTypeName, allXmlElementTypeInterface, xmlElementWrapperName, allXmlElementRequired, originalMethodName));
                }
            }
        } else if (xmlElement != null) {
            hasAnElement = true;
            // Get the pluralOf from the method
            pluralOf = originalMethod.getAnnotation(PluralOf.class.getName());
            String defaultValue = xmlElement.getStringValue("defaultValue");
            String namespace = xmlElement.getStringValue("namespace");
            String name = xmlElement.getStringValue("name");
            boolean required = xmlElement.getBooleanValue("required");
            if (Generator.JAXB_DEFAULT_STRING.equals(name)) {
                xmlNameMap.put(setterVariable, new XmlElementData(namespace, setterVariable, setterVariable, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, required, originalMethodName));
            } else {
                xmlNameMap.put(setterVariable, new XmlElementData(namespace, name, name, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, required, originalMethodName));
            }
        } else if (xmlAttribute != null) {
            String namespace = xmlAttribute.getStringValue("namespace");
            String name = xmlAttribute.getStringValue("name");
            boolean required = xmlAttribute.getBooleanValue("required");
            if (Generator.JAXB_DEFAULT_STRING.equals(name)) {
                xmlNameMap.put(setterVariable, new XmlElementData(namespace, setterVariable, setterVariable, Generator.JAXB_DEFAULT_DEFAULT, Format.ATTRIBUTE, null, true, xmlElementWrapperName, required, originalMethodName));
            } else {
                xmlNameMap.put(setterVariable, new XmlElementData(namespace, name, name, Generator.JAXB_DEFAULT_DEFAULT, Format.ATTRIBUTE, null, true, xmlElementWrapperName, required, originalMethodName));
            }
        } else if (xmlValue != null) {
            if (valueData != null) {
                throw new IllegalArgumentException("There may be only one XmlValue method on " + convertMe);
            }
            valueData = new XmlElementData(XmlService.DEFAULT_NAMESPACE, XML_VALUE_LOCAL_PART, XML_VALUE_LOCAL_PART, null, Format.VALUE, null, false, xmlElementWrapperName, true, originalMethodName);
            xmlNameMap.put(setterVariable, valueData);
        } else if (xmlAnyAttribute != null) {
            if (xmlAnyAttributeData != null) {
                throw new IllegalArgumentException("There may be only one XmlAnyAttribute method on " + convertMe);
            }
            xmlAnyAttributeData = new XmlElementData(XmlService.DEFAULT_NAMESPACE, XML_ANY_ATTRIBUTE_LOCAL_PART, XML_ANY_ATTRIBUTE_LOCAL_PART, null, Format.ATTRIBUTE, null, false, xmlElementWrapperName, false, originalMethodName);
            xmlNameMap.put(setterVariable, xmlAnyAttributeData);
        } else {
            unmappedNames.add(setterVariable);
        }
        if (pluralOf == null)
            pluralOf = new AnnotationAltAnnotationImpl(new PluralOfDefault(), null);
        String unDecapitalizedVariable = originalMethod.getName().substring(3);
        addMethodToVariableMap.put(getMethodName(MethodType.ADD, unDecapitalizedVariable, pluralOf), setterVariable);
        removeMethodToVariableMap.put(getMethodName(MethodType.REMOVE, unDecapitalizedVariable, pluralOf), setterVariable);
        lookupMethodToVariableMap.put(getMethodName(MethodType.LOOKUP, unDecapitalizedVariable, pluralOf), setterVariable);
    }
    if (valueData != null && hasAnElement) {
        throw new IllegalArgumentException("A bean cannot both have XmlElements and XmlValue methods in " + convertMe);
    }
    Set<String> noXmlElementNames = new LinkedHashSet<String>();
    for (String unmappedName : unmappedNames) {
        if (!xmlNameMap.containsKey(unmappedName)) {
            noXmlElementNames.add(unmappedName);
        }
    }
    return new NameInformation(xmlNameMap, noXmlElementNames, addMethodToVariableMap, removeMethodToVariableMap, lookupMethodToVariableMap, referenceSet, aliasMap, valueData);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PluralOf(org.glassfish.hk2.xml.api.annotations.PluralOf) XmlAttribute(jakarta.xml.bind.annotation.XmlAttribute) ArrayList(java.util.ArrayList) AltClass(org.glassfish.hk2.xml.internal.alt.AltClass) LinkedHashMap(java.util.LinkedHashMap) XmlElements(jakarta.xml.bind.annotation.XmlElements) XmlAnyAttribute(jakarta.xml.bind.annotation.XmlAnyAttribute) AltMethod(org.glassfish.hk2.xml.internal.alt.AltMethod) XmlValue(jakarta.xml.bind.annotation.XmlValue) ArrayList(java.util.ArrayList) List(java.util.List) XmlElementWrapper(jakarta.xml.bind.annotation.XmlElementWrapper) AltAnnotation(org.glassfish.hk2.xml.internal.alt.AltAnnotation) AnnotationAltAnnotationImpl(org.glassfish.hk2.xml.internal.alt.clazz.AnnotationAltAnnotationImpl) XmlElement(jakarta.xml.bind.annotation.XmlElement)

Aggregations

XmlAnyAttribute (jakarta.xml.bind.annotation.XmlAnyAttribute)1 XmlAttribute (jakarta.xml.bind.annotation.XmlAttribute)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlElementWrapper (jakarta.xml.bind.annotation.XmlElementWrapper)1 XmlElements (jakarta.xml.bind.annotation.XmlElements)1 XmlValue (jakarta.xml.bind.annotation.XmlValue)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 PluralOf (org.glassfish.hk2.xml.api.annotations.PluralOf)1 AltAnnotation (org.glassfish.hk2.xml.internal.alt.AltAnnotation)1 AltClass (org.glassfish.hk2.xml.internal.alt.AltClass)1 AltMethod (org.glassfish.hk2.xml.internal.alt.AltMethod)1 AnnotationAltAnnotationImpl (org.glassfish.hk2.xml.internal.alt.clazz.AnnotationAltAnnotationImpl)1