Пример #1
0
        /// <summary>
        /// Gets a settings at the specific path.
        /// </summary>
        public TSetting Get <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions) where TSetting : class, new()
        {
            SettingsManager containingManager;

            return(Get <TSetting>(path, searchOptions, out containingManager));
        }
Пример #2
0
        /// <summary>
        /// Gets a settings at the specific path.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <returns></returns>
        public TSetting Get <TSetting>(
            string path,
            SettingSearchOptions searchOptions) where TSetting : class, new()
        {
            var hierarchicalPath = new HierarchicalPath(path);

            return(Get <TSetting>(hierarchicalPath, searchOptions));
        }
Пример #3
0
        /// <summary>
        /// Tries to get settings at the given path without creating it if
        /// missing.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public bool TryGet <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions,
            out TSetting output) where TSetting : class, new()
        {
            SettingsManager manager;

            return(TryGet(path, searchOptions, out output, out manager));
        }
Пример #4
0
        /// <summary>
        /// Tries to get settings at the given path without creating it if
        /// missing.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <param name="containingManager">The containing manager.</param>
        /// <returns></returns>
        public bool TryGet <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions,
            out SettingsManager containingManager) where TSetting : class, new()
        {
            TSetting output;

            return(TryGet(path, searchOptions, out output, out containingManager));
        }
Пример #5
0
        /// <summary>
        /// Gets a settings at the specific path.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <param name="containingManager">The containing manager.</param>
        /// <returns></returns>
        public TSetting Get <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions,
            out SettingsManager containingManager) where TSetting : class, new()
        {
            // Attempt to retrieve the item from the collection.
            SettingSearchOptions newSearchOptions = searchOptions
                                                    | SettingSearchOptions.SerializeDeserializeMapping;

            TSetting output;

            if (TryGet(path, newSearchOptions, out output, out containingManager))
            {
                return(output);
            }

            // We couldn't find it, so create a new one and store it.
            output = new TSetting();
            objectSettings[path] = output;
            xmlSettings.Remove(path);

            return(output);
        }
Пример #6
0
        /// <summary>
        /// Determines whether the settings contain the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <returns>
        ///     <c>true</c> if [contains] [the specified path]; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(
            HierarchicalPath path,
            SettingSearchOptions searchOptions)
        {
            // If this settings has a setting for this specific path, then
            // attempt to map it. If we can't map it, return false.
            if (ContainsInternal(path))
            {
                return(true);
            }

            // We aren't in the exact path of this item, so we need to search
            // for the parent paths or up the HierarchicalPath reference.
            bool searchParent = (searchOptions
                                 & SettingSearchOptions.SearchParentSettings)
                                == SettingSearchOptions.SearchParentSettings;
            bool searchPaths = (searchOptions
                                & SettingSearchOptions.SearchHierarchicalParents)
                               == SettingSearchOptions.SearchHierarchicalParents;
            bool parentSettingsFirst = (searchOptions
                                        & SettingSearchOptions.ParentSettingsFirst)
                                       == SettingSearchOptions.ParentSettingsFirst;

            // If we are searching neither, then we couldn't find it.
            if (!searchParent &&
                !searchPaths)
            {
                return(false);
            }

            // Check to see if we are searching parents first.
            if (searchParent &&
                parentSettingsFirst &&
                Parent != null)
            {
                // We are searching the parent. We only have the parent search
                // its parents since we'll be searching the path in the next
                // step.
                if (Parent.Contains(path, SettingSearchOptions.SearchParentSettings))
                {
                    return(true);
                }
            }

            // Search the path three until we get to the top.
            if (searchPaths && path.Count != 1)
            {
                // Grab the current path.
                HierarchicalPath currentPath = path.Parent;

                while (true)
                {
                    // Try to retrieve the object.
                    if (Contains(currentPath, searchOptions))
                    {
                        // We found it through recursion.
                        return(true);
                    }

                    // We couldn't find it, so move up a level.
                    if (currentPath.Count == 1)
                    {
                        break;
                    }

                    currentPath = currentPath.Parent;
                }
            }

            // We have found it so far, so check to see if we are searching
            // parents
            if (searchParent &&
                !parentSettingsFirst &&
                Parent != null)
            {
                // We are searching the parent. We only have the parent search
                // its parents since we'll be searching the path in the next
                // step.
                if (Contains(path, SettingSearchOptions.SearchParentSettings))
                {
                    return(true);
                }
            }

            // We couldn't find it.
            return(false);
        }
Пример #7
0
        /// <summary>
        /// Attempts to map the setting in the collection to the desired goal.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The options.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        private bool TryMap <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions,
            out TSetting output) where TSetting : class, new()
        {
            // Check to see if we have an object already deserialized.
            bool mapObject = GetSerializeDeserializeMapping(searchOptions);

            // Check to see if we have an object already deserialized.
            if (objectSettings.ContainsKey(path))
            {
                // Pull it out and see if we can do anything with it.
                object input = objectSettings[path];

                if (input == null)
                {
                    output = null;
                    return(false);
                }

                // Check to see if we can cast the object into the new type.
                output = input as TSetting;

                if (output != null)
                {
                    return(true);
                }

                // If we aren't mapping, then we can't do anything so we need
                // to return false.
                if (!mapObject)
                {
                    return(false);
                }

                // We cannot cast it directly, but we are going to attempt to
                // deserialize it in a different type. So, we need to flush it
                // back to XML for the deserialization.
                Flush(path);
            }

            // We either never had this object deserialized or we couldn't map
            // it to the input, so deserialize it from the XML stream.
            var outputDeserializer = new XmlSerializer(typeof(TSetting));
            var reader             = new StringReader(xmlSettings[path]);

            try
            {
                output = (TSetting)outputDeserializer.Deserialize(reader);
            }
            catch
            {
                output = null;
                return(false);
            }

            // Remove it from the XML dictionary and put it into the object store.
            objectSettings[path] = output;
            xmlSettings.Remove(path);

            // Return the results.
            return(true);
        }
Пример #8
0
 private static bool GetSerializeDeserializeMapping(
     SettingSearchOptions searchOptions)
 {
     return((searchOptions & SettingSearchOptions.SerializeDeserializeMapping)
            == SettingSearchOptions.SerializeDeserializeMapping);
 }
Пример #9
0
 private bool GetSearchPaths(SettingSearchOptions searchOptions)
 {
     return((searchOptions & SettingSearchOptions.SearchHierarchicalParents)
            == SettingSearchOptions.SearchHierarchicalParents);
 }
Пример #10
0
 private static bool GetSearchParent(SettingSearchOptions searchOptions)
 {
     return((searchOptions & SettingSearchOptions.SearchParentSettings)
            == SettingSearchOptions.SearchParentSettings);
 }
Пример #11
0
 private bool GetParentSettingsFirst(SettingSearchOptions searchOptions)
 {
     return((searchOptions & SettingSearchOptions.ParentSettingsFirst)
            == SettingSearchOptions.ParentSettingsFirst);
 }
Пример #12
0
        /// <summary>
        /// Tries to get settings at the given path without creating it if missing.
        /// </summary>
        /// <typeparam name="TSetting">The type of the setting.</typeparam>
        /// <param name="path">The path.</param>
        /// <param name="searchOptions">The search options.</param>
        /// <param name="output">The output.</param>
        /// <param name="containingManager">
        /// The manager that actually contained the settings retrieved.
        /// </param>
        /// <returns></returns>
        public bool TryGet <TSetting>(
            HierarchicalPath path,
            SettingSearchOptions searchOptions,
            out TSetting output,
            out SettingsManager containingManager) where TSetting : class, new()
        {
            // Try the current settings for the key.
            if (ContainsInternal(path))
            {
                // Attempt to map the current path item. Depending on option
                // settings, this will serialize/deserialize objects that don't
                // match.
                if (TryMap(path, searchOptions, out output))
                {
                    containingManager = this;
                    return(true);
                }

                // We couldn't map it.
                output            = null;
                containingManager = null;
                return(false);
            }

            // We aren't in the exact path of this item, so we need to search
            // for the parent paths or up the path for an entry.
            bool searchParent        = GetSearchParent(searchOptions);
            bool searchPaths         = GetSearchPaths(searchOptions);
            bool parentSettingsFirst = GetParentSettingsFirst(searchOptions);

            // If we are searching neither, then we couldn't find it.
            if (!searchParent &&
                !searchPaths)
            {
                containingManager = this;
                output            = default(TSetting);
                return(false);
            }

            // Check to see if we are searching parents first.
            if (searchParent &&
                parentSettingsFirst &&
                Parent != null)
            {
                // We are searching the parent. We only have the parent search
                // its parents since we'll be searching the path in the next
                // step.
                if (Parent.TryGet(
                        path,
                        SettingSearchOptions.SearchParentSettings,
                        out output,
                        out containingManager))
                {
                    return(true);
                }
            }

            // Search the path three until we get to the top.
            if (searchPaths && path.Count != 1)
            {
                // Grab the current path.
                HierarchicalPath currentPath = path.Parent;

                while (true)
                {
                    // Try to retrieve the object.
                    if (TryGet(currentPath, searchOptions, out output, out containingManager))
                    {
                        // We found it through recursion.
                        return(true);
                    }

                    // We couldn't find it, so move up a level.
                    if (currentPath.Count == 1)
                    {
                        break;
                    }

                    currentPath = currentPath.Parent;
                }
            }

            // We have found it so far, so check to see if we are searching
            // parents
            if (searchParent &&
                !parentSettingsFirst &&
                Parent != null)
            {
                // We are searching the parent. We only have the parent search
                // its parents since we'll be searching the path in the next
                // step.
                if (Parent.TryGet(
                        path,
                        SettingSearchOptions.SearchParentSettings,
                        out output,
                        out containingManager))
                {
                    return(true);
                }
            }

            // If we got this far, we couldn't find it with the given settings. So, we set the default
            // value and return false.
            output            = default(TSetting);
            containingManager = null;
            return(false);
        }