Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RoutedList{T}"/> class that contains elements, route type, route mode, direction and current index copied from the source and has sufficient capacity to accommodate the number of elements copied.
 /// </summary>
 /// <param name="source"></param>
 public RoutedList(RoutedList <T> source) : base(source)
 {
     _remainder   = source._remainder;
     RouteType    = source.RouteType;
     RouteMode    = source.RouteMode;
     _increase    = source._increase;
     CurrentIndex = source.CurrentIndex;
     BeginIndex   = source.BeginIndex;
     EndIndex     = source.EndIndex;
     NextIndex    = source.NextIndex;
 }
Example #2
0
        /// <summary>
        /// Creates an object from its <see cref="XmlElement"/> representation.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="id_source"></param>
        /// <returns></returns>
        public static RoutedList <T> ToObject(XmlElement source, ref IDictionary <string, object> id_source)
        {
            Type type = typeof(RoutedList <T>);

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (id_source is null)
            {
                throw new ArgumentNullException(nameof(id_source));
            }
            if (source.HasAttribute("id"))
            {
                if (id_source.ContainsKey(source.GetAttribute("id")))
                {
                    throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" has already been created.");
                }
            }
            if (source.HasAttribute("ref"))
            {
                if (id_source.ContainsKey(source.GetAttribute("ref")))
                {
                    if (id_source[source.GetAttribute("ref")] is RoutedList <T> _source)
                    {
                        return(_source);
                    }
                    else
                    {
                        throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must be \"{type.FullName}\".");
                    }
                }
                else
                {
                    throw new XmlException($"The object with id=\"{source.GetAttribute("id")}\" must have been created earlier.");
                }
            }
            RoutedList <T> routedList = new RoutedList <T>();

            if (source.HasAttribute("id"))
            {
                id_source.Add(source.GetAttribute("id"), routedList);
            }
            XmlNodeList xmlEntriesList = source.ChildNodes;

            foreach (XmlElement xmlElement in xmlEntriesList)
            {
                switch (xmlElement.Name)
                {
                case "RouteType":
                    RouteType routeType;
                    if (Enum.TryParse(xmlElement.InnerText, out routeType))
                    {
                        routedList.RouteType = routeType;
                    }
                    else
                    {
                        throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\".");
                    }
                    break;

                case "RouteMode":
                    RouteMode routeMode;
                    if (Enum.TryParse(xmlElement.InnerText, out routeMode))
                    {
                        routedList.RouteMode = routeMode;
                    }
                    else
                    {
                        throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\".");
                    }
                    break;

                case "Increase":
                    if (xmlElement.InnerText == bool.TrueString)
                    {
                        routedList._increase = true;
                    }
                    else if (xmlElement.InnerText == bool.FalseString)
                    {
                        routedList._increase = false;
                    }
                    else
                    {
                        throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\".");
                    }
                    break;

                case "CurrentIndex":
                    if (xmlElement.InnerText == "null")
                    {
                        routedList.CurrentIndex = null;
                    }
                    else if (int.TryParse(xmlElement.InnerText, out int currentIndex))
                    {
                        routedList.CurrentIndex = currentIndex;
                    }
                    else
                    {
                        throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\".");
                    }
                    break;

                case "Entries":
                    List <T> list = routedList as List <T>;
                    foreach (XmlElement xmlEntry in xmlElement.ChildNodes)
                    {
                        if (xmlEntry.Name != "Entry")
                        {
                            throw new XmlException("The \"Entries\" node can only contain \"Entry\" nodes.");
                        }
                        else if (CyclicalMethods.ToObject(xmlEntry, ref id_source) is T t)
                        {
                            list.Add(t);
                        }
                        else
                        {
                            throw new XmlException($"Invalid data in the node \"{xmlElement.Name}\".");
                        }
                    }
                    break;
                }
            }
            routedList._increase = routedList.Increase;
            routedList.Update();
            return(routedList);
        }