Search in sources :

Example 6 with XmlQNameEnum

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;
}
Also used : QName(javax.xml.namespace.QName) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum) EnumMap(java.util.EnumMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

XmlQNameEnum (com.webcohesion.enunciate.metadata.qname.XmlQNameEnum)6 QName (javax.xml.namespace.QName)6 EnumMap (java.util.EnumMap)3 XmlQNameEnumValue (com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue)2 XmlUnknownQNameEnumValue (com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EnunciateException (com.webcohesion.enunciate.EnunciateException)1 Field (java.lang.reflect.Field)1 VariableElement (javax.lang.model.element.VariableElement)1 XmlSchema (javax.xml.bind.annotation.XmlSchema)1