Пример #1
0
        /// <summary>
        /// Map the ini file
        /// </summary>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public IList <T> Map(IErrors errors)
        {
            IList <T> items = new List <T>();

            // Check inputs.
            if (_doc == null || _doc.DocumentElement.ChildNodes == null ||
                _doc.DocumentElement.ChildNodes.Count == 0)
            {
                return(items);
            }

            int counter = 0;
            var propMap = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);

            XmlElement  rootNode = _doc.DocumentElement;
            XmlNodeList nodes    = rootNode.ChildNodes;

            foreach (XmlNode node in nodes)
            {
                // Represents single object.
                T item = new T();

                // Properties represented as xml child nodes.
                if (node.HasChildNodes)
                {
                    foreach (XmlNode propNode in node.ChildNodes)
                    {
                        // Check if property exists
                        string propName = propNode.Name.ToLower();
                        if (propMap.ContainsKey(propName))
                        {
                            PropertyInfo prop = propMap[propName];
                            string       val  = propNode.InnerText;
                            MapperHelper.SetProperty(prop, item, counter, errors, val);
                        }
                    }
                }
                items.Add(item);
            }
            return(items);
        }
Пример #2
0
        /// <summary>
        /// Map the ini file
        /// </summary>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public IList <T> Map(IErrors errors)
        {
            IList <T> items = new List <T>();

            // Check inputs.
            if (_data == null || _data.Count == 0)
            {
                return(items);
            }

            int counter        = 0;
            var propMapDefault = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);

            foreach (DictionaryEntry entry in _data)
            {
                // Represents single object.
                if (entry.Value is IDictionary)
                {
                    Tuple2 <T, IDictionary <string, PropertyInfo> > tuple = GetNewObjectAndPropertyMapAsTuple((IDictionary)entry.Value, propMapDefault);
                    MapperHelper.MapTo <T>(tuple.First, counter, tuple.Second, entry.Value as IDictionary);
                    items.Add(tuple.First);
                    counter++;
                }
                // Multiple sections with the same name ( "post", "post" )
                else if (entry.Value is List <object> )
                {
                    List <object> sections = entry.Value as List <object>;
                    foreach (object section in sections)
                    {
                        if (section is IDictionary)
                        {
                            Tuple2 <T, IDictionary <string, PropertyInfo> > tuple = GetNewObjectAndPropertyMapAsTuple((IDictionary)section, propMapDefault);
                            MapperHelper.MapTo <T>(tuple.First, counter, tuple.Second, section as IDictionary);
                            items.Add(tuple.First);
                            counter++;
                        }
                    }
                }
            }
            return(items);
        }
Пример #3
0
        /// <summary>
        /// Map the ini file
        /// </summary>
        /// <param name="errors"></param>
        /// <returns></returns>
        public IList <T> Map(IErrors errors)
        {
            IList <T> items = new List <T>();

            // Check inputs.
            if (_doc == null || _doc.Columns.Count == 0)
            {
                return(items);
            }

            var propMap = ReflectionUtils.GetPropertiesAsMap <T>(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, false);
            int counter = 0;

            foreach (OrderedDictionary record in _doc.Data)
            {
                // Represents single object.
                T item = new T();
                MapperHelper.MapTo <T>(item, counter, propMap, record as IDictionary);
                items.Add(item);
                counter++;
            }
            return(items);
        }
Пример #4
0
        /// <summary>
        /// Map the properties in the data to the properties of the item T using the propMap supplied.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="counterOrRefId">An counter to help associate errors in mapping w/ a specific item index or reference id.</param>
        /// <param name="item">The object to set the properties on.</param>
        /// <param name="propMap">Property map containing the names of the properties that can be mapped.</param>
        /// <param name="data">The source of the data to map.</param>
        /// <param name="errors">Error collection.</param>
        public static void MapTo <T>(T item, int counterOrRefId, IDictionary <string, PropertyInfo> propMap, IDictionary data, IErrors errors)
        {
            var handledProps = new Dictionary <string, bool>();

            foreach (DictionaryEntry entry in data)
            {
                var          propname = entry.Key as string;
                var          val      = entry.Value;
                PropertyInfo prop     = null;
                if (propMap.ContainsKey(propname))
                {
                    prop = propMap[propname];
                    SetProperty(prop, item, counterOrRefId, errors, val);
                }
                else if (propMap.ContainsKey(propname.ToLower().Trim()))
                {
                    prop = propMap[propname.Trim().ToLower()];
                    SetProperty(prop, item, counterOrRefId, errors, val);
                }
                else if (propname.Contains("."))
                {
                    string objectname = propname.Substring(0, propname.IndexOf("."));
                    if (!handledProps.ContainsKey(objectname))
                    {
                        if (propMap.ContainsKey(objectname.ToLower()))
                        {
                            prop = propMap[objectname.ToLower().Trim()];
                            // Composite object.
                            object obj = Activator.CreateInstance(prop.PropertyType);
                            prop.SetValue(item, obj, null);
                            MapperHelper.MapTo(obj, data, objectname, errors);
                            handledProps[objectname] = true;
                        }
                    }
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Map the items to a csv text.
        /// </summary>
        /// <param name="items">The items to map</param>
        /// <param name="errors">The errors to collect.</param>
        public void MapToFile(IList <T> items, IDictionary <string, string> excludeProps, string path, IErrors errors)
        {
            var props = new List <KeyValuePair <string, PropertyInfo> >();

            MapperHelper.GetProps(typeof(T), string.Empty, props);

            List <List <object> > records = new List <List <object> >();

            foreach (T item in items)
            {
                var rec = new List <object>();
                MapperHelper.MapFrom(item, excludeProps, props, (colname, val) =>
                {
                    if (val != null)
                    {
                        rec.Add(val);
                    }
                    else
                    {
                        rec.Add("");
                    }
                });
                records.Add(rec);
            }
            List <string> cols = new List <string>();

            foreach (var pair in props)
            {
                cols.Add(pair.Key);
            }

            CsvWriter writer = new CsvWriter(@"c:\temp\conference.csv", records, ",", cols, false, false, "\"", Environment.NewLine, false);

            writer.Write();
            writer.Dispose();
        }