public static ATCOCIFFile ParseATCOCIFFile(string[] cifFileLines, bool dealWithTranslink = false) { ATCOCIFFileHeaderRecord fileHeaderRecord = (ATCOCIFFileHeaderRecord)ParseRecord(cifFileLines[0], CIFRecordIdentity.ATCOCIFFileHeader); ATCOCIFFile atcoCifFile = new ATCOCIFFile(fileHeaderRecord); for (int lineNumber = 0; lineNumber < cifFileLines.Length; lineNumber++) { if (lineNumber == 0) { continue; } string recordString = cifFileLines[lineNumber]; CIFRecordIdentity recordIdentity = IdentifyRecord(recordString); if (dealWithTranslink) { if (recordIdentity == CIFRecordIdentity.QB || recordIdentity == CIFRecordIdentity.QP || recordIdentity == CIFRecordIdentity.QQ || recordIdentity == CIFRecordIdentity.QV || recordIdentity == CIFRecordIdentity.QD) { continue; } } ATCOCIFRecord record = (ATCOCIFRecord)ParseRecord(recordString, recordIdentity); atcoCifFile.Records.Add(record); } return(atcoCifFile); }
public static Type CIFRecordIdentityToRecordClass(CIFRecordIdentity cifRecordIdentity) { Assembly cifRecordAssembly = typeof(CIFRecord).Assembly; Type[] cifRecordClasses = cifRecordAssembly.GetTypes().Where(type => type.IsSubclassOf(typeof(CIFRecord))).ToArray(); foreach (Type cifRecordClass in cifRecordClasses) { CIFRecordIdentityAttribute cifRecordIdentityAttribute = (CIFRecordIdentityAttribute)Attribute.GetCustomAttribute(cifRecordClass, typeof(CIFRecordIdentityAttribute)); if (cifRecordIdentityAttribute != null) { if (cifRecordIdentityAttribute.cifRecordIdentity == cifRecordIdentity) { return(cifRecordClass); } } } throw new Exception("No record class exists for this record identity."); }
public static RJISCIFFile ParseRJISCIFFile(string[] cifFileLines) { RJISCIFFileHeaderRecord fileHeaderRecord = (RJISCIFFileHeaderRecord)ParseRecord(cifFileLines[0], CIFRecordIdentity.HD); RJISCIFFile rjisCifFile = new RJISCIFFile(fileHeaderRecord); for (int lineNumber = 0; lineNumber < cifFileLines.Length; lineNumber++) { if (lineNumber == 0) { continue; } string recordString = cifFileLines[lineNumber]; CIFRecordIdentity recordIdentity = IdentifyRecord(recordString); RJISCIFRecord record = (RJISCIFRecord)ParseRecord(recordString, recordIdentity); rjisCifFile.Records.Add(record); } return(rjisCifFile); }
public static CIFRecord ParseRecord(string recordString, CIFRecordIdentity cifRecordIdentity) { Type cifRecordClass = CIFRecordIdentityToRecordClass(cifRecordIdentity); dynamic recordToReturn = Activator.CreateInstance(cifRecordClass); foreach (PropertyInfo property in cifRecordClass.GetProperties()) { CIFRecordFieldLocationAttribute cifRecordFieldLocationAttribute = (CIFRecordFieldLocationAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldLocationAttribute)); if (cifRecordFieldLocationAttribute != null) { CIFRecordFieldConvertToBoolAttribute cifRecordFieldConvertToBoolAttribute = (CIFRecordFieldConvertToBoolAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldConvertToBoolAttribute)); CIFRecordFieldConvertToDateTimeAttribute cifRecordFieldConvertToDateTimeAttribute = (CIFRecordFieldConvertToDateTimeAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldConvertToDateTimeAttribute)); string fieldStringValue = recordString.Substring(cifRecordFieldLocationAttribute.startPosition - 1, cifRecordFieldLocationAttribute.length); object propertyValue; if (cifRecordFieldConvertToBoolAttribute != null) { if (fieldStringValue == cifRecordFieldConvertToBoolAttribute.trueString) { propertyValue = true; } else if (fieldStringValue == cifRecordFieldConvertToBoolAttribute.falseString) { propertyValue = false; } else if (string.IsNullOrWhiteSpace(fieldStringValue)) { propertyValue = false; } else { throw new Exception("Malformed record."); } } else if (cifRecordFieldConvertToDateTimeAttribute != null) { try { propertyValue = DateTime.ParseExact(fieldStringValue, cifRecordFieldConvertToDateTimeAttribute.format, CultureInfo.InvariantCulture); } catch (FormatException) { if (fieldStringValue == "99999999") { propertyValue = null; } else { throw new Exception("Invalid DateTime value."); } } } else { propertyValue = fieldStringValue; } property.SetValue(recordToReturn, propertyValue); } } return(recordToReturn); }
public CIFRecordIdentityAttribute(CIFRecordIdentity cifRecordIdentity) { this.cifRecordIdentity = cifRecordIdentity; }