Search in sources :

Example 1 with XmlQNameEnumValue

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue 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 XmlQNameEnumValue

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

the class QNameEnumTypeDefinition method loadEnumValues.

@Override
protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
    HashSet<String> enumValues = new HashSet<String>(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 = this.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();
            }
        }
        String uri = ns + localPart;
        if (!enumValues.add(uri)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + uri);
        }
        enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), uri));
    }
    if (unknownQNameConstant != null) {
        // enter the unknown qname constant as a null qname.
        enumValueMap.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
    }
    return enumValueMap;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) VariableElement(javax.lang.model.element.VariableElement)

Example 3 with XmlQNameEnumValue

use of com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue 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 XmlQNameEnumValue

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

the class QNameEnumTypeDefinition method loadEnumValues.

@Override
protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
    HashSet<String> enumValues = new HashSet<String>(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 = this.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();
            }
        }
        String uri = ns + localPart;
        if (!enumValues.add(uri)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + uri);
        }
        enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), uri));
    }
    if (unknownQNameConstant != null) {
        // enter the unknown qname constant as a null qname.
        enumValueMap.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
    }
    return enumValueMap;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) HashSet(java.util.HashSet)

Aggregations

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