Search in sources :

Example 1 with XmlElementDecl

use of jakarta.xml.bind.annotation.XmlElementDecl in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processObjectFactory.

public JavaClass[] processObjectFactory(JavaClass objectFactoryClass, List<JavaClass> classes) {
    String className = objectFactoryClass.getName();
    if (objectFactoryClassNames.contains(className)) {
        return new JavaClass[0];
    }
    objectFactoryClassNames.add(className);
    // if there is an xml-registry from XML for this JavaClass, create a map
    // of method names to XmlElementDecl objects to simplify processing
    // later on in this method
    Map<String, org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl> elemDecls = new HashMap<>();
    org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry xmlReg = xmlRegistries.get(objectFactoryClass.getQualifiedName());
    if (xmlReg != null) {
        // process xml-element-decl entries
        for (org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl xmlElementDecl : xmlReg.getXmlElementDecl()) {
            // key each element-decl on method name
            elemDecls.put(xmlElementDecl.getJavaMethod(), xmlElementDecl);
        }
    }
    Collection methods = objectFactoryClass.getDeclaredMethods();
    Iterator methodsIter = methods.iterator();
    PackageInfo packageInfo = getPackageInfoForPackage(objectFactoryClass);
    while (methodsIter.hasNext()) {
        JavaMethod next = (JavaMethod) methodsIter.next();
        if (next.getName().startsWith(CREATE)) {
            JavaClass type = next.getReturnType();
            if (JAVAX_XML_BIND_JAXBELEMENT.equals(type.getName())) {
                Object[] actualTypeArguments = type.getActualTypeArguments().toArray();
                if (actualTypeArguments.length == 0) {
                    type = helper.getObjectClass();
                } else {
                    type = (JavaClass) actualTypeArguments[0];
                }
                type = processXmlElementDecl(type, next, packageInfo, elemDecls);
            } else if (helper.getJaxbElementClass().isAssignableFrom(type)) {
                this.factoryMethods.put(type.getRawName(), next);
                type = processXmlElementDecl(type, next, packageInfo, elemDecls);
            } else {
                this.factoryMethods.put(type.getRawName(), next);
            }
            if (!helper.isBuiltInJavaType(type) && !helper.classExistsInArray(type, classes)) {
                classes.add(type);
            }
        }
    }
    if (classes.size() > 0) {
        classesToProcessPropertyTypes.addAll(classes);
        return classes.toArray(new JavaClass[classes.size()]);
    } else {
        return new JavaClass[0];
    }
}
Also used : HashMap(java.util.HashMap) XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl) XmlRegistry(jakarta.xml.bind.annotation.XmlRegistry) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Iterator(java.util.Iterator) Collection(java.util.Collection) JavaMethod(org.eclipse.persistence.jaxb.javamodel.JavaMethod)

Example 2 with XmlElementDecl

use of jakarta.xml.bind.annotation.XmlElementDecl in project eclipselink by eclipse-ee4j.

the class ObjectFactory method createTask.

@XmlElementDecl(name = "task")
public JAXBElement<Task> createTask(Task t) {
    JAXBElement<Task> task = new JAXBElement<Employee.Task>(new QName("task"), Task.class, t);
    task.setNil(true);
    return task;
}
Also used : Task(org.eclipse.persistence.testing.jaxb.xmlelementref.nills.Employee.Task) QName(javax.xml.namespace.QName) JAXBElement(jakarta.xml.bind.JAXBElement) XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl)

Example 3 with XmlElementDecl

use of jakarta.xml.bind.annotation.XmlElementDecl in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processXmlElementDecl.

private JavaClass processXmlElementDecl(JavaClass type, JavaMethod next, PackageInfo packageInfo, Map<String, org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl> elemDecls) {
    JavaClass returnType = type;
    // if there's an XmlElementDecl for this method from XML, use it
    // - otherwise look for an annotation
    org.eclipse.persistence.jaxb.xmlmodel.XmlRegistry.XmlElementDecl xmlEltDecl = elemDecls.get(next.getName());
    if ((xmlEltDecl != null) || helper.isAnnotationPresent(next, XmlElementDecl.class)) {
        QName qname;
        QName substitutionHead = null;
        String url;
        String localName;
        String defaultValue = null;
        Class<?> scopeClass = jakarta.xml.bind.annotation.XmlElementDecl.GLOBAL.class;
        if (xmlEltDecl != null) {
            url = xmlEltDecl.getNamespace();
            localName = xmlEltDecl.getName();
            String scopeClassName = xmlEltDecl.getScope();
            if (!scopeClassName.equals(ELEMENT_DECL_GLOBAL)) {
                JavaClass jScopeClass = helper.getJavaClass(scopeClassName);
                if (jScopeClass != null) {
                    scopeClass = helper.getClassForJavaClass(jScopeClass);
                    if (scopeClass == null) {
                        scopeClass = jakarta.xml.bind.annotation.XmlElementDecl.GLOBAL.class;
                    }
                }
            }
            if (!xmlEltDecl.getSubstitutionHeadName().equals(EMPTY_STRING)) {
                String subHeadLocal = xmlEltDecl.getSubstitutionHeadName();
                String subHeadNamespace = xmlEltDecl.getSubstitutionHeadNamespace();
                if (subHeadNamespace.equals(XMLProcessor.DEFAULT)) {
                    subHeadNamespace = packageInfo.getNamespace();
                }
                substitutionHead = new QName(subHeadNamespace, subHeadLocal);
            }
            if (!(xmlEltDecl.getDefaultValue().length() == 1 && xmlEltDecl.getDefaultValue().startsWith(ELEMENT_DECL_DEFAULT))) {
                defaultValue = xmlEltDecl.getDefaultValue();
            }
        } else {
            // there was no xml-element-decl for this method in XML,
            // so use the annotation
            XmlElementDecl elementDecl = (XmlElementDecl) helper.getAnnotation(next, XmlElementDecl.class);
            url = elementDecl.namespace();
            localName = elementDecl.name();
            scopeClass = elementDecl.scope();
            if (!elementDecl.substitutionHeadName().equals(EMPTY_STRING)) {
                String subHeadLocal = elementDecl.substitutionHeadName();
                String subHeadNamespace = elementDecl.substitutionHeadNamespace();
                if (subHeadNamespace.equals(XMLProcessor.DEFAULT)) {
                    subHeadNamespace = packageInfo.getNamespace();
                }
                substitutionHead = new QName(subHeadNamespace, subHeadLocal);
            }
            if (!(elementDecl.defaultValue().length() == 1 && elementDecl.defaultValue().startsWith(ELEMENT_DECL_DEFAULT))) {
                defaultValue = elementDecl.defaultValue();
            }
        }
        if (XMLProcessor.DEFAULT.equals(url)) {
            url = packageInfo.getNamespace();
        }
        if (Constants.EMPTY_STRING.equals(url)) {
            isDefaultNamespaceAllowed = false;
            qname = new QName(localName);
        } else {
            qname = new QName(url, localName);
        }
        boolean isList = false;
        if (JAVA_UTIL_LIST.equals(type.getName())) {
            isList = true;
            Collection args = type.getActualTypeArguments();
            if (args.size() > 0) {
                type = (JavaClass) args.iterator().next();
            }
        }
        ElementDeclaration declaration = new ElementDeclaration(qname, type, type.getQualifiedName(), isList, scopeClass);
        if (substitutionHead != null) {
            declaration.setSubstitutionHead(substitutionHead);
        }
        if (defaultValue != null) {
            declaration.setDefaultValue(defaultValue);
        }
        if (helper.isAnnotationPresent(next, XmlJavaTypeAdapter.class)) {
            XmlJavaTypeAdapter typeAdapter = (XmlJavaTypeAdapter) helper.getAnnotation(next, XmlJavaTypeAdapter.class);
            Class<? extends XmlAdapter> typeAdapterClass = typeAdapter.value();
            declaration.setJavaTypeAdapterClass(typeAdapterClass);
            Class<?> declJavaType = CompilerHelper.getTypeFromAdapterClass(typeAdapterClass);
            JavaClass adaptedType = helper.getJavaClass(declJavaType);
            declaration.setJavaType(adaptedType);
            declaration.setAdaptedJavaType(type);
            returnType = adaptedType;
        }
        if (helper.isAnnotationPresent(next, XmlMimeType.class)) {
            XmlMimeType mimeType = (XmlMimeType) helper.getAnnotation(next, XmlMimeType.class);
            declaration.setXmlMimeType(mimeType.value());
        }
        if (helper.isAnnotationPresent(next, XmlAttachmentRef.class)) {
            declaration.setXmlAttachmentRef(true);
        }
        Map<QName, ElementDeclaration> elements = getElementDeclarationsForScope(scopeClass.getName());
        if (elements == null) {
            elements = new HashMap<>();
            this.elementDeclarations.put(scopeClass.getName(), elements);
        }
        if (elements.containsKey(qname)) {
            throw JAXBException.duplicateElementName(qname);
        }
        elements.put(qname, declaration);
    }
    return returnType;
}
Also used : XmlMimeType(jakarta.xml.bind.annotation.XmlMimeType) QName(javax.xml.namespace.QName) XmlJavaTypeAdapter(jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter) XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl) XmlRegistry(jakarta.xml.bind.annotation.XmlRegistry) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Collection(java.util.Collection)

Example 4 with XmlElementDecl

use of jakarta.xml.bind.annotation.XmlElementDecl in project eclipselink by eclipse-ee4j.

the class ObjectFactory method createAddress.

@XmlElementDecl(name = "address")
public JAXBElement<Address> createAddress(Address a) {
    JAXBElement<Address> address = new JAXBElement<Address>(new QName("address"), Address.class, a);
    address.setNil(true);
    return address;
}
Also used : QName(javax.xml.namespace.QName) JAXBElement(jakarta.xml.bind.JAXBElement) XmlElementDecl(jakarta.xml.bind.annotation.XmlElementDecl)

Aggregations

XmlElementDecl (jakarta.xml.bind.annotation.XmlElementDecl)4 QName (javax.xml.namespace.QName)3 JAXBElement (jakarta.xml.bind.JAXBElement)2 XmlRegistry (jakarta.xml.bind.annotation.XmlRegistry)2 Collection (java.util.Collection)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 XmlMimeType (jakarta.xml.bind.annotation.XmlMimeType)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 JavaMethod (org.eclipse.persistence.jaxb.javamodel.JavaMethod)1 Task (org.eclipse.persistence.testing.jaxb.xmlelementref.nills.Employee.Task)1