Search in sources :

Example 11 with XMLField

use of org.eclipse.persistence.oxm.XMLField 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 12 with XMLField

use of org.eclipse.persistence.oxm.XMLField in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method setupInheritance.

/**
 * Setup inheritance for abstract superclass.
 *
 * NOTE: We currently only handle one level of inheritance in this case.
 * For multiple levels the code will need to be modified. The logic in
 * generateMappings() that determines when to copy down inherited
 * methods from the parent class will need to be changed as well.
 */
private void setupInheritance(JavaClass jClass) {
    TypeInfo tInfo = typeInfo.get(jClass.getName());
    Descriptor descriptor = tInfo.getDescriptor();
    if (descriptor == null) {
        return;
    }
    JavaClass superClass = CompilerHelper.getNextMappedSuperClass(jClass, typeInfo, helper);
    if (superClass == null) {
        return;
    }
    TypeInfo superTypeInfo = typeInfo.get(superClass.getName());
    if (superTypeInfo == null) {
        return;
    }
    Descriptor superDescriptor = superTypeInfo.getDescriptor();
    if (superDescriptor != null) {
        XMLSchemaReference sRef = descriptor.getSchemaReference();
        if (sRef == null || sRef.getSchemaContext() == null) {
            return;
        }
        JavaClass rootMappedSuperClass = getRootMappedSuperClass(superClass);
        TypeInfo rootTypeInfo = typeInfo.get(rootMappedSuperClass.getName());
        Descriptor rootDescriptor = rootTypeInfo.getDescriptor();
        if (rootDescriptor.getNamespaceResolver() == null) {
            rootDescriptor.setNamespaceResolver(new org.eclipse.persistence.oxm.NamespaceResolver());
        }
        if (rootDescriptor.getInheritancePolicy().getClassIndicatorField() == null) {
            Field classIndicatorField;
            if (rootTypeInfo.isSetXmlDiscriminatorNode()) {
                classIndicatorField = new XMLField(rootTypeInfo.getXmlDiscriminatorNode());
            } else {
                classIndicatorField = XMLConstants.DEFAULT_XML_TYPE_ATTRIBUTE;
                classIndicatorField.getXPathFragment().setNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
            }
            rootDescriptor.getInheritancePolicy().setClassIndicatorField(classIndicatorField);
        }
        Object sCtx = null;
        // TypeInfo tInfo = typeInfo.get(jClass.getName());
        if (tInfo.isSetXmlDiscriminatorValue()) {
            sCtx = tInfo.getXmlDiscriminatorValue();
        } else if (!tInfo.isAnonymousComplexType()) {
            sCtx = sRef.getSchemaContextAsQName();
        }
        if (sCtx != null) {
            descriptor.getInheritancePolicy().setParentClassName(superClass.getName());
            rootDescriptor.getInheritancePolicy().addClassNameIndicator(jClass.getName(), sCtx);
        }
        Object value = rootDescriptor.getInheritancePolicy().getClassNameIndicatorMapping().get(rootDescriptor.getJavaClassName());
        if (value == null) {
            if (rootTypeInfo.isSetXmlDiscriminatorValue()) {
                rootDescriptor.getInheritancePolicy().addClassNameIndicator(rootDescriptor.getJavaClassName(), rootTypeInfo.getXmlDiscriminatorValue());
            } else {
                XMLSchemaReference rootSRef = rootDescriptor.getSchemaReference();
                if (rootSRef != null && rootSRef.getSchemaContext() != null) {
                    QName rootSCtx = rootSRef.getSchemaContextAsQName();
                    rootDescriptor.getInheritancePolicy().addClassNameIndicator(rootDescriptor.getJavaClassName(), rootSCtx);
                }
            }
        }
        rootDescriptor.getInheritancePolicy().setShouldReadSubclasses(true);
        // Check for attributeGroups
        Map<String, AttributeGroup> childGroups = ((XMLDescriptor) descriptor).getAttributeGroups();
        Map<String, AttributeGroup> parentGroups = ((XMLDescriptor) rootDescriptor).getAttributeGroups();
        if (childGroups != null && !(childGroups.isEmpty()) && parentGroups != null && !(parentGroups.isEmpty())) {
            for (String nextKey : childGroups.keySet()) {
                AttributeGroup parentGroup = parentGroups.get(nextKey);
                if (parentGroup != null) {
                    AttributeGroup childGroup = childGroups.get(nextKey);
                    parentGroup.getSubClassGroups().put(descriptor.getJavaClassName(), childGroup);
                }
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) 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) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor)

Example 13 with XMLField

use of org.eclipse.persistence.oxm.XMLField in project eclipselink by eclipse-ee4j.

the class Property method isPositional.

/**
 * Indicates if this property is mapped by position, i.e. 'name="data[1]"',
 * or is mapped by attribute value (predicate mapping), i.e.
 * 'personal-info[@pi-type='last-name']/name[@name-type='surname']/text()'
 */
public boolean isPositional() {
    if (getXmlPath() == null) {
        return false;
    }
    Field<XMLConversionManager, NamespaceResolver> field = new XMLField(getXmlPath());
    XPathFragment frag = field.getXPathFragment();
    // loop until we have the last non-null, non-attribute, non-text fragment
    while (true) {
        if (frag.getNextFragment() != null && !frag.getNextFragment().isAttribute() && !frag.getNextFragment().nameIsText()) {
            frag = frag.getNextFragment();
        } else {
            break;
        }
    }
    return frag.containsIndex() || frag.getPredicate() != null;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 14 with XMLField

use of org.eclipse.persistence.oxm.XMLField in project eclipselink by eclipse-ee4j.

the class SchemaGenerator method addXPathToSchema.

/**
 * Process a given XmlPath, and create the required schema components.  The last
 * fragment may not be processed; in this case the returned XmlPathResult is
 * used to set the current schema and parent complex type, then the owning
 * property is processed as per usual. Note the last fragment may be processed
 * if it has a namespace or is an 'any'.
 *
 * @param property the property containing the XmlPath for which schema components are to be built
 * @param compositor the sequence/choice/all to modify
 * @param schema the schema being built when this method is called - this could
 *        if the XmlPath contains an entry that references a different namespace
 * @param isChoice indicates if the given property is a choice property
 * @param type the ComplexType which compositor(s) should be added to
 * @return AddToSchemaResult containing current schema and sequence/all/choice build
 *         based on the given XmlPath
 */
private AddToSchemaResult addXPathToSchema(Property property, TypeDefParticle compositor, Schema schema, boolean isChoice, ComplexType type) {
    // '.' xml-path requires special handling
    if (property.getXmlPath().equals(DOT)) {
        TypeInfo info = typeInfo.get(property.getActualType().getQualifiedName());
        JavaClass infoClass = property.getActualType();
        addSelfProperties(infoClass, info, compositor, type);
        return null;
    }
    // create the XPathFragment(s) for the path
    Field<XMLConversionManager, NamespaceResolver> xfld = new XMLField(property.getXmlPath());
    xfld.setNamespaceResolver(schema.getNamespaceResolver());
    xfld.initialize();
    // build the schema components for the xml-path
    return buildSchemaComponentsForXPath(xfld.getXPathFragment(), new AddToSchemaResult(compositor, schema), isChoice, property);
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 15 with XMLField

use of org.eclipse.persistence.oxm.XMLField in project eclipselink by eclipse-ee4j.

the class AnnotationsProcessor method processChoiceProperty.

/**
 * Complete creation of a 'choice' property. Here, a Property is created for
 * each XmlElement in the XmlElements list. Validation is performed as well.
 * Each created Property is added to the owning Property's list of choice
 * properties.
 */
private void processChoiceProperty(Property choiceProperty, TypeInfo info, JavaClass cls, JavaClass propertyType) {
    String propertyName = choiceProperty.getPropertyName();
    // validate XmlElementsXmlJoinNodes (if set)
    if (choiceProperty.isSetXmlJoinNodesList()) {
        // there must be one XmlJoinNodes entry per XmlElement
        if (choiceProperty.getXmlElements().getXmlElement().size() != choiceProperty.getXmlJoinNodesList().size()) {
            throw JAXBException.incorrectNumberOfXmlJoinNodesOnXmlElements(propertyName, cls.getQualifiedName());
        }
    }
    XmlPath[] paths = null;
    if (helper.isAnnotationPresent(choiceProperty.getElement(), XmlPaths.class)) {
        XmlPaths pathAnnotation = (XmlPaths) helper.getAnnotation(choiceProperty.getElement(), XmlPaths.class);
        paths = pathAnnotation.value();
    }
    List<Property> choiceProperties = new ArrayList<>();
    for (int i = 0; i < choiceProperty.getXmlElements().getXmlElement().size(); i++) {
        org.eclipse.persistence.jaxb.xmlmodel.XmlElement next = choiceProperty.getXmlElements().getXmlElement().get(i);
        Property choiceProp = new Property(helper);
        String name;
        String namespace;
        choiceProp.setNillable(next.isNillable());
        choiceProp.setIsRequired(next.isRequired());
        // handle XmlPath - if xml-path is set, we ignore name/namespace
        if (paths != null && next.getXmlPath() == null) {
            // Only set the path, if the path hasn't already been set from
            // xml
            XmlPath nextPath = paths[i];
            next.setXmlPath(nextPath.value());
        }
        if (next.getXmlPath() != null) {
            choiceProp.setXmlPath(next.getXmlPath());
            boolean isAttribute = new XMLField(next.getXmlPath()).getLastXPathFragment().isAttribute();
            // validate attribute - must be in nested path, not at root
            if (isAttribute && !next.getXmlPath().contains(SLASH)) {
                throw JAXBException.invalidXmlPathWithAttribute(propertyName, cls.getQualifiedName(), next.getXmlPath());
            }
            choiceProp.setIsAttribute(isAttribute);
            name = XMLProcessor.getNameFromXPath(next.getXmlPath(), propertyName, isAttribute);
            namespace = XMLProcessor.DEFAULT;
        } else {
            // no xml-path, so use name/namespace from xml-element
            name = next.getName();
            namespace = next.getNamespace();
        }
        if (name == null || name.equals(XMLProcessor.DEFAULT)) {
            if (next.getJavaAttribute() != null) {
                name = next.getJavaAttribute();
            } else {
                name = propertyName;
            }
        }
        // xml-element in the list must have an xml-id property
        if (choiceProperty.isXmlIdRef()) {
            JavaClass nextCls = helper.getJavaClass(next.getType());
            processReferencedClass(nextCls);
            TypeInfo tInfo = typeInfos.get(next.getType());
            if (tInfo == null || (!tInfo.isIDSet() && !preCheckXmlID(nextCls, tInfo))) {
                throw JAXBException.invalidXmlElementInXmlElementsList(propertyName, name);
            }
        }
        QName qName = null;
        if (!namespace.equals(XMLProcessor.DEFAULT)) {
            qName = new QName(namespace, name);
        } else {
            NamespaceInfo namespaceInfo = getPackageInfoForPackage(cls).getNamespaceInfo();
            if (namespaceInfo.isElementFormQualified()) {
                qName = new QName(namespaceInfo.getNamespace(), name);
            } else {
                qName = new QName(name);
            }
        }
        choiceProp.setPropertyName(name);
        // "XmlElement$DEFAULT"
        if (next.getType().equals("jakarta.xml.bind.annotation.XmlElement.DEFAULT") || next.getType().equals("jakarta.xml.bind.annotation.XmlElement$DEFAULT")) {
            choiceProp.setType(propertyType);
        } else {
            choiceProp.setType(helper.getJavaClass(next.getType()));
        }
        // handle case of XmlJoinNodes w/XmlElements
        if (choiceProperty.isSetXmlJoinNodesList()) {
            // assumes one corresponding xml-join-nodes entry per
            // xml-element
            org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes xmlJoinNodes = choiceProperty.getXmlJoinNodesList().get(i);
            if (xmlJoinNodes != null) {
                choiceProp.setXmlJoinNodes(xmlJoinNodes);
                // set type
                if (!xmlJoinNodes.getType().equals(XMLProcessor.DEFAULT)) {
                    JavaClass pType = helper.getJavaClass(xmlJoinNodes.getType());
                    if (helper.isCollectionType(choiceProp.getType())) {
                        choiceProp.setGenericType(pType);
                    } else {
                        choiceProp.setType(pType);
                    }
                }
            }
        }
        choiceProp.setSchemaName(qName);
        choiceProp.setSchemaType(getSchemaTypeFor(choiceProp.getType()));
        choiceProp.setIsXmlIdRef(choiceProperty.isXmlIdRef());
        choiceProp.setXmlElementWrapper(choiceProperty.getXmlElementWrapper());
        choiceProperties.add(choiceProp);
        processReferencedClass(choiceProp.getType());
        TypeInfo newInfo = typeInfos.get(choiceProp.getType().getQualifiedName());
        if (newInfo != null && newInfo.isTransient()) {
            throw JAXBException.invalidReferenceToTransientClass(info.getJavaClassName(), choiceProperty.getPropertyName(), newInfo.getJavaClassName());
        }
    }
    choiceProperty.setChoiceProperties(choiceProperties);
}
Also used : XmlPath(org.eclipse.persistence.oxm.annotations.XmlPath) XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlPaths(org.eclipse.persistence.oxm.annotations.XmlPaths) XmlProperty(org.eclipse.persistence.oxm.annotations.XmlProperty)

Aggregations

XMLField (org.eclipse.persistence.oxm.XMLField)302 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)141 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)99 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)48 QName (javax.xml.namespace.QName)43 Element (org.w3c.dom.Element)41 NodeList (org.w3c.dom.NodeList)39 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)36 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)30 ArrayList (java.util.ArrayList)27 Node (org.w3c.dom.Node)25 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)21 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)17 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)17 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)15 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)15 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)15 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)14 Field (org.eclipse.persistence.internal.oxm.mappings.Field)13