Search in sources :

Example 1 with XmlAnyElement

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

the class AnnotationsProcessor method buildNewProperty.

/*
    * Create a new Property Object and process the annotations that are common
    * to fields and methods
     */
Property buildNewProperty(TypeInfo info, JavaClass cls, JavaHasAnnotations javaHasAnnotations, String propertyName, JavaClass ptype) {
    Property property = null;
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlElements.class)) {
        property = buildChoiceProperty(javaHasAnnotations);
    } else if (helper.isAnnotationPresent(javaHasAnnotations, XmlElementRef.class) || helper.isAnnotationPresent(javaHasAnnotations, XmlElementRefs.class)) {
        findAndProcessObjectFactory(cls);
        property = buildReferenceProperty(info, javaHasAnnotations, propertyName, ptype);
        if (helper.isAnnotationPresent(javaHasAnnotations, XmlAnyElement.class)) {
            XmlAnyElement anyElement = (XmlAnyElement) helper.getAnnotation(javaHasAnnotations, XmlAnyElement.class);
            property.setIsAny(true);
            if (anyElement.value() != null) {
                property.setDomHandlerClassName(anyElement.value().getName());
            }
            property.setLax(anyElement.lax());
            info.setAnyElementPropertyName(propertyName);
        }
    } else if (helper.isAnnotationPresent(javaHasAnnotations, XmlAnyElement.class)) {
        findAndProcessObjectFactory(cls);
        XmlAnyElement anyElement = (XmlAnyElement) helper.getAnnotation(javaHasAnnotations, XmlAnyElement.class);
        property = new Property(helper);
        property.setIsAny(true);
        if (anyElement.value() != null) {
            property.setDomHandlerClassName(anyElement.value().getName());
        }
        property.setLax(anyElement.lax());
        info.setAnyElementPropertyName(propertyName);
    } else if (helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlTransformation.class) || helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlReadTransformer.class) || helper.isAnnotationPresent(javaHasAnnotations, org.eclipse.persistence.oxm.annotations.XmlWriteTransformer.class) || helper.isAnnotationPresent(javaHasAnnotations, XmlWriteTransformers.class)) {
        property = buildTransformationProperty(javaHasAnnotations, cls);
    } else {
        property = new Property(helper);
    }
    property.setPropertyName(propertyName);
    property.setElement(javaHasAnnotations);
    // check the class
    if (helper.isCollectionType(ptype)) {
        JavaClass componentType = helper.getJavaClass(Object.class);
        Collection typeArgs = ptype.getActualTypeArguments();
        if (!typeArgs.isEmpty()) {
            componentType = (JavaClass) typeArgs.iterator().next();
        }
        updatePropertyType(property, ptype, componentType);
    } else {
        updatePropertyType(property, ptype, ptype);
    }
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlVariableNode.class)) {
        XmlVariableNode variableNode = (XmlVariableNode) helper.getAnnotation(javaHasAnnotations, XmlVariableNode.class);
        if (variableNode.type() != XmlVariableNode.DEFAULT.class) {
            property.setVariableClassName(variableNode.type().getName());
            JavaClass componentType = helper.getJavaClass(variableNode.type());
            if (helper.isCollectionType(ptype)) {
                property.setGenericType(componentType);
            } else {
                property.setType(componentType);
            }
        }
        if (!variableNode.value().equals("##default")) {
            property.setVariableAttributeName(variableNode.value());
        }
        property.setVariableNodeAttribute(variableNode.attribute());
    }
    if ((ptype.isArray() && !areEquals(ptype, byte[].class)) || (helper.isCollectionType(ptype) && !helper.isAnnotationPresent(javaHasAnnotations, XmlList.class))) {
        property.setNillable(true);
    }
    processPropertyAnnotations(info, cls, javaHasAnnotations, property);
    if (helper.isAnnotationPresent(javaHasAnnotations, XmlPath.class)) {
        XmlPath xmlPath = (XmlPath) helper.getAnnotation(javaHasAnnotations, XmlPath.class);
        property.setXmlPath(xmlPath.value());
        Field<XMLConversionManager, NamespaceResolver> tempField = new XMLField(xmlPath.value());
        boolean isAttribute = tempField.getLastXPathFragment().isAttribute();
        property.setIsAttribute(isAttribute);
        // set schema name
        String schemaName = XMLProcessor.getNameFromXPath(xmlPath.value(), property.getPropertyName(), isAttribute);
        QName qName;
        NamespaceInfo nsInfo = getPackageInfoForPackage(cls).getNamespaceInfo();
        if (isAttribute) {
            if (nsInfo.isAttributeFormQualified()) {
                qName = new QName(nsInfo.getNamespace(), schemaName);
            } else {
                qName = new QName(schemaName);
            }
        } else {
            if (nsInfo.isElementFormQualified()) {
                qName = new QName(nsInfo.getNamespace(), schemaName);
            } else {
                qName = new QName(schemaName);
            }
        }
        property.setSchemaName(qName);
        // create properties for any predicates
        XPathFragment fragment = tempField.getXPathFragment();
        String currentPath = "";
        while (fragment != null && !(fragment.nameIsText()) && !(fragment.isAttribute())) {
            if (fragment.getPredicate() != null) {
                // can't append xpath directly since it will contain the predicate
                String fragmentPath = fragment.getLocalName();
                if (fragment.getPrefix() != null && !(Constants.EMPTY_STRING.equals(fragment.getPrefix()))) {
                    fragmentPath = fragment.getPrefix() + ":" + fragmentPath;
                }
                currentPath += fragmentPath;
                String predicatePath = currentPath;
                TypeInfo targetInfo = info;
                if (fragment.getNextFragment() == null) {
                    // if this is the last fragment, and there's no text after, then this is
                    // complex. May need to add the attribute property to the target type.
                    processReferencedClass(ptype);
                    TypeInfo predicateTypeInfo = typeInfos.get(ptype.getQualifiedName());
                    if (predicateTypeInfo != null) {
                        targetInfo = predicateTypeInfo;
                        predicatePath = "";
                    }
                }
                Property predicateProperty = new Property(helper);
                predicateProperty.setType(helper.getJavaClass("java.lang.String"));
                if (predicatePath.length() > 0) {
                    predicatePath += "/";
                }
                predicatePath += fragment.getPredicate().getXPathFragment().getXPath();
                predicateProperty.setXmlPath(predicatePath);
                predicateProperty.setIsAttribute(true);
                String predschemaName = XMLProcessor.getNameFromXPath(predicatePath, property.getPropertyName(), true);
                QName predQname;
                if (nsInfo.isAttributeFormQualified()) {
                    predQname = new QName(nsInfo.getNamespace(), predschemaName);
                } else {
                    predQname = new QName(predschemaName);
                }
                predicateProperty.setSchemaName(predQname);
                if (!targetInfo.hasPredicateProperty(predicateProperty)) {
                    targetInfo.getPredicateProperties().add(predicateProperty);
                }
            } else {
                currentPath += fragment.getXPath();
            }
            currentPath += "/";
            fragment = fragment.getNextFragment();
        }
    } else {
        property.setSchemaName(getQNameForProperty(property, propertyName, javaHasAnnotations, getPackageInfoForPackage(cls).getNamespaceInfo(), info));
    }
    ptype = property.getActualType();
    if (ptype.isPrimitive()) {
        if (property.getType().isArray() && helper.isAnnotationPresent(javaHasAnnotations, XmlElement.class)) {
            XmlElement elemAnno = (XmlElement) helper.getAnnotation(javaHasAnnotations, XmlElement.class);
            property.setIsRequired(elemAnno.required());
        } else {
            property.setIsRequired(true);
        }
    }
    // apply class level adapters - don't override property level adapter
    if (!property.isSetXmlJavaTypeAdapter()) {
        TypeInfo refClassInfo = getTypeInfos().get(ptype.getQualifiedName());
        if (refClassInfo != null && refClassInfo.isSetXmlJavaTypeAdapter()) {
            org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter xmlJavaTypeAdapter = null;
            try {
                xmlJavaTypeAdapter = refClassInfo.getXmlJavaTypeAdapter();
                property.setXmlJavaTypeAdapter(refClassInfo.getXmlJavaTypeAdapter());
            } catch (JAXBException e) {
                throw JAXBException.invalidTypeAdapterClass(xmlJavaTypeAdapter.getValue(), cls.getName());
            }
        }
    }
    if (property.isXmlTransformation()) {
        referencedByTransformer.add(property.getType().getName());
    }
    return property;
}
Also used : XmlPath(org.eclipse.persistence.oxm.annotations.XmlPath) XmlVariableNode(org.eclipse.persistence.oxm.annotations.XmlVariableNode) XmlAnyElement(jakarta.xml.bind.annotation.XmlAnyElement) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XmlWriteTransformer(org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation.XmlWriteTransformer) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty) XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) JAXBException(org.eclipse.persistence.exceptions.JAXBException) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) Collection(java.util.Collection) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XmlElement(jakarta.xml.bind.annotation.XmlElement) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Aggregations

XmlAnyElement (jakarta.xml.bind.annotation.XmlAnyElement)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 Collection (java.util.Collection)1 QName (javax.xml.namespace.QName)1 JAXBException (org.eclipse.persistence.exceptions.JAXBException)1 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)1 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)1 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)1 XmlWriteTransformer (org.eclipse.persistence.jaxb.xmlmodel.XmlTransformation.XmlWriteTransformer)1 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)1 XMLField (org.eclipse.persistence.oxm.XMLField)1 XmlPath (org.eclipse.persistence.oxm.annotations.XmlPath)1 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)1 XmlVariableNode (org.eclipse.persistence.oxm.annotations.XmlVariableNode)1