How to disassemble flat file trailer without tag identifiers in body part
Say we have a flat file like below. The first 3 lines are body elements but the number/looping count of bodies are uncertain(occurs from 1 to *). There is no tag identifier in body part. The last line in the file is always a trailer.
In this case, however can FFDASM disassemble the file and identify the last line is trailer but not a body element?
1115151651515950000055 00012913702613000000000003000 000139C0000007000
1215151651121510000054 00022913803603000000000009000 000279A0000009000
1315115950000065516515 00032813104643000000000007000 000399B0000003000
121515160003290003290000010000000003000
The answer is such flatfile disassembling will not be able to work without tag identifier on body part elements.
So the only solution is to use a custom pipeline component to add these tag identifiers before performing the disassembling. For example:
BODY1115151651515950000055 00012913702613000000000003000 000139C0000007000
BODY1215151651121510000054 00022913803603000000000009000 000279A0000009000
BODY1315115950000065516515 00032813104643000000000007000 000399B0000003000
121515160003290003290000010000000003000
(BODY is the tag identifier being added.)
The output XML files are like:
The following is the custom pipeline component code.
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
//Validate parameters
if (pContext == null) throw new ArgumentNullException("pContext");
if (pInMsg == null) throw new ArgumentNullException("pInMsg");
this.pipelineContext = pContext;
this.baseMessage = pInMsg;
string partName;
for (int i = 0; i < baseMessage.PartCount; i++)
{
MemoryStream outStream = new MemoryStream();
partName = null;
IBaseMessagePart part = baseMessage.GetPartByIndex(i, out partName);
StreamReader reader = new StreamReader(part.GetOriginalDataStream());
string partBody = reader.ReadToEnd();
StreamWriter writer = new StreamWriter(outStream, new UTF8Encoding());
string[] separator = new string[] {"\r\n"};
string[] strArray = partBody.Split(separator, StringSplitOptions.None);
for (int n = 0; n < strArray.Length; n++)
{
//There will be a blank string in the last line of the array. So we don't need to add tag to the last 2 lines.
if (n < (strArray.Length - 2))
{
strArray[n] = "BODY" + strArray[n];
}
//Add the line break back.
writer.Write(strArray[n] + "\r\n");
}
writer.Flush();
outStream.Seek(0, SeekOrigin.Begin);
part.Data = outStream;
}
return baseMessage;
}
Best regards,
WenJun Zhang