Skip to content

Commit

Permalink
* Modified code for MessageHeader property to use its own XmlReader.
Browse files Browse the repository at this point in the history
* Added code so that ApplyHeaderDefaults() could be called during product iteration.
  • Loading branch information
jaerith committed Feb 15, 2021
1 parent 7ac2c72 commit 6900c2f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 45 deletions.
77 changes: 51 additions & 26 deletions OnixData/OnixLegacyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public bool ReferenceVersion
get { return this.ParserRefVerFlag; }
}

public bool ShouldApplyDefaults { get; set; }

public bool AlwaysReturnInputXml { get; set; }

public OnixLegacyParser(FileInfo LegacyOnixFilepath,
Expand All @@ -71,9 +73,10 @@ public OnixLegacyParser(FileInfo LegacyOnixFilepath,

AlwaysReturnInputXml = false;

this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.PerformValidFlag = ExecuteValidation;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodings(true);
Expand Down Expand Up @@ -108,10 +111,11 @@ public OnixLegacyParser(bool ExecuteValidation,
if (!File.Exists(LegacyOnixFilepath.FullName))
throw new Exception("ERROR! File(" + LegacyOnixFilepath + ") does not exist.");

this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.PerformValidFlag = ExecuteValidation;
this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodings(true);
Expand All @@ -138,9 +142,10 @@ public OnixLegacyParser(string LegacyOnixContent,

AlwaysReturnInputXml = false;

this.ParserFileContent = new StringBuilder(LegacyOnixContent);
this.ParserRVWFlag = true;
this.PerformValidFlag = ExecuteValidation;
this.ParserFileContent = new StringBuilder(LegacyOnixContent);
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
{
Expand Down Expand Up @@ -178,10 +183,11 @@ public OnixLegacyParser(bool ExecuteValidation,
if (String.IsNullOrEmpty(LegacyOnixContent))
throw new Exception("ERROR! ONIX content provided is empty.");

this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileContent = new StringBuilder(LegacyOnixContent);
this.ParserRVWFlag = true;
this.PerformValidFlag = ExecuteValidation;
this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileContent = new StringBuilder(LegacyOnixContent);
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
{
Expand All @@ -201,6 +207,15 @@ public OnixLegacyParser(bool ExecuteValidation,
this.LegacyOnixMessage = null;
}

public XmlReader CreateXmlReader()
{
XmlReader OnixReader =
(this.ParserFileInfo != null) ?
CreateXmlReader(this.ParserFileInfo, this.ParserRVWFlag, false) : CreateXmlReader(this.ParserFileContent, this.ParserRVWFlag, false);

return OnixReader;
}

/// <summary>
///
/// This method will prepare the XmlReader that we will use to read the ONIX XML file.
Expand Down Expand Up @@ -355,21 +370,24 @@ public OnixLegacyHeader MessageHeader

if (this.LegacyOnixMessage != null)
LegacyHeader = this.LegacyOnixMessage.Header;
else if (this.LegacyOnixReader != null)
else
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(this.LegacyOnixReader);
using (XmlReader OnixReader = CreateXmlReader())
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(OnixReader);

XmlNodeList HeaderList = XMLDoc.GetElementsByTagName(sOnixHdrTag);
XmlNodeList HeaderList = XMLDoc.GetElementsByTagName(sOnixHdrTag);

if ((HeaderList != null) && (HeaderList.Count > 0))
{
XmlNode HeaderNode = HeaderList.Item(0);
string sHeaderBody = HeaderNode.OuterXml;
if ((HeaderList != null) && (HeaderList.Count > 0))
{
XmlNode HeaderNode = HeaderList.Item(0);
string sHeaderBody = HeaderNode.OuterXml;

LegacyHeader =
new XmlSerializer(typeof(OnixLegacyHeader), new XmlRootAttribute(sOnixHdrTag))
.Deserialize(new StringReader(sHeaderBody)) as OnixLegacyHeader;
LegacyHeader =
new XmlSerializer(typeof(OnixLegacyHeader), new XmlRootAttribute(sOnixHdrTag))
.Deserialize(new StringReader(sHeaderBody)) as OnixLegacyHeader;
}
}
}

Expand Down Expand Up @@ -541,6 +559,7 @@ public class OnixLegacyEnumerator : IDisposable, IEnumerator
private List<string> OtherTextList = new List<string>();
private XmlDocument OnixDoc = null;
private XmlNodeList ProductList = null;
private OnixLegacyHeader OnixHeader = null;
private OnixLegacyProduct CurrentRecord = null;
private XmlSerializer ProductSerializer = null;

Expand Down Expand Up @@ -590,14 +609,17 @@ public bool MoveNext()
{
bool bResult = true;

if (OnixDoc == null)
if (this.OnixDoc == null)
{
this.OnixDoc = new XmlDocument();
this.OnixDoc.Load(this.OnixReader);

this.ProductList = this.OnixDoc.GetElementsByTagName(this.ProductXmlTag);
}

if (this.OnixHeader == null)
this.OnixHeader = OnixParser.MessageHeader;

if (++CurrentIndex < this.ProductList.Count)
{
string sInputXml = this.ProductList[CurrentIndex].OuterXml;
Expand All @@ -610,6 +632,9 @@ public bool MoveNext()
if ((CurrentRecord != null) && OnixParser.AlwaysReturnInputXml)
CurrentRecord.SetInputXml(sInputXml);

if ((CurrentRecord != null) && OnixParser.ShouldApplyDefaults)
CurrentRecord.ApplyHeaderDefaults(this.OnixHeader);

if ((CurrentRecord != null) &&
(CurrentRecord.OnixOtherTextList != null) &&
(CurrentRecord.OnixOtherTextList.Length > 0))
Expand Down
25 changes: 18 additions & 7 deletions OnixData/OnixLegacyPlusParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ 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.PerformValidFlag = ExecuteValidation;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodingsMT(true);
Expand All @@ -82,10 +85,11 @@ public OnixLegacyPlusParser(bool ExecuteValidation,
if (!File.Exists(LegacyOnixFilepath.FullName))
throw new Exception("ERROR! File(" + LegacyOnixFilepath + ") does not exist.");

this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.PerformValidFlag = ExecuteValidation;
this.ParserRefVerFlag = ReferenceVersion;
this.ParserFileInfo = LegacyOnixFilepath;
this.ParserRVWFlag = true;
this.ShouldApplyDefaults = true;
this.PerformValidFlag = ExecuteValidation;

if (PreprocessOnixFile)
LegacyOnixFilepath.ReplaceIsoLatinEncodingsMT(true);
Expand Down Expand Up @@ -192,6 +196,7 @@ public class OnixLegacyPlusEnumerator : IDisposable, IEnumerator
private XmlReader OnixReader = null;

private string ProductXmlTag = null;
private OnixLegacyHeader OnixHeader = null;
private OnixLegacyProduct CurrentRecord = null;
private XmlSerializer ProductSerializer = null;

Expand Down Expand Up @@ -221,6 +226,9 @@ public bool MoveNext()
bool bResult = false;
string sProductBody = null;

if (this.OnixHeader == null)
this.OnixHeader = OnixParser.MessageHeader;

while (this.OnixReader.Read())
{
if ((this.OnixReader.NodeType == XmlNodeType.Element) && (this.OnixReader.Name == this.ProductXmlTag))
Expand All @@ -239,6 +247,9 @@ public bool MoveNext()
CurrentRecord =
this.ProductSerializer.Deserialize(new StringReader(sProductBody)) as OnixLegacyProduct;

if ((CurrentRecord != null) && OnixParser.ShouldApplyDefaults)
CurrentRecord.ApplyHeaderDefaults(this.OnixHeader);

bResult = true;
}
catch (Exception ex)
Expand Down
49 changes: 37 additions & 12 deletions OnixData/OnixParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public bool ReferenceVersion
get { return this.ParserRefVerFlag; }
}

public bool ShouldApplyDefaults { get; set; }

public OnixParser(FileInfo OnixFilepath,
bool ReportValidationWarnings,
bool PreprocessOnixFile = true,
Expand All @@ -50,6 +52,8 @@ public OnixParser(FileInfo OnixFilepath,
if (!File.Exists(OnixFilepath.FullName))
throw new Exception("ERROR! File(" + OnixFilepath + ") does not exist.");

this.ShouldApplyDefaults = true;

this.ParserFileInfo = OnixFilepath;
this.ParserRVWFlag = ReportValidationWarnings;

Expand Down Expand Up @@ -81,6 +85,8 @@ public OnixParser(string OnixContent,
if (String.IsNullOrEmpty(OnixContent))
throw new Exception("ERROR! Provided ONIX content is empty.");

this.ShouldApplyDefaults = true;

this.ParserFileInfo = null;
this.ParserRVWFlag = ReportValidationWarnings;

Expand All @@ -100,6 +106,14 @@ public OnixParser(string OnixContent,
new XmlSerializer(typeof(OnixMessage), new XmlRootAttribute(sOnixMsgTag)).Deserialize(this.CurrOnixReader) as OnixMessage;
}

public XmlReader CreateXmlReader()
{
XmlReader OnixReader =
(this.ParserFileInfo != null) ? CreateXmlReader(this.ParserFileInfo, this.ParserRVWFlag) : CreateXmlReader(this.ParserFileContent, this.ParserRVWFlag);

return OnixReader;
}

static public XmlReader CreateXmlReader(FileInfo CurrOnixFilepath, bool ReportValidationWarnings)
{
/*
Expand Down Expand Up @@ -147,21 +161,24 @@ public OnixHeader MessageHeader

if (this.CurrOnixMessage != null)
OnixHeader = this.CurrOnixMessage.Header;
else if (this.CurrOnixReader != null)
else
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(this.CurrOnixReader);
using (XmlReader OnixReader = CreateXmlReader())
{
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(OnixReader);

XmlNodeList HeaderList = XMLDoc.GetElementsByTagName(sOnixHdrTag);
XmlNodeList HeaderList = XMLDoc.GetElementsByTagName(sOnixHdrTag);

if ((HeaderList != null) && (HeaderList.Count > 0))
{
XmlNode HeaderNode = HeaderList.Item(0);
string sHeaderBody = HeaderNode.OuterXml;
if ((HeaderList != null) && (HeaderList.Count > 0))
{
XmlNode HeaderNode = HeaderList.Item(0);
string sHeaderBody = HeaderNode.OuterXml;

OnixHeader =
new XmlSerializer(typeof(OnixHeader), new XmlRootAttribute(sOnixHdrTag))
.Deserialize(new StringReader(sHeaderBody)) as OnixHeader;
OnixHeader =
new XmlSerializer(typeof(OnixHeader), new XmlRootAttribute(sOnixHdrTag))
.Deserialize(new StringReader(sHeaderBody)) as OnixHeader;
}
}
}

Expand Down Expand Up @@ -241,6 +258,7 @@ public class OnixEnumerator : IDisposable, IEnumerator
private string ProductXmlTag = null;
private XmlDocument OnixDoc = null;
private XmlNodeList ProductList = null;
private OnixHeader OnixHeader = null;
private OnixProduct CurrentRecord = null;
private XmlSerializer ProductSerializer = null;

Expand Down Expand Up @@ -294,6 +312,9 @@ public bool MoveNext()
this.ProductList = this.OnixDoc.GetElementsByTagName(this.ProductXmlTag);
}

if (this.OnixHeader == null)
this.OnixHeader = OnixParser.MessageHeader;

if (++CurrentIndex < this.ProductList.Count)
{
string sInputXml = this.ProductList[CurrentIndex].OuterXml;
Expand All @@ -303,9 +324,13 @@ public bool MoveNext()
CurrentRecord =
this.ProductSerializer.Deserialize(new StringReader(sInputXml)) as OnixProduct;

if ((CurrentRecord != null) && OnixParser.ShouldApplyDefaults)
CurrentRecord.ApplyHeaderDefaults(this.OnixHeader);

CurrentCommList.Clear();

if ((CurrentRecord.CollateralDetail != null) &&
if ((CurrentRecord != null) &&
(CurrentRecord.CollateralDetail != null) &&
(CurrentRecord.CollateralDetail.OnixTextContentList != null) &&
(CurrentRecord.CollateralDetail.OnixTextContentList.Length > 0))
{
Expand Down

0 comments on commit 6900c2f

Please sign in to comment.