Example #1
0
        //Ensures that all placeholders in the pattern refer to existing tag frame properties.
        //Then it attempts to mapping dictionary between the frame name and the corresponding frame
        //property, so that multiple renames can be done fast.
        private static Dictionary <string, PropertyInfo> ValidatePatternAndBuildMapping(string pattern)
        {
            //Find all placeholders within the pattern. If there are none, throw an exception.
            MatchCollection framePlaceholderMatches = FramePlaceholderPattern.Matches(pattern);

            if (framePlaceholderMatches.Count == 0)
            {
                throw new ArgumentException(string.Format(Id3FileMessages.MissingPlaceholdersInPattern, pattern));
            }

            //Get all public properties in the tag that derive from Id3Frame.
            PropertyInfo[] frameProperties =
                typeof(Id3Tag).GetProperties().Where(prop => prop.PropertyType.IsSubclassOf(typeof(Id3Frame))).ToArray();

            //Iterate through each placeholder and find the corresponding frame property. If such a
            //property does not exist, throw an exception; otherwise add a mapping from the frame name
            //to the property.
            var mapping = new Dictionary <string, PropertyInfo>(framePlaceholderMatches.Count);

            foreach (Match match in framePlaceholderMatches)
            {
                string       matchedFrameName      = match.Groups[1].Value.ToLowerInvariant();
                PropertyInfo matchingFrameProperty =
                    frameProperties.FirstOrDefault(frame => frame.Name.Equals(matchedFrameName, StringComparison.InvariantCultureIgnoreCase));
                if (matchingFrameProperty == null)
                {
                    throw new ArgumentException(string.Format(Id3FileMessages.MissingTagProperty, matchedFrameName));
                }
                if (!mapping.ContainsKey(matchedFrameName))
                {
                    mapping.Add(matchedFrameName, matchingFrameProperty);
                }
            }
            return(mapping);
        }
Example #2
0
        /// <summary>
        ///     Ensures that all placeholders in the pattern refer to existing tag frame properties. Then it attempts to mapping
        ///     dictionary between the frame name and the corresponding frame property, so that multiple renames can be done fast.
        /// </summary>
        /// <param name="patterns">The pattern to validate</param>
        /// <returns>A mapping of placeholder name to property info of the correspoding frame in <see cref="Id3Tag" /></returns>
        /// <exception cref="ArgumentException">Thrown if any pattern contains invalid placeholders.</exception>
        private static void ValidatePatterns(IEnumerable <string> patterns)
        {
            // Get all distinct placeholder names in all the patterns and check whether any of them
            // do not appear as a key in the _mappings dictionary. If so, throw an exception.
            string[] invalidPlaceholders = patterns
                                           .SelectMany(pattern => {
                MatchCollection matches = FramePlaceholderPattern.Matches(pattern);
                if (matches.Count == 0)
                {
                    throw new ArgumentException(string.Format(FileNamerMessages.MissingPlaceholdersInPattern,
                                                              pattern));
                }
                return(matches.Cast <Match>().Select(m => m.Groups[1].Value));
            })
                                           .Distinct(StringComparer.OrdinalIgnoreCase)
                                           .Where(ph => !_mapping.ContainsKey(ph))
                                           .ToArray();
            if (invalidPlaceholders.Length <= 0)
            {
                return;
            }

            //Build detailed exception and throw it.
            string invalidPlaceholderNames = string.Join(", ", invalidPlaceholders);
            string allowedPlaceholderNames = string.Join(", ", _allowedFrames);
            string exceptionMessage        = "The following placeholders are not allowed in the file naming patterns:" + Environment.NewLine +
                                             Environment.NewLine +
                                             invalidPlaceholderNames + Environment.NewLine +
                                             Environment.NewLine +
                                             "Only these placeholders are allowed:" + Environment.NewLine +
                                             Environment.NewLine +
                                             allowedPlaceholderNames;

            throw new ArgumentException(exceptionMessage, nameof(patterns));
        }