private void WriteData(CIFHeader header, ICIFWriterProtocol protocol) { Debug.Assert(_gatheredFieldNames != null && _gatheredFieldNames.Count > 0, "Field names should have been gathered here"); // DATA _writer.WriteLine("DATA"); var enumerator = protocol.GetCIFItemEnumerator(); foreach (var item in enumerator) { _writer.WriteLine(item.ToCSV(_gatheredFieldNames)); } // ENDOFDATA _writer.WriteLine("ENDOFDATA"); }
// TODO IsCollection internal static CIFItem Parse(CIFHeader header, string itemData) { var item = new CIFItem(); if (string.IsNullOrEmpty(itemData)) return item; var values = ParseCSV(itemData, false); // TODO use header encoding for item data here var names = header.FieldNamesList; if (names != null && names.Any()) { var namesArray = names.ToArray(); var valuesArray = values.ToArray(); // Field names are specified in header Debug.Assert(valuesArray.Length == namesArray.Length, "Field names count != field values count"); for (int i = 0; i < namesArray.Length; ++i) { var name = namesArray[i]; var value = valuesArray[i]; // Handle special cases like CIF Hashes in CIFItem instance item[name] = !string.IsNullOrEmpty(value) ? value : null; } } else { // Field names are not specified, so use the (12) default fields from CIF 2.1 var namesArray = Constants.RequiredFields; var valuesArray = values.ToArray(); for (int i = 0; i < namesArray.Length; ++i) { var name = namesArray[i]; var value = valuesArray[i]; // Handle special cases like CIF Hashes in CIFItem instance item[name] = !string.IsNullOrEmpty(value) ? value : null; } } return item; }
/// <summary> /// Tries to parse <paramref name="headerData"/> as a /// <see cref="CIFHeader"/>. /// </summary> /// <param name="headerData">Header data as string</param> /// <returns>An instance of <see cref="CIF"/>.</returns> internal static CIFHeader Parse(string headerData) { var header = new CIFHeader(); var lines = headerData.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); for(int i=0; i<lines.Length; ++i) { var line = lines[i]; if (i == 0) { // First line is version header.Version = line; continue; } var kvp = line.Split(new[] {':'}, 2, StringSplitOptions.RemoveEmptyEntries); var key = kvp[0]; var value = kvp[1]; if (0 == string.Compare(key, "CURRENCY", StringComparison.InvariantCultureIgnoreCase)) { header.Currency = value.Trim(); continue; } if (0 == string.Compare(key, "CHARSET", StringComparison.InvariantCultureIgnoreCase)) { header.Charset = value.Trim(); continue; } if (0 == string.Compare(key, "CODEFORMAT", StringComparison.InvariantCultureIgnoreCase)) { header.CodeFormat = value.Trim(); continue; } if (0 == string.Compare(key, "COMMENTS", StringComparison.InvariantCultureIgnoreCase)) { header.Comments = value.Trim(); continue; } if (0 == string.Compare(key, "DUNS", StringComparison.InvariantCultureIgnoreCase)) { header.DUNS = bool.Parse(value.Trim()); continue; } if (0 == string.Compare(key, "FIELDNAMES", StringComparison.InvariantCultureIgnoreCase)) { header.FieldNames = value.Trim(); continue; } if (0 == string.Compare(key, "ITEMCOUNT", StringComparison.InvariantCultureIgnoreCase)) { header.ItemCount = int.Parse(value.Trim(), NumberStyles.Integer, CultureInfo.InvariantCulture); continue; } if (0 == string.Compare(key, "LOADMODE", StringComparison.InvariantCultureIgnoreCase)) { header.LoadMode = value.Trim().FirstOrDefault(); continue; } if (0 == string.Compare(key, "SUPPLIERID_DOMAIN", StringComparison.InvariantCultureIgnoreCase)) { header.SupplierIDDomain = value.Trim(); continue; } if (0 == string.Compare(key, "TIMESTAMP", StringComparison.InvariantCultureIgnoreCase)) { header.Timestamp = value.Trim(); continue; } if (0 == string.Compare(key, "UNUOM", StringComparison.InvariantCultureIgnoreCase)) { header.UNUoM = bool.Parse(value.Trim()); continue; } } return header; }
public abstract void HandleHeader(CIFHeader header);
private void WriteHeader(CIFHeader header, ICIFWriterProtocol protocol) { _writer.WriteLine(header.Version); if (header.Currency != null) _writer.WriteLine("CURRENCY:" + header.Currency); if (header.Charset != null) _writer.WriteLine("CHARSET:" + header.Charset); if (header.CodeFormat != null) _writer.WriteLine("CODEFORMAT:" + header.CodeFormat); if (header.Comments != null) _writer.WriteLine("COMMENTS:" + header.Comments.Replace("\r\n", " ")); if (header.DUNS.HasValue) _writer.WriteLine("DUNS:" + (header.DUNS.Value ? "True" : "False")); _writer.WriteLine("ITEMCOUNT:" + header.ItemCount); _writer.WriteLine("LOADMODE:" + header.LoadMode); if (header.SupplierIDDomain != null) _writer.WriteLine("SUPPLIERID_DOMAIN:" + header.SupplierIDDomain); if (header.Timestamp != null) _writer.WriteLine("TIMESTAMP:" + header.Timestamp); if (!header.UNUoM.HasValue) _writer.WriteLine("UNUOM:" + (header.UNUoM.Value ? "True" : "False")); // Always writes all fields here, regardless of whether they're set // There's room for optimization here _gatheredFieldNames = GatherFieldNamesInOrder(protocol); _writer.WriteLine("FIELDNAMES:" + string.Join(",", _gatheredFieldNames)); }
private void WriteTrailer(CIFHeader header, ICIFWriterProtocol protocol) { var trailer = protocol.GetCIFTrailer(); if (trailer != null) { // TODO write TRAILER } }