public Player(dynamic obj) { Id = ChoUtility.CastTo <int>(obj.Id); Sea = ChoUtility.CastTo <int>(obj.Sea); First = obj.First; Last = obj.Last; Team = obj.Team; Coll = obj.Coll; Num = ChoUtility.CastTo <int>(obj.Num); Age = ChoUtility.CastTo <int>(obj.Age); Hgt = ChoUtility.CastTo <int>(obj.Hgt); Wgt = ChoUtility.CastTo <int>(obj.Wgt); Pos = obj.Pos; Flg = String.IsNullOrEmpty(obj.Flg) ? "None" : obj.Flg; Trait = String.IsNullOrEmpty(obj.Trait) ? "None" : obj.Trait; Attr = new PlayerAttr(); Attr.Str = ChoUtility.CastTo <int>(obj.Attr_Str); Attr.Agi = ChoUtility.CastTo <int>(obj.Attr_Agi); Per = new PlayerPer(); Per.Lea = ChoUtility.CastTo <int>(obj.Per_Lea); Per.Wor = ChoUtility.CastTo <int>(obj.Per_Wor); Skills = new PlayerSkills(); Skills.WR = ChoUtility.CastTo <int>(obj.Skills_WR); Skills.TE = ChoUtility.CastTo <int>(obj.Skills_TE); }
public void ReadXml(System.Xml.XmlReader reader) { XmlSerializer keySerializer = ChoUtility.GetXmlSerializer(typeof(TKey)); // new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = ChoUtility.GetXmlSerializer(typeof(TValue)); //new XmlSerializer(typeof(TValue)); bool wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) { return; } while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { reader.ReadStartElement("item"); reader.ReadStartElement("key"); TKey key = (TKey)keySerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement("value"); TValue value = (TValue)valueSerializer.Deserialize(reader); reader.ReadEndElement(); this.Add(key, value); reader.ReadEndElement(); reader.MoveToContent(); } reader.ReadEndElement(); }
public static void Test() { List <Customer> allCustomers = GetAllCustomers(); var result = allCustomers .Select(customer => new[] { customer.FirstName, customer.LastName } .Concat(customer.CreditCards.Select(cc => cc.Name)) .Concat(customer.Addresses.Select(address => address.Street))); foreach (var c in result) { Console.WriteLine(ChoUtility.ToStringEx(c.ToList().ToExpandoObject())); } return; // Customer has CreditCards list and Addresses list // how to flatten Customer, CreditCards list, and Addresses list into one flattened record/list? var flatenned = from c in allCustomers select c.FirstName + ", " + c.LastName + ", " + String.Join(", ", c.CreditCards.Select(c2 => c2.Name).ToArray()) + ", " + String.Join(", ", c.Addresses.Select(a => a.Street).ToArray()); flatenned.ToList().ForEach(Console.WriteLine); }
private string ResolveNGetPropetyValue(string format, string propertyName) { string configValue = null; string cmdLineArgValue = ChoEnvironment.GetCmdLineArgValue(propertyName); if (!cmdLineArgValue.IsNullOrWhiteSpace()) { return(cmdLineArgValue); } configValue = ChoETLFramework.GetConfigValue(propertyName); if (!configValue.IsNullOrWhiteSpace()) { try { if (format.IsNullOrWhiteSpace()) { return(configValue); } else { return(ChoUtility.Format(format, configValue)); } } catch { } } return(propertyName); }
private void GetXml(StringBuilder msg, object value, string key, ChoNullValueHandling nullValueHandling, string nsPrefix = null, bool isCDATA = false, bool emitDataType = false) { if (value is ChoDynamicObject) { msg.AppendFormat("{0}{1}", Environment.NewLine, ((ChoDynamicObject)value).GetXml(((ChoDynamicObject)value).NName, nullValueHandling, nsPrefix).Indent(1, " ")); } else { if (value != null) { if (value.GetType().IsSimple()) { if (isCDATA) { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}><![CDATA[{1}]]></{0}>".FormatString(key, value).Indent(1, " ")); } else { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>{1}</{0}>".FormatString(key, value).Indent(1, " ")); } } else { key = value is IList?key.ToPlural() != key?key.ToPlural() : key.Length > 1 && key.EndsWith("s", StringComparison.InvariantCultureIgnoreCase) ? key : "{0}s".FormatString(key) : key; msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>".FormatString(key).Indent(1, " ")); if (value is IList) { foreach (var collValue in ((IList)value).OfType <ChoDynamicObject>()) { msg.AppendFormat("{0}{1}", Environment.NewLine, collValue.GetXml(collValue.NName == DefaultName ? key.ToSingular() : collValue.NName, nullValueHandling, nsPrefix).Indent(2, " ")); } } else { msg.AppendFormat("{0}{1}", Environment.NewLine, ChoUtility.XmlSerialize(value).Indent(2, " ")); } msg.AppendFormat("{0}{1}", Environment.NewLine, "</{0}>".FormatString(key).Indent(1, " ")); } } else { switch (nullValueHandling) { case ChoNullValueHandling.Empty: msg.AppendFormat("{0}{1}", Environment.NewLine, @"<{0}/>".FormatString(key).Indent(1, " ")); break; case ChoNullValueHandling.Ignore: break; default: msg.AppendFormat("{0}{1}", Environment.NewLine, @"<{0} xsi:nil=""true"" xmlns:xsi=""{1}""/>".FormatString(key, ChoXmlSettings.XmlSchemaInstanceNamespace).Indent(1, " ")); break; } } } }
static void LoadTextTest() { string txt = "Id, Name\r\n1, Mark"; foreach (var e in ChoCSVReader.LoadText(txt).WithFirstLineHeader()) { Console.WriteLine(ChoUtility.ToStringEx(e)); } }
public string ReplaceProperty(string propertyName, string format) { if (String.IsNullOrEmpty(propertyName)) { return(propertyName); } switch (propertyName) { case "CURRENT_DIRECTORY": return(ChoUtility.Format(format, Environment.CurrentDirectory)); case "MACHINE_NAME": return(ChoUtility.Format(format, Environment.MachineName)); case "OS_VERSION": return(ChoUtility.Format(format, Environment.OSVersion)); case "PROCESSOR_COUNT": return(ChoUtility.Format(format, Environment.ProcessorCount)); case "SYSTEM_DIRECTORY": return(ChoUtility.Format(format, Environment.SystemDirectory)); case "SYSTEM_PAGE_SIZE": return(ChoUtility.Format(format, Environment.SystemPageSize)); case "TICK_COUNT": return(ChoUtility.Format(format, Environment.TickCount)); case "USER_DOMAIN_NAME": return(ChoUtility.Format(format, Environment.UserDomainName)); case "USER_NAME": return(ChoUtility.Format(format, Environment.UserName)); case "VERSION": return(ChoUtility.Format(format, Environment.Version)); case "WORKING_SET": return(ChoUtility.Format(format, Environment.WorkingSet)); default: { Environment.SpecialFolder specialFolder; if (Enum.TryParse <Environment.SpecialFolder>(propertyName, out specialFolder)) { return(ChoUtility.Format(format, Environment.GetFolderPath(specialFolder))); } else { return(ChoUtility.Format(format, Environment.GetEnvironmentVariable(propertyName))); } } } }
static void Sample8() { using (var jr = new ChoJSONReader <DataMapper>("sample8.json")) { foreach (var x in jr) { Console.WriteLine(ChoUtility.DumpAsJson(x)); } } }
public static void Sample15() { using (var parser = new ChoXmlReader("sample15.xml") ) { foreach (dynamic rec in parser) { Console.WriteLine(ChoUtility.Dump(rec)); } } }
static void Sample6() { using (var parser = new ChoXmlReader <JobApplication>("sample6.xml") ) { foreach (dynamic rec in parser) { Console.WriteLine(ChoUtility.Dump(rec)); } } }
//public static bool SetFieldErrorMsg(this object target, string fieldName, string msg) //{ // PropertyInfo pi = null; // if (ChoType.HasProperty(target.GetType(), "FieldErrorMsg", out pi) // && !ChoType.IsReadOnlyMember(pi)) // ChoType.SetPropertyValue(target, pi, msg); // else // return false; // return true; //} //public static bool SetErrorMsg(this object target, string msg) //{ // //if (target is ChoRecord) // // ((ChoRecord)target).SetErrorMsg(msg); // //else // //{ // MethodInfo mi = null; // if (ChoType.HasMethod(target.GetType(), "SetErrorMsg", new Type[] { typeof(string) }, out mi)) // ChoType.SetPropertyValue(target, pi, msg); // else // return false; // //} // return true; //} //public static string GetErrorMsg(this object target) //{ // //if (target is ChoRecord) // // return ((ChoRecord)target).GetErrorMsg(); // //else // //{ // PropertyInfo pi = null; // if (ChoType.HasProperty(target.GetType(), "ErrorMsg", out pi)) // return ChoType.GetPropertyValue(target, pi).CastTo<string>(); // else // return null; // //} //} public static string GetXml(this object target, string tag = null) { if (target is ChoDynamicObject) { return(((ChoDynamicObject)target).GetXml()); } else { return(ChoUtility.XmlSerialize(target)); } }
static void Sample11Test() { using (var parser = new ChoXmlReader("sample11.xml").WithXPath("/members/father") .WithField("id") .WithField("sons") ) { foreach (dynamic rec in parser) { Console.WriteLine(ChoUtility.DumpAsJson(rec.sons)); } } }
/// <inheritdoc /> public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); object targetObj = Activator.CreateInstance(objectType); dynamic ctx = serializer.Context.Context; StringComparison comparision = ctx != null? ChoUtility.CastTo <StringComparison>(ctx.StringComparision, StringComparison.InvariantCultureIgnoreCase) : StringComparison.InvariantCultureIgnoreCase; foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite)) { string jsonPath = null; ChoJSONPathAttribute att1 = prop.GetCustomAttributes(true) .OfType <ChoJSONPathAttribute>() .FirstOrDefault(); if (att1 != null && !att1.JSONPath.IsNullOrWhiteSpace()) { jsonPath = att1.JSONPath; } else if (att1 == null) { JsonPropertyAttribute att = prop.GetCustomAttributes(true) .OfType <JsonPropertyAttribute>() .FirstOrDefault(); jsonPath = att != null ? att.PropertyName : null; // prop.Name; } if (serializer.ContractResolver is DefaultContractResolver) { var resolver = (DefaultContractResolver)serializer.ContractResolver; jsonPath = resolver.GetResolvedPropertyName(jsonPath); } JToken token = jsonPath.IsNullOrWhiteSpace() ? jo.GetProperty(prop.Name, comparision) : jo.SelectToken(jsonPath); if (token != null && token.Type != JTokenType.Null) { object value = token.ToObject(prop.PropertyType, serializer); prop.SetValue(targetObj, value, null); } } return(targetObj); }
public string ReplaceProperty(string propertyName, string format) { if (String.IsNullOrEmpty(propertyName)) { return(propertyName); } switch (propertyName) { case "APPLICATION_NAME": return(ChoUtility.Format(format, Process.GetCurrentProcess().ProcessName)); case "PROCESS_ID": return(ChoUtility.Format(format, Process.GetCurrentProcess().Id)); case "THREAD_ID": return(ChoUtility.Format(format, Thread.CurrentThread.ManagedThreadId)); case "THREAD_NAME": return(ChoUtility.Format(format, Thread.CurrentThread.Name)); case "RANDOM_NO": ChoCryptoRandom rnd = new ChoCryptoRandom(); return(ChoUtility.Format(format, rnd.Next())); case "TODAY": if (String.IsNullOrEmpty(format)) { return(GetTodaysDate().ToShortDateString()); } else { return(ChoUtility.Format(format, GetTodaysDate())); } case "NOW": if (String.IsNullOrEmpty(format)) { return(GetNowTime().ToShortTimeString()); } else { return(ChoUtility.Format(format, GetNowTime())); } default: return(ResolveNGetPropetyValue(format, propertyName)); } }
public static object CreateMoqInstance(Type objType, ChoIniFile iniFile = null) { //if (typeof(ChoRecord).IsAssignableFrom(objType)) // return CreateDynamicMoqInstance(objType, iniFile); object obj = ChoActivator.CreateInstanceAndInit(objType); foreach (KeyValuePair <MemberInfo, Attribute> kv in ChoUtility.DiscoverMembers(objType, typeof(ChoRandomAttribute))) { if (!(kv.Value is ChoRandomAttribute)) { continue; } ChoType.SetMemberValue(obj, kv.Key, ((ChoRandomAttribute)kv.Value).NextValue()); } return(obj); }
static void Sample7() { using (var jr = new ChoJSONReader <Family>("sample7.json").WithJSONPath("$.fathers")) { foreach (var x in jr) { Console.WriteLine(x.Id); foreach (var fm in x.Daughters) { Console.WriteLine(fm); } } } return; using (var jr = new ChoJSONReader("sample7.json").WithJSONPath("$.fathers") .WithField("id") .WithField("married") .WithField("name") .WithField("sons") .WithField("daughters", fieldType: typeof(Dictionary <string, object>[])) ) { foreach (var item in jr) { var x = item.id; Console.WriteLine(x.GetType()); Console.WriteLine(item.id); Console.WriteLine(item.married); Console.WriteLine(item.name); foreach (dynamic son in item.sons) { var x1 = son.address; //Console.WriteLine(ChoUtility.ToStringEx(son.address.street)); } foreach (var daughter in item.daughters) { Console.WriteLine(ChoUtility.ToStringEx(daughter)); } } } }
public void WriteXml(System.Xml.XmlWriter writer) { XmlSerializer keySerializer = ChoUtility.GetXmlSerializer(typeof(TKey)); // new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = ChoUtility.GetXmlSerializer(typeof(TValue)); //new XmlSerializer(typeof(TValue)); foreach (TKey key in this.Keys) { writer.WriteStartElement("item"); writer.WriteStartElement("key"); keySerializer.Serialize(writer, key); writer.WriteEndElement(); writer.WriteStartElement("value"); TValue value = this[key]; valueSerializer.Serialize(writer, value); writer.WriteEndElement(); writer.WriteEndElement(); } }
public static void Sample14() { using (var w = new ChoXmlWriter("sample14out.xml")) { using (var parser = new ChoXmlReader("sample14.xml") ) { foreach (dynamic rec in parser) { //dynamic x = rec.description; //rec.description = new ChoDynamicObject(); //rec.description.val = "100"; //rec.description.Value = new FamilyMember(); w.Write(rec); Console.WriteLine(ChoUtility.Dump(rec)); } } } }
static void QuickPOCOTest() { //List<EmployeeRecSimple> objs = new List<EmployeeRecSimple>(); //EmployeeRecSimple rec1 = new EmployeeRecSimple(); //rec1.Id = null; //rec1.Name = "Mark"; //rec1.Depends = new List<string>() { "AA", "BB" }; //rec1.Courses = new Dictionary<int, string>() { { 1, "AA" }, { 2, "BB" } }; //objs.Add(rec1); //EmployeeRecSimple rec2 = new EmployeeRecSimple(); //rec2.Id = "2"; //rec2.Name = null; //objs.Add(rec2); //using (var parser = new ChoXmlWriter<EmployeeRecSimple>("Emp.xml").WithXPath("Employees/Employee")) //{ // parser.Write(objs); //} // using (var reader = new ChoXmlReader("emp.xml").WithXPath("Employees/Employee") //.WithField("Id") //.WithField("Name") //.WithField("Depends", isArray: false, fieldType: typeof(List<string>)) //.WithField("KVP", isArray: false, fieldType: typeof(List<ChoKeyValuePair<int, string>>)) //) // { // foreach (var i in reader) // Console.WriteLine(ChoUtility.ToStringEx(i)); // } using (var reader = new ChoXmlReader <EmployeeRecSimple>("emp.xml").WithXPath("Employees/Employee")) { foreach (var i in reader) { Console.WriteLine(ChoUtility.ToStringEx(i)); } } }
static void Sample12() { using (var jr = new ChoJSONReader("sample12.json") ) { foreach (var x1 in jr) { foreach (var z1 in x1) { dynamic newObj = new ChoDynamicObject(); newObj.name = z1.Key; foreach (var kvp in (ChoDynamicObject)z1.Value) { ((ChoDynamicObject)newObj).AddOrUpdate(kvp.Key, kvp.Value); } Console.WriteLine(ChoUtility.DumpAsJson(newObj)); } } } }
public static void AABillingTest() { using (var p = new ChoFixedLengthReader("AABilling.txt") .WithRecordSelector(0, 1, null, typeof(AABillingDetailRecord), typeof(AABillingTrailerRecord), typeof(AABillingHeaderRecord)) //.WithCustomRecordSelector((l) => //{ // Tuple<long, string> kvp = l as Tuple<long, string>; // if (kvp.Item2.StartsWith("H")) // return typeof(AABillingHeaderRecord); // else if (kvp.Item2.StartsWith("T")) // return typeof(AABillingTrailerRecord); // else // return typeof(AABillingDetailRecord); //}) ) { foreach (var rec in p) { Console.WriteLine(ChoUtility.Dump(rec)); } } }
public override string ToString() { StringBuilder msg = new StringBuilder(Name.IsNullOrWhiteSpace() ? "ExpandoObject Properties" : "{0} ExpandoObject Properties".FormatString(Name)); if (_properties != null) { foreach (string key in _properties.Keys) { if (!(_properties[key] is ChoPropertyBag)) { msg.AppendFormat("{0}: {1}{2}".FormatString(key, ChoUtility.ToStringEx(_properties[key]))); } } foreach (string key in _properties.Keys) { if (_properties[key] is ChoPropertyBag) { msg.AppendFormat("{0}".FormatString(ChoUtility.ToStringEx(_properties[key]))); } } } return(msg.ToString()); }
public string GetXml(string tag = null) { if (tag.IsNullOrWhiteSpace()) { tag = NName; } bool hasAttrs = false; StringBuilder msg = new StringBuilder("<{0}".FormatString(tag)); foreach (string key in this.Keys.Where(k => k.StartsWith("@") && k != "@@Value")) { hasAttrs = true; msg.AppendFormat(@" {0}=""{1}""", key.Substring(1), this[key]); } if (ContainsKey("@@Value")) { if (hasAttrs) { msg.AppendFormat(">"); msg.AppendFormat("{0}{1}", Environment.NewLine, this["@@Value"].ToNString().Indent(1, " ")); msg.AppendFormat("{0}</{1}>", Environment.NewLine, tag); } else { msg.AppendFormat(">"); msg.AppendFormat("{0}", this["@@Value"].ToNString()); msg.AppendFormat("</{0}>", tag); } } else if (this.Keys.Any(k => !k.StartsWith("@"))) { object value = null; msg.AppendFormat(">"); foreach (string key in this.Keys.Where(k => !k.StartsWith("@"))) { value = this[key]; if (value is ChoDynamicObject) { msg.AppendFormat("{0}{1}", Environment.NewLine, ((ChoDynamicObject)value).GetXml(((ChoDynamicObject)value).NName).Indent(1, " ")); } else { if (value != null) { if (value.GetType().IsSimple()) { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>{1}</{0}>".FormatString(key, value).Indent(1, " ")); } else { msg.AppendFormat("{0}{1}", Environment.NewLine, "<{0}>".FormatString(key).Indent(1, " ")); if (value is IList) { foreach (var collValue in ((IList)value).OfType <ChoDynamicObject>()) { msg.AppendFormat("{0}{1}", Environment.NewLine, collValue.GetXml(collValue.NName == "dynamic" ? key.ToSingular() : collValue.NName).Indent(2, " ")); } } else { msg.AppendFormat("{0}{1}", Environment.NewLine, ChoUtility.XmlSerialize(value).Indent(2, " ")); } msg.AppendFormat("{0}{1}", Environment.NewLine, "</{0}>".FormatString(key).Indent(1, " ")); } } else { } } } msg.AppendFormat("{0}</{1}>", Environment.NewLine, tag); } else { msg.AppendFormat(" />"); } return(msg.ToString()); }
static ChoActivator() { ChoUtility.Init(); }
public override bool TryConvert(ConvertBinder binder, out object result) { result = ChoUtility.CastObjectTo(Value, binder.Type, _dynamicObject == null ? null : ChoType.GetDefaultValue(binder.Type)); return(true); }
public string Dump() { return(ChoUtility.ToStringEx(this)); }
public string DumpAsJson() { return(ChoUtility.DumpAsJson(this)); }
private bool ToText(long index, object rec, out string recText) { if (typeof(IChoScalarObject).IsAssignableFrom(Configuration.RecordType)) { rec = Activator.CreateInstance(Configuration.RecordType, rec); } recText = null; if (rec == null) { if (Configuration.NullValueHandling == ChoNullValueHandling.Ignore) { return(false); } else if (Configuration.NullValueHandling == ChoNullValueHandling.Default) { rec = Activator.CreateInstance(Configuration.RecordType); } else { recText = @"<{0} xsi:nil=""true"" />".FormatString(Configuration.NodeName ).Indent(Configuration.Indent * 1, Configuration.IndentChar.ToString()); return(true); } } StringBuilder msg = new StringBuilder(); object fieldValue = null; string fieldText = null; ChoXmlRecordFieldConfiguration fieldConfig = null; if (Configuration.ColumnCountStrict) { CheckColumnsStrict(rec); } //bool firstColumn = true; PropertyInfo pi = null; bool isElementClosed = false; bool isElementStart = false; foreach (KeyValuePair <string, ChoXmlRecordFieldConfiguration> kvp in GetOrderedKVP()) { fieldConfig = kvp.Value; fieldValue = null; fieldText = String.Empty; if (Configuration.PIDict != null) { Configuration.PIDict.TryGetValue(kvp.Key, out pi); } if (Configuration.ThrowAndStopOnMissingField) { if (Configuration.IsDynamicObject) { var dict = rec.ToDynamicObject() as IDictionary <string, Object>; if (!dict.ContainsKey(kvp.Key)) { throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Xml node.".FormatString(fieldConfig.FieldName)); } } else { if (pi == null) { throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Xml node.".FormatString(fieldConfig.FieldName)); } } } try { if (Configuration.IsDynamicObject) { IDictionary <string, Object> dict = rec.ToDynamicObject() as IDictionary <string, Object>; fieldValue = dict[kvp.Key]; // dict.GetValue(kvp.Key, Configuration.FileHeaderConfiguration.IgnoreCase, Configuration.Culture); if (kvp.Value.FieldType == null) { if (fieldValue == null) { kvp.Value.FieldType = typeof(string); } else { kvp.Value.FieldType = fieldValue.GetType(); } } } else { if (pi != null) { fieldValue = ChoType.GetPropertyValue(rec, pi); if (kvp.Value.FieldType == null) { kvp.Value.FieldType = pi.PropertyType; } } else { kvp.Value.FieldType = typeof(string); } } //Discover default value, use it if null if (fieldValue == null) { if (fieldConfig.IsDefaultValueSpecified) { fieldValue = fieldConfig.DefaultValue; } } if (!RaiseBeforeRecordFieldWrite(rec, index, kvp.Key, ref fieldValue)) { return(false); } if (fieldConfig.ValueConverter != null) { fieldValue = fieldConfig.ValueConverter(fieldValue); } else { rec.GetNConvertMemberValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue, true); } if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.MemberLevel) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } if (!RaiseAfterRecordFieldWrite(rec, index, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { if (Configuration.IsDynamicObject) { var dict = rec.ToDynamicObject() as IDictionary <string, Object>; if (dict.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else if (dict.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else { throw new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); } } else if (pi != null) { if (rec.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (rec.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else { throw new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); } } else { throw new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); } } catch (Exception innerEx) { if (ex == innerEx.InnerException) { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else { if (!RaiseRecordFieldWriteError(rec, index, kvp.Key, fieldText, ex)) { throw new ChoWriterException($"Failed to write '{fieldValue}' value of '{kvp.Key}' member.", ex); } } } else { throw new ChoWriterException("Failed to use '{0}' fallback value for '{1}' member.".FormatString(fieldValue, kvp.Key), innerEx); } } } if (fieldValue == null) { if (!fieldConfig.IsXmlAttribute && fieldConfig.IsNullable) { if (Configuration.RecordType == typeof(ChoScalarObject)) { if (!isElementStart) { msg.Append(@"<{0} xsi:nil=""true""".FormatString(Configuration.NodeName).Indent(Configuration.Indent, Configuration.IndentChar.ToString())); isElementStart = true; } if (!isElementClosed) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } } else { if (!isElementStart) { msg.Append("<{0}".FormatString(Configuration.NodeName).Indent(Configuration.Indent, Configuration.IndentChar.ToString())); isElementStart = true; } if (!isElementClosed) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } msg.Append(@"<{0} xsi:nil=""true"" />{1}".FormatString(fieldConfig.FieldName, Configuration.EOLDelimiter).Indent(Configuration.Indent * 2, Configuration.IndentChar.ToString())); } } else { if (Configuration.RecordType != typeof(ChoScalarObject)) { if (!isElementStart) { msg.Append("<{0}".FormatString(Configuration.NodeName).Indent(Configuration.Indent, Configuration.IndentChar.ToString())); isElementStart = true; } } //isElementClosed = true; fieldText = String.Empty; } } else { if (!isElementStart) { msg.Append("<{0}".FormatString(Configuration.NodeName).Indent(Configuration.Indent, Configuration.IndentChar.ToString())); isElementStart = true; } if (fieldValue.GetType().IsSimple()) { fieldText = fieldValue.ToString(); if (Configuration.RecordType == typeof(ChoScalarObject)) { if (fieldConfig.IsXmlAttribute) { msg.Append(@" {0}=""{1}""".FormatString(fieldConfig.FieldName, NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType), GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false))); } else { if (!isElementClosed) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } msg.Append("{0}{1}".FormatString( NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType), GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false), Configuration.EOLDelimiter).Indent(Configuration.Indent * 2, Configuration.IndentChar.ToString())); } } else if (fieldConfig.IsXmlAttribute) { msg.Append(@" {0}=""{1}""".FormatString(fieldConfig.FieldName, NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType), GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false))); } else { if (!isElementClosed) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } msg.Append("<{0}>{1}</{0}>{2}".FormatString(fieldConfig.FieldName, NormalizeFieldValue(kvp.Key, fieldText, kvp.Value.Size, kvp.Value.Truncate, kvp.Value.QuoteField, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType), GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false), Configuration.EOLDelimiter).Indent(Configuration.Indent * 2, Configuration.IndentChar.ToString())); } } else { fieldText = ChoUtility.XmlSerialize(fieldValue); fieldText = _beginTagRegex.Replace(fieldText, delegate(Match thisMatch) { return("<{0}>".FormatString(fieldConfig.FieldName)); }); fieldText = _endTagRegex.Replace(fieldText, delegate(Match thisMatch) { return("</{0}>".FormatString(fieldConfig.FieldName)); }); if (!isElementClosed) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } msg.Append("{0}{1}".FormatString(fieldText, Configuration.EOLDelimiter).Indent(Configuration.Indent * 2, Configuration.IndentChar.ToString())); } } } if (!isElementClosed && msg.Length > 0) { msg.AppendFormat(">{0}", Configuration.EOLDelimiter); isElementClosed = true; } if (isElementStart) { msg.Append("</{0}>".FormatString(Configuration.NodeName).Indent(Configuration.Indent, Configuration.IndentChar.ToString())); isElementStart = false; } recText = msg.ToString(); return(true); }
public override IEnumerable <object> WriteTo(object writer, IEnumerable <object> records, Func <object, bool> predicate = null) { TextWriter sw = writer as TextWriter; ChoGuard.ArgumentNotNull(sw, "TextWriter"); if (records == null) { yield break; } CultureInfo prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; _se = new Lazy <XmlSerializer>(() => Configuration.XmlSerializer == null ? null : Configuration.XmlSerializer); string recText = String.Empty; try { foreach (object record in records) { _index++; if (TraceSwitch.TraceVerbose) { if (record is IChoETLNameableObject) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(((IChoETLNameableObject)record).Name)); } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Writing [{0}] object...".FormatString(_index)); } } recText = String.Empty; if (predicate == null || predicate(record)) { //Discover and load Xml columns from first record if (!_configCheckDone) { if (record == null) { continue; } string[] fieldNames = null; Type recordType = ElementType == null?record.GetType() : ElementType; if (typeof(ICollection).IsAssignableFrom(recordType)) { recordType = recordType.GetEnumerableItemType().GetUnderlyingType(); } else { recordType = recordType.GetUnderlyingType(); } Configuration.IsDynamicObject = recordType.IsDynamicType(); if (!Configuration.IsDynamicObject) { if (recordType.IsSimple()) { Configuration.RecordType = typeof(ChoScalarObject <>).MakeGenericType(recordType); } else { Configuration.RecordType = recordType; } } if (Configuration.IsDynamicObject) { var dict = record.ToDynamicObject() as IDictionary <string, Object>; fieldNames = dict.Keys.ToArray(); } else { fieldNames = ChoTypeDescriptor.GetProperties <ChoXmlNodeRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray(); if (fieldNames.Length == 0) { fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray(); } } Configuration.Validate(fieldNames); _configCheckDone = true; if (!RaiseBeginWrite(sw)) { yield break; } sw.Write("<{0}{1}>".FormatString(Configuration.RootName, GetNamespaceText())); } if (!RaiseBeforeRecordWrite(record, _index, ref recText)) { yield break; } if (recText == null) { continue; } try { if (!Configuration.UseXmlSerialization) { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.ObjectLevel) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (ToText(_index, record, out recText)) { if (!recText.IsNullOrEmpty()) { sw.Write("{1}{0}", recText, Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, recText)) { yield break; } } } else { if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.Off) != ChoObjectValidationMode.Off) { record.DoObjectLevelValidation(Configuration, Configuration.XmlRecordFieldConfigurations); } if (record != null) { if (_se.Value != null) { _se.Value.Serialize(sw, record); } else { sw.Write("{1}{0}", ChoUtility.XmlSerialize(record).Indent(2, Configuration.IndentChar.ToString()), Configuration.EOLDelimiter); } if (!RaiseAfterRecordWrite(record, _index, null)) { yield break; } } } } //catch (ChoParserException) //{ // throw; //} catch (Exception ex) { ChoETLFramework.HandleException(ex); if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordWriteError(record, _index, recText, ex)) { throw; } else { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message)); } } else { throw; } } } yield return(record); if (Configuration.NotifyAfter > 0 && _index % Configuration.NotifyAfter == 0) { if (RaisedRowsWritten(_index)) { ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Abort requested."); yield break; } } } } finally { System.Threading.Thread.CurrentThread.CurrentCulture = prevCultureInfo; } }