public void FullClassBuildingFixed() { var cb = new FixedLengthClassBuilder("Customers"); cb.AddField("Field1", 8, typeof (DateTime)); cb.LastField.Converter.Kind = ConverterKind.Date; cb.LastField.Converter.Arg1 = "ddMMyyyy"; cb.LastField.FieldNullValue = DateTime.Now; cb.AddField("Field2", 3, typeof (string)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = ' '; cb.AddField("Field3", 3, typeof (int)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = '0'; cb.LastField.TrimMode = TrimMode.Both; mEngine = new FileHelperEngine(cb.CreateRecordClass()); DataTable dt = mEngine.ReadFileAsDT(FileTest.Good.Test1.Path); Assert.AreEqual(4, dt.Rows.Count); Assert.AreEqual(4, mEngine.TotalRecords); Assert.AreEqual(0, mEngine.ErrorManager.ErrorCount); }
public void SaveLoadXmlFileFixed() { var cb = new FixedLengthClassBuilder("Customers"); cb.FixedMode = FixedMode.ExactLength; cb.AddField("Field1", 8, typeof (DateTime)); cb.LastField.Converter.Kind = ConverterKind.Date; cb.LastField.Converter.Arg1 = "ddMMyyyy"; cb.LastField.FieldNullValue = DateTime.Now; cb.AddField("FieldSecond", 3, typeof (string)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = ' '; cb.AddField("Field33", 3, typeof (int)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = '0'; cb.LastField.TrimMode = TrimMode.Both; cb.SaveToXml(@"dynamic.xml"); var loaded = (FixedLengthClassBuilder)ClassBuilder.LoadFromXml(@"dynamic.xml"); Assert.AreEqual("Field1", loaded.FieldByIndex(0).FieldName); Assert.AreEqual("FieldSecond", loaded.FieldByIndex(1).FieldName); Assert.AreEqual("Field33", loaded.FieldByIndex(2).FieldName); Assert.AreEqual("System.DateTime", loaded.FieldByIndex(0).FieldType); Assert.AreEqual("System.String", loaded.FieldByIndex(1).FieldType); Assert.AreEqual("System.Int32", loaded.FieldByIndex(2).FieldType); Assert.AreEqual(ConverterKind.Date, loaded.FieldByIndex(0).Converter.Kind); Assert.AreEqual("ddMMyyyy", loaded.FieldByIndex(0).Converter.Arg1); Assert.AreEqual(AlignMode.Right, loaded.FieldByIndex(1).AlignMode); Assert.AreEqual(' ', loaded.FieldByIndex(1).AlignChar); Assert.AreEqual(FixedMode.ExactLength, loaded.FixedMode); }
// FIXED LENGTH private void CreateFixedLengthOptions(string[][] data, List<RecordFormatInfo> res) { RecordFormatInfo format = new RecordFormatInfo(); double average = CalculateAverageLineWidth(data); double deviation = CalculateDeviationLineWidth(data, average); format.mConfidence = (int)(Math.Max(0, 1 - deviation / average) * 100); FixedLengthClassBuilder builder = new FixedLengthClassBuilder("AutoDetectedClass"); CreateFixedLengthFields(data, builder); format.mClassBuilder = builder; res.Add(format); }
private void CreateFixedLengthFields(string[][] data, FixedLengthClassBuilder builder) { List<FixedColumnInfo> res = null; foreach (string[] dataFile in data) { List<FixedColumnInfo> candidates = CreateFixedLengthCandidates(dataFile); res = JoinFixedColCandidates(res, candidates); } for (int i = 0; i < res.Count; i++) { FixedColumnInfo col = res[i]; builder.AddField("Field" + i.ToString().PadLeft(4, '0'), col.Length, typeof(string)); } }
public ClassBuilder CommonCreate() { var cb = new FixedLengthClassBuilder("Customers"); cb.AddField("Field1", 8, typeof (DateTime)); cb.LastField.Converter.Kind = ConverterKind.Date; cb.LastField.Converter.Arg1 = "ddMMyyyy"; cb.LastField.FieldNullValue = DateTime.Now; cb.AddField("Field2", 3, typeof (string)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = ' '; cb.AddField("Field3", 3, typeof (int)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = '0'; cb.LastField.TrimMode = TrimMode.Both; return cb; }
public void SaveLoadXmlFileFixed2() { var cb = new FixedLengthClassBuilder("Customers"); cb.AddField("Field1", 8, typeof (DateTime)); cb.LastField.Converter.Kind = ConverterKind.Date; cb.LastField.Converter.Arg1 = "ddMMyyyy"; cb.LastField.FieldNullValue = DateTime.Now; cb.AddField("FieldSecond", 3, typeof (string)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = ' '; cb.AddField("Field33", 3, typeof (int)); cb.LastField.AlignMode = AlignMode.Right; cb.LastField.AlignChar = '0'; cb.LastField.TrimMode = TrimMode.Both; cb.SaveToXml(@"runtime.xml"); engine = new FileHelperEngine(ClassBuilder.ClassFromXmlFile("runtime.xml")); Assert.AreEqual("Customers", engine.RecordType.Name); Assert.AreEqual(3, engine.RecordType.GetFields().Length); Assert.AreEqual("Field1", engine.RecordType.GetFields()[0].Name); }
/// <summary> /// Loads the XML representation of a ClassBuilder inheritor and return /// it. (for XML saved with SaveToXml method) /// </summary> /// <remarks> /// ClassBuilder inheritors: <see cref="DelimitedClassBuilder"/> or <see cref="FixedLengthClassBuilder"/> /// </remarks> /// <param name="document">The XML document with the representation of the record class.</param> /// <returns>A new instance of a ClassBuilder inheritor: <see cref="DelimitedClassBuilder"/> or <see cref="FixedLengthClassBuilder"/> </returns> public static ClassBuilder LoadFromXml(XmlDocument document) { ClassBuilder res = null; string classtype = document.DocumentElement.LocalName; if (classtype == "DelimitedClass") { res = DelimitedClassBuilder.LoadXmlInternal(document); } else { res = FixedLengthClassBuilder.LoadXmlInternal(document); } XmlNode node = document.DocumentElement["IgnoreLastLines"]; if (node != null) { res.IgnoreLastLines = int.Parse(node.InnerText); } node = document.DocumentElement["IgnoreFirstLines"]; if (node != null) { res.IgnoreFirstLines = int.Parse(node.InnerText); } node = document.DocumentElement["IgnoreEmptyLines"]; if (node != null) { res.IgnoreEmptyLines = true; } node = document.DocumentElement["CommentMarker"]; if (node != null) { res.IgnoreCommentedLines.CommentMarker = node.InnerText; } node = document.DocumentElement["CommentInAnyPlace"]; if (node != null) { res.IgnoreCommentedLines.InAnyPlace = bool.Parse(node.InnerText.ToLower()); } node = document.DocumentElement["SealedClass"]; res.SealedClass = node != null; node = document.DocumentElement["Namespace"]; if (node != null) { res.Namespace = node.InnerText; } node = document.DocumentElement["Visibility"]; if (node != null) { res.Visibility = (NetVisibility)Enum.Parse(typeof(NetVisibility), node.InnerText); } node = document.DocumentElement["RecordCondition"]; if (node != null) { res.RecordCondition.Condition = (RecordCondition)Enum.Parse(typeof(RecordCondition), node.InnerText); } node = document.DocumentElement["RecordConditionSelector"]; if (node != null) { res.RecordCondition.Selector = node.InnerText; } node = document.DocumentElement["CommentText"]; if (node != null) { res.CommentText = node.InnerText; } res.ReadClassElements(document); node = document.DocumentElement["Fields"]; XmlNodeList nodes; if (classtype == "DelimitedClass") { nodes = node.SelectNodes("/DelimitedClass/Fields/Field"); } else { nodes = node.SelectNodes("/FixedLengthClass/Fields/Field"); } foreach (XmlNode n in nodes) { res.ReadField(n); } return(res); }
// FIXED LENGTH private void CreateFixedLengthOptions(string[][] data, List<RecordFormatInfo> res) { var format = new RecordFormatInfo(); var stats = Indicators.CalculateAsFixedSize (data); format.mConfidence = (int)(Math.Max (0, 1 - stats.Deviation / stats.Avg) * 100); var builder = new FixedLengthClassBuilder("AutoDetectedClass"); CreateFixedLengthFields(data, builder); format.mClassBuilder = builder; res.Add(format); }