public void StartElement(String localName)
{
_inElement = true;
String[] localNameArray = localName.Split(new[] { ID_DELIMETER });
if (_rootLevel == -1)
{
_rootLevel = localNameArray.Length;
}
/*
* Skip the root element, MSH.1, and MSH.2 since those don't have any data that we care
* about.
*/
if ((localNameArray.Length == 1) && (localNameArray[0].Equals(MESSAGE_ROOT_ID)))
{
_rootLevel = 0;
return;
}
else if (localNameArray.Length == 2)
{
if (IsHeaderSegment(localNameArray[0]))
{
if ((localNameArray[1].Length == 1) && (localNameArray[1][0] == '1' || localNameArray[1][0] == '2'))
{
_previousFieldNameArray = localNameArray;
return;
}
}
}
/*
* If the element that we've found is the same as the last, then we have a repetition, so we
* remove the last separator that was added and append to repetition separator.
*/
if (_previousFieldNameArray != null && localNameArray.SequenceEqual(_previousFieldNameArray))
{
_output.Remove(_output.Length - 1, 1);
_output.Append(_repetitionSeparator);
_previousComponentNameArray = null;
return;
}
/*
* To find the delimeter count we are splitting the element name by the ID delimeter.
*/
int currentDelimeterCount = localNameArray.Length - 1;
/*
* MIRTH-2078: Don't add missing fields/components/subcomponents if the current level was
* the starting level. This only pertains to partial XML messages where the root is a field
* or component.
*/
if (currentDelimeterCount == 1 && _rootLevel <= 1)
{
/*
* This will add missing fields if any (ex. between OBX.1 and OBX.5).
*/
int previousFieldId = 0;
if (_previousFieldNameArray != null)
{
previousFieldId = Int32.Parse(_previousFieldNameArray[1]);
}
int currentFieldId = Int32.Parse(localNameArray[1]);
for (int i = 1; i < (currentFieldId - previousFieldId); i++)
{
_output.Append(_fieldSeparator);
}
_previousFieldNameArray = localNameArray;
}
else if (currentDelimeterCount == 2 && _rootLevel <= 2)
{
/*
* This will add missing components if any (ex. between OBX.1.1 and OBX.1.5).
*/
int previousComponentId = 0;
if (_previousComponentNameArray != null)
{
previousComponentId = Int32.Parse(_previousComponentNameArray[2]);
}
int currentComponentId = Int32.Parse(localNameArray[2]);
for (int i = 1; i < (currentComponentId - previousComponentId); i++)
{
_output.Append(_componentSeparator);
_previousDelimiterLength = _componentSeparator.Length;
}
_previousComponentNameArray = localNameArray;
}
else if (currentDelimeterCount == 3 && _rootLevel <= 3)
{
/*
* This will add missing subcomponents if any (ex. between OBX.1.1.1 and OBX.1.1.5).
*/
int previousSubcomponentId = 0;
if (_previousSubcomponentNameArray != null)
{
previousSubcomponentId = Int32.Parse(_previousSubcomponentNameArray[3]);
}
int currentSubcomponentId = Int32.Parse(localNameArray[3]);
for (int i = 1; i < (currentSubcomponentId - previousSubcomponentId); i++)
{
_output.Append(_subcomponentSeparator);
_previousDelimiterLength = _subcomponentSeparator.Length;
}
_previousSubcomponentNameArray = localNameArray;
}
/*
* If we have an element with no periods, then we know its the name of the segment, so write
* it to the output buffer followed by the field separator.
*/
if (currentDelimeterCount == 0)
{
_output.Append(localName);
_output.Append(_fieldSeparator);
/*
* Also set previousFieldName to null so that multiple segments in a row with only one
* field don't trigger a repetition character. (i.e. NTE|1<CR>NTE|2)
*/
_previousFieldNameArray = null;
}
else if (currentDelimeterCount == 1)
{
_previousComponentNameArray = null;
}
else if (currentDelimeterCount == 2)
{
_previousSubcomponentNameArray = null;
}
}