/// <summary>
 /// Will search the <paramref name="configuration"/> for the specified <paramref name="searchGroupAddressAndItemName"/>.
 /// </summary>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="searchGroupAddressAndItemName">
 /// The Group Address and Item Name to search for.
 /// The Group Address and Item Name must be separated by the <see cref="GroupNameSeparator"/>.
 /// The Group Address can also include subgroups separated by the <see cref="GroupNameSeparator"/>.
 /// </param>
 /// <returns>If found, will return the <see cref="IConfigurationItem"/> with matching <paramref name="searchGroupAddressAndItemName"/>; otherwise, if not found, will return <b>null</b>.</returns>
 public static IConfigurationItem SearchForGroupItem(IConfigurationGroup configuration, string searchGroupAddressAndItemName)
 {
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     if (string.IsNullOrWhiteSpace(searchGroupAddressAndItemName))
     {
         throw new ArgumentNullException(nameof(searchGroupAddressAndItemName));
     }
     //
     if (searchGroupAddressAndItemName.Contains(GroupNameSeparator))
     {
         // search all groups
         var groupAddress = searchGroupAddressAndItemName.Substring(0, searchGroupAddressAndItemName.LastIndexOf(GroupNameSeparator));
         var itemName     = searchGroupAddressAndItemName.Substring(searchGroupAddressAndItemName.LastIndexOf(GroupNameSeparator) + 1);
         return(SearchForGroupItem(configuration, groupAddress, itemName));
     }
     else
     {
         // search local group's item
         var itemName = searchGroupAddressAndItemName;
         foreach (var item in configuration.Items)
         {
             if (item.Key.Equals(itemName, StringComparison.InvariantCultureIgnoreCase))
             {
                 return(item);
             }
         }
         //
         return(null);
     }
 }
Esempio n. 2
0
        // ######## Find...

        internal static IConfigurationGroup FindGroup(IConfigurationGroup searchGroup, string groupKey, char groupNameSeparator)
        {
            var gk = groupKey;

            if (!gk.Contains(groupNameSeparator))
            {
                // check for existing group
                var foundExisting = searchGroup.FirstOrDefault(x => { return(x.Key == gk); });
                if (foundExisting != null)
                {
                    return(foundExisting);
                }
                else
                {
                    var newGroup = new ConfigurationGroup(groupKey);
                    searchGroup.Add(newGroup);
                    return(newGroup);
                }
            }
            else
            {
                var parts = gk.Split(groupNameSeparator);
                gk = parts[0];
                // ----
                var subGroup = FindSubGroup(searchGroup, gk);
                if (subGroup != null)
                {
                    var dd    = groupKey.Substring(groupKey.IndexOf(groupNameSeparator) + 1);
                    var group = FindGroup(subGroup, dd, groupNameSeparator);
                    return(group);
                }
            }
            return(FindSubGroup(searchGroup, gk));
        }
        // ################ INFORMATION

        /// <summary>
        /// Returns all of the Group Addresses found in the given <paramref name="configuration"/>.
        /// </summary>
        /// <param name="configuration">The <see cref="IConfigurationGroup"/> to retrieve the Group Addresses from.</param>
        /// <returns>All of the Group Addresses found in the given <paramref name="configuration"/>.</returns>
        public static IEnumerable <string> AllGroupAddresses(IConfigurationGroup configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            //
            var list = new List <string>();

            foreach (var group in configuration)
            {
                AllGroupAddressesRecursiveFunc(group, list);
            }
            //
            return(list);


            // ################################
            // Internal Methods
            // ################################

            void AllGroupAddressesRecursiveFunc(IConfigurationGroup group_, List <string> list_)
            {
                if (!list_.Contains(group_.GroupAddress))
                {
                    list_.Add(group_.GroupAddress);
                    foreach (var subGroup in group_)
                    {
                        AllGroupAddressesRecursiveFunc(subGroup, list_);
                    }
                }
            }
        }
        /// <summary>
        /// Finds the named <see cref="IConfigurationItem"/> in the given configuration group and tests it's type and returns an instance of the <paramref name="configuration"/> strongly typed to <typeparamref name="T"/>.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> the configuration item should be instantiated into.</typeparam>
        /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
        /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
        /// <param name="throwException">If the item is not found, <b>True</b> will throw an <see cref="Exception"/>, <b>false</b> will return null.</param>
        /// <returns>An instance of the <paramref name="configuration"/> strongly typed to <typeparamref name="T"/>.</returns>
        public static T InstantiateTypeFromConfigurationItem <T>(IConfigurationGroup configuration, string name, bool throwException)
        {
            Type searchType = typeof(T);

            if (!ConfigurationShared._AllKnownTypes.Contains(searchType))
            {
                // if it is not a known type then assume it is a "System.Type"
                searchType = typeof(Type);
            }
            //
            var item = FindConfigurationItem(configuration, name, searchType);

            if (item != null)
            {
                if (searchType == typeof(Type))
                {
                    var typeActual = Type.GetType(item.Value as string);
                    return((T)Common.InstantiationHelper.InvokeDefaultCtor(typeActual));
                }
                else
                {
                    return((T)item.Value);
                }
            }
            //
            if (throwException)
            {
                throw new Exception($"Configuration missing item. Configuration must have item: '{name}'.");
            }
            return(default(T));
        }
 public ConfigurationGroupDataItem(IConfigurationGroup configurationGroup, DataGroup @group, int configurationItemsCount)
     : base(configurationGroup.UniqueId, configurationGroup.Name, "", @group, 55, 40)
 {
     ConfigurationState = configurationGroup.GroupConfigurationState;
     _configurationGroup = configurationGroup;
     _configurationItemsCount = configurationItemsCount;
 }
        /// <summary>
        /// Adds the <see cref="IConfigurationGroup"/> to the collection.
        /// </summary>
        /// <param name="item">The <see cref="IConfigurationGroup"/> to add.</param>
        /// <returns>The added <see cref="IConfigurationGroup"/>.</returns>
        public IConfigurationGroup Add(IConfigurationGroup item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            IConfigurationGroup oldItem = null;

            if (Contains(item))
            {
                oldItem = _SubGroups[item.Key];
                Remove(oldItem);
                (oldItem as IConfigurationGroupAdvanced).Parent = null;
            }
            _SubGroups.Add(item.Key, item);
            (item as IConfigurationGroupAdvanced).Parent = this;
            MarkDirty();
            if (oldItem == null)
            {
                RaiseCollectionChangedEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
            }
            else
            {
                RaiseCollectionChangedEvent(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, oldItem));
            }
            return(item);
        }
Esempio n. 7
0
        private void InternalSerializeGroup(string parentGroupAddress, IConfigurationGroup group, StringBuilder buffer, bool includeTypeInformation, int indentionDepth)
        {
            // get local INI Group Address
            var address = group.Key;

            if (!string.IsNullOrWhiteSpace(parentGroupAddress))
            {
                address = parentGroupAddress + _IniGroupNameSeparator_ + group.Key;
            }
            // create group header
            if (!string.IsNullOrWhiteSpace(address))
            {
                // group name
                buffer.AppendLine($"{_IniGroupNameStartCharacter_}{address}{_IniGroupNameStopCharacter_}");
            }
            // process items
            if (group.Items.Count > 0)
            {
                // serialize items
                var prepend = (string.IsNullOrWhiteSpace(address)) ? "" : (new string(' ', indentionDepth));
                foreach (var item in group.Items)
                {
                    buffer.AppendLine(prepend + InternalSerializeItem(item, includeTypeInformation));
                }
            }
            // process groups
            if (group.Count > 0)
            {
                foreach (var subGroup in group)
                {
                    InternalSerializeGroup(address, subGroup, buffer, includeTypeInformation, indentionDepth);
                }
            }
        }
 private void RecursiveSerializeGroup(IConfigurationGroup rootGroup, XmlWriter xw)
 {
     xw.WriteStartElement(_XmlElementGroupName_);
     xw.WriteAttributeString(_XmlElementGroupKeyName_, rootGroup.Key);
     if (rootGroup.Items.Count > 0)
     {
         xw.WriteStartElement(_XmlElementGroupItemsName_);
         foreach (var item in rootGroup.Items)
         {
             xw.WriteStartElement(_XmlElementItemName_);
             xw.WriteAttributeString(_XmlElementItemKeyName_, item.Key);
             xw.WriteAttributeString(_XmlElementItemTypeName_, ConfigurationShared.ConvertItemTypeToString(item));
             xw.WriteString(ConfigurationShared.ConvertItemObjectToString(item, _Compressor));
             xw.WriteEndElement();
         }
         xw.WriteEndElement();
     }
     if (rootGroup.Count > 0)
     {
         xw.WriteStartElement(_XmlElementGroupsName_);
         foreach (var subGroup in rootGroup)
         {
             RecursiveSerializeGroup(subGroup, xw);
         }
         xw.WriteEndElement();
     }
     xw.WriteEndElement();
 }
        // ----------------
        /// <summary>
        /// Will search the <paramref name="configuration"/> for the specified <paramref name="searchGroupAddress"/> and <paramref name="searchItemName"/>.
        /// </summary>
        /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
        /// <param name="searchGroupAddress">The Group Address to search for. This can include subgroups separated by the <see cref="GroupNameSeparator"/>.</param>
        /// <param name="searchItemName">The Item to search for.</param>
        /// <returns>If found, will return the <see cref="IConfigurationItem"/> with matching <paramref name="searchGroupAddress"/> and <paramref name="searchItemName"/>; otherwise, if not found, will return <b>null</b>.</returns>
        public static IConfigurationItem SearchForGroupItem(IConfigurationGroup configuration, string searchGroupAddress, string searchItemName)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (string.IsNullOrWhiteSpace(searchGroupAddress))
            {
                throw new ArgumentNullException(nameof(searchGroupAddress));
            }
            //
            var group = FindGroupByGroupAddressRecursiveFunc(configuration, searchGroupAddress);

            if (group != null)
            {
                foreach (var item in group.Items)
                {
                    if (item.Key.Equals(searchItemName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(item);
                    }
                }
            }
            return(null);
        }
 /// <summary>
 /// Removes the specified <see cref="IConfigurationGroup"/> from the collection.
 /// </summary>
 /// <param name="item">The <see cref="IConfigurationGroup"/> to remove.</param>
 /// <returns><b>True</b> if the <see cref="IConfigurationGroup"/> was removed; otherwise <b>false</b>. This method returns <b>false</b> if the key was not found.</returns>
 public bool Remove(IConfigurationGroup item)
 {
     if (item == null)
     {
         throw new ArgumentNullException(nameof(item));
     }
     return(Remove(item.Key));
 }
        // ----------------
        /// <summary>
        /// Will search the given <paramref name="configuration"/> for the specified <see cref="IConfigurationGroup"/> by <paramref name="name"/> and attempt to instantiate a type from the found <see cref="IConfigurationGroup"/>.
        /// The type must be defined in the <paramref name="configuration"/> with an <see cref="Configuration.IConfigurationItem"/> with the key: "Type" and the string value of either a <see cref="Type.FullName"/> or <see cref="Type.AssemblyQualifiedName"/>.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> the configuration should be instantiated into.</typeparam>
        /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
        /// <param name="name">The group name to search for.</param>
        /// <param name="throwException">If the group is not found, <b>True</b> will throw an <see cref="Exception"/>, <b>false</b> will return null.</param>
        /// <returns>The instantiated type or null.</returns>
        public static T InstantiateTypeFromConfigurationSubGroup <T>(IConfigurationGroup configuration, string name, bool throwException)
            where T : class
        {
            var group = FindConfigurationGroup(configuration, name, throwException);

            if (group != null)
            {
                return(InstantiateTypeFromConfigurationGroup(typeof(T), group) as T);
            }
            return(null);
        }
 /// <summary>
 /// Finds the named <see cref="IConfigurationItem"/> in the given configuration group and tests it's type.
 /// </summary>
 /// <remarks>Only searches the items in the <paramref name="configuration"/>, it will not search items from other subgroups; to search through the hierarchy of groups and their items, see: <see cref="SearchForGroupItem(IConfigurationGroup, string)"/> and <see cref="SearchForGroupItem(IConfigurationGroup, string, string)"/>.</remarks>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
 /// <param name="type">The <see cref="Type"/> the item should be.</param>
 /// <returns>The named <see cref="IConfigurationItem"/> or null.</returns>
 public static IConfigurationItem FindConfigurationItem(IConfigurationGroup configuration, string name, Type type)
 {
     if (ConfigurationShared._AllKnownTypes.Contains(type))
     {
         return(FindConfigurationItem(configuration, name, type.FullName));
     }
     else
     {
         return(FindConfigurationItem(configuration, name, type.AssemblyQualifiedName));
     }
 }
        /// <summary>
        /// Converts the specified group to a <see cref="StringBuilder"/> type.
        /// </summary>
        /// <param name="configuration">The group to convert.</param>
        /// <returns>The group converted into a <see cref="StringBuilder"/> type.</returns>
        public StringBuilder Serialize(IConfigurationGroup configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            var buffer = new StringBuilder(2560);

            InternalSerializeGroup(configuration, buffer);
            return(buffer);
        }
Esempio n. 14
0
        /// <summary>
        /// Converts the specified configuration to a <see cref="StringBuilder"/> type.
        /// </summary>
        /// <param name="configuration">The configuration to convert.</param>
        /// <returns>The configuration converted into a <see cref="StringBuilder"/> type.</returns>
        public StringBuilder Serialize(IConfigurationGroup configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            var table = CreateTable();

            InternalSerializeGroup("", configuration, table);
            return(new StringBuilder(table.WriteString()));
        }
        // ################ SEARCH

        /// <summary>
        /// Will search the <paramref name="configuration"/> for the specified <paramref name="searchGroupAddress"/>.
        /// </summary>
        /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
        /// <param name="searchGroupAddress">The Group Address to search for. This can include subgroups separated by the <see cref="GroupNameSeparator"/>.</param>
        /// <returns>If found, will return the <see cref="IConfigurationGroup"/> with matching <paramref name="searchGroupAddress"/>; otherwise, if not found, will return <b>null</b>.</returns>
        public static IConfigurationGroup SearchForGroup(IConfigurationGroup configuration, string searchGroupAddress)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (string.IsNullOrWhiteSpace(searchGroupAddress))
            {
                throw new ArgumentNullException(nameof(searchGroupAddress));
            }
            //
            return(FindGroupByGroupAddressRecursiveFunc(configuration, searchGroupAddress));
        }
 /// <summary>
 /// Attempts to find the named <see cref="IConfigurationItem"/> in the given configuration group and returns it's strongly typed <see cref="IConfigurationItem.Value"/>.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type"/> the item should be.</typeparam>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
 /// <param name="value">The <see cref="IConfigurationItem"/>'s <see cref="IConfigurationItem.Value"/> that was found or null.</param>
 /// <param name="exception">An exception describing any problems with trying to find the configuration item or null.</param>
 /// <returns><b>True</b> if found, with the named <see cref="IConfigurationItem"/> which matched the <paramref name="name"/>; otherwise, <b>false</b> if not found.</returns>
 public static bool TryFindConfigurationItemValue <T>(IConfigurationGroup configuration,
                                                      string name,
                                                      out T value,
                                                      out Exception exception)
 {
     if (TryFindConfigurationItem <T>(configuration, name, out IConfigurationItem configurationItem, out exception))
     {
         value = (T)configurationItem.Value;
         return(true);
     }
     value = default;
     return(false);
 }
Esempio n. 17
0
        internal static IConfigurationGroup FindSubGroup(IConfigurationGroup group, string itemKey)
        {
            foreach (var item in group)
            {
                if (item.Key == itemKey)
                {
                    return(item);
                }
            }
            var newGroup = new ConfigurationGroup(itemKey);

            group.Add(newGroup);
            return(newGroup);
        }
 /// <summary>
 /// Will attempt to instantiate a type from the given <paramref name="configuration"/>.
 /// The type must be defined in the <paramref name="configuration"/> as an <see cref="Configuration.IConfigurationItem"/> with the key: "Type" and the string value of either a <see cref="Type.FullName"/> or <see cref="Type.AssemblyQualifiedName"/>.
 /// </summary>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to use to configure the type being instantiated.</param>
 /// <param name="result">The instantiated type or null.</param>
 /// <param name="exception">An exception describing any problems trying to instantiate the type.</param>
 /// <returns><b>True</b> if successfully instantiated; otherwise, <b>false</b> if not.</returns>
 public static bool TryInstantiateTypeFromConfigurationGroup(IConfigurationGroup configuration, out object result, out Exception exception)
 {
     try
     {
         result    = InstantiateTypeFromConfigurationGroup(configuration);
         exception = null;
         return(result != null);
     }
     catch (Exception ex)
     {
         result    = null;
         exception = ex;
         return(false);
     }
 }
 /// <summary>
 /// Attempts to finds the named <see cref="IConfigurationGroup"/> in the given configuration group.
 /// </summary>
 /// <remarks>Only searches the groups in the <paramref name="configuration"/>, it will not search subgroups; to search through the hierarchy of groups, see: <see cref="SearchForGroup(IConfigurationGroup, string)"/>.</remarks>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The group name to search for.</param>
 /// <param name="foundConfigurationGroup">The named <see cref="IConfigurationGroup"/> or null.</param>
 /// <param name="exception">An exception describing any problems with trying to find the specified <see cref="IConfigurationGroup"/>.</param>
 /// <returns>
 /// Returns whether the <paramref name="configuration"/> was found or not.
 /// <b>True</b> indicates that the <paramref name="configuration"/> was found; otherwise, <b>false</b> indicates that the <paramref name="configuration"/> was not found.
 /// </returns>
 public static bool TryFindConfigurationGroup(IConfigurationGroup configuration, string name, out IConfigurationGroup foundConfigurationGroup, out Exception exception)
 {
     try
     {
         foundConfigurationGroup = FindConfigurationGroup(configuration, name, true);
         exception = null;
         return(foundConfigurationGroup != null);
     }
     catch (Exception ex)
     {
         exception = ex;
         foundConfigurationGroup = null;
         return(false);
     }
 }
 /// <summary>
 /// Will search the <paramref name="configuration"/> for the specified <paramref name="searchGroupAddressAndItemName"/>.
 /// </summary>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="searchGroupAddressAndItemName">
 /// The Group Address and Item Name to search for.
 /// The Group Address and Item Name must be separated by the <see cref="GroupNameSeparator"/>.
 /// The Group Address can also include subgroups separated by the <see cref="GroupNameSeparator"/>.
 /// </param>
 /// <param name="foundConfigurationItem">The <see cref="IConfigurationItem"/> with the matching <paramref name="searchGroupAddressAndItemName"/>; otherwise, <b>null</b> if not found.</param>
 /// <param name="exception">An exception describing any problems with trying to find the configuration item.</param>
 /// <returns><b>True</b> if found, with the <paramref name="foundConfigurationItem"/> populated with the <see cref="IConfigurationItem"/> matching the <paramref name="searchGroupAddressAndItemName"/>; otherwise, <b>false</b> if not found.</returns>
 public static bool TrySearchForGroupItem(IConfigurationGroup configuration, string searchGroupAddressAndItemName, out IConfigurationItem foundConfigurationItem, out Exception exception)
 {
     try
     {
         exception = null;
         foundConfigurationItem = SearchForGroupItem(configuration, searchGroupAddressAndItemName);
         return(foundConfigurationItem != null);
     }
     catch (Exception ex)
     {
         exception = ex;
         foundConfigurationItem = null;
         return(false);
     }
 }
 /// <summary>
 /// Attempts to find the <see cref="IConfigurationGroup"/> for the specified <paramref name="searchGroupAddress"/> in the given <paramref name="configuration"/>.
 /// </summary>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="searchGroupAddress">The Group Address to search for. This can include subgroups separated by the <see cref="GroupNameSeparator"/>.</param>
 /// <param name="foundConfigurationGroup">The <see cref="IConfigurationGroup"/> with the matching <paramref name="searchGroupAddress"/>; otherwise, <b>null</b> if not found.</param>
 /// <param name="exception">An exception describing any problems with trying to find the configuration group.</param>
 /// <returns><b>True</b> if found, with the <paramref name="foundConfigurationGroup"/> populated with the <see cref="IConfigurationGroup"/> matching the <paramref name="searchGroupAddress"/>; otherwise, <b>false</b> if not found.</returns>
 public static bool TrySearchForGroup(IConfigurationGroup configuration, string searchGroupAddress, out IConfigurationGroup foundConfigurationGroup, out Exception exception)
 {
     try
     {
         exception = null;
         foundConfigurationGroup = SearchForGroup(configuration, searchGroupAddress);
         return(foundConfigurationGroup != null);
     }
     catch (Exception ex)
     {
         exception = ex;
         foundConfigurationGroup = null;
         return(false);
     }
 }
 /// <summary>
 /// Finds the named <see cref="IConfigurationItem"/> in the given configuration group and tests it's type and returns an instance of the <paramref name="configuration"/> strongly typed to <typeparamref name="T"/>.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type"/> the configuration item should be instantiated into.</typeparam>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
 /// <param name="result">The value of the named <see cref="IConfigurationItem"/> or the default value for <typeparamref name="T"/>.</param>
 /// <param name="exception">An exception describing any problems with trying to instantiate the found <see cref="IConfigurationItem"/>.</param>
 /// <returns><b>True</b> if the <see cref="IConfigurationItem"/> was found and successfully instantiated; otherwise, <b>false</b>.</returns>
 public static bool TryInstantiateTypeFromConfigurationItem <T>(IConfigurationGroup configuration, string name, out T result, out Exception exception)
 {
     try
     {
         result    = InstantiateTypeFromConfigurationItem <T>(configuration, name, true);
         exception = null;
         return(true);
     }
     catch (Exception ex)
     {
         result    = default(T);
         exception = ex;
         return(false);
     }
 }
 /// <summary>
 /// Will search the given <paramref name="configuration"/> for the specified <see cref="IConfigurationGroup"/> by <paramref name="name"/> and attempt to instantiate a type from the found <see cref="IConfigurationGroup"/>.
 /// The type must be defined in the <paramref name="configuration"/> with an <see cref="Configuration.IConfigurationItem"/> with the key: "Type" and the string value of either a <see cref="Type.FullName"/> or <see cref="Type.AssemblyQualifiedName"/>.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type"/> the configuration should be instantiated into.</typeparam>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The group name to search for.</param>
 /// <param name="result">The instantiated type or null.</param>
 /// <param name="exception">An exception describing any problems with trying to instantiate the found <see cref="IConfigurationGroup"/>.</param>
 /// <returns><b>True</b> if the <see cref="IConfigurationGroup"/> was found and successfully instantiated; otherwise, <b>false</b>.</returns>
 public static bool TryInstantiateTypeFromConfigurationSubGroup <T>(IConfigurationGroup configuration, string name, out T result, out Exception exception)
     where T : class
 {
     try
     {
         result    = InstantiateTypeFromConfigurationSubGroup <T>(configuration, name, true);
         exception = null;
         return(result != null);
     }
     catch (Exception ex)
     {
         result    = null;
         exception = ex;
         return(false);
     }
 }
 private void InternalSerializeGroup(IConfigurationGroup group, StringBuilder buffer)
 {
     using (var xw = System.Xml.XmlWriter.Create(buffer, new System.Xml.XmlWriterSettings()
     {
         Indent = true, CheckCharacters = true, OmitXmlDeclaration = false
     }))
     {
         xw.WriteStartDocument();
         //
         RecursiveSerializeGroup(group, xw);
         //
         xw.WriteEndDocument();
     }
     // fix for changing UTF-16 to UTF-8
     buffer.Replace(System.Text.Encoding.Unicode.WebName, System.Text.Encoding.UTF8.WebName, 0, 56);
 }
        public void Search(IRelationshipSearchParameters parameters, Action <ISearchResult> resultAccumulator)
        {
            Requires.NotNull(parameters, nameof(parameters));
            Requires.NotNull(resultAccumulator, nameof(resultAccumulator));

            if (_providers.Length == 0)
            {
                // No providers registered
                return;
            }

            if (!parameters.Options.SearchExternalItems)
            {
                // Consider the dependencies tree as containing 'external items', allowing the
                // tree to be excluded from search results via this option.
                return;
            }

            using var context = new DependenciesTreeSearchContext(parameters, resultAccumulator);

            _joinableTaskContext.Factory.Run(SearchSolutionAsync);

            Task SearchSolutionAsync()
            {
                // Search projects concurrently
                return(Task.WhenAll(_projectServiceAccessor.GetProjectService().LoadedUnconfiguredProjects.Select(SearchProjectAsync)));
            }

            async Task SearchProjectAsync(UnconfiguredProject unconfiguredProject)
            {
                IUnconfiguredProjectVsServices?  projectVsServices         = unconfiguredProject.Services.ExportProvider.GetExportedValue <IUnconfiguredProjectVsServices>();
                IActiveConfigurationGroupService?configurationGroupService = unconfiguredProject.Services.ExportProvider.GetExportedValue <IActiveConfigurationGroupService>();
                IProjectTree?dependenciesNode = projectVsServices?.ProjectTree.CurrentTree?.FindChildWithFlags(DependencyTreeFlags.DependenciesRootNode);

                if (projectVsServices != null &&
                    dependenciesNode != null &&
                    configurationGroupService is IActiveConfigurationGroupService2 activeConfigurationGroupService)
                {
                    IConfigurationGroup <ConfiguredProject> configuredProjects = await activeConfigurationGroupService.GetActiveLoadedConfiguredProjectGroupAsync();

                    var projectContext = new DependenciesTreeProjectSearchContext(context, unconfiguredProject, dependenciesNode, _hierarchyItemManager, projectVsServices, _relationProvider);

                    // Search providers concurrently
                    await Task.WhenAll(_providers.Select(provider => provider.SearchAsync(projectContext)));
                }
            }
        }
 private static IConfigurationGroup FindGroupByGroupAddressRecursiveFunc(IConfigurationGroup group, string searchGroupAddress)
 {
     if (group.GroupAddress == searchGroupAddress)
     {
         return(group);
     }
     //
     foreach (var subGroup in group)
     {
         var dude = FindGroupByGroupAddressRecursiveFunc(subGroup, searchGroupAddress);
         if (dude != null)
         {
             return(dude);
         }
     }
     return(null);
 }
 // --------
 private void ProcessGroup(XmlReader xr, IConfigurationGroup rootGroup, IConfigurationGroup currentGroup)
 {
     while (xr.Read())
     {
         if (xr.IsStartElement(_XmlElementGroupItemsName_))
         {
             while (xr.Read())
             {
                 if (xr.IsStartElement(_XmlElementItemName_))
                 {
                     currentGroup.Items.Add(ProcessItemSegment(xr));
                 }
                 else
                 {
                     break;
                 }
             }
         }
         else if (xr.IsStartElement(_XmlElementGroupsName_))
         {
             while (xr.Read())
             {
                 if (xr.IsStartElement(_XmlElementGroupName_))
                 {
                     var key = xr.GetAttribute(_XmlElementGroupKeyName_);
                     if (xr.IsEmptyElement)
                     {
                         currentGroup.Add(key);
                     }
                     else
                     {
                         ProcessGroup(xr, rootGroup, ConfigurationShared.FindGroup(currentGroup, key, _XmlElementGroupKeyNameSeparator_));
                     }
                 }
                 else
                 {
                     break;
                 }
             }
         }
         else
         {
             break;
         }
     }
 }
Esempio n. 28
0
        private void InternalSerializeGroup(string parentGroupAddress, IConfigurationGroup group, DelimiterSeparatedValues.DsvTable table)
        {
            // get local Group Address
            var groupAddress = group.Key;

            if (!string.IsNullOrWhiteSpace(parentGroupAddress))
            {
                groupAddress = parentGroupAddress + _CsvGroupNameSeparator_ + group.Key;
            }
            // process items
            if (group.Items.Count > 0)
            {
                // serialize items
                foreach (var item in group.Items)
                {
                    var row = table.NewRow(groupAddress, item.Key, ConfigurationShared.ConvertItemTypeToString(item), ConfigurationShared.ConvertItemObjectToString(item, _Compressor));
                    table.Rows.Add(row);
                }
            }
            else
            {
                var row = table.NewRow(groupAddress);
                if (row != null)
                {
                    if (!string.IsNullOrWhiteSpace((string)row[0]))
                    {
                        table.Rows.Add(row);
                    }
                    else if ((!string.IsNullOrWhiteSpace((string)row[1])) &&
                             (!string.IsNullOrWhiteSpace((string)row[2])) &&
                             (!string.IsNullOrWhiteSpace((string)row[3])))
                    {
                        table.Rows.Add(row);
                    }
                }
            }
            // process groups
            if (group.Count > 0)
            {
                foreach (var subGroup in group)
                {
                    InternalSerializeGroup(groupAddress, subGroup, table);
                }
            }
        }
 // ----------------
 /// <summary>
 /// Finds the named <see cref="IConfigurationItem"/> in the given configuration group and tests it's type.
 /// </summary>
 /// <remarks>Only searches the items in the <paramref name="configuration"/>, it will not search items from other subgroups; to search through the hierarchy of groups and their items, see: <see cref="SearchForGroupItem(IConfigurationGroup, string)"/> and <see cref="SearchForGroupItem(IConfigurationGroup, string, string)"/>.</remarks>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
 /// <param name="type">The <see cref="Type.FullName"/> or <see cref="Type.AssemblyQualifiedName"/> the item should be.</param>
 /// <returns>The named <see cref="IConfigurationItem"/> or null.</returns>
 private static IConfigurationItem FindConfigurationItem(IConfigurationGroup configuration, string name, string type)
 {
     // check name
     if (!configuration.Items.ContainsKey(name))
     {
         throw new Exception($"Configuration missing item. Configuration must have item: '{name}'.");
     }
     // test for internal, runtime only, type (in theory, this should never happen)
     if (type.StartsWith("System.RuntimeType"))
     {
         throw new Exception("dodSON.Core.Configuration.ConfigurationHelper::FindConfigurationItem(IConfigurationGroup, string, string) --> Received a System.RuntimeType...");
     }
     // check type
     if (!configuration.Items[name].ValueType.StartsWith(type))
     {
         throw new Exception($"Configuration item invalid format. Configuration item '{name}' must be a <{type}>.");
     }
     return(configuration.Items[name]);
 }
 /// <summary>
 /// Will serialize the <paramref name="source"/> using an <see cref="BinaryConfigurationSerializer"/> and save it to the <paramref name="filename"/>.
 /// </summary>
 /// <param name="source">The <see cref="IConfigurationGroup"/> to serialize and save.</param>
 /// <param name="filename">The path and filename to save the <paramref name="source"/> to.</param>
 /// <param name="overwrite">Determines if the file should be written over if it already exists. <b>True</b> will overwrite the file; <b>false</b> will throw an exception.</param>
 /// <returns>Whether the save operation was successful. <b>True</b> indicates the save operation succeeded; <b>false</b> will be expressed by throwing an exception.</returns>
 public static void SaveFile(IConfigurationGroup source,
                             string filename,
                             bool overwrite)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     if (string.IsNullOrWhiteSpace(filename))
     {
         throw new ArgumentNullException(nameof(filename));
     }
     if (!overwrite && System.IO.File.Exists(filename))
     {
         throw new ArgumentException($"File already exists.", nameof(overwrite));
     }
     //
     System.IO.File.WriteAllBytes(filename, (new BinaryConfigurationSerializer()).Serialize(source));
 }
 /// <summary>
 /// Attempts to find the named <see cref="IConfigurationItem"/> in the given configuration group and tests it's type.
 /// </summary>
 /// <param name="configuration">The <see cref="IConfigurationGroup"/> to search.</param>
 /// <param name="name">The name of the <see cref="IConfigurationItem"/> to search for.</param>
 /// <param name="type">The <see cref="Type"/> the item should be.</param>
 /// <param name="configurationItem">The <see cref="IConfigurationItem"/> that was found or null.</param>
 /// <param name="exception">An exception describing any problems with trying to find the configuration item.</param>
 /// <returns><b>True</b> if found, with the <paramref name="configurationItem"/> populated with the <see cref="IConfigurationItem"/> matching the <paramref name="name"/>; otherwise, <b>false</b> if not found.</returns>
 public static bool TryFindConfigurationItem(IConfigurationGroup configuration,
                                             string name,
                                             Type type,
                                             out IConfigurationItem configurationItem,
                                             out Exception exception)
 {
     try
     {
         configurationItem = FindConfigurationItem(configuration, name, type);
         exception         = null;
         return(configurationItem != null);
     }
     catch (Exception ex)
     {
         configurationItem = null;
         exception         = ex;
         return(false);
     }
 }
 public void SelectAlternativeInGroup(IConfigurationGroup configurationGroupToUpdate)
 {
     var configurationGroup = FindConfigurationGroupInConfiguration(configurationGroupToUpdate.UniqueId);
     configurationGroup.SelectedAlternative = configurationGroupToUpdate.SelectedAlternative;
     configurationGroup.GroupConfigurationState = ConfigurationState.VALID;
     CalculateNewConfigurationState();
 }
 public void RemoveGroupFromConfiguration(IConfigurationGroup configurationGroup)
 {
     var currentConfiguration = GetConfiguration();
     currentConfiguration.ConfigurationGroups.Remove(configurationGroup);
     foreach (var aircraft in configurationGroup.Aircrafts)
     {
         currentConfiguration.SelectedAircrafts.Remove(aircraft);
     }
 }
 private void AskUserAndRemoveGroupFromConfiguration(IConfigurationGroup configurationGroup)
 {
     var messageDialog = new MessageDialog("Do you want to delete this group?",
         "Delete Group " + configurationGroup.Name);
     var command = new UICommandInvokedHandler((IUICommand paramter) =>
     {
         _model.RemoveGroupFromConfiguration(configurationGroup);
         InitializeDataGrid();
     });
     messageDialog.Commands.Add(new UICommand("Yes", command));
 }