forked from jaerith/ONIX-Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnixLegacyPlusParser.cs
271 lines (214 loc) · 9.09 KB
/
OnixLegacyPlusParser.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.Schema;
using OnixData.Legacy;
using OnixData.Extensions;
namespace OnixData
{
/// <summary>
///
/// This class serves as a way to parse files of the ONIX 2.1 standard (and earlier).
/// It is intended to be used when parsing very large ONIX files, in the attempt to balance
/// performance with memory use.
///
/// </summary>
public class OnixLegacyPlusParser : IDisposable, IEnumerable
{
#region CONSTANTS
private const int CONST_MSG_REFERENCE_LENGTH = 512;
private const int CONST_BLOCK_COUNT_SIZE = 50000000;
private const string CONST_ONIX_MESSAGE_REFERENCE_TAG = "ONIXMessage";
private const string CONST_ONIX_MESSAGE_SHORT_TAG = "ONIXmessage";
private const string CONST_ONIX_HEADER_REFERENCE_TAG = "Header";
private const string CONST_ONIX_HEADER_SHORT_TAG = "header";
#endregion
private bool ParserRefVerFlag = false;
private bool ParserRVWFlag = false;
private bool PerformValidFlag = false;
private FileInfo ParserFileInfo = null;
public bool PerformValidation
{
get { return this.PerformValidFlag; }
}
public bool ReferenceVersion
{
get { return this.ParserRefVerFlag; }
}
public bool ShouldApplyDefaults { get; set; }
public OnixLegacyPlusParser(FileInfo LegacyOnixFilepath,
bool ExecuteValidation,
bool PreprocessOnixFile = true)
{
if (!File.Exists(LegacyOnixFilepath.FullName))
throw new Exception("ERROR! File(" + LegacyOnixFilepath + ") does not exist.");
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;
if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodingsMT(true);
bool ReferenceVersion = DetectVersionReference(LegacyOnixFilepath);
string sOnixMsgTag = ReferenceVersion ? CONST_ONIX_MESSAGE_REFERENCE_TAG : CONST_ONIX_MESSAGE_SHORT_TAG;
this.ParserRefVerFlag = ReferenceVersion;
}
public OnixLegacyPlusParser(bool ExecuteValidation,
FileInfo LegacyOnixFilepath,
bool ReferenceVersion,
bool PreprocessOnixFile = true)
{
string sOnixMsgTag = ReferenceVersion ? CONST_ONIX_MESSAGE_REFERENCE_TAG : CONST_ONIX_MESSAGE_SHORT_TAG;
if (!File.Exists(LegacyOnixFilepath.FullName))
throw new Exception("ERROR! File(" + LegacyOnixFilepath + ") does not exist.");
this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;
if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodingsMT(true);
}
static public XmlReader CreateXmlReader(FileInfo LegacyOnixFilepath, bool ReportValidationWarnings, bool ExecutionValidation)
{
XmlReader OnixXmlReader =
new OnixXmlTextReader(LegacyOnixFilepath) { DtdProcessing = DtdProcessing.Ignore };
return OnixXmlReader;
}
public OnixLegacyHeader MessageHeader
{
get
{
string sOnixHdrTag =
this.ParserRefVerFlag ? CONST_ONIX_HEADER_REFERENCE_TAG : CONST_ONIX_HEADER_SHORT_TAG;
OnixLegacyHeader LegacyHeader = new OnixLegacyHeader();
using (XmlReader reader = CreateXmlReader(this.ParserFileInfo, false, true))
{
reader.MoveToContent();
for (int nLineCount = 0; reader.Read(); ++nLineCount)
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == sOnixHdrTag))
{
// XElement xHeader = XElement.ReadFrom(reader) as XElement;
string sHeaderBody = reader.ReadOuterXml();
LegacyHeader =
new XmlSerializer(typeof(OnixLegacyHeader), new XmlRootAttribute(sOnixHdrTag))
.Deserialize(new StringReader(sHeaderBody)) as OnixLegacyHeader;
break;
}
else if (nLineCount > 100)
break;
}
}
return LegacyHeader;
}
}
public void Dispose()
{ }
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public OnixLegacyPlusEnumerator GetEnumerator()
{
return new OnixLegacyPlusEnumerator(this, this.ParserFileInfo);
}
#region Support Methods
public bool DetectVersionReference(FileInfo LegacyOnixFileInfo)
{
bool bReferenceVersion = true;
if (LegacyOnixFileInfo.Length < CONST_MSG_REFERENCE_LENGTH)
throw new Exception("ERROR! ONIX File is smaller than expected!");
byte[] buffer = new byte[CONST_MSG_REFERENCE_LENGTH];
using (FileStream fs = new FileStream(LegacyOnixFileInfo.FullName, FileMode.Open, FileAccess.Read))
{
fs.Read(buffer, 0, buffer.Length);
fs.Close();
}
string sRefMsgTag = "<" + CONST_ONIX_MESSAGE_REFERENCE_TAG;
string sFileHead = Encoding.Default.GetString(buffer);
if (sFileHead.Contains(sRefMsgTag))
bReferenceVersion = true;
else
bReferenceVersion = false;
return bReferenceVersion;
}
#endregion
}
public class OnixLegacyPlusEnumerator : IDisposable, IEnumerator
{
private OnixLegacyPlusParser OnixParser = null;
private XmlReader OnixReader = null;
private string ProductXmlTag = null;
private OnixLegacyHeader OnixHeader = null;
private OnixLegacyProduct CurrentRecord = null;
private XmlSerializer ProductSerializer = null;
public OnixLegacyPlusEnumerator(OnixLegacyPlusParser ProvidedParser, FileInfo LegacyOnixFilepath)
{
this.ProductXmlTag = ProvidedParser.ReferenceVersion ? "Product" : "product";
this.OnixParser = ProvidedParser;
this.OnixReader = OnixLegacyPlusParser.CreateXmlReader(LegacyOnixFilepath, false, ProvidedParser.PerformValidation);
this.OnixReader.MoveToContent();
ProductSerializer = new XmlSerializer(typeof(OnixLegacyProduct), new XmlRootAttribute(this.ProductXmlTag));
}
public void Dispose()
{
if (this.OnixReader != null)
{
this.OnixReader.Close();
this.OnixReader = null;
}
}
public bool MoveNext()
{
bool bResult = false;
string sProductBody = null;
if (this.OnixHeader == null)
this.OnixHeader = OnixParser.MessageHeader;
do
{
if ((this.OnixReader.NodeType == XmlNodeType.Element) && (this.OnixReader.Name == this.ProductXmlTag))
{
// XElement product = XElement.ReadFrom(reader) as XElement;
sProductBody = this.OnixReader.ReadOuterXml();
break;
}
} while (this.OnixReader.Read());
if (!String.IsNullOrEmpty(sProductBody))
{
try
{
CurrentRecord =
this.ProductSerializer.Deserialize(new StringReader(sProductBody)) as OnixLegacyProduct;
if ((CurrentRecord != null) && OnixParser.ShouldApplyDefaults)
CurrentRecord.ApplyHeaderDefaults(this.OnixHeader);
bResult = true;
}
catch (Exception ex)
{
CurrentRecord = new OnixLegacyProduct();
CurrentRecord.SetParsingError(ex);
CurrentRecord.SetInputXml(sProductBody);
}
}
return bResult;
}
public void Reset()
{
return;
}
public object Current
{
get
{
return CurrentRecord;
}
}
}
}