Example #1
0
        private SegmentObject ExtractSegmentValueFromPattern(Segment segment, string source, Dictionary <string, string> defaultFragmentValues, Dictionary <string, string> fragmentValues, AutoSegmentPattern pattern)
        {
            MatchCollection matches         = pattern.Regex.Matches(source);
            int             fragmentCounter = 0;

            foreach (Match match in matches)
            {
                if (!match.Success)
                {
                    continue;
                }

                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

                    /*Fix bug when getting url like this(two same params):http://www.888.com/texasholdem1/?sr=855961/?sr=867151 (we get index out of range)
                     *
                     */
                    if (!fragmentValues.ContainsKey(groupName))
                    {
                        fragmentValues[pattern.Fragments[fragmentCounter++]] = group.Value;
                    }
                    else
                    {
                        Edge.Core.Utilities.Log.Write(string.Format("Duplicate tracker in same Creative has been found. DestURL:{0}", source), Core.Utilities.LogMessageType.Warning);
                    }
                }
            }

            if (defaultFragmentValues != null)
            {
                foreach (var pair in defaultFragmentValues)
                {
                    if (!fragmentValues.ContainsKey(pair.Key))
                    {
                        fragmentValues.Add(pair.Key, pair.Value);
                    }
                }
            }

            // found all the values, create a segment value
            if (fragmentValues.Count == pattern.Fragments.Length)
            {
                return(CreateValueFromFragments(segment, fragmentValues, pattern));
            }
            else
            {
                return(null);
            }
        }
Example #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);
        }
Example #3
0
        private SegmentValue CreateValueFromFragments(Segment segment, Dictionary <string, string> fragments, AutoSegmentPattern pattern)
        {
            if (fragments == null || fragments.Count < 1)
            {
                return(null);
            }

            // Custom segment generation
            if (SegmentFound != null)
            {
                var e = new AutoSegmentFoundEventArgs()
                {
                    Segment = segment, Fragments = fragments, Pattern = pattern
                };
                SegmentFound(this, e);
                if (e.Value != null)
                {
                    return(e.Value);
                }
            }

            string originalID = null;
            string value;

            // EVERYBODY DO THE FORMATTING DANCE

            originalID = !String.IsNullOrWhiteSpace(pattern.OriginalID) ?
                         // use custom format
                         String.Format(pattern.OriginalID, fragments.Values.ToArray())
                        :
                         // json serialize fragments if no custom format defined
                         originalID = JsonSerialize(fragments)
            ;

            value = !String.IsNullOrWhiteSpace(pattern.Value) ?
                    // use custom format
                    String.Format(pattern.Value, fragments.Values.ToArray())
                        :
                    // no custom format
                    value = fragments.Count == 1 ?
                            // one fragment only, just use it
                            fragments.First().Value
                                :
                            // json serialize fragments (re-use originalID if it was serialized above)
                            (originalID != null ?
                             originalID :
                             JsonSerialize(fragments)
                            )
            ;

            // Return the value!
            return(new SegmentValue()
            {
                OriginalID = originalID, Value = value
            });
        }