use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class SAXUnmarshaller method unmarshal.
public Object unmarshal(InputSource inputSource, Class<?> clazz, XMLReader xmlReader) {
if (inputSource != null && null == inputSource.getSystemId()) {
inputSource.setSystemId(this.systemId);
}
UnmarshalRecord unmarshalRecord = null;
Descriptor xmlDescriptor = null;
// for XMLObjectReferenceMappings we need a non-shared cache, so
// try and get a Unit Of Work from the XMLContext
CoreAbstractSession session = null;
// check for case where the reference class is a primitive wrapper - in this case, we
// need to use the conversion manager to convert the node's value to the primitive
// wrapper class, then create, populate and return an XMLRoot. This will be done
// via XMLRootRecord.
boolean isPrimitiveWrapper = false;
if (clazz == CoreClassConstants.OBJECT) {
try {
SAXUnmarshallerHandler saxUnmarshallerHandler = new SAXUnmarshallerHandler(xmlUnmarshaller.getContext());
saxUnmarshallerHandler.setXMLReader(xmlReader);
saxUnmarshallerHandler.setUnmarshaller(xmlUnmarshaller);
saxUnmarshallerHandler.setKeepAsElementPolicy(KEEP_UNKNOWN_AS_ELEMENT);
setContentHandler(xmlReader, saxUnmarshallerHandler);
xmlReader.parse(inputSource);
// resolve any mapping references
saxUnmarshallerHandler.resolveReferences();
return saxUnmarshallerHandler.getObject();
} catch (IOException e) {
throw XMLMarshalException.unmarshalException(e);
} catch (SAXException e) {
throw convertSAXException(e);
}
} else {
// try and get a Unit Of Work from the XMLContext
try {
session = xmlUnmarshaller.getContext().getSession(clazz);
xmlDescriptor = (Descriptor) session.getDescriptor(clazz);
unmarshalRecord = xmlUnmarshaller.createUnmarshalRecord(xmlDescriptor, session);
} catch (XMLMarshalException xme) {
if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
isPrimitiveWrapper = isPrimitiveWrapper(clazz);
if (isPrimitiveWrapper) {
unmarshalRecord = xmlUnmarshaller.createRootUnmarshalRecord(clazz);
} else {
throw xme;
}
} else {
throw xme;
}
}
}
try {
unmarshalRecord.setXMLReader(xmlReader);
unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
setContentHandler(xmlReader, unmarshalRecord);
xmlReader.setLexicalHandler(unmarshalRecord);
xmlReader.parse(inputSource);
} catch (IOException e) {
throw XMLMarshalException.unmarshalException(e);
} catch (SAXException e) {
throw convertSAXException(e);
} finally {
xmlUnmarshaller.getStringBuffer().reset();
}
// resolve mapping references
unmarshalRecord.resolveReferences(session, xmlUnmarshaller.getIDResolver());
if (isPrimitiveWrapper) {
return unmarshalRecord.getCurrentObject();
}
return xmlDescriptor.wrapObjectInXMLRoot(unmarshalRecord, this.isResultAlwaysXMLRoot);
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XMLObjectBuilder method buildIntoNestedRow.
public AbstractRecord buildIntoNestedRow(AbstractRecord row, Object originalObject, Object object, AbstractSession session, Descriptor refDesc, Field xmlField, boolean wasXMLRoot) {
// PERF: Avoid synchronized enumerator as is concurrency bottleneck.
XMLRecord record = (XMLRecord) row;
record.setSession(session);
XMLMarshaller marshaller = record.getMarshaller();
if ((marshaller != null) && (marshaller.getMarshalListener() != null)) {
marshaller.getMarshalListener().beforeMarshal(object);
}
List extraNamespaces = null;
if (isXmlDescriptor()) {
Descriptor xmlDescriptor = (Descriptor) getDescriptor();
extraNamespaces = addExtraNamespacesToNamespaceResolver(xmlDescriptor, record, session, false, false);
writeExtraNamespaces(extraNamespaces, record);
record.addXsiTypeAndClassIndicatorIfRequired(xmlDescriptor, refDesc, xmlField, originalObject, object, wasXMLRoot, false);
}
writeOutMappings(record, object, session);
// If this descriptor is involved in inheritance add the class type.
if (isXmlDescriptor()) {
record.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
}
// the non default tables.
if (!getDescriptor().isAggregateDescriptor()) {
addPrimaryKeyForNonDefaultTable(row);
}
if ((marshaller != null) && (marshaller.getMarshalListener() != null)) {
marshaller.getMarshalListener().afterMarshal(object);
}
return row;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XMLObjectReferenceMappingNodeValue method marshalSingleValue.
@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object targetObject, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
Object fieldValue = xmlObjectReferenceMapping.buildFieldValue(targetObject, xmlField, session);
if (fieldValue == null) {
if (null != targetObject) {
Field fkField = (Field) xmlObjectReferenceMapping.getSourceToTargetKeyFieldAssociations().get(xmlField);
if (null == fkField) {
Descriptor targetDescriptor = (Descriptor) session.getDescriptor(targetObject);
fieldValue = marshalRecord.getMarshaller().getContext().getValueByXPath(targetObject, ((CoreField) targetDescriptor.getPrimaryKeyFields().get(0)).getName(), targetDescriptor.getNamespaceResolver(), Object.class);
} else {
fieldValue = marshalRecord.getMarshaller().getContext().getValueByXPath(targetObject, fkField.getXPath(), fkField.getNamespaceResolver(), Object.class);
}
}
if (null == fieldValue) {
return false;
}
}
QName schemaType = xmlField.getSchemaTypeForValue(fieldValue, session);
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
if (xPathFragment != null && xPathFragment.isAttribute()) {
marshalRecord.attribute(xPathFragment, namespaceResolver, fieldValue, schemaType);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
marshalRecord.closeStartGroupingElements(groupingFragment);
marshalRecord.characters(schemaType, fieldValue, null, false);
}
return true;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XMLCompositeCollectionMappingNodeValue method marshalSingleValue.
@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object value, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
Marshaller marshaller = marshalRecord.getMarshaller();
// convert the value - if necessary
boolean isNil = false;
if (value instanceof Root) {
isNil = ((Root) value).nil;
value = ((Root) value).getObject();
}
value = xmlCompositeCollectionMapping.convertObjectValueToDataValue(value, session, marshaller);
if (null == value) {
return xmlCompositeCollectionMapping.getNullPolicy().compositeObjectMarshal(xPathFragment, marshalRecord, object, session, namespaceResolver);
}
Descriptor descriptor = (Descriptor) xmlCompositeCollectionMapping.getReferenceDescriptor();
if (descriptor == null) {
descriptor = (Descriptor) session.getDescriptor(value.getClass());
} else if (descriptor.hasInheritance()) {
Class<?> objectValueClass = value.getClass();
if (!(objectValueClass == descriptor.getJavaClass())) {
descriptor = (Descriptor) session.getDescriptor(objectValueClass);
}
}
UnmarshalKeepAsElementPolicy keepAsElementPolicy = xmlCompositeCollectionMapping.getKeepAsElementPolicy();
if (null != keepAsElementPolicy && (keepAsElementPolicy.isKeepUnknownAsElement() || keepAsElementPolicy.isKeepAllAsElement()) && value instanceof org.w3c.dom.Node) {
marshalRecord.node((org.w3c.dom.Node) value, marshalRecord.getNamespaceResolver());
return true;
}
if (descriptor != null) {
marshalRecord.beforeContainmentMarshal(value);
ObjectBuilder objectBuilder = (ObjectBuilder) descriptor.getObjectBuilder();
CoreAttributeGroup group = marshalRecord.getCurrentAttributeGroup();
CoreAttributeGroup nestedGroup = XMLRecord.DEFAULT_ATTRIBUTE_GROUP;
CoreAttributeItem item = group.getItem(getMapping().getAttributeName());
if (item != null) {
if (item.getGroups() != null) {
nestedGroup = item.getGroup(descriptor.getJavaClass());
}
if (nestedGroup == null) {
nestedGroup = item.getGroup() == null ? XMLRecord.DEFAULT_ATTRIBUTE_GROUP : item.getGroup();
}
}
marshalRecord.pushAttributeGroup(nestedGroup);
xPathNode.startElement(marshalRecord, xPathFragment, object, session, namespaceResolver, objectBuilder, value);
if (isNil) {
marshalRecord.nilSimple(namespaceResolver);
}
List extraNamespaces = objectBuilder.addExtraNamespacesToNamespaceResolver(descriptor, marshalRecord, session, true, false);
writeExtraNamespaces(extraNamespaces, marshalRecord, session);
marshalRecord.addXsiTypeAndClassIndicatorIfRequired(descriptor, (Descriptor) xmlCompositeCollectionMapping.getReferenceDescriptor(), (Field) xmlCompositeCollectionMapping.getField(), false);
objectBuilder.buildRow(marshalRecord, value, session, marshaller, xPathFragment);
marshalRecord.afterContainmentMarshal(object, value);
marshalRecord.popAttributeGroup();
marshalRecord.endElement(xPathFragment, namespaceResolver);
marshalRecord.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
} else {
if (Constants.UNKNOWN_OR_TRANSIENT_CLASS.equals(xmlCompositeCollectionMapping.getReferenceClassName())) {
throw XMLMarshalException.descriptorNotFoundInProject(value.getClass().getName());
}
xPathNode.startElement(marshalRecord, xPathFragment, object, session, namespaceResolver, null, value);
QName schemaType = ((Field) xmlCompositeCollectionMapping.getField()).getSchemaTypeForValue(value, session);
updateNamespaces(schemaType, marshalRecord, ((Field) xmlCompositeCollectionMapping.getField()));
marshalRecord.characters(schemaType, value, null, false);
marshalRecord.endElement(xPathFragment, namespaceResolver);
}
return true;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XMLCompositeObjectMappingNodeValue method buildSelfRecord.
@Override
public UnmarshalRecord buildSelfRecord(UnmarshalRecord unmarshalRecord, Attributes atts) {
try {
Descriptor xmlDescriptor = (Descriptor) xmlCompositeObjectMapping.getReferenceDescriptor();
if (null == xmlDescriptor) {
xmlDescriptor = findReferenceDescriptor(null, unmarshalRecord, atts, xmlCompositeObjectMapping, xmlCompositeObjectMapping.getKeepAsElementPolicy());
}
if (xmlDescriptor != null) {
if (xmlDescriptor.hasInheritance()) {
unmarshalRecord.setAttributes(atts);
Class<?> clazz = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).classFromRow(unmarshalRecord, unmarshalRecord.getSession());
if (clazz == null) {
// no xsi:type attribute - look for type indicator on the default root element
XPathQName leafElementType = unmarshalRecord.getLeafElementType();
// if we have a user-set type, try to get the class from the inheritance policy
if (leafElementType != null) {
Object indicator = xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(leafElementType);
if (indicator != null) {
clazz = (Class) indicator;
}
}
}
if (clazz != null) {
xmlDescriptor = (Descriptor) unmarshalRecord.getSession().getDescriptor(clazz);
} else {
// on the mapping - make sure it is non-abstract
if (Modifier.isAbstract(xmlDescriptor.getJavaClass().getModifiers())) {
// need to throw an exception here
throw DescriptorException.missingClassIndicatorField(unmarshalRecord, (org.eclipse.persistence.oxm.XMLDescriptor) xmlDescriptor.getInheritancePolicy().getDescriptor());
}
}
}
UnmarshalRecord childRecord = unmarshalRecord.getChildUnmarshalRecord((ObjectBuilder) xmlDescriptor.getObjectBuilder());
childRecord.setSelfRecord(true);
unmarshalRecord.setChildRecord(childRecord);
childRecord.startDocument();
childRecord.initializeRecord(this.xmlCompositeObjectMapping);
return childRecord;
} else {
return null;
}
} catch (SAXException e) {
throw XMLMarshalException.unmarshalException(e);
}
}
Aggregations