Search in sources :

Example 1 with XmlElementWrapper

use of org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method getXPathForField.

public Field getXPathForField(Property property, NamespaceInfo namespaceInfo, boolean isTextMapping, boolean isAny) {
    Field xmlField = null;
    String xPath = property.getXmlPath();
    if (null != xPath) {
        String newXPath = prefixCustomXPath(xPath, property, namespaceInfo);
        xmlField = new XMLField(newXPath);
    } else {
        StringBuilder xPathBuilder = new StringBuilder();
        if (property.isSetXmlElementWrapper()) {
            XmlElementWrapper wrapper = property.getXmlElementWrapper();
            String namespace = wrapper.getNamespace();
            if (namespace.equals(XMLProcessor.DEFAULT)) {
                if (namespaceInfo.isElementFormQualified()) {
                    namespace = namespaceInfo.getNamespace();
                } else {
                    namespace = "";
                }
            }
            if (namespace.equals("")) {
                xPathBuilder.append(wrapper.getName()).append("/");
            } else {
                String prefix = getPrefixForNamespace(namespace, getNamespaceResolverForDescriptor(namespaceInfo));
                xPathBuilder.append(getQualifiedString(prefix, wrapper.getName() + "/"));
            }
            if (isAny || property.isMap()) {
                xmlField = new XMLField(xPathBuilder.toString());
                return xmlField;
            }
        }
        if (property.isAttribute()) {
            if (property.isSetXmlPath()) {
                xPathBuilder.append(property.getXmlPath());
            } else {
                QName name = property.getSchemaName();
                String namespace = name.getNamespaceURI();
                if (namespace.equals("")) {
                    xPathBuilder.append(ATT).append(name.getLocalPart());
                } else {
                    String prefix = getPrefixForNamespace(namespace, getNamespaceResolverForDescriptor(namespaceInfo));
                    xPathBuilder.append(ATT).append(getQualifiedString(prefix, name.getLocalPart()));
                }
            }
            xmlField = new XMLField(xPathBuilder.toString());
        } else if (property.isXmlValue()) {
            if (isBinaryData(property.getActualType())) {
                xmlField = new XMLField(".");
            } else {
                xmlField = new XMLField("text()");
            }
        } else {
            QName elementName = property.getSchemaName();
            xmlField = getXPathForElement(xPathBuilder.toString(), elementName, namespaceInfo, isTextMapping);
        }
    }
    QName schemaType = userDefinedSchemaTypes.get(property.getActualType().getQualifiedName());
    if (property.getSchemaType() != null) {
        schemaType = property.getSchemaType();
    }
    if (schemaType == null) {
        String propertyActualTypeRawName = property.getActualType().getRawName();
        if (QName.class.getCanonicalName().equals(propertyActualTypeRawName)) {
            schemaType = helper.getXMLToJavaTypeMap().get(propertyActualTypeRawName);
        }
    }
    if (schemaType != null && !schemaType.equals(Constants.NORMALIZEDSTRING_QNAME)) {
        xmlField.setSchemaType(schemaType);
    }
    return xmlField;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) QName(javax.xml.namespace.QName) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)

Example 2 with XmlElementWrapper

use of org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper in project eclipselink by eclipse-ee4j.

the class SchemaGenerator method addXmlElementWrapperToSchema.

/**
 * Convenience method for processing an XmlElementWrapper for a given property. Required schema
 * components will be generated and set accordingly.
 *
 * @param property the property containing an XmlElementWrapper
 * @param schema the schema currently being generated
 * @param compositor sequence/choice/all that the generated wrapper Element will be added to
 * @return AddToSchemaResult containing current ComplexType and TypeDefParticle
 */
private AddToSchemaResult addXmlElementWrapperToSchema(Property property, Schema schema, TypeDefParticle compositor) {
    XmlElementWrapper wrapper = property.getXmlElementWrapper();
    Element wrapperElement = new Element();
    String name = wrapper.getName();
    // handle nillable
    wrapperElement.setNillable(wrapper.isNillable());
    // namespace in not the target or ##default, create a ref with min/max = 1
    String wrapperNS = wrapper.getNamespace();
    if (!wrapperNS.equals(XMLProcessor.DEFAULT) && !wrapperNS.equals(schema.getTargetNamespace())) {
        wrapperElement.setMinOccurs(Occurs.ONE);
        wrapperElement.setMaxOccurs(Occurs.ONE);
        String prefix = getOrGeneratePrefixForNamespace(wrapperNS, schema);
        wrapperElement.setRef(prefix + COLON + name);
        compositor.addElement(wrapperElement);
        // assume that the element exists and does not need to be created
        return null;
    }
    wrapperElement.setName(name);
    if (wrapper.isRequired()) {
        wrapperElement.setMinOccurs(Occurs.ONE);
    } else {
        wrapperElement.setMinOccurs(Occurs.ZERO);
    }
    if (!wrapperNS.equals(XMLProcessor.DEFAULT)) {
        String lookupNamespace = schema.getTargetNamespace();
        if (lookupNamespace == null) {
            lookupNamespace = EMPTY_STRING;
        }
        NamespaceInfo namespaceInfo = getNamespaceInfoForNamespace(lookupNamespace);
        boolean isElementFormQualified = false;
        if (namespaceInfo != null) {
            isElementFormQualified = namespaceInfo.isElementFormQualified();
        }
        shouldAddRefAndSetForm(wrapperElement, wrapperNS, lookupNamespace, isElementFormQualified, true);
    }
    compositor.addElement(wrapperElement);
    ComplexType wrapperType = new ComplexType();
    Sequence wrapperSequence = new Sequence();
    wrapperType.setSequence(wrapperSequence);
    wrapperElement.setComplexType(wrapperType);
    return new AddToSchemaResult(wrapperSequence, wrapperType);
}
Also used : Element(org.eclipse.persistence.internal.oxm.schema.model.Element) Sequence(org.eclipse.persistence.internal.oxm.schema.model.Sequence) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)

Example 3 with XmlElementWrapper

use of org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXmlVariableNodeAttribute.

private Property processXmlVariableNodeAttribute(XmlVariableNode xmlVariableNode, Property oldProperty, TypeInfo tInfo, JavaType javaType) {
    processObjectFactory(tInfo);
    // reset any existing values
    resetProperty(oldProperty, tInfo);
    oldProperty.setVariableAttributeName(xmlVariableNode.getJavaVariableAttribute());
    String type = xmlVariableNode.getType();
    if (!type.equals(DEFAULT)) {
        oldProperty.setVariableClassName(type);
    }
    if (xmlVariableNode.getXmlPath() != null) {
        oldProperty.setXmlPath(xmlVariableNode.getXmlPath());
    } else {
        // no xml-path, so use name/namespace from xml-element, and process wrapper
        XmlElementWrapper xmlElementWrapper = xmlVariableNode.getXmlElementWrapper();
        if (xmlElementWrapper != null) {
            if (DEFAULT.equals(xmlElementWrapper.getName())) {
                xmlElementWrapper.setName(tInfo.getXmlNameTransformer().transformElementName(oldProperty.getPropertyName()));
            }
            oldProperty.setXmlElementWrapper(xmlVariableNode.getXmlElementWrapper());
        }
    }
    return oldProperty;
}
Also used : XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)

Example 4 with XmlElementWrapper

use of org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateMappingForReferenceProperty.

public Mapping generateMappingForReferenceProperty(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    boolean isCollection = helper.isCollectionType(property.getType()) || property.getType().isArray();
    Mapping mapping;
    if (isCollection) {
        mapping = new XMLChoiceCollectionMapping();
        initializeXMLContainerMapping((ChoiceCollectionMapping) mapping, property.getType().isArray());
        JavaClass collectionType = property.getType();
        collectionType = containerClassImpl(collectionType);
        ((ChoiceCollectionMapping) mapping).useCollectionClassName(collectionType.getRawName());
        JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
        if (property.isSetXmlJavaTypeAdapter()) {
            JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
            jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
        }
        ((ChoiceCollectionMapping) mapping).setConverter(jaxbERConverter);
        if (property.isSetWriteOnly()) {
            ((ChoiceCollectionMapping) mapping).setIsWriteOnly(property.isWriteOnly());
        }
        if (property.isSetXmlElementWrapper()) {
            ((ChoiceCollectionMapping) mapping).setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
        }
    } else {
        mapping = new XMLChoiceObjectMapping();
        JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
        if (property.isSetXmlJavaTypeAdapter()) {
            JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
            jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
        }
        ((ChoiceObjectMapping) mapping).setConverter(jaxbERConverter);
        if (property.isSetWriteOnly()) {
            ((ChoiceObjectMapping) mapping).setIsWriteOnly(property.isWriteOnly());
        }
    }
    initializeXMLMapping((XMLMapping) mapping, property);
    List<ElementDeclaration> referencedElements = property.getReferencedElements();
    JavaClass propertyType = property.getType();
    if (propertyType.isArray()) {
        JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
        accessor.setComponentClassName(property.getType().getComponentType().getQualifiedName());
        JavaClass componentType = propertyType.getComponentType();
        if (componentType.isArray()) {
            Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getQualifiedName());
            accessor.setAdaptedClassName(adaptedClass.getName());
        }
        mapping.setAttributeAccessor(accessor);
    }
    String wrapperXPath = "";
    // handle XmlElementWrapper
    if (property.isSetXmlElementWrapper()) {
        XmlElementWrapper wrapper = property.getXmlElementWrapper();
        String namespace = wrapper.getNamespace();
        if (namespace.equals(XMLProcessor.DEFAULT)) {
            if (namespaceInfo.isElementFormQualified()) {
                namespace = namespaceInfo.getNamespace();
            } else {
                namespace = "";
            }
        }
        if (namespace.equals("")) {
            wrapperXPath += (wrapper.getName() + "/");
        } else {
            String prefix = getPrefixForNamespace(namespace, getNamespaceResolverForDescriptor(namespaceInfo));
            wrapperXPath += getQualifiedString(prefix, wrapper.getName() + "/");
        }
    }
    if (property.isMixedContent() && isCollection) {
        if (wrapperXPath.length() == 0) {
            ((ChoiceCollectionMapping) mapping).setMixedContent(true);
        } else {
            ((ChoiceCollectionMapping) mapping).setMixedContent(wrapperXPath.substring(0, wrapperXPath.length() - 1));
        }
    }
    for (ElementDeclaration element : referencedElements) {
        QName elementName = element.getElementName();
        JavaClass pType = element.getJavaType();
        String pTypeName = element.getJavaTypeName();
        boolean isBinaryType = (areEquals(pType, AnnotationsProcessor.JAVAX_ACTIVATION_DATAHANDLER) || areEquals(pType, byte[].class) || areEquals(pType, Image.class) || areEquals(pType, Source.class) || areEquals(pType, AnnotationsProcessor.JAVAX_MAIL_INTERNET_MIMEMULTIPART));
        boolean isText = pType.isEnum() || (!isBinaryType && !(this.typeInfo.containsKey(element.getJavaTypeName())) && !(element.getJavaTypeName().equals(OBJECT_CLASS_NAME)));
        String xPath = wrapperXPath;
        Field xmlField = this.getXPathForElement(xPath, elementName, namespaceInfo, isText);
        // ensure byte[] goes to base64 instead of the default hex.
        if (helper.getXMLToJavaTypeMap().get(pType.getRawName()) == Constants.BASE_64_BINARY_QNAME) {
            xmlField.setSchemaType(Constants.BASE_64_BINARY_QNAME);
        }
        if (areEquals(pType, Object.class)) {
            setTypedTextField(xmlField);
        }
        Mapping nestedMapping;
        AbstractNullPolicy nullPolicy = null;
        if (isCollection) {
            ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
            xmlChoiceCollectionMapping.addChoiceElement(xmlField, pTypeName);
            nestedMapping = (Mapping) xmlChoiceCollectionMapping.getChoiceElementMappings().get(xmlField);
            if (nestedMapping.isAbstractCompositeCollectionMapping()) {
                ((CompositeCollectionMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
                nullPolicy = ((CompositeCollectionMapping) nestedMapping).getNullPolicy();
            }
            if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
                DirectCollectionMapping nestedCompositeDirectCollectionMapping = (DirectCollectionMapping) nestedMapping;
                nullPolicy = nestedCompositeDirectCollectionMapping.getNullPolicy();
                if (pType.isEnum()) {
                    TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
                    nestedCompositeDirectCollectionMapping.setValueConverter(buildJAXBEnumTypeConverter(nestedCompositeDirectCollectionMapping, (EnumTypeInfo) enumTypeInfo));
                }
                if (element.isList()) {
                    XMLListConverter listConverter = new XMLListConverter();
                    listConverter.setObjectClassName(pType.getQualifiedName());
                    ((DirectCollectionMapping) nestedMapping).setValueConverter(listConverter);
                }
            } else if (nestedMapping instanceof BinaryDataCollectionMapping) {
                nullPolicy = ((BinaryDataCollectionMapping) nestedMapping).getNullPolicy();
                if (element.isList()) {
                    ((XMLField) nestedMapping.getField()).setUsesSingleNode(true);
                }
            }
        } else {
            ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
            xmlChoiceObjectMapping.addChoiceElement(xmlField, pTypeName);
            nestedMapping = (Mapping) xmlChoiceObjectMapping.getChoiceElementMappings().get(xmlField);
            if (pType.isEnum()) {
                TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
                ((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) enumTypeInfo));
            }
            if (nestedMapping.isAbstractCompositeObjectMapping()) {
                ((CompositeObjectMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
                nullPolicy = ((CompositeObjectMapping) nestedMapping).getNullPolicy();
            } else if (nestedMapping instanceof BinaryDataMapping) {
                nullPolicy = ((BinaryDataMapping) nestedMapping).getNullPolicy();
            } else if (nestedMapping instanceof DirectMapping) {
                nullPolicy = ((DirectMapping) nestedMapping).getNullPolicy();
            }
        }
        if (nullPolicy != null) {
            nullPolicy.setNullRepresentedByEmptyNode(false);
            nullPolicy.setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
            nullPolicy.setNullRepresentedByXsiNil(true);
            nullPolicy.setIgnoreAttributesForNil(false);
        }
        if (!element.isXmlRootElement()) {
            Class<?> scopeClass = element.getScopeClass();
            if (scopeClass == jakarta.xml.bind.annotation.XmlElementDecl.GLOBAL.class) {
                scopeClass = JAXBElement.GlobalScope.class;
            }
            Class<?> declaredType = null;
            if (element.getAdaptedJavaType() != null) {
                declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(element.getAdaptedJavaType().getQualifiedName(), helper.getClassLoader());
            } else {
                declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(pType.getQualifiedName(), helper.getClassLoader());
            }
            JAXBElementConverter converter = new JAXBElementConverter(xmlField, declaredType, scopeClass);
            if (isCollection) {
                ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
                if (element.getJavaTypeAdapterClass() != null) {
                    converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
                } else {
                    CoreConverter originalConverter = xmlChoiceCollectionMapping.getConverter(xmlField);
                    converter.setNestedConverter(originalConverter);
                }
                xmlChoiceCollectionMapping.addConverter(xmlField, converter);
            } else {
                ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
                if (element.getJavaTypeAdapterClass() != null) {
                    converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
                } else {
                    CoreConverter originalConverter = xmlChoiceObjectMapping.getConverter(xmlField);
                    converter.setNestedConverter(originalConverter);
                }
                xmlChoiceObjectMapping.addConverter(xmlField, converter);
            }
        }
    }
    if (property.isAny()) {
        if (isCollection) {
            XMLChoiceCollectionMapping xmlChoiceCollectionMapping = (XMLChoiceCollectionMapping) mapping;
            xmlChoiceCollectionMapping.setIsAny(true);
        }
    }
    return mapping;
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) XMLVariableXPathObjectMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathObjectMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) VariableXPathCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLVariableXPathCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathCollectionMapping) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) XMLAnyAttributeMapping(org.eclipse.persistence.oxm.mappings.XMLAnyAttributeMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLContainerMapping(org.eclipse.persistence.internal.oxm.mappings.XMLContainerMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) Source(javax.xml.transform.Source) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XmlAbstractNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlAbstractNullPolicy) AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) QName(javax.xml.namespace.QName) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) JAXBElement(jakarta.xml.bind.JAXBElement) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)

Example 5 with XmlElementWrapper

use of org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper in project eclipselink by eclipse-ee4j.

the class XMLProcessor method processXmlElement.

/**
 * XmlElement override will completely replace the existing values.
 */
private Property processXmlElement(XmlElement xmlElement, Property oldProperty, TypeInfo typeInfo, NamespaceInfo nsInfo, JavaType javaType) {
    // reset any existing values
    resetProperty(oldProperty, typeInfo);
    if (xmlElement.getXmlMap() != null) {
        processXmlMap(xmlElement.getXmlMap(), oldProperty);
    }
    if (xmlElement.isXmlLocation()) {
        if (!aProcessor.getHelper().getJavaClass(Constants.LOCATOR_CLASS).isAssignableFrom(oldProperty.getType())) {
            throw JAXBException.invalidXmlLocation(oldProperty.getPropertyName(), oldProperty.getType().getName());
        }
        oldProperty.setXmlLocation(true);
    }
    // handle xml-id
    if (xmlElement.isXmlId()) {
        oldProperty.setIsXmlId(true);
        oldProperty.setIsXmlIdExtension(true);
        typeInfo.setIDProperty(oldProperty);
    } else {
        // account for XmlID un-set via XML
        if (typeInfo.getIDProperty() != null && typeInfo.getIDProperty().getPropertyName().equals(oldProperty.getPropertyName())) {
            typeInfo.setIDProperty(null);
        }
    }
    if (xmlElement.getXmlInverseReference() != null) {
        String mappedBy = xmlElement.getXmlInverseReference().getMappedBy();
        oldProperty.setInverseReference(true, true);
        oldProperty.setInverseReferencePropertyName(mappedBy);
    }
    // handle xml-idref
    oldProperty.setIsXmlIdRef(xmlElement.isXmlIdref());
    // handle xml-key
    if (xmlElement.isXmlKey()) {
        typeInfo.addXmlKeyProperty(oldProperty);
    }
    // set required
    oldProperty.setIsRequired(xmlElement.isRequired());
    // set xml-inline-binary-data
    oldProperty.setisInlineBinaryData(xmlElement.isXmlInlineBinaryData());
    // set nillable
    oldProperty.setNillable(xmlElement.isNillable());
    // set defaultValue
    if (xmlElement.getDefaultValue().equals("\u0000")) {
        oldProperty.setDefaultValue(null);
    } else {
        oldProperty.setDefaultValue(xmlElement.getDefaultValue());
    }
    String name;
    String namespace;
    // if xml-path is set, we ignore xml-element-wrapper, as well as name/namespace on xml-element
    if (xmlElement.getXmlPath() != null) {
        oldProperty.setXmlPath(xmlElement.getXmlPath());
        name = getNameFromXPath(xmlElement.getXmlPath(), oldProperty.getPropertyName(), false);
        namespace = DEFAULT;
    } else {
        // no xml-path, so use name/namespace from xml-element, and process wrapper
        name = xmlElement.getName();
        namespace = xmlElement.getNamespace();
        XmlElementWrapper xmlElementWrapper = xmlElement.getXmlElementWrapper();
        if (xmlElementWrapper != null) {
            if (DEFAULT.equals(xmlElementWrapper.getName())) {
                xmlElementWrapper.setName(typeInfo.getXmlNameTransformer().transformElementName(oldProperty.getPropertyName()));
            }
            oldProperty.setXmlElementWrapper(xmlElement.getXmlElementWrapper());
            if (oldProperty.isMap()) {
                name = xmlElement.getXmlElementWrapper().getName();
                namespace = xmlElement.getXmlElementWrapper().getNamespace();
            }
        }
    }
    if (javaType.getXmlType() != null && javaType.getXmlType().getNamespace() != null && (xmlElement.getNamespace() != null && xmlElement.getNamespace().equals(DEFAULT))) {
        // Inherit type-level namespace if there is one
        namespace = javaType.getXmlType().getNamespace();
    }
    // set schema name
    QName qName;
    if (name.equals(DEFAULT)) {
        name = typeInfo.getXmlNameTransformer().transformElementName(oldProperty.getPropertyName());
    }
    if (namespace.equals(DEFAULT)) {
        if (nsInfo.isElementFormQualified()) {
            qName = new QName(nsInfo.getNamespace(), name);
        } else {
            qName = new QName(name);
        }
    } else {
        qName = new QName(namespace, name);
    }
    oldProperty.setSchemaName(qName);
    // check for container type
    if (!xmlElement.getContainerType().equals(DEFAULT)) {
        setContainerType(oldProperty, xmlElement.getContainerType());
    }
    // set type
    if (xmlElement.getType().equals("jakarta.xml.bind.annotation.XmlElement.DEFAULT")) {
        // @XmlElement, reset it to the original value
        if (oldProperty.isXmlElementType()) {
            oldProperty.setType(oldProperty.getOriginalType());
        }
    } else if (xmlElement.getXmlMap() != null) {
        getLogger().logWarning(JAXBMetadataLogger.INVALID_TYPE_ON_MAP, new Object[] { xmlElement.getName() });
    } else {
        JavaClass pType = jModelInput.getJavaModel().getClass(xmlElement.getType());
        if (aProcessor.getHelper().isCollectionType(oldProperty.getType())) {
            oldProperty.setGenericType(pType);
        } else {
            oldProperty.setType(pType);
        }
        oldProperty.setHasXmlElementType(true);
        // may need to generate a type info for the type
        if (aProcessor.shouldGenerateTypeInfo(pType) && aProcessor.getTypeInfos().get(pType.getQualifiedName()) == null) {
            aProcessor.buildNewTypeInfo(new JavaClass[] { pType });
        }
    }
    reapplyPackageAndClassAdapters(oldProperty, typeInfo);
    // handle XmlJavaTypeAdapter
    if (xmlElement.getXmlJavaTypeAdapter() != null) {
        try {
            oldProperty.setXmlJavaTypeAdapter(xmlElement.getXmlJavaTypeAdapter());
        } catch (JAXBException e) {
            throw JAXBException.invalidPropertyAdapterClass(xmlElement.getXmlJavaTypeAdapter().getValue(), xmlElement.getJavaAttribute(), javaType.getName());
        }
    }
    // for primitives we always set required, a.k.a. minOccurs="1"
    if (!oldProperty.isRequired()) {
        JavaClass ptype = oldProperty.getActualType();
        oldProperty.setIsRequired(ptype.isPrimitive() || ptype.isArray() && ptype.getComponentType().isPrimitive());
    }
    // handle xml-list
    if (xmlElement.isSetXmlList()) {
        // Make sure XmlList annotation is on a collection or array
        if (!aProcessor.getHelper().isCollectionType(oldProperty.getType()) && !oldProperty.getType().isArray()) {
            throw JAXBException.invalidList(oldProperty.getPropertyName());
        }
        oldProperty.setIsXmlList(xmlElement.isXmlList());
    }
    // handle xml-mime-type
    if (xmlElement.getXmlMimeType() != null) {
        oldProperty.setMimeType(xmlElement.getXmlMimeType());
    }
    // handle xml-attachment-ref
    if (xmlElement.isXmlAttachmentRef()) {
        oldProperty.setIsSwaAttachmentRef(true);
        oldProperty.setSchemaType(Constants.SWA_REF_QNAME);
    } else if (aProcessor.isMtomAttachment(oldProperty)) {
        oldProperty.setIsMtomAttachment(true);
        oldProperty.setSchemaType(Constants.BASE_64_BINARY_QNAME);
    }
    // handle xml-schema-type
    if (xmlElement.getXmlSchemaType() != null) {
        oldProperty.setSchemaType(new QName(xmlElement.getXmlSchemaType().getNamespace(), xmlElement.getXmlSchemaType().getName()));
    }
    // handle get/set methods
    if (xmlElement.getXmlAccessMethods() != null) {
        oldProperty.setMethodProperty(true);
        oldProperty.setGetMethodName(xmlElement.getXmlAccessMethods().getGetMethod());
        oldProperty.setSetMethodName(xmlElement.getXmlAccessMethods().getSetMethod());
    }
    // handle read-only
    if (xmlElement.isSetReadOnly()) {
        oldProperty.setReadOnly(xmlElement.isReadOnly());
    }
    // handle write-only
    if (xmlElement.isSetWriteOnly()) {
        oldProperty.setWriteOnly(xmlElement.isWriteOnly());
    }
    // handle cdata
    if (xmlElement.isSetCdata()) {
        oldProperty.setCdata(xmlElement.isCdata());
    }
    // handle null policy
    if (xmlElement.getXmlAbstractNullPolicy() != null) {
        JAXBElement<? extends XmlAbstractNullPolicy> jaxbElt = xmlElement.getXmlAbstractNullPolicy();
        oldProperty.setNullPolicy(jaxbElt.getValue());
    }
    // set user-defined properties
    if (xmlElement.getXmlProperties() != null && xmlElement.getXmlProperties().getXmlProperty().size() > 0) {
        oldProperty.setUserProperties(createUserPropertyMap(xmlElement.getXmlProperties().getXmlProperty()));
    }
    return oldProperty;
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) QName(javax.xml.namespace.QName) JAXBException(org.eclipse.persistence.exceptions.JAXBException) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)

Aggregations

XmlElementWrapper (org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper)5 QName (javax.xml.namespace.QName)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)2 XMLField (org.eclipse.persistence.oxm.XMLField)2 JAXBElement (jakarta.xml.bind.JAXBElement)1 Source (javax.xml.transform.Source)1 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)1 CoreConverter (org.eclipse.persistence.core.mappings.converters.CoreConverter)1 JAXBException (org.eclipse.persistence.exceptions.JAXBException)1 JAXBElementConverter (org.eclipse.persistence.internal.jaxb.JAXBElementConverter)1 JAXBElementRootConverter (org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter)1 XMLJavaTypeConverter (org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)1 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)1 AnyAttributeMapping (org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping)1 AnyCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping)1 AnyObjectMapping (org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping)1 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)1