Search in sources :

Example 1 with XmlQNameEnum

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.

the class QNameEnumUtil method createQNameMap.

private static <Q extends Enum<Q>> Map<? extends Enum, QName> createQNameMap(Class<Q> clazz) {
    EnumMap<Q, QName> enumQNameEnumMap = new EnumMap<Q, QName>(clazz);
    XmlQNameEnum enumInfo = clazz.getAnnotation(XmlQNameEnum.class);
    if (enumInfo == null) {
        throw new IllegalArgumentException("Class " + clazz.getName() + " isn't a QName enum.");
    }
    String namespace = enumInfo.namespace();
    if ("##default".equals(namespace)) {
        Package pkg = clazz.getPackage();
        if (pkg != null) {
            XmlSchema schemaInfo = pkg.getAnnotation(XmlSchema.class);
            namespace = schemaInfo.namespace();
        }
    }
    Field[] fields = clazz.getDeclaredFields();
    for (Q e : clazz.getEnumConstants()) {
        for (Field field : fields) {
            if (field.isEnumConstant() && field.getName().equals(e.name())) {
                if (field.getAnnotation(XmlUnknownQNameEnumValue.class) != null) {
                    enumQNameEnumMap.put(e, UNKNOWN_QNAME_ENUM);
                    break;
                }
                XmlQNameEnumValue enumValueInfo = field.getAnnotation(XmlQNameEnumValue.class);
                String ns = namespace;
                String localPart = field.getName();
                if (enumValueInfo != null) {
                    if (enumValueInfo.exclude()) {
                        enumQNameEnumMap.put(e, EXCLUDED_QNAME_ENUM);
                        break;
                    } else {
                        if (!"##default".equals(enumValueInfo.namespace())) {
                            ns = enumValueInfo.namespace();
                        }
                        if (!"##default".equals(enumValueInfo.localPart())) {
                            localPart = enumValueInfo.localPart();
                        }
                    }
                }
                enumQNameEnumMap.put(e, new QName(ns, localPart));
            }
        }
    }
    return enumQNameEnumMap;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) QName(javax.xml.namespace.QName) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) Field(java.lang.reflect.Field) XmlSchema(javax.xml.bind.annotation.XmlSchema) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum) EnumMap(java.util.EnumMap)

Example 2 with XmlQNameEnum

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.

the class QNameEnumUtil method fromURI.

/**
 * Convert a URI to a QName enum. See <a href="http://docs.codehaus.org/display/ENUNCIATE/QName+Enums">QName Enums</a>.
 *
 * @param uriValue The value of the uri to convert.
 * @param clazz The enum clazz.
 * @param defaultBaseUri The default base uri, used to resolve relative URI references (null is allowed).
 * @return The matching enum, or the {@link XmlUnknownQNameEnumValue unknown enum} if unable to find an enum for the specified URI, or <code>null</code>
 * if unable to find an enum for the specified URI 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 fromURI(String uriValue, Class<Q> clazz, String defaultBaseUri) {
    if (uriValue == null) {
        return null;
    }
    if (defaultBaseUri != null) {
        uriValue = URI.create(defaultBaseUri).resolve(uriValue).toString();
    }
    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.URI) {
        throw new IllegalArgumentException("Class " + clazz.getName() + " is supposed to be converted to a QName (not URI).");
    }
    Q defaultValue = null;
    for (Map.Entry<? extends Enum, QName> qNameEntry : qNameMap.entrySet()) {
        String uri = qNameEntry.getValue().getNamespaceURI() + qNameEntry.getValue().getLocalPart();
        if (uri.equals(uriValue)) {
            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)

Example 3 with XmlQNameEnum

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.

the class QNameEnumTypeDefinition method loadEnumValues.

@Override
protected List<EnumValue> loadEnumValues() {
    String namespace = getPackage().getNamespace();
    XmlQNameEnum xmlQNameEnum = getAnnotation(XmlQNameEnum.class);
    if (xmlQNameEnum != null && !"##default".equals(xmlQNameEnum.namespace())) {
        namespace = xmlQNameEnum.namespace();
    }
    if (namespace == null) {
        namespace = "";
    }
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValues = new ArrayList<EnumValue>();
    HashSet<QName> enumValueValues = new HashSet<QName>(enumConstants.size());
    VariableElement unknownQNameConstant = null;
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        XmlUnknownQNameEnumValue unknownQNameEnumValue = enumConstant.getAnnotation(XmlUnknownQNameEnumValue.class);
        if (unknownQNameEnumValue != null) {
            if (unknownQNameConstant != null) {
                throw new EnunciateException(getQualifiedName() + ": no more than two constants can be annotated with @XmlUnknownQNameEnumValue.");
            }
            unknownQNameConstant = enumConstant;
            continue;
        }
        String ns = namespace;
        String localPart = enumConstant.getSimpleName().toString();
        XmlQNameEnumValue enumValueInfo = enumConstant.getAnnotation(XmlQNameEnumValue.class);
        if (enumValueInfo != null) {
            if (enumValueInfo.exclude()) {
                continue;
            }
            if (!"##default".equals(enumValueInfo.namespace())) {
                ns = enumValueInfo.namespace();
            }
            if (!"##default".equals(enumValueInfo.localPart())) {
                localPart = enumValueInfo.localPart();
            }
        }
        QName qname = new QName(ns, localPart);
        if (!enumValueValues.add(qname)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + qname);
        }
        enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), qname));
    }
    if (unknownQNameConstant != null) {
        // enter the unknown qname constant as a null qname.
        enumValues.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
    }
    return enumValues;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) QName(javax.xml.namespace.QName) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) VariableElement(javax.lang.model.element.VariableElement) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum)

Example 4 with XmlQNameEnum

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.

the class QNameEnumUtil method toURI.

/**
 * Convert an enum to a URI. See <a href="http://docs.codehaus.org/display/ENUNCIATE/QName+Enums">QName Enums</a>.
 *
 * @param e The enum.
 * @param defaultBaseUri The default base uri, used to resolve relative URI references (null is allowed).
 * @return The URI.
 * @throws IllegalArgumentException If <code>e</code> isn't of a valid QName enum type,
 * or if <code>e</code> is the {@link XmlUnknownQNameEnumValue unknown enum},
 * or if {@link com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue#exclude() the enum is excluded as an enum value}.
 */
public static String toURI(Enum<?> e, String defaultBaseUri) {
    if (e == null) {
        return null;
    }
    if (!e.getDeclaringClass().isEnum()) {
        throw new IllegalArgumentException(String.format("Class %s isn't a QName enum.", e.getDeclaringClass().getName()));
    }
    Class<? extends Enum<?>> clazz = e.getDeclaringClass();
    XmlQNameEnum enumInfo = clazz.getAnnotation(XmlQNameEnum.class);
    if (enumInfo.base() != XmlQNameEnum.BaseType.URI) {
        throw new IllegalArgumentException("Class " + clazz.getName() + " is supposed to be converted to a QName (not URI).");
    }
    Map<? extends Enum, QName> qNameMap = QNAME_CACHE.get(clazz);
    if (qNameMap == null) {
        qNameMap = createQNameMap((Class<? extends Enum>) clazz);
        QNAME_CACHE.put(clazz, qNameMap);
    }
    QName result = qNameMap.get(e);
    if (result == null) {
        throw new IllegalStateException("Unable to find " + e.getDeclaringClass().getName() + "." + e + " as a QName enum value.");
    } else if (UNKNOWN_QNAME_ENUM.equals(result)) {
        throw new IllegalArgumentException(e.getDeclaringClass().getName() + "." + e + " is not a QName enum value.");
    } else if (EXCLUDED_QNAME_ENUM.equals(result)) {
        throw new IllegalArgumentException(e.getDeclaringClass().getName() + "." + e + " is excluded a QName enum value.");
    } else if (result.getNamespaceURI().equals(defaultBaseUri) && isWriteRelativeUris()) {
        return result.getLocalPart();
    } else {
        return result.getNamespaceURI() + result.getLocalPart();
    }
}
Also used : XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum) QName(javax.xml.namespace.QName) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum)

Example 5 with XmlQNameEnum

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnum in project enunciate by stoicflame.

the class QNameEnumUtil method toQName.

/**
 * Convert an enum to a QName. See <a href="http://docs.codehaus.org/display/ENUNCIATE/QName+Enums">QName Enums</a>.
 *
 * @param e The enum.
 * @return The QName.
 * @throws IllegalArgumentException If <code>e</code> isn't of a valid QName enum type,
 * or if <code>e</code> is the {@link XmlUnknownQNameEnumValue unknown enum},
 * or if {@link com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue#exclude() the enum is excluded as an enum value}.
 */
public static QName toQName(Enum e) {
    if (e == null) {
        return null;
    }
    if (!e.getDeclaringClass().isEnum()) {
        throw new IllegalArgumentException(String.format("Class %s isn't a QName enum.", e.getDeclaringClass().getName()));
    }
    Class<Enum> clazz = e.getDeclaringClass();
    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).");
    }
    QName result = qNameMap.get(e);
    if (result == null) {
        throw new IllegalStateException("Unable to find " + e.getDeclaringClass().getName() + "." + e + " as a QName enum value.");
    } else if (UNKNOWN_QNAME_ENUM.equals(result)) {
        throw new IllegalArgumentException(e.getDeclaringClass().getName() + "." + e + " is not a QName enum value.");
    } else if (EXCLUDED_QNAME_ENUM.equals(result)) {
        throw new IllegalArgumentException(e.getDeclaringClass().getName() + "." + e + " is excluded a QName enum value.");
    } else {
        return result;
    }
}
Also used : XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum) QName(javax.xml.namespace.QName) XmlQNameEnum(com.webcohesion.enunciate.metadata.qname.XmlQNameEnum)

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