XmlLightElement GetReplacementNode(XmlLightElement e, object key)
            {
                HttpCloneOptimizationReplace replacement = _replaces[key];

                if (String.IsNullOrEmpty(replacement.ReplacementValue))
                {
                    return(null);
                }

                string replaceText = replacement.ReplacementValue;

                if (replacement.ExpandValue)
                {
                    replaceText = RegexPatterns.FormatNameSpecifier.Replace(replaceText,
                                                                            m =>
                    {
                        string tmp;
                        if (_namedValues.TryGetValue(m.Groups["field"].Value, out tmp) ||
                            e.Attributes.TryGetValue(m.Groups["field"].Value, out tmp))
                        {
                            return(tmp);
                        }
                        return(m.Value);
                    });
                }

                XmlLightElement newElem = new XmlLightDocument(replaceText).Root;

                newElem.Parent = null;
                return(newElem);
            }
            private string GetReplacementText(Match match, int groupId, object key)
            {
                string prefix = String.Empty;
                string suffix = String.Empty;

                if (groupId > 0)
                {
                    if (match.Groups[groupId].Success == false)
                    {
                        return(match.Value);
                    }

                    prefix = match.Value.Substring(0,
                                                   match.Groups[groupId].Index - match.Index
                                                   );
                    suffix = match.Value.Substring(
                        match.Groups[groupId].Index
                        + match.Groups[groupId].Length
                        - match.Index);
                }

                string replaceText = String.Empty;

                HttpCloneOptimizationReplace replacement = _replaces[key];

                if (!String.IsNullOrEmpty(replacement.ReplacementValue))
                {
                    replaceText = replacement.ReplacementValue;
                    if (replacement.ExpandValue)
                    {
                        replaceText = RegexPatterns.FormatNameSpecifier.Replace(replaceText,
                                                                                m =>
                        {
                            if (_namedValues.ContainsKey(m.Groups["field"].Value))
                            {
                                return(_namedValues[m.Groups["field"].Value]);
                            }
                            else if (match.Groups[m.Groups["field"].Value].Success)
                            {
                                return(match.Groups[m.Groups["field"].Value].Value);
                            }
                            return(m.Value);
                        });
                    }
                }

                return(prefix + replaceText + suffix);
            }