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().getMediaType().isApplicationJSON()){
List<XPathFragment> frags = new ArrayList();
List<List> values = new ArrayList<List>();
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;