use of org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy 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.UnmarshalKeepAsElementPolicy in project eclipselink by eclipse-ee4j.
the class XMLCompositeObjectMappingNodeValue method endSelfNodeValue.
@Override
public void endSelfNodeValue(UnmarshalRecord unmarshalRecord, UnmarshalRecord selfRecord, Attributes attributes) {
if (xmlCompositeObjectMapping.getNullPolicy().valueIsNull(attributes)) {
xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), null);
return;
}
unmarshalRecord.removeNullCapableValue(this);
if (unmarshalRecord.getFragmentBuilder().getDocument() != null) {
UnmarshalKeepAsElementPolicy keepAsElementPolicy = xmlCompositeObjectMapping.getKeepAsElementPolicy();
SAXFragmentBuilder builder = unmarshalRecord.getFragmentBuilder();
if ((((keepAsElementPolicy.isKeepUnknownAsElement()) || (keepAsElementPolicy.isKeepAllAsElement()))) && (builder.getNodes().size() != 0)) {
if (unmarshalRecord.getTypeQName() != null) {
Class<Object> theClass = unmarshalRecord.getConversionManager().javaType(unmarshalRecord.getTypeQName());
if (theClass != null) {
// handle simple text
endElementProcessText(unmarshalRecord, xmlCompositeObjectMapping, null, null);
return;
}
}
Element element = (Element) builder.getNodes().remove(builder.getNodes().size() - 1);
String xsiType = null;
if (null != element) {
if (unmarshalRecord.isNamespaceAware()) {
xsiType = element.getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE);
} else {
xsiType = element.getAttribute(Constants.SCHEMA_TYPE_ATTRIBUTE);
}
}
if (null != xsiType) {
xsiType = xsiType.trim();
Object value = element;
String namespace = null;
int colonIndex = xsiType.indexOf(unmarshalRecord.getNamespaceSeparator());
if (colonIndex > -1) {
String prefix = xsiType.substring(0, colonIndex);
namespace = unmarshalRecord.resolveNamespacePrefix(prefix);
if (null == namespace) {
namespace = XMLPlatformFactory.getInstance().getXMLPlatform().resolveNamespacePrefix(element, prefix);
}
QName qName = new QName(namespace, xsiType.substring(colonIndex + 1));
ConversionManager conversionManager = unmarshalRecord.getConversionManager();
Class<Object> theClass = conversionManager.javaType(qName);
if (theClass != null) {
value = conversionManager.convertObject(element.getTextContent(), theClass, qName);
}
} else {
if (!unmarshalRecord.isNamespaceAware() || !unmarshalRecord.getUnmarshaller().getJsonTypeConfiguration().useXsdTypesWithPrefix()) {
QName qName = new QName(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI, xsiType);
ConversionManager conversionManager = unmarshalRecord.getConversionManager();
Class<Object> theClass = conversionManager.javaType(qName);
if (theClass != null) {
value = conversionManager.convertObject(element.getTextContent(), theClass, qName);
}
}
}
xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), value);
} else {
xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), element);
}
}
} else {
Object valueToSet = selfRecord.getCurrentObject();
valueToSet = xmlCompositeObjectMapping.convertDataValueToObjectValue(valueToSet, unmarshalRecord.getSession(), unmarshalRecord.getUnmarshaller());
xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), valueToSet);
InverseReferenceMapping inverseReferenceMapping = xmlCompositeObjectMapping.getInverseReferenceMapping();
if (null != inverseReferenceMapping) {
inverseReferenceMapping.getAttributeAccessor().setAttributeValueInObject(valueToSet, unmarshalRecord.getCurrentObject());
}
}
}
use of org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy in project eclipselink by eclipse-ee4j.
the class XMLCompositeObjectMappingNodeValue method endElement.
@Override
public void endElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord) {
if (unmarshalRecord.isNil() && xmlCompositeObjectMapping.getNullPolicy().isNullRepresentedByXsiNil() && (unmarshalRecord.getChildRecord() == null)) {
unmarshalRecord.resetStringBuffer();
return;
}
if (null == unmarshalRecord.getChildRecord()) {
SAXFragmentBuilder builder = unmarshalRecord.getFragmentBuilder();
UnmarshalKeepAsElementPolicy keepAsElementPolicy = xmlCompositeObjectMapping.getKeepAsElementPolicy();
if (null != keepAsElementPolicy && (keepAsElementPolicy.isKeepUnknownAsElement() || keepAsElementPolicy.isKeepAllAsElement()) && builder.getNodes().size() != 0) {
if (unmarshalRecord.getTypeQName() != null) {
Class<Object> theClass = unmarshalRecord.getConversionManager().javaType(unmarshalRecord.getTypeQName());
if (theClass != null) {
// handle simple text
endElementProcessText(unmarshalRecord, xmlCompositeObjectMapping, xPathFragment, null);
return;
}
}
if (builder.getDocument() != null) {
setOrAddAttributeValueForKeepAsElement(builder, xmlCompositeObjectMapping, xmlCompositeObjectMapping, unmarshalRecord, false, null);
return;
}
} else {
// handle simple text
endElementProcessText(unmarshalRecord, xmlCompositeObjectMapping, xPathFragment, null);
return;
}
} else {
Object object = unmarshalRecord.getChildRecord().getCurrentObject();
setAttributeValue(object, unmarshalRecord);
unmarshalRecord.setChildRecord(null);
}
}
use of org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy in project eclipselink by eclipse-ee4j.
the class XMLCompositeObjectMappingNodeValue method startElement.
@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
try {
unmarshalRecord.removeNullCapableValue(this);
Descriptor xmlDescriptor = (Descriptor) xmlCompositeObjectMapping.getReferenceDescriptor();
if (null == xmlDescriptor) {
xmlDescriptor = findReferenceDescriptor(xPathFragment, unmarshalRecord, atts, xmlCompositeObjectMapping, xmlCompositeObjectMapping.getKeepAsElementPolicy());
if (xmlDescriptor == null) {
if (xmlCompositeObjectMapping.getField() != null) {
// try leaf element type
QName leafType = ((Field) xmlCompositeObjectMapping.getField()).getLastXPathFragment().getLeafElementType();
if (leafType != null) {
XPathFragment frag = new XPathFragment();
frag.setNamespaceAware(unmarshalRecord.isNamespaceAware());
String xpath = leafType.getLocalPart();
String uri = leafType.getNamespaceURI();
if (uri != null && uri.length() > 0) {
frag.setNamespaceURI(uri);
String prefix = ((Descriptor) xmlCompositeObjectMapping.getDescriptor()).getNonNullNamespaceResolver().resolveNamespaceURI(uri);
if (prefix != null && prefix.length() > 0) {
xpath = prefix + Constants.COLON + xpath;
}
}
frag.setXPath(xpath);
Context xmlContext = unmarshalRecord.getUnmarshaller().getContext();
xmlDescriptor = xmlContext.getDescriptorByGlobalType(frag);
}
}
}
UnmarshalKeepAsElementPolicy policy = xmlCompositeObjectMapping.getKeepAsElementPolicy();
if (null != policy && ((xmlDescriptor == null && policy.isKeepUnknownAsElement()) || policy.isKeepAllAsElement())) {
QName schemaType = unmarshalRecord.getTypeQName();
if (schemaType == null) {
schemaType = ((Field) xmlCompositeObjectMapping.getField()).getSchemaType();
unmarshalRecord.setTypeQName(schemaType);
}
if (schemaType != null) {
Class<Object> theClass = unmarshalRecord.getConversionManager().javaType(schemaType);
if (theClass == null) {
setupHandlerForKeepAsElementPolicy(unmarshalRecord, xPathFragment, atts);
return true;
}
} else {
setupHandlerForKeepAsElementPolicy(unmarshalRecord, xPathFragment, atts);
return true;
}
}
}
//
// Null Composite Objects are marshalled in 2 ways when the input XML node is empty.
// (1) as null
// - isNullRepresentedByEmptyNode = true
// (2) as empty object
// - isNullRepresentedByEmptyNode = false
// A deferred contentHandler is used to queue events until we are able to determine
// whether we are in one of empty/simple/complex state.
// Control is returned to the UnmarshalHandler after creation of (1) or (2) above is started.
// Object creation was deferred to the DeferredContentHandler
//
// Check if we need to create the DeferredContentHandler based on policy state
AbstractNullPolicy nullPolicy = xmlCompositeObjectMapping.getNullPolicy();
if (nullPolicy.isNullRepresentedByEmptyNode()) {
String qnameString = xPathFragment.getLocalName();
if (xPathFragment.getPrefix() != null) {
qnameString = xPathFragment.getPrefix() + Constants.COLON + qnameString;
}
if (null != xmlDescriptor) {
// Process null capable value
CompositeObjectMappingContentHandler aHandler = new //
CompositeObjectMappingContentHandler(unmarshalRecord, this, xmlCompositeObjectMapping, atts, xPathFragment, xmlDescriptor);
// Send control to the handler
aHandler.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
XMLReader xmlReader = unmarshalRecord.getXMLReader();
xmlReader.setContentHandler(aHandler);
xmlReader.setLexicalHandler(aHandler);
}
} else {
if (unmarshalRecord.getXMLReader().isNullRecord(nullPolicy, atts, unmarshalRecord) && nullPolicy.ignoreAttributesForNil()) {
xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), null);
} else {
Field xmlFld = (Field) this.xmlCompositeObjectMapping.getField();
if (xmlFld.hasLastXPathFragment()) {
unmarshalRecord.setLeafElementType(xmlFld.getLastXPathFragment().getLeafElementType());
}
processChild(xPathFragment, unmarshalRecord, atts, xmlDescriptor, xmlCompositeObjectMapping);
}
}
} catch (SAXException e) {
throw XMLMarshalException.unmarshalException(e);
}
return true;
}
use of org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy in project eclipselink by eclipse-ee4j.
the class XMLAnyCollectionMappingNodeValue method marshalSingleValue.
@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object value, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
if (null == value) {
return false;
}
boolean wasXMLRoot = false;
XPathFragment xmlRootFragment = null;
Object originalValue = value;
Descriptor descriptor;
ObjectBuilder objectBuilder;
CoreAbstractSession childSession;
Marshaller marshaller = marshalRecord.getMarshaller();
XPathFragment rootFragment;
if (xmlAnyCollectionMapping.usesXMLRoot() && (value instanceof Root)) {
xmlRootFragment = new XPathFragment();
xmlRootFragment.setNamespaceAware(marshalRecord.isNamespaceAware());
wasXMLRoot = true;
value = ((Root) value).getObject();
if (null == value) {
setupFragment(((Root) originalValue), xmlRootFragment, marshalRecord);
marshalRecord.nilComplex(xmlRootFragment, namespaceResolver);
return true;
}
}
UnmarshalKeepAsElementPolicy keepAsElementPolicy = xmlAnyCollectionMapping.getKeepAsElementPolicy();
if (value instanceof String) {
marshalSimpleValue(xmlRootFragment, marshalRecord, originalValue, object, value, session, namespaceResolver);
} else if (null != keepAsElementPolicy && (keepAsElementPolicy.isKeepUnknownAsElement() || keepAsElementPolicy.isKeepAllAsElement()) && value instanceof org.w3c.dom.Node) {
marshalRecord.node((org.w3c.dom.Node) value, marshalRecord.getNamespaceResolver());
} else {
try {
childSession = marshaller.getContext().getSession(value);
} catch (XMLMarshalException e) {
marshalSimpleValue(xmlRootFragment, marshalRecord, originalValue, object, value, session, namespaceResolver);
return true;
}
descriptor = (Descriptor) childSession.getDescriptor(value);
pushAttributeGroup(marshalRecord, descriptor, getMapping().getAttributeName());
objectBuilder = (ObjectBuilder) descriptor.getObjectBuilder();
List extraNamespaces = objectBuilder.addExtraNamespacesToNamespaceResolver(descriptor, marshalRecord, session, true, true);
if (wasXMLRoot) {
setupFragment(((Root) originalValue), xmlRootFragment, marshalRecord);
}
/*
* B5112171: 25 Apr 2006
* During marshalling - XML AnyObject and AnyCollection
* mappings throw a NullPointerException when the
* "document root element" on child object descriptors are not
* all defined. These nodes will be ignored with a warning.
*/
String defaultRootElementString = descriptor.getDefaultRootElement();
if (!wasXMLRoot && (defaultRootElementString == null)) {
throw XMLMarshalException.defaultRootElementNotSpecified(descriptor);
} else {
marshalRecord.beforeContainmentMarshal(value);
if (xmlRootFragment != null) {
rootFragment = xmlRootFragment;
} else {
rootFragment = new XPathFragment(defaultRootElementString);
// resolve URI
if (rootFragment.getNamespaceURI() == null) {
String uri = descriptor.getNonNullNamespaceResolver().resolveNamespacePrefix(rootFragment.getPrefix());
rootFragment.setNamespaceURI(uri);
}
}
if (!wasXMLRoot) {
marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
}
getXPathNode().startElement(marshalRecord, rootFragment, object, childSession, marshalRecord.getNamespaceResolver(), objectBuilder, value);
writeExtraNamespaces(extraNamespaces, marshalRecord, session);
pushAttributeGroup(marshalRecord, descriptor, rootFragment.getLocalName());
marshalRecord.addXsiTypeAndClassIndicatorIfRequired(descriptor, descriptor, (Field) xmlAnyCollectionMapping.getField(), originalValue, value, wasXMLRoot, false);
objectBuilder.buildRow(marshalRecord, value, session, marshaller, null);
marshalRecord.afterContainmentMarshal(object, value);
marshalRecord.endElement(rootFragment, namespaceResolver);
marshalRecord.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
marshalRecord.popAttributeGroup();
}
marshalRecord.popAttributeGroup();
}
return true;
}
Aggregations