Skip to content

Commit

Permalink
Trim whitespace before start of header (fix #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
c0shea committed Jun 4, 2018
1 parent d550f24 commit 43204ce
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
5 changes: 4 additions & 1 deletion IdParser.Client/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ private void ParseScanData(string input)
lblIdType.Text = "Identification Card";
}

txtParsedId.Text = JsonConvert.SerializeObject(id, Formatting.Indented);
txtParsedId.Text = JsonConvert.SerializeObject(id, Formatting.Indented, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
catch (Exception ex)
{
Expand Down
52 changes: 52 additions & 0 deletions IdParser.Test/DriversLicenseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,5 +2421,57 @@ public void TestWVLicense()
Assert.AreEqual("12345", idCard.Address.PostalCodeDisplay);
Assert.AreEqual("West Virginia", idCard.IssuerIdentificationNumber.GetDescription());
}

[TestMethod]
public void TestLeadingWhitespaceLicense()
{
var expected = new DriversLicense
{
Name = new Name
{
First = "MOTORIST",
Middle = "R",
Last = "SHEEHAN",
WasFirstTruncated = false,
WasMiddleTruncated = false,
WasLastTruncated = false
},

Address = new Address
{
StreetLine1 = "2 ROBERTS DRIVE",
City = "PLYMOUTH",
JurisdictionCode = "MA",
PostalCode = "023601234",
Country = Country.Usa
},

DateOfBirth = new DateTime(1939, 12, 07),
Sex = Sex.Male,
Height = Height.FromImperial(71),

IdNumber = "S58239477",
AamvaVersionNumber = Version.Aamva2009,

IssueDate = new DateTime(2014, 11, 14),
ExpirationDate = new DateTime(2018, 12, 07),
RevisionDate = new DateTime(2009, 07, 15),

Jurisdiction = new DriversLicenseJurisdiction
{
VehicleClass = "DM",
RestrictionCodes = "B"
}
};

var file = File.ReadAllText("Leading Whitespace.txt");
var idCard = Barcode.Parse(file, Validation.None);

AssertIdCard(expected, idCard);
AssertLicense(expected, idCard);

Assert.AreEqual("02360-1234", idCard.Address.PostalCodeDisplay);
Assert.AreEqual("Massachusetts", idCard.IssuerIdentificationNumber.GetDescription());
}
}
}
3 changes: 3 additions & 0 deletions IdParser.Test/IdParser.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<Content Include="LA License.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Leading Whitespace.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="MT License.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
31 changes: 31 additions & 0 deletions IdParser.Test/Leading Whitespace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
 @
6ANSI 636002040002DL00410256ZM02970036DLDCADM
DCBB
DCDNONE
DBA12072018
DCSSHEEHAN
DACMOTORIST
DADR
DBD11142014
DBB12071939
DBC1
DAYUNK
DAU071 IN
DAG2 ROBERTS DRIVE
DAIPLYMOUTH
DAJMA
DAK023601234
DAQS58239477
DCF11-15-2014 REV 07-15-2009
DCGUSA
DDEN
DDFN
DDGN
DCK15441S582394770602
DDB07152009
ZMZMAN
ZMB
ZMC
ZMD11152014
ZME
ZMF
4 changes: 1 addition & 3 deletions IdParser/Barcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public static IdentificationCard Parse(string rawPdf417Input, Validation validat
}
else
{
rawPdf417Input = rawPdf417Input.RemoveInvalidCharactersFromHeader()
.FixIncorrectHeader()
.RemoveIncorrectCarriageReturns();
rawPdf417Input = Fixes.Apply(rawPdf417Input);
}

var version = ParseAamvaVersion(rawPdf417Input);
Expand Down
17 changes: 12 additions & 5 deletions IdParser/Fixes.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace IdParser
{
internal static class Fixes
{
internal static string Apply(string input)
{
return input.RemoveInvalidCharactersFromHeader()
.FixIncorrectHeader()
.RemoveIncorrectCarriageReturns();
}

/// <summary>
/// HID keyboard emulation, especially entered via a web browser, tends to mutilate the header.
/// As long as part of the header is correct, this will fix the rest of it to make it parse-able.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
internal static string FixIncorrectHeader(this string input)
private static string FixIncorrectHeader(this string input)
{
if (input[0] == '@' &&
input[1] == Barcode.ExpectedSegmentTerminator &&
Expand All @@ -30,8 +35,10 @@ internal static string FixIncorrectHeader(this string input)
/// Sometimes bad characters (e.g. @a ANSI) get into the header (usually through HID keyboard emulation).
/// Replace the header with what we are expecting.
/// </summary>
internal static string RemoveInvalidCharactersFromHeader(this string input)
private static string RemoveInvalidCharactersFromHeader(this string input)
{
input = input.TrimStart();

if (input[0] != '@' || input.StartsWith(Barcode.ExpectedHeader))
{
return input;
Expand All @@ -53,7 +60,7 @@ internal static string RemoveInvalidCharactersFromHeader(this string input)
/// HID keyboard emulation (and some other methods) tend to replace the \r with \r\n
/// which is invalid and doesn't conform to the AAMVA standard. This fixes it before attempting to parse the fields.
/// </summary>
internal static string RemoveIncorrectCarriageReturns(this string input)
private static string RemoveIncorrectCarriageReturns(this string input)
{
var crLf = Barcode.ExpectedSegmentTerminator.ToString() + Barcode.ExpectedDataElementSeparator;
var doesInputContainCrLf = input.IndexOf(crLf, StringComparison.Ordinal) >= 0;
Expand Down

0 comments on commit 43204ce

Please sign in to comment.