use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.
the class SchemaModelGenerator method buildComplexType.
/**
* Create and return a ComplexType for a given XMLDescriptor. Assumes that the descriptor has a schema context
* set.
*/
protected ComplexType buildComplexType(boolean anonymous, Descriptor desc, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors) {
ComplexType ct = new ComplexType();
if (!anonymous) {
ct.setName(desc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()).getLocalPart());
}
CoreInheritancePolicy inheritancePolicy = desc.getInheritancePolicyOrNull();
Extension extension = null;
if (inheritancePolicy != null && inheritancePolicy.getParentClass() != null) {
extension = new Extension();
extension.setBaseType(desc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()).getLocalPart());
ComplexContent complexContent = new ComplexContent();
complexContent.setExtension(extension);
ct.setComplexContent(complexContent);
}
Sequence seq = new Sequence();
for (CoreMapping mapping : (Vector<CoreMapping>) desc.getMappings()) {
processMapping(mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors);
}
if (extension != null) {
extension.setSequence(seq);
} else {
ct.setSequence(seq);
}
return ct;
}
use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.
the class ReferenceResolver method getValue.
/**
* Add java doc if you understand this code.
*/
private Object getValue(final CoreAbstractSession session, final Reference reference, final CacheId primaryKey, final ErrorHandler handler) {
final Class<?> referenceTargetClass = reference.getTargetClass();
if (null == referenceTargetClass || referenceTargetClass == CoreClassConstants.OBJECT) {
for (Object entry : session.getDescriptors().values()) {
Object value = null;
final Descriptor targetDescriptor = (Descriptor) entry;
final List pkFields = targetDescriptor.getPrimaryKeyFields();
if (null != pkFields && 1 == pkFields.size()) {
Field pkField = (Field) pkFields.get(0);
pkField = (Field) targetDescriptor.getTypedField(pkField);
final Class<?> targetType = pkField.getType();
if (targetType == CoreClassConstants.STRING || targetType == CoreClassConstants.OBJECT) {
value = getValue(targetDescriptor.getJavaClass(), primaryKey);
} else {
try {
final Object[] pkValues = primaryKey.getPrimaryKey();
final Object[] convertedPkValues = new Object[pkValues.length];
for (int x = 0; x < pkValues.length; x++) {
convertedPkValues[x] = session.getDatasourcePlatform().getConversionManager().convertObject(pkValues[x], targetType);
}
value = getValue(targetDescriptor.getJavaClass(), new CacheId(convertedPkValues));
} catch (ConversionException ignored) {
}
}
if (null != value) {
return value;
}
}
}
if (primaryKey.getPrimaryKey()[0] != null) {
final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(Object.class.getName(), primaryKey.getPrimaryKey());
if (handler != null) {
final SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
try {
handler.warning(saxParseException);
} catch (SAXException saxException) {
throw e;
}
}
}
return null;
} else {
Object value = getValue(referenceTargetClass, primaryKey);
if (null == value) {
final CoreMapping mapping = (CoreMapping) reference.getMapping();
final CoreDescriptor targetDescriptor = mapping.getReferenceDescriptor();
if (targetDescriptor.hasInheritance()) {
final CoreInheritancePolicy inheritancePolicy = targetDescriptor.getInheritancePolicy();
final List<CoreDescriptor> childDescriptors = inheritancePolicy.getAllChildDescriptors();
for (CoreDescriptor childDescriptor : childDescriptors) {
value = getValue(childDescriptor.getJavaClass(), primaryKey);
if (null != value) {
return value;
}
}
}
}
if (value == null && (primaryKey.getPrimaryKey()[0] != null)) {
final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(referenceTargetClass.getName(), primaryKey.getPrimaryKey());
if (handler != null) {
SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
try {
handler.warning(saxParseException);
} catch (SAXException saxException) {
throw e;
}
}
}
return value;
}
}
use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.
the class UnmarshalRecordImpl method initializeRecord.
private void initializeRecord(Attributes attrs) throws SAXException {
this.setAttributes(attrs);
Descriptor xmlDescriptor = (Descriptor) treeObjectBuilder.getDescriptor();
if (!xmlDescriptor.hasInheritance() || xmlDescriptor.getInheritancePolicy().getClassIndicatorField() == null) {
initialize((ObjectBuilder) xmlDescriptor.getObjectBuilder());
initializeRecord((Mapping) null);
return;
}
CoreInheritancePolicy inheritancePolicy = xmlDescriptor.getInheritancePolicy();
Class<?> classValue = treeObjectBuilder.classFromRow(this, session);
if (classValue == null) {
// no xsi:type attribute - look for type indicator on the default root element
QName leafElementType = xmlDescriptor.getDefaultRootElementType();
// if we have a user-set type, try to get the class from the inheritance policy
if (leafElementType != null) {
XPathQName xpathQName = new XPathQName(leafElementType, isNamespaceAware());
Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
if (indicator != null) {
classValue = (Class) indicator;
}
}
}
if (classValue != null) {
xmlDescriptor = (Descriptor) session.getDescriptor(classValue);
}
initialize((ObjectBuilder) xmlDescriptor.getObjectBuilder());
initializeRecord((Mapping) null);
}
use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.
the class AbstractMarshalRecordImpl method addXsiTypeAndClassIndicatorIfRequired.
@Override
public boolean addXsiTypeAndClassIndicatorIfRequired(Descriptor descriptor, Descriptor referenceDescriptor, Field xmlField, boolean isRootElement) {
ObjectBuilder objectBuilder = (ObjectBuilder) descriptor.getObjectBuilder();
boolean xsiTypeIndicatorField = objectBuilder.isXsiTypeIndicatorField();
if (objectBuilder.addClassIndicatorFieldToRow(this)) {
return true;
}
QName leafType = null;
if (xmlField != null) {
leafType = xmlField.getLeafElementType();
XMLSchemaReference xmlRef = descriptor.getSchemaReference();
if (xmlRef != null) {
if (leafType == null) {
if (xmlRef.getType() == XMLSchemaReference.ELEMENT) {
return false;
}
if (referenceDescriptor == null) {
writeXsiTypeAttribute(descriptor, xmlRef, isRootElement);
return true;
}
} else if (((xmlRef.getType() == XMLSchemaReference.COMPLEX_TYPE) || (xmlRef.getType() == XMLSchemaReference.SIMPLE_TYPE)) && xmlRef.getSchemaContext() != null && xmlRef.isGlobalDefinition()) {
QName ctxQName = xmlRef.getSchemaContextAsQName(descriptor.getNamespaceResolver());
if (!ctxQName.equals(leafType)) {
writeXsiTypeAttribute(descriptor, xmlRef, isRootElement);
return true;
}
}
}
}
if (referenceDescriptor != null && referenceDescriptor == descriptor) {
return false;
}
if (descriptor.hasInheritance() && !descriptor.getInheritancePolicy().isRootParentDescriptor()) {
CoreInheritancePolicy inheritancePolicy = descriptor.getInheritancePolicy();
Field indicatorField = (Field) inheritancePolicy.getClassIndicatorField();
if (indicatorField != null && xsiTypeIndicatorField) {
Object classIndicatorValueObject = inheritancePolicy.getClassIndicatorMapping().get(descriptor.getJavaClass());
String classIndicatorUri = null;
String classIndicatorLocal = null;
String classIndicatorPrefix = null;
if (classIndicatorValueObject instanceof QName) {
QName classIndicatorQName = (QName) classIndicatorValueObject;
classIndicatorUri = classIndicatorQName.getNamespaceURI();
classIndicatorLocal = classIndicatorQName.getLocalPart();
classIndicatorPrefix = classIndicatorQName.getPrefix();
} else {
String classIndicatorValue = (String) inheritancePolicy.getClassIndicatorMapping().get(descriptor.getJavaClass());
int nsindex = classIndicatorValue.indexOf(Constants.COLON);
String prefix = null;
if (nsindex != -1) {
classIndicatorLocal = classIndicatorValue.substring(nsindex + 1);
prefix = classIndicatorValue.substring(0, nsindex);
} else {
classIndicatorLocal = classIndicatorValue;
}
classIndicatorUri = descriptor.getNonNullNamespaceResolver().resolveNamespacePrefix(prefix);
}
if (leafType == null || isRootElement && marshaller.isApplicationJSON() && !marshaller.isIncludeRoot() || !(leafType.getLocalPart().equals(classIndicatorLocal)) || (classIndicatorUri == null && (leafType.getNamespaceURI() != null && leafType.getNamespaceURI().length() > 0)) || (classIndicatorUri != null && !classIndicatorUri.equals(leafType.getNamespaceURI()))) {
if (inheritancePolicy.hasClassExtractor()) {
objectBuilder.addClassIndicatorFieldToRow(this);
} else {
writeXsiTypeAttribute(descriptor, classIndicatorUri, classIndicatorLocal, classIndicatorPrefix, isRootElement);
}
return true;
}
return false;
}
}
return false;
}
Aggregations