Operation sourceOp = context.getSourceOperation();
boolean sourceWrapped = sourceOp != null && sourceOp.isWrapperStyle() && sourceOp.getWrapper() != null;
boolean sourceNotSubjectToWrapping = sourceOp != null && sourceOp.isNotSubjectToWrapping();
// Find the wrapper handler for source data
WrapperHandler sourceWrapperHandler = null;
String sourceDataBinding = getDataBinding(sourceOp);
sourceWrapperHandler = getWrapperHandler(sourceDataBinding, sourceWrapped);
// Check if the target operation is wrapped
DataType<List<DataType>> targetType = context.getTargetDataType();
Operation targetOp = (Operation)context.getTargetOperation();
boolean targetWrapped = targetOp != null && targetOp.isWrapperStyle() && targetOp.getWrapper() != null;
boolean targetNotSubjectToWrapping = targetOp != null && targetOp.isNotSubjectToWrapping();
// Find the wrapper handler for target data
WrapperHandler targetWrapperHandler = null;
String targetDataBinding = getDataBinding(targetOp);
targetWrapperHandler = getWrapperHandler(targetDataBinding, targetWrapped);
if ((!sourceWrapped && !sourceNotSubjectToWrapping) && targetWrapped) {
// Unwrapped --> Wrapped
WrapperInfo wrapper = targetOp.getWrapper();
// ElementInfo wrapperElement = wrapper.getInputWrapperElement();
// Class<?> targetWrapperClass = wrapper != null ? wrapper.getInputWrapperClass() : null;
if (source == null) {
// Empty child elements
Object targetWrapper = targetWrapperHandler.create(targetOp, true);
return new Object[] {targetWrapper};
}
// If the source can be wrapped, wrapped it first
if (sourceWrapperHandler != null) {
WrapperInfo sourceWrapperInfo = sourceOp.getWrapper();
DataType sourceWrapperType = sourceWrapperInfo != null ? sourceWrapperInfo.getInputWrapperType() : null;
// We only do wrapper to wrapper transformation if the source has a wrapper and both sides
// match by XML structure
if (sourceWrapperType != null && matches(sourceOp.getWrapper(), targetOp.getWrapper())) {
Class<?> sourceWrapperClass = sourceWrapperType.getPhysical();
// Create the source wrapper
Object sourceWrapper = sourceWrapperHandler.create(sourceOp, true);
// Populate the source wrapper
if (sourceWrapper != null) {
sourceWrapperHandler.setChildren(sourceWrapper,
source,
sourceOp,
true);
// Transform the data from source wrapper to target wrapper
Object targetWrapper =
mediator.mediate(sourceWrapper, sourceWrapperType, targetType.getLogical().get(0), context
.getMetadata());
return new Object[] {targetWrapper};
}
}
}
// Fall back to child by child transformation
Object targetWrapper = targetWrapperHandler.create(targetOp, true);
List<DataType> argTypes = wrapper.getUnwrappedInputType().getLogical();
Object[] targetChildren = new Object[source.length];
for (int i = 0; i < source.length; i++) {
// ElementInfo argElement = wrapper.getInputChildElements().get(i);
DataType<XMLType> argType = argTypes.get(i);
targetChildren[i] =
mediator.mediate(source[i], sourceType.getLogical().get(i), argType, context.getMetadata());
}
targetWrapperHandler.setChildren(targetWrapper,
targetChildren,
targetOp,
true);
return new Object[] {targetWrapper};
} else if (sourceWrapped && (!targetWrapped && !targetNotSubjectToWrapping)) {
// Wrapped to Unwrapped
Object sourceWrapper = source[0];
Object[] target = null;
// List<ElementInfo> childElements = sourceOp.getWrapper().getInputChildElements();
if (targetWrapperHandler != null) {
// ElementInfo wrapperElement = sourceOp.getWrapper().getInputWrapperElement();
// FIXME: This is a workaround for the wsdless support as it passes in child elements
// under the wrapper that only matches by position
if (sourceWrapperHandler.isInstance(sourceWrapper, sourceOp, true)) {
WrapperInfo targetWrapperInfo = targetOp.getWrapper();
DataType targetWrapperType =
targetWrapperInfo != null ? targetWrapperInfo.getInputWrapperType() : null;
if (targetWrapperType != null && matches(sourceOp.getWrapper(), targetOp.getWrapper())) {
Object targetWrapper =
mediator.mediate(sourceWrapper, sourceType.getLogical().get(0), targetWrapperType, context
.getMetadata());
target = targetWrapperHandler.getChildren(targetWrapper, targetOp, true).toArray();
return target;
}
}
}
Object[] sourceChildren = sourceWrapperHandler.getChildren(sourceWrapper, sourceOp, true).toArray();