use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.
the class QNameEnumUtil method fromQName.
/**
* Convert a QName to a QName enum. See <a href="http://docs.codehaus.org/display/ENUNCIATE/QName+Enums">QName Enums</a>.
*
* @param qname The qname to convert.
* @param clazz The enum clazz.
* @return The matching enum, or the {@link XmlUnknownQNameEnumValue unknown enum} if unable to find an enum for the specified QName, or <code>null</code>
* if unable to find an enum for the specified QName and there is no unknown enum specified.
* @throws IllegalArgumentException If <code>clazz</code> isn't a QName enum.
*/
public static <Q extends Enum<Q>> Q fromQName(final QName qname, Class<Q> clazz) {
if (qname == null) {
return null;
}
if (!clazz.isEnum()) {
throw new IllegalArgumentException(String.format("Class %s isn't a QName enum.", clazz.getName()));
}
Map<? extends Enum, QName> qNameMap = QNAME_CACHE.get(clazz);
if (qNameMap == null) {
qNameMap = createQNameMap(clazz);
QNAME_CACHE.put(clazz, qNameMap);
}
XmlQNameEnum enumInfo = clazz.getAnnotation(XmlQNameEnum.class);
if (enumInfo.base() != XmlQNameEnum.BaseType.QNAME) {
throw new IllegalArgumentException("Class " + clazz.getName() + " is supposed to be converted from a URI (not QName).");
}
Q defaultValue = null;
for (Map.Entry<? extends Enum, QName> qNameEntry : qNameMap.entrySet()) {
if (qNameEntry.getValue().equals(qname)) {
return (Q) qNameEntry.getKey();
} else if (defaultValue == null && UNKNOWN_QNAME_ENUM.equals(qNameEntry.getValue())) {
defaultValue = (Q) qNameEntry.getKey();
}
}
return defaultValue;
}
Aggregations