Search in sources :

Example 6 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project midpoint by Evolveum.

the class XmlTypeConverter method toXmlEnum.

public static <T> T toXmlEnum(Class<T> expectedType, String stringValue) {
    if (stringValue == null) {
        return null;
    }
    for (T enumConstant : expectedType.getEnumConstants()) {
        Field field;
        try {
            field = expectedType.getField(((Enum) enumConstant).name());
        } catch (SecurityException e) {
            throw new IllegalArgumentException("Error getting field from '" + enumConstant + "' in " + expectedType, e);
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException("Error getting field from '" + enumConstant + "' in " + expectedType, e);
        }
        XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);
        if (annotation.value().equals(stringValue)) {
            return enumConstant;
        }
    }
    throw new IllegalArgumentException("No enum value '" + stringValue + "' in " + expectedType);
}
Also used : Field(java.lang.reflect.Field) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 7 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project midpoint by Evolveum.

the class XmlTypeConverter method fromXmlEnum.

public static <T> String fromXmlEnum(T enumValue) {
    if (enumValue == null) {
        return null;
    }
    String fieldName = ((Enum) enumValue).name();
    Field field;
    try {
        field = enumValue.getClass().getField(fieldName);
    } catch (SecurityException e) {
        throw new IllegalArgumentException("Error getting field from " + enumValue, e);
    } catch (NoSuchFieldException e) {
        throw new IllegalArgumentException("Error getting field from " + enumValue, e);
    }
    XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class);
    return annotation.value();
}
Also used : Field(java.lang.reflect.Field) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 8 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project midpoint by Evolveum.

the class DomToSchemaProcessor method parseEnumAllowedValues.

private <T> Collection<? extends DisplayableValue<T>> parseEnumAllowedValues(QName typeName, ComplexTypeDefinition ctd, XSType xsType) {
    if (xsType.isSimpleType()) {
        if (xsType.asSimpleType().isRestriction()) {
            XSRestrictionSimpleType restriction = xsType.asSimpleType().asRestriction();
            List<XSFacet> enumerations = restriction.getDeclaredFacets(XSFacet.FACET_ENUMERATION);
            List<DisplayableValueImpl<T>> enumValues = new ArrayList<DisplayableValueImpl<T>>(enumerations.size());
            for (XSFacet facet : enumerations) {
                String value = facet.getValue().value;
                Element descriptionE = SchemaProcessorUtil.getAnnotationElement(facet.getAnnotation(), SCHEMA_DOCUMENTATION);
                Element appInfo = SchemaProcessorUtil.getAnnotationElement(facet.getAnnotation(), SCHEMA_APP_INFO);
                Element valueE = null;
                if (appInfo != null) {
                    NodeList list = appInfo.getElementsByTagNameNS(PrismConstants.A_LABEL.getNamespaceURI(), PrismConstants.A_LABEL.getLocalPart());
                    if (list.getLength() != 0) {
                        valueE = (Element) list.item(0);
                    }
                }
                String label = null;
                if (valueE != null) {
                    label = valueE.getTextContent();
                } else {
                    label = value;
                }
                DisplayableValueImpl<T> edv = null;
                Class compileTimeClass = prismContext.getSchemaRegistry().getCompileTimeClass(typeName);
                if (ctd != null && !ctd.isRuntimeSchema() && compileTimeClass != null) {
                    String fieldName = null;
                    for (Field field : compileTimeClass.getDeclaredFields()) {
                        XmlEnumValue xmlEnumValue = field.getAnnotation(XmlEnumValue.class);
                        if (xmlEnumValue != null && xmlEnumValue.value() != null && xmlEnumValue.value().equals(value)) {
                            fieldName = field.getName();
                        }
                    }
                    if (fieldName != null) {
                        T enumValue = (T) Enum.valueOf((Class<Enum>) compileTimeClass, fieldName);
                        edv = new DisplayableValueImpl(enumValue, label, descriptionE != null ? descriptionE.getTextContent() : null);
                    } else {
                        edv = new DisplayableValueImpl(value, label, descriptionE != null ? descriptionE.getTextContent() : null);
                    }
                } else {
                    edv = new DisplayableValueImpl(value, label, descriptionE != null ? descriptionE.getTextContent() : null);
                }
                enumValues.add(edv);
            }
            if (enumValues != null && !enumValues.isEmpty()) {
                return enumValues;
            }
        }
    }
    return null;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 9 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project enunciate by stoicflame.

the class EnumTypeDefinition method loadEnumValues.

protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
    HashSet<String> enumValues = new HashSet<String>(enumConstants.size());
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        String value = enumConstant.getSimpleName().toString();
        if (context.isHonorJaxb()) {
            XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
            if (enumValue != null) {
                value = enumValue.value();
            }
        }
        if (!enumValues.add(value)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
        }
        enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
    }
    return enumValueMap;
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue) VariableElement(javax.lang.model.element.VariableElement) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Aggregations

XmlEnumValue (javax.xml.bind.annotation.XmlEnumValue)9 Field (java.lang.reflect.Field)6 EnunciateException (com.webcohesion.enunciate.EnunciateException)3 VariableElement (javax.lang.model.element.VariableElement)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ArrayList (java.util.ArrayList)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1