Search in sources :

Example 21 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project cxf by apache.

the class SchemaCollectionContextProxy method getBeanInfo.

public Object getBeanInfo(Class<?> cls) {
    Class<?> origCls = cls;
    String postfix = "";
    while (cls.isArray()) {
        cls = cls.getComponentType();
        postfix = "Array";
    }
    XmlRootElement xre = cls.getAnnotation(XmlRootElement.class);
    String name = xre == null ? "##default" : xre.name();
    String namespace = xre == null ? "##default" : xre.namespace();
    if ("##default".equals(name)) {
        name = java.beans.Introspector.decapitalize(cls.getSimpleName());
    }
    if ("##default".equals(namespace) && cls.getPackage() != null) {
        XmlSchema sc = cls.getPackage().getAnnotation(XmlSchema.class);
        if (sc != null) {
            namespace = sc.namespace();
        }
    }
    if ("##default".equals(namespace) || StringUtils.isEmpty(namespace)) {
        namespace = JAXBUtils.getPackageNamespace(cls);
        if (namespace == null) {
            namespace = defaultNamespace;
        }
    }
    final QName qname = new QName(namespace, name + postfix);
    final XmlSchemaElement el = schemas.getElementByQName(qname);
    XmlSchemaType type = null;
    if (el != null) {
        type = el.getSchemaType();
    }
    if (type == null) {
        type = schemas.getTypeByQName(getTypeQName(origCls, namespace));
        if (type == null) {
            type = schemas.getTypeByQName(qname);
        }
    }
    if (type == null) {
        type = mapToSchemaType(origCls, namespace);
    /*
            if (type == null) {
                type = mapToSchemaType(cls, namespace);
            }
            */
    }
    if (el == null && type == null) {
        return null;
    }
    final QName typeName = type == null ? null : type.getQName();
    return new JAXBBeanInfo() {

        public boolean isElement() {
            return el == null ? false : true;
        }

        public Collection<QName> getTypeNames() {
            return Collections.singletonList(typeName);
        }

        public String getElementNamespaceURI(Object object) {
            return qname.getNamespaceURI();
        }

        public String getElementLocalName(Object object) {
            return qname.getLocalPart();
        }
    };
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlSchema(javax.xml.bind.annotation.XmlSchema) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType)

Example 22 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project jersey by jersey.

the class AbstractCollectionJaxbProvider method getElementName.

/**
     * Get the element name for a given Java type.
     * <p>
     * In case the element is annotated with a {@link javax.xml.bind.annotation.XmlRootElement} annotation
     * and the {@link javax.xml.bind.annotation.XmlRootElement#name() specified element name} is not default,
     * the method returns the specified element name in the annotation. Otherwise, the method returns the name of
     * the element class instead.
     * </p>
     *
     * @param elementType element Java type.
     * @return element name for a given element Java type.
     */
protected static String getElementName(Class<?> elementType) {
    String name = elementType.getName();
    XmlRootElement xre = elementType.getAnnotation(XmlRootElement.class);
    if (xre != null && !"##default".equals(xre.name())) {
        name = xre.name();
    }
    return name;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement)

Example 23 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class SpringAnnotationProcessor method processModelClass.

protected void processModelClass(final ProcessingEnvironment processingEnv, final RoundEnvironment roundEnv, final TypeElement classElement) {
    final String javaTypeName = canonicalClassName(classElement.getQualifiedName().toString());
    String packageName = javaTypeName.substring(0, javaTypeName.lastIndexOf("."));
    // skip abstract classes
    if (classElement.getModifiers().contains(Modifier.ABSTRACT)) {
        return;
    }
    final XmlRootElement rootElement = classElement.getAnnotation(XmlRootElement.class);
    if (rootElement == null) {
        return;
    }
    String aName = rootElement.name();
    if (isNullOrEmpty(aName) || "##default".equals(aName)) {
        XmlType typeElement = classElement.getAnnotation(XmlType.class);
        aName = typeElement.name();
    }
    final String name = aName;
    // lets use the xsd name as the file name
    String fileName;
    if (isNullOrEmpty(name) || "##default".equals(name)) {
        fileName = classElement.getSimpleName().toString() + ".json";
    } else {
        fileName = name + ".json";
    }
    // write json schema
    Func1<PrintWriter, Void> handler = new Func1<PrintWriter, Void>() {

        @Override
        public Void call(PrintWriter writer) {
            writeJSonSchemeDocumentation(processingEnv, writer, roundEnv, classElement, rootElement, javaTypeName, name);
            return null;
        }
    };
    processFile(processingEnv, packageName, fileName, handler);
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) XmlType(javax.xml.bind.annotation.XmlType) PrintWriter(java.io.PrintWriter)

Example 24 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class CoreEipAnnotationProcessor method processVerbs.

/**
     * Special for processing an @XmlElementRef verbs field (rest-dsl)
     */
private void processVerbs(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement originalClassType, XmlElementRef elementRef, VariableElement fieldElement, String fieldName, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    if ("verbs".equals(fieldName) && supportOutputs(originalClassType)) {
        String kind = "element";
        String name = elementRef.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, originalClassType, true);
        // gather oneOf which extends any of the output base classes
        Set<String> oneOfTypes = new TreeSet<String>();
        // find all classes that has that superClassName
        Set<TypeElement> children = new LinkedHashSet<TypeElement>();
        for (String superclass : ONE_OF_VERBS) {
            findTypeElementChildren(processingEnv, roundEnv, children, superclass);
        }
        for (TypeElement child : children) {
            XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
            if (rootElement != null) {
                String childName = rootElement.name();
                if (childName != null) {
                    oneOfTypes.add(childName);
                }
            }
        }
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, true, "", docComment, false, false, null, true, oneOfTypes, false);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements)

Example 25 with XmlRootElement

use of javax.xml.bind.annotation.XmlRootElement in project camel by apache.

the class CoreEipAnnotationProcessor method processElement.

private void processElement(ProcessingEnvironment processingEnv, RoundEnvironment roundEnv, TypeElement classElement, XmlElement element, VariableElement fieldElement, Set<EipOption> eipOptions, String prefix) {
    Elements elementUtils = processingEnv.getElementUtils();
    String fieldName;
    fieldName = fieldElement.getSimpleName().toString();
    if (element != null) {
        String kind = "element";
        String name = element.name();
        if (isNullOrEmpty(name) || "##default".equals(name)) {
            name = fieldName;
        }
        name = prefix + name;
        TypeMirror fieldType = fieldElement.asType();
        String fieldTypeName = fieldType.toString();
        TypeElement fieldTypeElement = findTypeElement(processingEnv, roundEnv, fieldTypeName);
        String defaultValue = findDefaultValue(fieldElement, fieldTypeName);
        String docComment = findJavaDoc(elementUtils, fieldElement, fieldName, name, classElement, true);
        boolean required = element.required();
        // metadata may overrule element required
        required = findRequired(fieldElement, required);
        // is it used as predicate (check field first and then fallback to its class)
        boolean asPredicate = fieldElement.getAnnotation(AsPredicate.class) != null;
        if (!asPredicate) {
            asPredicate = classElement.getAnnotation(AsPredicate.class) != null;
        }
        // gather enums
        Set<String> enums = new LinkedHashSet<String>();
        boolean isEnum = fieldTypeElement != null && fieldTypeElement.getKind() == ElementKind.ENUM;
        if (isEnum) {
            TypeElement enumClass = findTypeElement(processingEnv, roundEnv, fieldTypeElement.asType().toString());
            if (enumClass != null) {
                // find all the enum constants which has the possible enum value that can be used
                List<VariableElement> fields = ElementFilter.fieldsIn(enumClass.getEnclosedElements());
                for (VariableElement var : fields) {
                    if (var.getKind() == ElementKind.ENUM_CONSTANT) {
                        String val = var.toString();
                        enums.add(val);
                    }
                }
            }
        }
        // gather oneOf expression/predicates which uses language
        Set<String> oneOfTypes = new TreeSet<String>();
        boolean isOneOf = ONE_OF_TYPE_NAME.equals(fieldTypeName);
        if (isOneOf) {
            // okay its actually an language expression, so favor using that in the eip option
            kind = "expression";
            for (String language : ONE_OF_LANGUAGES) {
                fieldTypeName = language;
                TypeElement languages = findTypeElement(processingEnv, roundEnv, language);
                if (languages != null) {
                    String superClassName = canonicalClassName(languages.toString());
                    // find all classes that has that superClassName
                    Set<TypeElement> children = new LinkedHashSet<TypeElement>();
                    findTypeElementChildren(processingEnv, roundEnv, children, superClassName);
                    for (TypeElement child : children) {
                        XmlRootElement rootElement = child.getAnnotation(XmlRootElement.class);
                        if (rootElement != null) {
                            String childName = rootElement.name();
                            oneOfTypes.add(childName);
                        }
                    }
                }
            }
        }
        // special for otherwise as we want to indicate that the element is
        if ("otherwise".equals(name)) {
            isOneOf = true;
            oneOfTypes.add("otherwise");
        }
        boolean deprecated = fieldElement.getAnnotation(Deprecated.class) != null;
        String displayName = null;
        Metadata metadata = fieldElement.getAnnotation(Metadata.class);
        if (metadata != null) {
            displayName = metadata.displayName();
        }
        EipOption ep = new EipOption(name, displayName, kind, fieldTypeName, required, defaultValue, docComment, deprecated, isEnum, enums, isOneOf, oneOfTypes, asPredicate);
        eipOptions.add(ep);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) AsPredicate(org.apache.camel.spi.AsPredicate) TypeElement(javax.lang.model.element.TypeElement) AnnotationProcessorHelper.findTypeElement(org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement) Metadata(org.apache.camel.spi.Metadata) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) XmlElements(javax.xml.bind.annotation.XmlElements) TypeMirror(javax.lang.model.type.TypeMirror) TreeSet(java.util.TreeSet)

Aggregations

XmlRootElement (javax.xml.bind.annotation.XmlRootElement)39 XmlSchema (javax.xml.bind.annotation.XmlSchema)11 XmlType (javax.xml.bind.annotation.XmlType)9 QName (javax.xml.namespace.QName)9 LinkedHashSet (java.util.LinkedHashSet)6 TreeSet (java.util.TreeSet)6 TypeElement (javax.lang.model.element.TypeElement)6 AnnotationProcessorHelper.findTypeElement (org.apache.camel.tools.apt.AnnotationProcessorHelper.findTypeElement)6 Method (java.lang.reflect.Method)5 TypeMirror (javax.lang.model.type.TypeMirror)5 Elements (javax.lang.model.util.Elements)5 XmlElements (javax.xml.bind.annotation.XmlElements)5 Metadata (org.apache.camel.spi.Metadata)5 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2