use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.
the class JAXBValueStore method copy.
@Override
public ValueStore copy() {
AbstractSession session = ((JAXBContext) jaxbHelperContext.getJAXBContext()).getXMLContext().getSession(entity);
Object originalEntity = entity;
entity = descriptor.getInstantiationPolicy().buildNewInstance();
for (SDOProperty sdoProperty : (List<SDOProperty>) dataObject.getType().getProperties()) {
if (!sdoProperty.getType().isChangeSummaryType()) {
Mapping mapping = getJAXBMappingForProperty(sdoProperty);
CoreAttributeAccessor attributeAccessor = mapping.getAttributeAccessor();
Object attributeValue = attributeAccessor.getAttributeValueFromObject(originalEntity);
if (mapping.isCollectionMapping()) {
Object containerCopy = null;
SDOChangeSummary sdoChangeSummary = dataObject.getChangeSummary();
if (null != sdoChangeSummary) {
List list = listWrappers.get(sdoProperty);
containerCopy = sdoChangeSummary.getOriginalElements().get(list);
}
if (null == containerCopy) {
CoreContainerPolicy containerPolicy = mapping.getContainerPolicy();
containerCopy = containerPolicy.containerInstance();
if (null != attributeValue) {
Object attributeValueIterator = containerPolicy.iteratorFor(attributeValue);
while (containerPolicy.hasNext(attributeValueIterator)) {
containerPolicy.addInto(containerPolicy.nextEntry(attributeValueIterator), containerCopy, session);
}
}
}
attributeAccessor.setAttributeValueInObject(entity, containerCopy);
} else {
attributeAccessor.setAttributeValueInObject(entity, attributeValue);
}
}
}
return new JAXBValueStore(jaxbHelperContext, originalEntity, descriptor, dataObject, listWrappers);
}
use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLVariableXPathCollectionMappingNodeValue method marshal.
@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
if (mapping.isReadOnly()) {
return false;
}
CoreContainerPolicy cp = mapping.getContainerPolicy();
Object collection = mapping.getAttributeAccessor().getAttributeValueFromObject(object);
if (null == collection) {
AbstractNullPolicy wrapperNP = mapping.getWrapperNullPolicy();
if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation().equals(XMLNullRepresentationType.XSI_NIL)) {
marshalRecord.nilSimple(namespaceResolver);
return true;
} else {
return false;
}
}
Object iterator = cp.iteratorFor(collection);
if (null != iterator && cp.hasNext(iterator)) {
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, mapping.getWrapperNullPolicy() != null);
}
if (marshalRecord.getMarshaller().isApplicationJSON()) {
List<XPathFragment> frags = new ArrayList();
List<List> values = new ArrayList<>();
// sort the elements. Results will be a list of xpathfragments and a corresponding list of
// collections associated with those xpathfragments
XPathFragment xmlRootFragment;
while (cp.hasNext(iterator)) {
Object nextValue = cp.next(iterator, session);
nextValue = mapping.convertObjectValueToDataValue(nextValue, session, marshalRecord.getMarshaller());
XPathFragment frag = mapping.getXPathFragmentForValue(nextValue, marshalRecord.getNamespaceResolver(), marshalRecord.isNamespaceAware(), marshalRecord.getNamespaceSeparator());
if (frag != null) {
int index = frags.indexOf(frag);
if (index > -1) {
values.get(index).add(nextValue);
} else {
frags.add(frag);
List valuesList = new ArrayList();
valuesList.add(nextValue);
values.add(valuesList);
}
}
}
for (int i = 0; i < frags.size(); i++) {
XPathFragment nextFragment = frags.get(i);
List listValue = values.get(i);
if (nextFragment != null) {
int valueSize = listValue.size();
if (valueSize > 1) {
marshalRecord.startCollection();
}
for (int j = 0; j < valueSize; j++) {
marshalSingleValue(nextFragment, marshalRecord, object, listValue.get(j), session, namespaceResolver, ObjectMarshalContext.getInstance());
}
if (valueSize > 1) {
marshalRecord.endCollection();
}
}
}
} else {
Object objectValue;
while (cp.hasNext(iterator)) {
objectValue = cp.next(iterator, session);
objectValue = mapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
}
}
return true;
}
use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLCompositeDirectCollectionMappingNodeValue method marshal.
/**
* Override the method in XPathNode such that the marshaller can be set on the
* marshalRecord - this is required for XMLConverter usage.
*/
@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
if (xmlCompositeDirectCollectionMapping.isReadOnly()) {
return false;
}
CoreContainerPolicy cp = getContainerPolicy();
Object collection = xmlCompositeDirectCollectionMapping.getAttributeAccessor().getAttributeValueFromObject(object);
if (null == collection) {
AbstractNullPolicy wrapperNP = xmlCompositeDirectCollectionMapping.getWrapperNullPolicy();
if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation() == XMLNullRepresentationType.XSI_NIL) {
marshalRecord.nilSimple(namespaceResolver);
return true;
} else {
return false;
}
}
Object iterator = cp.iteratorFor(collection);
Field xmlField = (Field) xmlCompositeDirectCollectionMapping.getField();
if (null != iterator && cp.hasNext(iterator)) {
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
if (xmlField.usesSingleNode() && !xmlCompositeDirectCollectionMapping.isDefaultEmptyContainer()) {
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, xmlCompositeDirectCollectionMapping.getWrapperNullPolicy() != null);
}
}
Object objectValue;
if (xmlField.usesSingleNode()) {
StringBuilder stringValueStringBuilder = new StringBuilder();
String newValue;
QName schemaType = null;
if (xPathFragment != null && !xPathFragment.isAttribute() && !xPathFragment.nameIsText) {
marshalRecord.openStartElement(xPathFragment, namespaceResolver);
}
while (cp.hasNext(iterator)) {
objectValue = cp.next(iterator, session);
objectValue = xmlCompositeDirectCollectionMapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
schemaType = xmlField.getSchemaTypeForValue(objectValue, session);
newValue = marshalRecord.getValueToWrite(schemaType, objectValue, (ConversionManager) session.getDatasourcePlatform().getConversionManager());
if (null != newValue) {
stringValueStringBuilder.append(newValue);
if (cp.hasNext(iterator)) {
stringValueStringBuilder.append(SPACE);
}
}
}
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
if (xPathFragment != null && xPathFragment.isAttribute()) {
marshalRecord.attribute(xPathFragment, namespaceResolver, stringValueStringBuilder.toString());
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
marshalRecord.closeStartGroupingElements(groupingFragment);
if (xmlCompositeDirectCollectionMapping.isCDATA()) {
marshalRecord.cdata(stringValueStringBuilder.toString());
} else {
marshalRecord.characters(stringValueStringBuilder.toString());
if (xPathFragment != null && !xPathFragment.isAttribute() && !xPathFragment.nameIsText) {
marshalRecord.endElement(xPathFragment, namespaceResolver);
}
}
}
} else {
int valueSize = cp.sizeFor(collection);
if (marshalRecord.getMarshaller().isApplicationJSON() && (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays())) {
marshalRecord.startCollection();
}
while (cp.hasNext(iterator)) {
objectValue = cp.next(iterator, session);
marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
}
if (marshalRecord.getMarshaller().isApplicationJSON() && (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays())) {
marshalRecord.endCollection();
}
}
return true;
}
use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLAnyAttributeMappingNodeValue method marshal.
@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
if (xmlAnyAttributeMapping.isReadOnly()) {
return false;
}
Object collection = xmlAnyAttributeMapping.getAttributeValueFromObject(object);
if (collection == null) {
return false;
}
CoreContainerPolicy cp = getContainerPolicy();
Object iter = cp.iteratorFor(collection);
if (!cp.hasNext(iter)) {
return false;
}
XPathFragment groupingElements = marshalRecord.openStartGroupingElements(namespaceResolver);
List extraNamespaces = new ArrayList();
NamespaceResolver nr = marshalRecord.getNamespaceResolver();
while (cp.hasNext(iter)) {
Map.Entry entry = (Map.Entry) cp.nextEntry(iter, session);
Object key = entry.getKey();
if (key instanceof QName) {
QName name = (QName) key;
String value = entry.getValue().toString();
String qualifiedName = name.getLocalPart();
if (nr != null) {
String prefix = nr.resolveNamespaceURI(name.getNamespaceURI());
if ((prefix != null) && prefix.length() > 0) {
qualifiedName = prefix + Constants.COLON + qualifiedName;
} else if (name.getNamespaceURI() != null && name.getNamespaceURI().length() > 0) {
String generatedPrefix = nr.generatePrefix();
if (marshalRecord.hasCustomNamespaceMapper()) {
String customPrefix = marshalRecord.getMarshaller().getNamespacePrefixMapper().getPreferredPrefix(name.getNamespaceURI(), generatedPrefix, true);
if (customPrefix != null && customPrefix.length() > 0) {
generatedPrefix = customPrefix;
}
}
qualifiedName = generatedPrefix + Constants.COLON + qualifiedName;
nr.put(generatedPrefix, name.getNamespaceURI());
extraNamespaces.add(generatedPrefix);
marshalRecord.namespaceDeclaration(generatedPrefix, name.getNamespaceURI());
}
}
if (javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(name.getNamespaceURI())) {
marshalRecord.namespaceDeclaration(name.getLocalPart(), name.getNamespaceURI());
} else {
marshalRecord.attribute(name.getNamespaceURI(), name.getLocalPart(), qualifiedName, value);
}
}
}
for (int i = 0; i < extraNamespaces.size(); i++) {
marshalRecord.getNamespaceResolver().removeNamespace((String) extraNamespaces.get(i));
}
marshalRecord.closeStartGroupingElements(groupingElements);
return true;
}
use of org.eclipse.persistence.internal.core.queries.CoreContainerPolicy in project eclipselink by eclipse-ee4j.
the class XMLAnyCollectionMappingNodeValue method marshal.
@Override
public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) {
if (xmlAnyCollectionMapping.isReadOnly()) {
return false;
}
CoreContainerPolicy cp = xmlAnyCollectionMapping.getContainerPolicy();
Object collection = xmlAnyCollectionMapping.getAttributeAccessor().getAttributeValueFromObject(object);
if (null == collection) {
AbstractNullPolicy wrapperNP = xmlAnyCollectionMapping.getWrapperNullPolicy();
if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation().equals(XMLNullRepresentationType.XSI_NIL)) {
marshalRecord.nilSimple(namespaceResolver);
return true;
} else {
return false;
}
}
Object iterator = cp.iteratorFor(collection);
if (null != iterator && cp.hasNext(iterator)) {
XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
return marshalRecord.emptyCollection(xPathFragment, namespaceResolver, xmlAnyCollectionMapping.getWrapperNullPolicy() != null);
}
if (marshalRecord.getMarshaller().isApplicationJSON()) {
List<XPathFragment> frags = new ArrayList();
List<List> values = new ArrayList<>();
List mixedValues = new ArrayList();
// sort the elements. Results will be a list of xpathfragments and a corresponding list of
// collections associated with those xpathfragments
XPathFragment xmlRootFragment;
while (cp.hasNext(iterator)) {
Object nextValue = cp.next(iterator, session);
nextValue = xmlAnyCollectionMapping.convertObjectValueToDataValue(nextValue, session, marshalRecord.getMarshaller());
XPathFragment frag = getXPathFragmentForValue(nextValue, marshalRecord, marshalRecord.getMarshaller());
if (frag != null) {
if (frag == SIMPLE_FRAGMENT) {
mixedValues.add(nextValue);
} else {
int index = frags.indexOf(frag);
if (index > -1) {
values.get(index).add(nextValue);
} else {
frags.add(frag);
List valuesList = new ArrayList();
valuesList.add(nextValue);
values.add(valuesList);
}
}
}
}
if (mixedValues.size() > 0) {
frags.add(SIMPLE_FRAGMENT);
values.add(mixedValues);
}
for (int i = 0; i < frags.size(); i++) {
XPathFragment nextFragment = frags.get(i);
List listValue = values.get(i);
if (nextFragment != null) {
int valueSize = listValue.size();
if (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays()) {
marshalRecord.startCollection();
}
for (int j = 0; j < valueSize; j++) {
marshalSingleValue(nextFragment, marshalRecord, object, listValue.get(j), session, namespaceResolver, ObjectMarshalContext.getInstance());
}
if (valueSize > 1 || !marshalRecord.getMarshaller().isReduceAnyArrays()) {
marshalRecord.endCollection();
}
}
}
return true;
} else {
Object objectValue;
marshalRecord.startCollection();
while (cp.hasNext(iterator)) {
objectValue = cp.next(iterator, session);
objectValue = xmlAnyCollectionMapping.convertObjectValueToDataValue(objectValue, session, marshalRecord.getMarshaller());
marshalSingleValue(xPathFragment, marshalRecord, object, objectValue, session, namespaceResolver, ObjectMarshalContext.getInstance());
}
marshalRecord.endCollection();
return true;
}
}
Aggregations