use of org.eclipse.persistence.internal.oxm.mappings.Descriptor 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);
}
}
}
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor 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);
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappings.
public void generateMappings() {
Iterator<String> javaClasses = this.typeInfo.keySet().iterator();
while (javaClasses.hasNext()) {
String next = javaClasses.next();
JavaClass javaClass = helper.getJavaClass(next);
TypeInfo info = this.typeInfo.get(next);
if (info.isEnumerationType()) {
continue;
}
NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
Descriptor descriptor = info.getDescriptor();
if (descriptor != null) {
generateMappings(info, descriptor, javaClass, namespaceInfo);
// set primary key fields (if necessary)
CoreMapping mapping;
// handle XmlID
if (info.isIDSet()) {
mapping = descriptor.getMappingForAttributeName(info.getIDProperty().getPropertyName());
if (mapping != null) {
descriptor.addPrimaryKeyField(mapping.getField());
}
}
// handle XmlKey
if (info.hasXmlKeyProperties()) {
for (Property keyProp : info.getXmlKeyProperties()) {
mapping = descriptor.getMappingForAttributeName(keyProp.getPropertyName());
if (mapping != null) {
descriptor.addPrimaryKeyField(mapping.getField());
}
}
}
}
info.postInitialize();
if (descriptor != null) {
logMappingGeneration(descriptor);
}
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class SchemaModelGenerator method generateSchemas.
/**
* Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
* The descriptors are assumed to have been initialized. One Schema object will be generated
* per namespace.
*
* @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
* @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
* @return a map of namespaces to EclipseLink schema model Schema objects
* @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
* @see Schema
*/
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties) throws DescriptorException {
HashMap<String, Schema> schemaForNamespace = new HashMap<>();
Schema workingSchema = null;
if (properties == null) {
properties = new SchemaModelGeneratorProperties();
}
// set up schemas for the descriptors
for (Descriptor desc : descriptorsToProcess) {
String namespace;
XMLSchemaReference schemaRef = desc.getSchemaReference();
if (schemaRef != null) {
namespace = schemaRef.getSchemaContextAsQName(desc.getNamespaceResolver()).getNamespaceURI();
workingSchema = getSchema(namespace, desc.getNamespaceResolver(), schemaForNamespace, properties);
addNamespacesToWorkingSchema(desc.getNamespaceResolver(), workingSchema);
} else {
// default root element set we will need to generate a global element for it
for (DatabaseTable table : (Vector<DatabaseTable>) desc.getTables()) {
namespace = getDefaultRootElementAsQName(desc, table.getName()).getNamespaceURI();
workingSchema = getSchema(namespace, desc.getNamespaceResolver(), schemaForNamespace, properties);
addNamespacesToWorkingSchema(desc.getNamespaceResolver(), workingSchema);
}
}
}
// process the descriptors
for (Descriptor xdesc : descriptorsToProcess) {
processDescriptor(xdesc, schemaForNamespace, workingSchema, properties, descriptorsToProcess);
}
// return the generated schema(s)
return schemaForNamespace;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class SchemaModelGenerator method processXMLObjectReferenceMapping.
/**
* Process a given XMLObjectReferenceMapping. In the case of an XMLCollectionReferenceMapping,
* i.e. the isCollection flag is set to true, maxOccurs will be set to 'unbounded' on any
* source elements
*/
protected void processXMLObjectReferenceMapping(ObjectReferenceMapping mapping, Sequence seq, ComplexType ct, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors, boolean isCollection) {
String tgtClassName = mapping.getReferenceClassName();
Descriptor tgtDesc = getDescriptorByName(tgtClassName, descriptors);
if (tgtDesc == null) {
throw DescriptorException.descriptorIsMissing(tgtClassName, (DatabaseMapping) mapping);
}
// get the target mapping(s) to determine the appropriate type(s)
String schemaTypeString = null;
Map<Field, Field> associations = mapping.getSourceToTargetKeyFieldAssociations();
for (Entry<Field, Field> entry : associations.entrySet()) {
Field tgtField = entry.getValue();
Vector mappings = tgtDesc.getMappings();
// schemaTypeString = Constants.SCHEMA_PREFIX + COLON + IDREF;
for (Enumeration mappingsNum = mappings.elements(); mappingsNum.hasMoreElements(); ) {
Mapping nextMapping = (Mapping) mappingsNum.nextElement();
if (nextMapping.getField() != null && nextMapping.getField() instanceof Field) {
Field xFld = (Field) nextMapping.getField();
if (xFld == tgtField) {
schemaTypeString = getSchemaTypeForElement(tgtField, nextMapping.getAttributeClassification(), workingSchema);
}
}
}
if (schemaTypeString == null) {
schemaTypeString = getSchemaTypeString(Constants.STRING_QNAME, workingSchema);
}
XPathFragment frag = entry.getKey().getXPathFragment();
if (frag.isAttribute()) {
Attribute attr = buildAttribute(frag, schemaTypeString);
ct.getOrderedAttributes().add(attr);
} else {
Element elem = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
if (isCollection) {
elem.setMaxOccurs(Occurs.UNBOUNDED);
}
seq.addElement(elem);
}
}
}
Aggregations