Search in sources :

Example 21 with XMLDescriptor

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

the class MappingsGenerator method setupAttributeGroups.

private void setupAttributeGroups(JavaClass javaClass) {
    TypeInfo info = this.typeInfo.get(javaClass.getQualifiedName());
    XMLDescriptor descriptor = (XMLDescriptor) info.getDescriptor();
    if (!info.getObjectGraphs().isEmpty()) {
        for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
            AttributeGroup group = descriptor.getAttributeGroup(next.getName());
            Map<String, List<CoreAttributeGroup>> subgraphs = processSubgraphs(next.getXmlNamedSubgraph());
            for (XmlNamedAttributeNode nextAttributeNode : next.getXmlNamedAttributeNode()) {
                if (nextAttributeNode.getSubgraph() == null || nextAttributeNode.getSubgraph().length() == 0) {
                    group.addAttribute(nextAttributeNode.getName());
                } else {
                    List<CoreAttributeGroup> nestedGroups = subgraphs.get(nextAttributeNode.getSubgraph());
                    if (nestedGroups == null || nestedGroups.isEmpty()) {
                        Property property = info.getProperties().get(nextAttributeNode.getName());
                        if (property == null) {
                            // if there's no property associated with the attributeNode, just ignore it
                            continue;
                        }
                        JavaClass cls = property.getActualType();
                        TypeInfo referenceType = typeInfo.get(cls.getQualifiedName());
                        if (referenceType != null) {
                            AttributeGroup targetGroup = (AttributeGroup) referenceType.getDescriptor().getAttributeGroup(nextAttributeNode.getSubgraph());
                            group.addAttribute(nextAttributeNode.getName(), targetGroup);
                        } else {
                        // TODO: Exception
                        }
                    } else {
                        if (nestedGroups.size() == 1) {
                            group.addAttribute(nextAttributeNode.getName(), nestedGroups.get(0));
                        } else {
                            group.addAttribute(nextAttributeNode.getName(), nestedGroups);
                        }
                    }
                }
            }
            for (XmlNamedSubgraph nextSubclass : next.getXmlNamedSubclassGraph()) {
                AttributeGroup subclassGroup = new AttributeGroup(next.getName(), nextSubclass.getType(), true);
                group.getSubClassGroups().put(nextSubclass.getType(), subclassGroup);
                for (XmlNamedAttributeNode nextAttributeNode : nextSubclass.getXmlNamedAttributeNode()) {
                    if (nextAttributeNode.getSubgraph() == null || nextAttributeNode.getSubgraph().length() == 0) {
                        subclassGroup.addAttribute(nextAttributeNode.getName());
                    } else {
                        List<CoreAttributeGroup> nestedGroups = subgraphs.get(nextAttributeNode.getSubgraph());
                        if (nestedGroups == null || nestedGroups.isEmpty()) {
                            Property property = info.getProperties().get(nextAttributeNode.getName());
                            JavaClass cls = property.getActualType();
                            TypeInfo referenceType = typeInfo.get(cls.getQualifiedName());
                            if (referenceType != null) {
                                AttributeGroup targetGroup = (AttributeGroup) referenceType.getDescriptor().getAttributeGroup(nextAttributeNode.getSubgraph());
                                subclassGroup.addAttribute(nextAttributeNode.getName(), targetGroup);
                            } else {
                            // TODO: Exception
                            }
                        } else {
                            if (nestedGroups.size() == 1) {
                                subclassGroup.addAttribute(nextAttributeNode.getName(), nestedGroups.get(0));
                            } else {
                                subclassGroup.addAttribute(nextAttributeNode.getName(), nestedGroups);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) XmlNamedObjectGraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedObjectGraph) XmlNamedAttributeNode(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedAttributeNode) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XmlNamedSubgraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedSubgraph) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 22 with XMLDescriptor

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

the class MappingsGenerator method generateDescriptor.

public void generateDescriptor(JavaClass javaClass, CoreProject project) {
    String jClassName = javaClass.getQualifiedName();
    TypeInfo info = typeInfo.get(jClassName);
    if (info.isTransient()) {
        return;
    }
    NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
    String packageNamespace = namespaceInfo.getNamespace();
    String elementName;
    String namespace;
    if (javaClass.getSuperclass() != null && javaClass.getSuperclass().getName().equals("jakarta.xml.bind.JAXBElement")) {
        generateDescriptorForJAXBElementSubclass(javaClass, project, getNamespaceResolverForDescriptor(namespaceInfo));
        return;
    }
    Descriptor descriptor = new XMLDescriptor();
    org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement rootElem = info.getXmlRootElement();
    if (rootElem == null) {
        try {
            elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
        } catch (Exception ex) {
            throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
        }
        namespace = packageNamespace;
    } else {
        elementName = rootElem.getName();
        if (elementName.equals(XMLProcessor.DEFAULT)) {
            try {
                elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
            } catch (Exception ex) {
                throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
            }
        }
        namespace = rootElem.getNamespace();
    }
    descriptor.setJavaClassName(jClassName);
    if (info.getFactoryMethodName() != null) {
        descriptor.getInstantiationPolicy().useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
    }
    if (namespace.equals(XMLProcessor.DEFAULT)) {
        namespace = namespaceInfo.getNamespace();
    }
    NamespaceResolver resolverForDescriptor = getNamespaceResolverForDescriptor(namespaceInfo);
    JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
    if (!manyValueJavaClass.isAssignableFrom(javaClass)) {
        if (isDefaultNamespaceAllowed && namespace.length() != 0 && globalNamespaceResolver.getDefaultNamespaceURI() == null && !resolverForDescriptor.getPrefixesToNamespaces().containsValue(namespace)) {
            globalNamespaceResolver.setDefaultNamespaceURI(namespace);
            resolverForDescriptor.setDefaultNamespaceURI(namespace);
        }
        if (rootElem == null) {
            descriptor.setDefaultRootElement("");
        } else {
            if (namespace.length() == 0) {
                descriptor.setDefaultRootElement(elementName);
            } else {
                descriptor.setDefaultRootElement(getQualifiedString(getPrefixForNamespace(namespace, resolverForDescriptor), elementName));
            }
        }
    }
    descriptor.setNamespaceResolver(resolverForDescriptor);
    setSchemaContext(descriptor, info);
    // set the ClassExtractor class name if necessary
    if (info.isSetClassExtractorName()) {
        descriptor.getInheritancePolicy().setClassExtractorName(info.getClassExtractorName());
    }
    // set any user-defined properties
    if (info.getUserProperties() != null) {
        descriptor.setProperties(info.getUserProperties());
    }
    if (info.isLocationAware()) {
        Property locProp = null;
        Iterator<Property> i = info.getPropertyList().iterator();
        while (i.hasNext()) {
            Property p = i.next();
            if (p.getType().getName().equals(Constants.LOCATOR_CLASS_NAME)) {
                locProp = p;
            }
        }
        if (locProp != null && locProp.isTransient()) {
            // don't make a mapping
            if (locProp.isMethodProperty()) {
                MethodAttributeAccessor aa = new MethodAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                aa.setSetMethodName(locProp.getSetMethodName());
                aa.setGetMethodName(locProp.getGetMethodName());
                descriptor.setLocationAccessor(aa);
            } else {
                // instance variable property
                InstanceVariableAttributeAccessor aa = new InstanceVariableAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                descriptor.setLocationAccessor(aa);
            }
        }
    }
    if (!info.getObjectGraphs().isEmpty()) {
        // these will be populated later to allow for linking
        for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
            AttributeGroup attributeGroup = new AttributeGroup(next.getName(), info.getJavaClassName(), false);
            ((XMLDescriptor) descriptor).addAttributeGroup(attributeGroup);
        // process subclass graphs for inheritance
        // for(NamedSubgraph nextSubclass:next.getNamedSubclassGraph()) {
        // attributeGroup.insertSubClass(new AttributeGroup(next.getName(), nextSubclass.getType()));
        // }
        }
    }
    project.addDescriptor((CoreDescriptor) descriptor);
    info.setDescriptor(descriptor);
}
Also used : InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XmlNamedObjectGraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedObjectGraph) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) 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) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Example 23 with XMLDescriptor

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

the class MultipleMappingPerFieldTestCases method testMappings.

/**
 * We expect two mappings for 'currencyPairCode'.  We will verify mapping count,
 * user-set properties, etc.
 *
 * Positive test.
 */
public void testMappings() {
    XMLDescriptor xDesc = ((org.eclipse.persistence.jaxb.JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("QuoteRequest"));
    assertNotNull("No descriptor was generated for CustomQuoteRequest.", xDesc);
    int currencyPairCodeCount = 0;
    int dateCount = 0;
    Vector<DatabaseMapping> mappings = xDesc.getMappings();
    for (int i = 0; i < mappings.size(); i++) {
        DatabaseMapping mapping = mappings.get(i);
        if (mapping.getAttributeName().equals("currencyPairCode")) {
            currencyPairCodeCount++;
            // check user-set properties
            Map props = mapping.getProperties();
            assertNotNull("No user-defined properties exist on the mapping for [currencyPairCode]", props);
            assertTrue("Expected [2] user-defined properties, but there were [" + props.size() + "]", props.size() == 2);
            if (mapping.getField().getName().equals("QuoteReq/Instrmt/@Sym")) {
                // verify value-types
                assertTrue("Expected value-type [String] for key [1] but was [" + props.get("1").getClass().getName() + "]", props.get("1") instanceof String);
                assertTrue("Expected value-type [Integer] for key [2] but was [" + props.get("2").getClass().getName() + "]", props.get("2") instanceof Integer);
                // verify values
                assertTrue("Expected property value [A] for key [" + 1 + "] but was [" + props.get("1") + "]", "A".equals(props.get("1")));
                assertTrue("Expected property value [66] for key [" + 2 + "] but was [" + props.get("2") + "]", 66 == (Integer) props.get("2"));
            } else {
                // assume "QuoteReq/Leg/@Sym"
                // verify value-types
                assertTrue("Expected value-type [String] for key [2] but was [" + props.get("1").getClass().getName() + "]", props.get("1") instanceof String);
                assertTrue("Expected value-type [Double] for key [1] but was [" + props.get("2").getClass().getName() + "]", props.get("2") instanceof Double);
                // verify values
                assertTrue("Expected property value [B] for key [" + 1 + "] but was [" + props.get("1") + "]", "B".equals(props.get("1")));
                assertTrue("Expected property value [9.9] for key [" + 2 + "] but was [" + props.get("2") + "]", 9.9 == (Double) props.get("2"));
            }
        } else if (mapping.getAttributeName().equals("date")) {
            dateCount++;
        }
    }
    assertTrue("Expected [2] mappings for attribute [currencyPairCode], but was [" + currencyPairCodeCount + "]", currencyPairCodeCount == 2);
    assertTrue("Expected [3] mappings for attribute [date], but was [" + dateCount + "]", dateCount == 3);
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) QName(javax.xml.namespace.QName) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with XMLDescriptor

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

the class XmlElementTestCases method testContainerType.

/*
    public void testTeamWithListOfPlayersSchemaGen() {
        String metadataFile = PATH + "team-oxm.xml";

        InputStream iStream = loader.getResourceAsStream(metadataFile);
        if (iStream == null) {
            fail("Couldn't load metadata file [" + metadataFile + "]");
        }
        HashMap<String, Source> metadataSourceMap = new HashMap<String, Source>();
        metadataSourceMap.put(CONTEXT_PATH, new StreamSource(iStream));
        Map<String, Map<String, Source>> properties = new HashMap<String, Map<String, Source>>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);

        try {
            JAXBContext jContext = (JAXBContext) JAXBContextFactory.createContext(new Class[] { Team.class }, properties, loader);
            MySchemaOutputResolver oResolver = new MySchemaOutputResolver();
            jContext.generateSchema(oResolver);
            // validate schema
            String controlSchema = PATH + "team-schema.xsd";
            compareSchemas(oResolver.schemaFiles.get(EMPTY_NAMESPACE), new File(controlSchema));
        } catch (JAXBException e) {
            fail("An exception occurred creating the JAXBContext: " + e.getMessage());
        }
    }
    */
/**
 * Test setting the container class via container-type attribute.
 *
 * Positive test.
 */
public void testContainerType() {
    XMLDescriptor xDesc = ((JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("employee"));
    assertNotNull("No descriptor was generated for Employee.", xDesc);
    DatabaseMapping mapping = xDesc.getMappingForAttributeName("myEmployees");
    assertNotNull("No mapping exists on Employee for attribute [myEmployees].", mapping);
    assertTrue("Expected an XMLCompositeCollectionMapping for attribute [myEmployees], but was [" + mapping.toString() + "].", mapping instanceof XMLCompositeCollectionMapping);
    assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) QName(javax.xml.namespace.QName) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 25 with XMLDescriptor

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

the class XmlJoinNodeTestCases method testContainerType.

public void testContainerType() {
    JAXBContext jCtx = null;
    try {
        InputStream inputStream = ClassLoader.getSystemResourceAsStream(OXM_DOC_V2);
        HashMap<String, Source> metadataSourceMap = new HashMap<>();
        metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.xmljoinnode", new StreamSource(inputStream));
        Map<String, Object> invalidProperties = new HashMap<>();
        invalidProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
        jCtx = (JAXBContext) JAXBContextFactory.createContext(new Class<?>[] { Company.class }, invalidProperties);
    } catch (JAXBException e) {
        e.printStackTrace();
        fail("An exception occurred while creating the JAXBContext.");
    }
    XMLDescriptor xDesc = jCtx.getXMLContext().getDescriptor(new QName("company"));
    assertNotNull("No descriptor was generated for Company.", xDesc);
    DatabaseMapping mapping = xDesc.getMappingForAttributeName("employees");
    assertNotNull("No mapping exists on Customer for attribute [employees].", mapping);
    assertTrue("Expected an XMLCollectionReferenceMapping for attribute [employees], but was [" + mapping.toString() + "].", mapping instanceof XMLCollectionReferenceMapping);
    assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor)

Aggregations

XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1206 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)629 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)198 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)151 XMLField (org.eclipse.persistence.oxm.XMLField)143 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)141 QName (javax.xml.namespace.QName)75 XMLSchemaClassPathReference (org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)69 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)49 ArrayList (java.util.ArrayList)37 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)37 XMLSchemaURLReference (org.eclipse.persistence.oxm.schema.XMLSchemaURLReference)36 XMLRoot (org.eclipse.persistence.oxm.XMLRoot)34 XMLCollectionReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)32 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)32 Document (org.w3c.dom.Document)31 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)30 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)29 URL (java.net.URL)27 ObjectTypeConverter (org.eclipse.persistence.mappings.converters.ObjectTypeConverter)27