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);
}
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();
}
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;
}
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;
}
Aggregations