Extending the Flat File Disassembler Pipeline Component
The following sample illustrates how to create a custom disassembler to parse flat file documents that are UTF-7 encoded. To process UTF-7 documents, the component inherits from the FFDasmComp class and then overrides its GetDataReader method.
using System;
using System.Text;
using System.IO;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.ParsingEngine;
using Microsoft.BizTalk.Component;
using System.Runtime.InteropServices;
namespace Microsoft.BizTalk.Test
{
/// <summary>
/// Implements FF disassembler which always uses UTF-7 encoding.
/// </summary>
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
[ComponentCategory(CategoryTypes.CATID_Streamer)]
[Guid("A6E5F54F-7902-4e1a-84D8-5C7584F0ECF2")]
public class Utf7FFDasm :
FFDasmComp,
IBaseComponent
{
/// <summary>
/// Initializes a Utf7FFAsmDasm instance.
/// </summary>
public Utf7FFDasm()
{
}
/// <summary>
/// Name property
/// </summary>
public new string Name
{
get
{
return "UTF-7 FlatFile Disassembler";
}
}
/// <summary>
/// Version property
/// </summary>
public new string Version
{
get
{
return "1.0";
}
}
/// <summary>
/// Description property
/// </summary>
public new string Description
{
get
{
return "UTF-7 FlatFile Disassembler";
}
}
/// <summary>
/// Gets a data reader instance
/// </summary>
/// <param name="dataStream">Data stream</param>
/// <param name="dataEncoding">Data encoding</param>
/// <param name="detectEncodingFromByteOrderMarks">Detect encoding from a byte order mark</param>
/// <returns>IDataReader instance</returns>
protected override IDataReader GetDataReader(Stream dataStream, Encoding dataEncoding, bool detectEncodingFromByteOrderMarks)
{
// Delegate call to the base implementation passing fixed UTF-7 encoding
return base.GetDataReader(dataStream, Encoding.UTF7, false);
}
}
}
Example
The following example illustrates how to create a custom disassembler for transactional processing of flat file interchanges. It differs from the standard Flat File Disassembler in that it does not produce any disassembled documents until the entire input interchange is completely processed. This component implementation inherits from the FFDasmComp class and overrides the GetNext method. On the first call to the GetNext method, it processes all messages in the interchange, stores them in an ArrayList, and returns the first message from the ArrayList. On subsequent calls, it returns the next message from the ArrayList.
Note
The implementation of the GetNext() method in the code sample below would not be suitable for the processing of large documents because it retains the entire interchange in memory. Using this technique for large documents could exhaust memory resources and cause degraded performance or unstable behavior.
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.Component;
namespace Microsoft.BizTalk.Component
{
/// <summary>
/// Summary description for Class1.
/// </summary>
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
[Guid("EB4714A8-FD97-43de-84E2-E011648F349B")]
public class TransactionalFFDasm :
FFDasmComp,
IBaseComponent,
IDisassemblerComponent
{
public TransactionalFFDasm()
{
}
#region IBaseComponent Members
public new string Description
{
get
{
return "Transactional Flat File Disassembler";
}
}
public new string Name
{
get
{
return "Transactional Flat File Disassembler";
}
}
public new string Version
{
get
{
return "1.0";
}
}
#endregion
#region IDisassemblerComponent Members
public new void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
{
base.Disassemble(pContext, pInMsg);
}
public new IBaseMessage GetNext(IPipelineContext pContext)
{
// Check if don't need to stop collecting messages
if (currentMessage == 0)
{
messageInterchange = new ArrayList();
currentMessage = 0;
// Collect messages from the standard disassembler
IBaseMessage disassembledMessage = null;
while ((disassembledMessage = base.GetNext(pContext)) != null)
{
byte[] buffer = new byte[4096];
int count = 0;
// Create a new memory stream to contain the disassembled message.
MemoryStream messageStream = new MemoryStream();
// Get a stream pointer to the disassembled stream.
Stream disassembledStream = disassembledMessage.BodyPart.GetOriginalDataStream();
// Write the disassembled message to the memory stream
while (0 != (count == disassembledStream.Read(buffer, 0, 4096)))
{
messageStream.Write(buffer, 0, count);
} // end-while
// Rewind the stream to the beginning
messageStream.Seek(0, SeekOrigin.Begin);
// Replace the stream on the disassembled message with the memory stream.
disassembledMessage.BodyPart.data = messageStream;
// Add this message to the ArrayList of messages.
messageInterchange.Add(disassembledMessage);
} // end-while
// Check if no messages were collected, just return nothing
if (0 == messageInterchange.Count)
return null;
} // end-if
// Check if all collected messages are returned
if (currentMessage == messageInterchange.Count)
return null;
// Return the current collected message
return (IBaseMessage)messageInterchange[currentMessage++];
} // end-method GetNext()
#endregion
private ArrayList messageInterchange = null;
private int currentMessage = 0;
}
}