Beispiel #1
0
        /// <summary>
        ///     Builds a preference entry for the provided parent based on the provided XElement node
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="foundEntry">The found entry.</param>
        /// <returns></returns>
        private TPreferenceEntry BuildPreferenceEntry <TPreferenceEntry>(IPreference parent, XElement foundEntry)
            where TPreferenceEntry : class, IPreferenceEntry
        {
            // Rather ugly way of reading shared properties + returning the newly created entry
            // Several unplesant compromises here
            Func <IPreferenceEntry, TPreferenceEntry> readSharedProperties = entry =>
            {
                entry.FriendlyName = XElementHelper.ReadStringValue(foundEntry, "friendlyName");
                entry.Description  = XElementHelper.ReadStringValue(foundEntry, "description", false);
                entry.IsSelected   = XElementHelper.ReadBoolValue(foundEntry, "isDefault", false);

                //if (isSelectedByDefault && parent.HasDirectlyEditableValue)
                //{
                //    /*
                //     * This preference has both a list of pre-defined entries - one of which are selected by default -
                //     * and a default value specified in the config file. There is really no obvious way to know which
                //     * the user actually wanted to be the default value, so log this as a warning, and let the
                //     * pre-defined option override the directly specified default value.
                //     */

                //    parent.Value = name;
                //}

                return(entry as TPreferenceEntry);
            };

            if (parent is INumericPreference)
            {
                var entry = new NumericPreferenceEntry
                {
                    Parent = (INumericPreference)parent,
                    Name   = XElementHelper.ReadDoubleValue(foundEntry, "name")
                };

                return(readSharedProperties(entry));
            }
            if (parent is IDatePreference)
            {
                var dateFormat = ((IDatePreference)parent).DateFormat;

                var entry = new DatePreferenceEntry
                {
                    Parent = (IDatePreference)parent,
                    Name   = XElementHelper.ReadDateValue(foundEntry, "name", dateFormat)
                };

                return(readSharedProperties(entry));
            }
            if (parent is IStringPreference)
            {
                var entry = new StringPreferenceEntry
                {
                    Parent = (IStringPreference)parent,
                    Name   = XElementHelper.ReadStringValue(foundEntry, "name")
                };

                return(readSharedProperties(entry));
            }
            throw new NotSupportedException("Invalid parent object");
        }
        /// <summary>
        ///     Builds a preference entry for the provided parent based on the provided XElement node
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="foundEntry">The found entry.</param>
        /// <returns></returns>
        private TPreferenceEntry BuildPreferenceEntry <TPreferenceEntry>(IPreference parent, XElement foundEntry)
            where TPreferenceEntry : class, IPreferenceEntry
        {
            // Rather ugly way of reading shared properties + returning the newly created entry
            // Several unplesant compromises here
            Func <IPreferenceEntry, TPreferenceEntry> readSharedProperties = entry =>
            {
                entry.FriendlyName = XElementHelper.ReadStringValue(foundEntry, "friendlyName");
                entry.Description  = XElementHelper.ReadStringValue(foundEntry, "description", false);
                entry.IsSelected   = XElementHelper.ReadBoolValue(foundEntry, "isDefault", false);

                return(entry as TPreferenceEntry);
            };

            if (parent is INumericPreference)
            {
                var entry = new NumericPreferenceEntry
                {
                    Parent = (INumericPreference)parent,
                    Name   = XElementHelper.ReadDoubleValue(foundEntry, "name")
                };

                return(readSharedProperties(entry));
            }
            if (parent is IDatePreference)
            {
                var dateFormat = ((IDatePreference)parent).DateFormat;

                var entry = new DatePreferenceEntry
                {
                    Parent = (IDatePreference)parent,
                    Name   = XElementHelper.ReadDateValue(foundEntry, "name", dateFormat)
                };

                return(readSharedProperties(entry));
            }
            if (parent is IStringListPreference)
            {
                var entry = new StringListPreferenceEntry
                {
                    Parent = (IStringListPreference)parent,
                    Name   = XElementHelper.ReadStringValue(foundEntry, "name")
                };

                return(readSharedProperties(entry));
            }
            if (parent is IStringPreference)
            {
                var entry = new StringPreferenceEntry
                {
                    Parent = (IStringPreference)parent,
                    Name   = XElementHelper.ReadStringValue(foundEntry, "name")
                };

                return(readSharedProperties(entry));
            }
            throw new NotSupportedException("Invalid parent object");
        }
        protected override T OnBuildElement <T>(XElement element)
        {
            var category = new PreferenceCategory();

            category.FriendlyName = XElementHelper.ReadStringValue(element, "friendlyName");

            // Read the list of Preference tags
            var foundPreferences = XElementHelper.ReadEnumerable(element, "preference");

            // For each preference Tag, store the values into a new Preference object
            foreach (var foundPreference in foundPreferences)
            {
                // Ugly way of reading shared properties without duplicating code.
                Action <IPreference> readSharedProperties = preference =>
                {
                    preference.Name         = XElementHelper.ReadStringValue(foundPreference, "name");
                    preference.FriendlyName = XElementHelper.ReadStringValue(foundPreference, "friendlyName");
                    preference.Description  = XElementHelper.ReadStringValue(foundPreference, "description", false);

                    preference.HasDirectlyEditableValue = XElementHelper.ReadBoolValue(foundPreference,
                                                                                       "hasDirectlyEditableValue", false);

                    preference.useCurlyBraces = XElementHelper.ReadBoolValue(foundPreference,
                                                                             "useCurlyBraces", false);

                    preference.AllowMultipleSelections = XElementHelper.ReadBoolValue(foundPreference, "allowMultipleSelections", false);

                    category.Preferences.Add(preference);
                };

                // Determine preference type
                var isDate    = XElementHelper.ReadBoolValue(foundPreference, "isDate", false);
                var isNumeric = XElementHelper.ReadBoolValue(foundPreference, "isNumeric", false);
                var allowsMultipleSelections = XElementHelper.ReadBoolValue(foundPreference, "allowMultipleSelections", false);

                if (isDate)
                {
                    var preference = new DatePreference();

                    preference.DateFormat = XElementHelper.ReadStringValue(foundPreference, "dateFormat");
                    preference.MinValue   = XElementHelper.ReadDateValue(foundPreference, "minDateValue",
                                                                         preference.DateFormat, false);
                    preference.MaxValue = XElementHelper.ReadDateValue(foundPreference, "maxDateValue",
                                                                       preference.DateFormat, false);
                    preference.Value = XElementHelper.ReadDateValue(foundPreference, "value", preference.DateFormat,
                                                                    true);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IDatePreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else if (isNumeric)
                {
                    var preference = new NumericPreference();

                    preference.MinValue = XElementHelper.ReadDoubleValue(foundPreference, "minValue", false);
                    preference.MaxValue = XElementHelper.ReadDoubleValue(foundPreference, "maxValue", false);
                    preference.Value    = XElementHelper.ReadDoubleValue(foundPreference, "value", true);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <INumericPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else if (allowsMultipleSelections)
                {
                    var preference = new StringListPreference();

                    //preference.MinValue = XElementHelper.ReadStringValue(foundPreference, "minValue", false);
                    //preference.MaxValue = XElementHelper.ReadStringValue(foundPreference, "maxValue", false);
                    //preference.Value = XElementHelper.ReadStringValue(foundPreference, "value", false);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IStringListPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
                else
                {
                    var preference = new StringPreference();

                    preference.MinValue = XElementHelper.ReadStringValue(foundPreference, "minValue", false);
                    preference.MaxValue = XElementHelper.ReadStringValue(foundPreference, "maxValue", false);
                    preference.Value    = XElementHelper.ReadStringValue(foundPreference, "value", false);

                    // Read the list of entryOption tags
                    var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                    // For each tag, read the values into a new PreferenceEntry object
                    foreach (var entry in foundEntries)
                    {
                        preference.Entries.Add(BuildPreferenceEntry <IStringPreferenceEntry>(preference, entry));
                    }

                    readSharedProperties(preference);
                }
            }

            return(category as T);
        }
        /// <summary>
        /// Reads the category configuration.
        /// </summary>
        /// <returns></returns>
        private IList <PreferenceCategory> ReadCategoryConfig()
        {
            var categories     = new List <PreferenceCategory>();
            var configFileName = "Configuration.xml";

            var config = XmlHelper.ReadXmlFile(configFileName);

            if (config == null)
            {
                return(null);
            }

            try
            {
                // Read the list of categories from the xml
                var foundCategories = config.Descendants("category");

                // For each category, store the values in a new PreferenceCategory object.
                foreach (var foundCategory in foundCategories)
                {
                    var category = new PreferenceCategory();
                    category.FriendlyName = XElementHelper.ReadStringValue(foundCategory, "friendlyName");
                    categories.Add(category);

                    // Read the list of Preference tags
                    var foundPreferences = XElementHelper.ReadEnumerable(foundCategory, "preference");

                    // For each preference Tag, store the values into a new Preference object
                    foreach (var foundPreference in foundPreferences)
                    {
                        IPreference preference = new Preference();

                        preference.Name                     = XElementHelper.ReadStringValue(foundPreference, "name");
                        preference.FriendlyName             = XElementHelper.ReadStringValue(foundPreference, "friendlyName");
                        preference.Description              = XElementHelper.ReadStringValue(foundPreference, "description", false);
                        preference.MinValue                 = XElementHelper.ReadDoubleValue(foundPreference, "minValue", false);
                        preference.MaxValue                 = XElementHelper.ReadDoubleValue(foundPreference, "maxValue", false);
                        preference.Value                    = XElementHelper.ReadStringValue(foundPreference, "value", false);
                        preference.HasDirectlyEditableValue = XElementHelper.ReadBoolValue(foundPreference, "hasDirectlyEditableValue", false);

                        category.Preferences.Add(preference);

                        // Read the list of entryOption tags
                        var foundEntries = XElementHelper.ReadEnumerable(foundPreference, "entryOption", false);

                        // For each tag, read the values into a new PreferenceEntry object
                        foreach (var entry in foundEntries)
                        {
                            preference.Entries.Add(this.BuildPreferenceEntry(preference, entry));
                        }
                    }
                }
            }
            ////catch (MissingRequiredXMLPropertyException ex)
            ////{
            ////}
            ////catch (MalformedXMLException ex)
            ////{
            ////}
            ////catch (UnParsableDataValueException ex)
            ////{
            ////}
            catch (XMLParseExceptionBase ex)
            {
                this.ShowMalformedOrMissingXMLElementError(configFileName, ex);
            }
            catch (Exception ex)
            {
                this.options.Logger.AddLogEntry(new LogEntry(ex.Message + " - " + ex.InnerException, LogEntrySeverity.Error, LogEntrySource.UI));
            }

            return(categories);
        }