-
Notifications
You must be signed in to change notification settings - Fork 90
/
Program.cs
106 lines (96 loc) · 4.88 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.IO;
using OopFactory.X12.Parsing;
namespace OopFactory.X12.X12Parser
{
class Program
{
static void Main(string[] args)
{
int maxBatchSize = 10 * 1012 * 1012; // 10 Mbytes
if (ConfigurationManager.AppSettings["MaxBatchSize"] != null)
maxBatchSize = Convert.ToInt32(ConfigurationManager.AppSettings["MaxBatchSize"]);
bool throwException = Convert.ToBoolean(ConfigurationManager.AppSettings["ThrowExceptionOnSyntaxErrors"]);
string x12Filename = args[0];
string outputFilename = args.Length > 1 ? args[1] : x12Filename + ".xml";
OopFactory.X12.Parsing.X12Parser parser = new Parsing.X12Parser(throwException);
parser.ParserWarning += new Parsing.X12Parser.X12ParserWarningEventHandler(parser_ParserWarning);
byte[] header = new byte[6];
using (FileStream fs = new FileStream(x12Filename, FileMode.Open, FileAccess.Read))
{
// peak at first 6 characters to determine if this is a unicode file
fs.Read(header, 0, 6);
fs.Close();
}
Encoding encoding = (header[1] == 0 && header[3] == 0 && header[5] == 0) ? Encoding.Unicode : Encoding.UTF8;
if (new FileInfo(x12Filename).Length <= maxBatchSize)
{
using (FileStream fs = new FileStream(x12Filename, FileMode.Open, FileAccess.Read))
{
var interchanges = parser.ParseMultiple(fs, encoding);
if (interchanges.Count >= 1)
{
using (FileStream outputFs = new FileStream(outputFilename, FileMode.Create))
{
interchanges.First().Serialize(outputFs);
}
}
if (interchanges.Count > 1)
{
for (int i = 1; i < interchanges.Count; i++)
{
outputFilename = string.Format("{0}_{1}.xml", args.Length > 1 ? args[1] : x12Filename, i + 1);
using (FileStream outputFs = new FileStream(outputFilename, FileMode.Create))
{
interchanges[i].Serialize(outputFs);
}
}
}
}
}
else
{
using (FileStream fs = new FileStream(x12Filename, FileMode.Open, FileAccess.Read))
{
// Break up output files by batch size
X12StreamReader reader = new X12StreamReader(fs, encoding);
X12FlatTransaction currentTransactions = reader.ReadNextTransaction();
X12FlatTransaction nextTransaction = reader.ReadNextTransaction();
int i = 1;
while (!string.IsNullOrEmpty(nextTransaction.Transactions.First()))
{
if (currentTransactions.GetSize() + nextTransaction.GetSize() < maxBatchSize
&& currentTransactions.IsaSegment == nextTransaction.IsaSegment
&& currentTransactions.GsSegment == nextTransaction.GsSegment)
{
currentTransactions.Transactions.AddRange(nextTransaction.Transactions);
}
else
{
outputFilename = string.Format("{0}_{1}.xml", args.Length > 1 ? args[1] : x12Filename, i++);
using (FileStream outputFs = new FileStream(outputFilename, FileMode.Create))
{
parser.ParseMultiple(currentTransactions.ToString()).First().Serialize(outputFs);
}
currentTransactions = nextTransaction;
}
nextTransaction = reader.ReadNextTransaction();
}
outputFilename = string.Format("{0}_{1}.xml", args.Length > 1 ? args[1] : x12Filename, i++);
using (FileStream outputFs = new FileStream(outputFilename, FileMode.Create))
{
parser.ParseMultiple(currentTransactions.ToString()).First().Serialize(outputFs);
}
}
}
}
static void parser_ParserWarning(object sender, X12ParserWarningEventArgs args)
{
Console.WriteLine(args.Message);
}
}
}