Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.jpg binary

# Treat these files as text so we don;t get inconsistency with line endings between OS
tests/NHapi.NUnit/TestData/BadInputs/**/* -text
14 changes: 6 additions & 8 deletions src/NHapi.Base/Model/AbstractGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ private IStructure TryToInstantiateStructure(Type c, string name)
if (!(o is IStructure))
{
throw new HL7Exception(
$"Class {c.FullName} does not implement ca.on.uhn.hl7.message.Structure",
$"Class {c.FullName} does not implement IStructure",
ErrorCode.APPLICATION_INTERNAL_ERROR);
}

Expand All @@ -546,12 +546,10 @@ private IStructure TryToInstantiateStructure(Type c, string name)
}
catch (Exception e)
{
if (e is HL7Exception)
{
throw (HL7Exception)e;
}

throw new HL7Exception($"Can't instantiate class {c.FullName}", ErrorCode.APPLICATION_INTERNAL_ERROR, e);
throw new HL7Exception(
$"Can't instantiate class {c.FullName}",
ErrorCode.APPLICATION_INTERNAL_ERROR,
e);
}

return s;
Expand All @@ -572,7 +570,7 @@ private string GetStructureName(Type c)
if (typeof(IGroup).IsAssignableFrom(c) && !typeof(IMessage).IsAssignableFrom(c))
{
var messageName = Message.GetStructureName();
if (name.StartsWith(messageName) && name.Length > messageName.Length)
if (name.StartsWith(messageName, StringComparison.Ordinal) && name.Length > messageName.Length)
{
name = name.Substring(messageName.Length + 1);
}
Expand Down
5 changes: 5 additions & 0 deletions src/NHapi.Base/PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public IList<Hl7Package> GetAllPackages()

public bool IsValidVersion(string version)
{
if (version == null)
{
return false;
}

version = version.ToUpper().Trim();
foreach (var package in Packages)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHapi.Base/Parser/EncodingDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void AssertEr7Encoded(string message)
}

// string should start with "MSH"
if (!message.StartsWith("MSH"))
if (!message.StartsWith("MSH", StringComparison.Ordinal))
{
throw new ArgumentException("The message does not start with MSH");
}
Expand Down
17 changes: 13 additions & 4 deletions src/NHapi.Base/Parser/Escape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,18 @@ private void BuildLookups(EncodingCharacters encoding)

for (var i = 0; i < Codes.Length; i++)
{
var key = SpecialCharacters[i];
var seq = $"{encoding.EscapeCharacter}{Codes[i]}{encoding.EscapeCharacter}";

EscapeSequences.Add(SpecialCharacters[i], seq);
TryAddEscapeSequence(key, seq);
}

// Escaping of truncation # is not implemented yet. It may only be escaped if it is the first character that
// exceeds the conformance length of the component (ch 2.5.5.2). As of now, this information is not
// available at this place.
EscapeSequences.Add(TruncateChar, $"{TruncateChar}");
EscapeSequences.Add(LineFeed, $"\\{LineFeedHexadecimal}\\");
EscapeSequences.Add(CarriageReturn, $"\\{CarriageReturnHexadecimal}\\");
TryAddEscapeSequence(TruncateChar, $"{TruncateChar}");
TryAddEscapeSequence(LineFeed, $"\\{LineFeedHexadecimal}\\");
TryAddEscapeSequence(CarriageReturn, $"\\{CarriageReturnHexadecimal}\\");
}

private void LoadHexadecimalConfiguration()
Expand All @@ -317,6 +318,14 @@ private void LoadHexadecimalConfiguration()
CarriageReturnHexadecimal = configSection.HexadecimalEscaping.CarriageReturnHexadecimal;
}
}

private void TryAddEscapeSequence(char key, string value)
{
if (!EscapeSequences.ContainsKey(key))
{
EscapeSequences.Add(key, value);
}
}
}
}
}
26 changes: 14 additions & 12 deletions src/NHapi.Base/Parser/LegacyPipeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public override string GetEncoding(string message)
var ok = true;

// string should start with "MSH"
if (!message.StartsWith("MSH"))
if (!message.StartsWith("MSH", StringComparison.Ordinal))
{
return null;
}
Expand Down Expand Up @@ -620,21 +620,23 @@ public override string GetVersion(string message)
ErrorCode.REQUIRED_FIELD_MISSING);
}

string version;
if (fields.Length >= 12)
if (fields.Length < 12)
{
if (fields[11] == null)
{
throw new HL7Exception("MSH Version field is null.", ErrorCode.REQUIRED_FIELD_MISSING);
}
throw new HL7Exception(
$"Can't find version ID - MSH has only {fields.Length} fields.",
ErrorCode.REQUIRED_FIELD_MISSING);
}

version = Split(fields[11], compSep)[0];
if (fields[11] == null)
{
throw new HL7Exception("MSH Version field is null.", ErrorCode.REQUIRED_FIELD_MISSING);
}
else

var version = Split(fields[11], compSep)[0];

if (version == null)
{
throw new HL7Exception(
"Can't find version ID - MSH has only " + fields.Length + " fields.",
ErrorCode.REQUIRED_FIELD_MISSING);
throw new HL7Exception("MSH Version field is invalid.", ErrorCode.REQUIRED_FIELD_MISSING);
}

return version.Trim();
Expand Down
55 changes: 28 additions & 27 deletions src/NHapi.Base/Parser/MessageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public virtual bool MoveNext()

var structureDefinition = currentPosition.StructureDefinition;

if (structureDefinition.IsSegment && structureDefinition.Name.StartsWith(direction) && (structureDefinition.IsRepeating || currentPosition.Repetition == -1))
if (structureDefinition.IsSegment && structureDefinition.Name.StartsWith(direction, StringComparison.Ordinal) && (structureDefinition.IsRepeating || currentPosition.Repetition == -1))
{
nextIsSet = true;
currentPosition.IncrementRepetition();
Expand Down Expand Up @@ -224,29 +224,31 @@ private void AddNonStandardSegmentAtCurrentPosition()
// Check if the structure already has a non-standard segment in the appropriate
// position
var currentNames = parentStructure.Names;
if (index < currentNames.Length && currentNames[index].StartsWith(direction))
if (index < currentNames.Length && currentNames[index].StartsWith(direction, StringComparison.Ordinal))
{
newSegmentName = currentNames[index];
}
else
{
try
{
newSegmentName = parentStructure.AddNonstandardSegment(direction, index);
}
catch (HL7Exception e)
{
throw new InvalidOperationException($"Unable to add nonstandard segment {direction}: ", e);
}
newSegmentName = parentStructure.AddNonstandardSegment(direction, index);
}

var previousSibling = GetCurrentPosition().StructureDefinition;
var parentStructureDefinition = parentDefinitionPath.Last().StructureDefinition;
var nextDefinition = new NonStandardStructureDefinition(parentStructureDefinition, previousSibling, newSegmentName, index);
currentDefinitionPath = parentDefinitionPath;
currentDefinitionPath.Add(new Position(nextDefinition, 0));
try
{
var previousSibling = GetCurrentPosition().StructureDefinition;
var parentStructureDefinition = parentDefinitionPath.Last().StructureDefinition;
var nextDefinition = new NonStandardStructureDefinition(parentStructureDefinition, previousSibling, newSegmentName, index);
currentDefinitionPath = parentDefinitionPath;
currentDefinitionPath.Add(new Position(nextDefinition, 0));

nextIsSet = true;
nextIsSet = true;
}
catch (Exception ex)
{
throw new HL7Exception(
$"Could not add non-standard segment at current position.",
ex);
}
}

private IStructure NavigateToStructure(IEnumerable<Position> theDefinitionPath)
Expand All @@ -260,17 +262,10 @@ private IStructure NavigateToStructure(IEnumerable<Position> theDefinitionPath)
}
else
{
try
{
var structureDefinition = next.StructureDefinition;
var currentStructureGroup = (IGroup)currentStructure;
var nextStructureName = structureDefinition.NameAsItAppearsInParent;
currentStructure = currentStructureGroup.GetStructure(nextStructureName, next.Repetition);
}
catch (HL7Exception e)
{
throw new InvalidOperationException("Failed to retrieve structure: ", e);
}
var structureDefinition = next.StructureDefinition;
var currentStructureGroup = (IGroup)currentStructure;
var nextStructureName = structureDefinition.NameAsItAppearsInParent;
currentStructure = currentStructureGroup.GetStructure(nextStructureName, next.Repetition);
}
}

Expand All @@ -283,6 +278,12 @@ private List<Position> PopUntilMatchFound(List<Position> theDefinitionPath)
{
theDefinitionPath = new List<Position>(theDefinitionPath.GetRange(0, theDefinitionPath.Count - 1));

if (theDefinitionPath.Count == 0)
{
Log.Debug($"The created list of positions contains no elements.");
return null;
}

var newCurrentPosition = theDefinitionPath.Last();
var newCurrentStructureDefinition = newCurrentPosition.StructureDefinition;

Expand Down
20 changes: 17 additions & 3 deletions src/NHapi.Base/Parser/ParserBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public virtual IMessage Parse(string message)
if (!SupportsEncoding(encoding))
{
string startOfMessage = null;
if (message.StartsWith("MSH"))
if (message.StartsWith("MSH", StringComparison.Ordinal))
{
var indexOfCarriageReturn = message.IndexOf('\r');
if (indexOfCarriageReturn > 0)
Expand Down Expand Up @@ -421,10 +421,24 @@ public bool SupportsEncoding(string encoding)
/// <exception cref="HL7Exception">Thrown when the version is not recognized or no appropriate class can be found or the Message.</exception>
protected internal virtual IMessage InstantiateMessage(string theName, string theVersion, bool isExplicit)
{
var messageClass = Factory.GetMessageClass(theName, theVersion, isExplicit);
Type messageClass;
try
{
messageClass = Factory.GetMessageClass(theName, theVersion, isExplicit);
}
catch (Exception ex)
{
throw new HL7Exception(
$"Can't find message class in current package list: {theName}",
ErrorCode.UNSUPPORTED_MESSAGE_TYPE,
ex);
}

if (messageClass == null)
{
throw new HL7Exception($"Can't find message class in current package list: {theName}", ErrorCode.UNSUPPORTED_MESSAGE_TYPE);
throw new HL7Exception(
$"Can't find message class in current package list: {theName}",
ErrorCode.UNSUPPORTED_MESSAGE_TYPE);
}

Log.Info($"Instantiating msg of class {messageClass.FullName}");
Expand Down
39 changes: 19 additions & 20 deletions src/NHapi.Base/Parser/PipeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,13 @@ public override void Parse(IMessage message, string @string)

if (messageIterator.MoveNext())
{
var next = (ISegment)messageIterator.Current;
var next = messageIterator.Current as ISegment;

if (next == null)
{
throw new HL7Exception($"Current segment does not implement ISegment and therefore cannot be parsed.");
}

Parse(next, segments[i], GetEncodingChars(@string), repetition);
}
}
Expand Down Expand Up @@ -602,7 +608,7 @@ public override string GetVersion(string message)
var fields = Split(msh, fieldSep);

string compSep;
if (fields.Length >= 2 && (fields[1].Length == 4 || fields[1].Length == 5))
if (fields.Length >= 2 && (fields[1]?.Length == 4 || fields[1]?.Length == 5))
{
compSep = Convert.ToString(fields[1][0]); // get component separator as 1st encoding char
}
Expand All @@ -613,29 +619,22 @@ public override string GetVersion(string message)
ErrorCode.REQUIRED_FIELD_MISSING);
}

string version;
if (fields.Length >= 12)
if (fields.Length < 12)
{
var comp = Split(fields[11], compSep);
if (comp.Length >= 1)
{
version = comp[0];
}
else
{
throw new HL7Exception(
$"Can't find version ID - MSH.12 is {fields[11]}",
ErrorCode.REQUIRED_FIELD_MISSING);
}
throw new HL7Exception(
$"Can't find version ID - MSH has only {fields.Length} fields.",
ErrorCode.REQUIRED_FIELD_MISSING);
}
else

var comp = Split(fields[11], compSep);
if (comp.Length < 1)
{
throw new HL7Exception(
"Can't find version ID - MSH has only " + fields.Length + " fields.",
$"Can't find version ID - MSH.12 is {fields[11]}",
ErrorCode.REQUIRED_FIELD_MISSING);
}

return version;
return comp[0];
}

/// <inheritdoc />
Expand Down Expand Up @@ -724,10 +723,10 @@ private static EncodingCharacters GetEncodingChars(string message)
{
if (message.Length < 9)
{
throw new HL7Exception($"Invalid message content: \"{message}\"");
throw new HL7Exception($"Invalid message content: '{message}'");
}

return new EncodingCharacters(message[3], message.Substring(4, 4));
return new EncodingCharacters(message[3], message.Substring(4, 4));
}

private static string EncodePrimitive(IPrimitive p, EncodingCharacters encodingChars)
Expand Down
2 changes: 1 addition & 1 deletion src/NHapi.Base/Util/MessageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static Index GetIndex(IGroup parent, IStructure child)
var names = parent.Names;
for (var i = 0; i < names.Length; i++)
{
if (names[i].StartsWith(child.GetStructureName()))
if (names[i].StartsWith(child.GetStructureName(), StringComparison.Ordinal))
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHapi.Base/Util/Terser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ private PathSpec ParsePathSpec(string spec)
{
var ps = new PathSpec(this);

if (spec.StartsWith("."))
if (spec.StartsWith(".", StringComparison.Ordinal))
{
ps.Find = true;
spec = spec.Substring(1);
Expand Down
3 changes: 1 addition & 2 deletions src/NHapi.SourceGeneration/Generators/DataTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace NHapi.SourceGeneration.Generators
using System;
using System.Collections;
using System.Data.Common;
using System.Data.Odbc;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -205,7 +204,7 @@ public static void Make(FileInfo targetDirectory, string dataType, string versio
// trim all CE_x to CE
if (dt != null)
{
if (dt.StartsWith("CE"))
if (dt.StartsWith("CE", StringComparison.Ordinal))
{
dt = "CE";
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHapi.SourceGeneration/Generators/SegmentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static string MakeSegment(string name, string version)
se.Type = Convert.ToString(rs[10 - 1]);

// shorten CE_x to CE
if (se.Type.StartsWith("CE"))
if (se.Type.StartsWith("CE", StringComparison.Ordinal))
{
se.Type = "CE";
}
Expand Down
Loading