private static Stream TransformStream(Stream stream, string mapName, bool validate, ref string messageType, ref string targetDocumentSpecName) { Type type = Type.GetType(mapName); if (null == type) { throw new Exception("Invalid MapType" + mapName); } TransformMetaData transformMetaData = TransformMetaData.For(type); SchemaMetadata sourceSchemaMetadata = transformMetaData.SourceSchemas[0]; string schemaName = sourceSchemaMetadata.SchemaName; SchemaMetadata targetSchemaMetadata = transformMetaData.TargetSchemas[0]; if (validate && string.Compare(messageType, schemaName, false, CultureInfo.CurrentCulture) != 0) { throw new Exception("Source Document Mismatch. MessageType: " + messageType + " Schema Name: " + schemaName); } messageType = targetSchemaMetadata.SchemaName; targetDocumentSpecName = targetSchemaMetadata.SchemaBase.GetType().AssemblyQualifiedName; XmlReader reader = XmlReader.Create(stream); XPathDocument input = new XPathDocument(reader); ITransform transform = transformMetaData.Transform; Stream outStream = new MemoryStream(); transform.Transform(input, transformMetaData.ArgumentList, outStream, new XmlUrlResolver()); outStream.Flush(); outStream.Seek(0L, SeekOrigin.Begin); return(outStream); }
private string GetSourceMessageMatchingMapName(ArrayList mapList, string btsMsgType, IPipelineContext context) { string mapName = string.Empty; if (mapList.Count > 1) { foreach (string map in mapList) { try { Type type = Type.GetType(map); TransformMetaData transformMetaData = TransformMetaData.For(type); SchemaMetadata sourceSchemaMetadata = transformMetaData.SourceSchemas[0]; string mapSourceSchemaName = sourceSchemaMetadata.SchemaName; IDocumentSpec documentSpec = context.GetDocumentSpecByType(btsMsgType); string msgSourceSchemaName = documentSpec.DocType; Logger.WriteTrace("Map Source Schema Name: " + mapSourceSchemaName); Logger.WriteTrace("Msg Source Schema Name: " + msgSourceSchemaName); if (string.Compare(mapSourceSchemaName, msgSourceSchemaName, false, CultureInfo.CurrentCulture) == 0) { Logger.WriteTrace("Match found: " + map); mapName = map; break; } } catch (Exception) { } } } else { mapName = mapList[0].ToString(); } return(mapName); }
public void Execute(ref Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg, Microsoft.BizTalk.Component.Interop.IPipelineContext pc) { dynamic transformMetaData; SchemaMetadata sourceSchemaMetadata; string schemaName; SchemaMetadata targetSchemaMetadata; try { transformMetaData = TransformMetaData.For(mapType); sourceSchemaMetadata = transformMetaData.SourceSchemas[0]; schemaName = sourceSchemaMetadata.SchemaName; targetSchemaMetadata = transformMetaData.TargetSchemas[0]; } catch (Exception e) { throw new Exception(String.Format("Exception encountered while trying to instantiate map {0}, exception details - {1}", mapName, e.Message)); } if (validateSourceSchema == TransformationSourceSchemaValidation.ValidateSourceSchema || validateSourceSchema == TransformationSourceSchemaValidation.ValidateSourceSchemaIfKnown) { object property = inmsg.Context.Read(BizTalkGlobalPropertySchemaEnum.MessageType.ToString(), ContextPropertyNamespaces._BTSPropertyNamespace); if (property == null) { if (validateSourceSchema == TransformationSourceSchemaValidation.ValidateSourceSchema) { throw new Exception("Unable to read source messageType while performing transformation against map " + mapName); } } else { string messageType = property.ToString(); if (!string.IsNullOrEmpty(messageType)) { if (string.Compare(messageType, schemaName, false, CultureInfo.CurrentCulture) != 0) { throw new Exception(String.Format("Transformation mismatch exception for map {0}, was expecting source schema to be {1} but was actually {2}.", mapName, schemaName, messageType)); } } } } try { dynamic transform = transformMetaData.Transform2; Stream output = new VirtualStream(); TraceManager.PipelineComponent.TraceInfo("{0} - Applying transformation {1} to the message", callToken, mapName); TraceManager.PipelineComponent.TraceInfo("{0} - Message is being transformed from message type {1} to message type {2}", callToken, schemaName, targetSchemaMetadata.SchemaName); //TODO figure out what is suppose to go in this null object instead of null. transform.Transform(inmsg.BodyPart.GetOriginalDataStream(), null, output); output.Position = 0; pc.ResourceTracker.AddResource(output); inmsg.BodyPart.Data = output; inmsg.Context.Write(BizTalkGlobalPropertySchemaEnum.SchemaStrongName.ToString(), ContextPropertyNamespaces._BTSPropertyNamespace, null); inmsg.Context.Promote(BizTalkGlobalPropertySchemaEnum.MessageType.ToString(), ContextPropertyNamespaces._BTSPropertyNamespace, targetSchemaMetadata.SchemaName); } catch (Exception e) { throw new Exception("Exception encountered while trying to execute map - " + e.Message); } }