Beispiel #1
0
        /// <summary>
        /// Returns a segment value that matches patterns in the auto segment configuration, or null if nothing is found.
        /// </summary>
        /// <param name="segment">The segment type to extract (uses this segment's configuration).</param>
        /// <param name="source">The string to search.</param>
        /// <param name="defaultFragmentValues">If not found using the regex pattern, use these values.</param>
        /// <returns></returns>
        public SegmentObject ExtractSegmentValue(Segment segment, string source, string patternName = null, Dictionary <string, string> defaultFragmentValues = null)
        {
            if (this.Definitions == null)
            {
                return(null);
            }

            if (segment == null)
            {
                throw new ArgumentNullException("segment");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source", "Segments can only be extracted from a non-null source.");
            }

            AutoSegmentDefinition def = this.Definitions[segment.Name];

            if (def == null)
            {
                throw new ArgumentException(String.Format("The segment '{0}' was not found in the {1} configuration.", segment.Name, AutoSegmentDefinitionCollection.ExtensionName), "segmentName");
            }

            var           fragmentValues = new Dictionary <string, string>();
            SegmentObject value          = null;

            if (patternName == null)
            {
                // Find a definition that works
                for (int p = 0; p < def.Patterns.Count && value == null; p++)
                {
                    // reset because previous iteration found nothing
                    fragmentValues.Clear();
                    value = ExtractSegmentValueFromPattern(segment, source, defaultFragmentValues, fragmentValues, def.Patterns[p]);
                }
            }
            else
            {
                value = ExtractSegmentValueFromPattern(segment, source, defaultFragmentValues, fragmentValues, def.Patterns[patternName]);
            }

            return(value);
        }
Beispiel #2
0
        public SegmentValue ExtractSegmentValue(Segment segment, string source)
        {
            if (this.Definitions == null)
            {
                return(null);
            }

            if (segment == null)
            {
                throw new ArgumentNullException("segment");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source", "Segments can only be extracted from a non-null source.");
            }

            AutoSegmentDefinition def = this.Definitions[segment.Name];

            if (def == null)
            {
                throw new ArgumentException(String.Format("The segment '{0}' was not found in the {1} configuration.", segment.Name, AutoSegmentDefinitionCollection.ExtensionName), "segmentName");
            }

            var fragmentValues = new Dictionary <string, string>();

            // Find a definition that works
            for (int p = 0; p < def.Patterns.Count; p++)
            {
                // reset because previous iteration found nothing
                fragmentValues.Clear();

                AutoSegmentPattern pattern = def.Patterns[p];

                MatchCollection matches = pattern.Regex.Matches(source);
                foreach (Match match in matches)
                {
                    if (!match.Success)
                    {
                        continue;
                    }

                    int fragmentCounter = 0;
                    for (int g = 0; g < match.Groups.Count; g++)
                    {
                        Group  group     = match.Groups[g];
                        string groupName = pattern.RawGroupNames[g];
                        if (!group.Success || !AutoSegmentPattern.IsValidFragmentName(groupName))
                        {
                            continue;
                        }

                        // Save the fragment
                        fragmentValues[pattern.Fragments[fragmentCounter++]] = group.Value;
                    }
                }

                // found all the values, create a segment value
                if (fragmentValues.Count == pattern.Fragments.Length)
                {
                    return(CreateValueFromFragments(segment, fragmentValues, pattern));
                }
            }

            // found nothing
            return(null);
        }