Exemplo n.º 1
0
        internal HashtableEntryDefinition MatchEntry(string keyName, TerminatingErrorContext invocationContext)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                PSTraceSource.NewArgumentNullException("keyName");
            }
            HashtableEntryDefinition matchingEntry = null;

            for (int i = 0; i < this.hashEntries.Count; i++)
            {
                if (this.hashEntries[i].IsKeyMatch(keyName))
                {
                    if (matchingEntry == null)
                    {
                        matchingEntry = this.hashEntries[i];
                    }
                    else
                    {
                        ProcessAmbiguousKey(invocationContext, keyName, matchingEntry, this.hashEntries[i]);
                    }
                }
            }
            if (matchingEntry != null)
            {
                return(matchingEntry);
            }
            ProcessIllegalKey(invocationContext, keyName);
            return(null);
        }
Exemplo n.º 2
0
        private static void ProcessAmbiguousKey(TerminatingErrorContext invocationContext,
                                                string keyName,
                                                HashtableEntryDefinition matchingEntry,
                                                HashtableEntryDefinition currentEntry)
        {
            string msg = StringUtil.Format(FormatAndOut_MshParameter.AmbiguousKeyError,
                                           keyName, matchingEntry.KeyName, currentEntry.KeyName);

            ParameterProcessor.ThrowParameterBindingException(invocationContext, "DictionaryKeyAmbiguous", msg);
        }
Exemplo n.º 3
0
        private Hashtable VerifyHashTable(IDictionary hash, TerminatingErrorContext invocationContext)
        {
            Hashtable hashtable = new Hashtable();

            foreach (DictionaryEntry entry in hash)
            {
                if (entry.Key == null)
                {
                    ProcessNullHashTableKey(invocationContext);
                }
                string key = entry.Key as string;
                if (key == null)
                {
                    ProcessNonStringHashTableKey(invocationContext, entry.Key);
                }
                HashtableEntryDefinition definition = this.paramDef.MatchEntry(key, invocationContext);
                if (hashtable.Contains(definition.KeyName))
                {
                    ProcessDuplicateHashTableKey(invocationContext, key, definition.KeyName);
                }
                bool flag = false;
                if ((definition.AllowedTypes == null) || (definition.AllowedTypes.Length == 0))
                {
                    flag = true;
                }
                else
                {
                    for (int i = 0; i < definition.AllowedTypes.Length; i++)
                    {
                        if (entry.Value == null)
                        {
                            ProcessMissingKeyValue(invocationContext, key);
                        }
                        if (definition.AllowedTypes[i].IsAssignableFrom(entry.Value.GetType()))
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag)
                {
                    ProcessIllegalHashTableKeyValue(invocationContext, key, entry.Value.GetType(), definition.AllowedTypes);
                }
                hashtable.Add(definition.KeyName, entry.Value);
            }
            return(hashtable);
        }
Exemplo n.º 4
0
        /// <summary>
        /// for a key name, verify it is a legal entry:
        ///     1. it must match (partial match allowed)
        ///     2. it must be unambiguous (if partial match)
        /// If an error condition occurs, an exception will be thrown
        /// </summary>
        /// <param name="keyName">Key to verify.</param>
        /// <param name="invocationContext">Invocation context for error reporting.</param>
        /// <returns>Matching hash table entry.</returns>
        /// <exception cref="ArgumentException"></exception>
        internal HashtableEntryDefinition MatchEntry(string keyName, TerminatingErrorContext invocationContext)
        {
            if (string.IsNullOrEmpty(keyName))
            {
                PSTraceSource.NewArgumentNullException("keyName");
            }

            HashtableEntryDefinition matchingEntry = null;

            for (int k = 0; k < this.hashEntries.Count; k++)
            {
                if (this.hashEntries[k].IsKeyMatch(keyName))
                {
                    // we have a match
                    if (matchingEntry == null)
                    {
                        // this is the first match, we save the entry
                        // and we keep going for ambiguity check
                        matchingEntry = this.hashEntries[k];
                    }
                    else
                    {
                        // we already had a match, we have an ambiguous key
                        ProcessAmbiguousKey(invocationContext, keyName, matchingEntry, this.hashEntries[k]);
                    }
                }
            }

            if (matchingEntry != null)
            {
                // we found an unambiguous match
                return(matchingEntry);
            }
            // we did not have a match
            ProcessIllegalKey(invocationContext, keyName);
            return(null);
        }
Exemplo n.º 5
0
        /// <exception cref="ArgumentException"></exception>
        private Hashtable VerifyHashTable(IDictionary hash, TerminatingErrorContext invocationContext)
        {
            // full blown hash, need to:
            // 1. verify names(keys) and expand names if there are partial matches
            // 2. verify value types
            Hashtable retVal = new Hashtable();

            foreach (DictionaryEntry e in hash)
            {
                if (e.Key is null)
                {
                    ProcessNullHashTableKey(invocationContext);
                }

                string currentStringKey = e.Key as string;
                if (currentStringKey is null)
                {
                    ProcessNonStringHashTableKey(invocationContext, e.Key);
                }

                // find a match for the key
                HashtableEntryDefinition def = _paramDef.MatchEntry(currentStringKey, invocationContext);
                if (retVal.Contains(def.KeyName))
                {
                    // duplicate key error
                    ProcessDuplicateHashTableKey(invocationContext, currentStringKey, def.KeyName);
                }

                // now the key is verified, need to check the type
                bool matchType = false;

                if (def.AllowedTypes is null || def.AllowedTypes.Length == 0)
                {
                    // we match on any type, it will be up to the entry to further check
                    matchType = true;
                }
Exemplo n.º 6
0
        /// <exception cref="ArgumentException"></exception>
        private Hashtable VerifyHashTable(IDictionary hash, TerminatingErrorContext invocationContext)
        {
            // full blown hash, need to:
            // 1. verify names(keys) and expand names if there are partial matches
            // 2. verify value types
            Hashtable retVal = new Hashtable();

            foreach (DictionaryEntry e in hash)
            {
                if (e.Key == null)
                {
                    ProcessNullHashTableKey(invocationContext);
                }

                string currentStringKey = e.Key as string;
                if (currentStringKey == null)
                {
                    ProcessNonStringHashTableKey(invocationContext, e.Key);
                }

                // find a match for the key
                HashtableEntryDefinition def = _paramDef.MatchEntry(currentStringKey, invocationContext);
                if (retVal.Contains(def.KeyName))
                {
                    // duplicate key error
                    ProcessDuplicateHashTableKey(invocationContext, currentStringKey, def.KeyName);
                }

                // now the key is verified, need to check the type
                bool matchType = false;

                if (def.AllowedTypes == null || def.AllowedTypes.Length == 0)
                {
                    // we match on any type, it will be up to the entry to further check
                    matchType = true;
                }
                else
                {
                    for (int t = 0; t < def.AllowedTypes.Length; t++)
                    {
                        if (e.Value == null)
                        {
                            ProcessMissingKeyValue(invocationContext, currentStringKey);
                        }

                        if (def.AllowedTypes[t].IsAssignableFrom(e.Value.GetType()))
                        {
                            matchType = true;
                            break;
                        }
                    }
                }

                if (!matchType)
                {
                    // bad type error
                    ProcessIllegalHashTableKeyValue(invocationContext, currentStringKey, e.Value.GetType(), def.AllowedTypes);
                }

                retVal.Add(def.KeyName, e.Value);
            }

            return(retVal);
        }
Exemplo n.º 7
0
        private static void ProcessAmbiguousKey(TerminatingErrorContext invocationContext,
                                            string keyName,
                                            HashtableEntryDefinition matchingEntry,
                                            HashtableEntryDefinition currentEntry)
        {
            string msg = StringUtil.Format(FormatAndOut_MshParameter.AmbiguousKeyError,
                keyName, matchingEntry.KeyName, currentEntry.KeyName);

            ParameterProcessor.ThrowParameterBindingException(invocationContext, "DictionaryKeyAmbiguous", msg);
        }