public void EndElement(String localName)
{
_inElement = false;
String[] localNameArray = localName.Split(ID_DELIMETER);
/*
* Once we see the closing of MSH.1 or MSH.2 tags, we know that the separator characters
* have been added to the output buffer, so we can grab them and set the local variables.
*/
if ((localNameArray.Length == 1) && (localNameArray[0].Equals(MESSAGE_ROOT_ID)))
{
return;
}
else if (localNameArray.Length == 2)
{
if (IsHeaderSegment(localNameArray[0]))
{
if ((localNameArray[1].Length == 1) && (localNameArray[1][0] == '1'))
{
_output = _output.Replace("&", "&");
_fieldSeparator = _output[_output.Length - 1].ToString();
return;
}
else if ((localNameArray[1].Length == 1) && (localNameArray[1][0] == '2'))
{
_output = _output.Replace("&", "&");
string separators = _output.ToString(4, _output.Length - 4);
_componentSeparator = separators[0].ToString();
_repetitionSeparator = separators[1].ToString();
_escapeCharacter = separators.Length > 2 ? separators[2].ToString() : "";
_subcomponentSeparator = separators.Length > 3 ? separators[3].ToString() : "";
}
}
}
int currentDelimeterCount = localNameArray.Length - 1;
/*
* We don't want to have trailing separators, so once we get to the last element of a nested
* level, we delete the last character.
*/
if (currentDelimeterCount > _previousDelimeterCount)
{
_previousDelimeterCount = currentDelimeterCount;
}
else if (currentDelimeterCount < _previousDelimeterCount && _previousDelimiterLength > 0)
{
_output.Remove(_output.Length - 1, 1);
_previousDelimeterCount = currentDelimeterCount;
}
/*
* The number of periods in the element tells us the level. So, MSH is at level 0, MSH.3 is
* at level 1, MSH.3.1 at level 2, and so on. We can use this to determine which seperator
* to append once the element is closed.
*
* MIRTH-2078: Only add the last character if the root delimiter is 0 (HL7Message) or the
* current element level is deeper than the root level. This only pertains to partial XML
* messages where the root is a field or component.
*/
if (_rootLevel == 0 || currentDelimeterCount >= _rootLevel)
{
switch (currentDelimeterCount)
{
case 0:
_output.Append(_segmentSeparator);
break;
case 1:
_output.Append(_fieldSeparator);
break;
case 2:
_output.Append(_componentSeparator);
_previousDelimiterLength = _componentSeparator.Length;
break;
case 3:
_output.Append(_subcomponentSeparator);
_previousDelimiterLength = _subcomponentSeparator.Length;
break;
default:
break;
}
}
}