private static bool ForEachParameter(IEnumerable <PathSegment> segments,
                                             Func <ParameterSubsegment, bool> action)
        {
            foreach (PathSegment segment in segments)
            {
                if (segment is SeparatorSegment)
                {
                    continue;
                }

                ContentSegment content = (segment as ContentSegment);
                if (content != null)
                {
                    foreach (PathSubsegment subsegment in content.Segments)
                    {
                        ParameterSubsegment arg = (subsegment as ParameterSubsegment);
                        if (arg == null)
                        {
                            continue;
                        }

                        if (!action(arg))
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
		private static void VisitSegment(ContentSegment segment, ValueDictionary variables)
		{
			foreach (PathSubsegment subsegment in segment.Segments)
			{
				VariableSubsegment variable = (subsegment as VariableSubsegment);
				CompositeSubsegment composite = (subsegment as CompositeSubsegment);

				if (variable != null)
					VisitVariable(variable, variables);

				else if (composite != null)
					VisitComposite(composite, variables);
			}
		}
        public static void Assign(ParsedRoute route, ValueDictionary variables)
        {
            Precondition.Require(route, () => Error.ArgumentNull("route"));
            Precondition.Require(variables, () => Error.ArgumentNull("variables"));

            foreach (PathSegment segment in route.Segments)
            {
                ContentSegment content = (segment as ContentSegment);
                if (content == null)
                {
                    continue;
                }

                VisitSegment(content, variables);
            }
        }
        private static void VisitSegment(ContentSegment segment, ValueDictionary variables)
        {
            foreach (PathSubsegment subsegment in segment.Segments)
            {
                VariableSubsegment  variable  = (subsegment as VariableSubsegment);
                CompositeSubsegment composite = (subsegment as CompositeSubsegment);

                if (variable != null)
                {
                    VisitVariable(variable, variables);
                }

                else if (composite != null)
                {
                    VisitComposite(composite, variables);
                }
            }
        }
        private void MatchCatchAll(ContentSegment segment, IEnumerable <string> remainingSegments,
                                   ValueDictionary defaults, ValueDictionary matchedValues)
        {
            object value;
            string remainingPart          = String.Join(String.Empty, remainingSegments.ToArray());
            ParameterSubsegment parameter = (segment.Segments.FirstOrDefault() as ParameterSubsegment);

            if (remainingPart.Length > 0)
            {
                value = remainingPart;
            }
            else
            {
                defaults.TryGetValue(parameter.ParameterName, out value);
            }

            matchedValues.Add(parameter.ParameterName, value);
        }
        private bool MatchContent(ContentSegment segment, string pathSegment, 
            ValueDictionary defaults, ValueDictionary matchedValues)
        {
            if (String.IsNullOrEmpty(pathSegment))
            {
                if (segment.Segments.Count > 0)
                {
                    object value;
                    ParameterSubsegment ps = (segment.Segments.FirstOrDefault(
                        s => (s is ParameterSubsegment)) as ParameterSubsegment);
                    
                    if (ps == null)
                        return false;
                    
                    if (defaults.TryGetValue(ps.ParameterName, out value))
                    {
                        matchedValues.Add(ps.ParameterName, value);
                        return true;
                    }
                }
                return false;
            }

            int segmentLength = pathSegment.Length;
            int segmentIndex = (segment.Segments.Count - 1);

            ParameterSubsegment lastParameter = null;
            LiteralSubsegment lastLiteral = null;

            while (segmentIndex >= 0)
            {
                int index = segmentLength;
                ParameterSubsegment parameter = (segment.Segments[segmentIndex] as ParameterSubsegment);
                LiteralSubsegment literal = (segment.Segments[segmentIndex] as LiteralSubsegment);

                if (parameter != null)
                    lastParameter = parameter;

                if (literal != null)
                {
                    lastLiteral = literal;

                    int literalIndex = pathSegment.LastIndexOf(literal.Literal, 
                        segmentLength - 1, StringComparison.OrdinalIgnoreCase);

                    if (literalIndex == -1)
                        return false;
                    
                    if ((segmentIndex == segment.Segments.Count - 1) && 
                        ((literalIndex + literal.Literal.Length) != pathSegment.Length))
                        return false;
                    
                    index = literalIndex;
                }

                if (lastParameter != null && ((lastLiteral != null && 
                    parameter == null) || segmentIndex == 0))
                {
                    int startIndex;
                    int lastIndex;

                    if (lastLiteral == null)
                    {
                        startIndex = (segmentIndex == 0) ? 0 : index + lastLiteral.Literal.Length;
                        lastIndex = segmentLength;
                    }
                    else if (segmentIndex == 0 && parameter != null)
                    {
                        startIndex = 0;
                        lastIndex = segmentLength;
                    }
                    else
                    {
                        startIndex = index + lastLiteral.Literal.Length;
                        lastIndex = segmentLength - startIndex;
                    }

                    string part = pathSegment.Substring(startIndex, lastIndex);
                    
                    if (String.IsNullOrEmpty(part))
                        return false;
                    
                    matchedValues.Add(lastParameter.ParameterName, part);

                    lastParameter = null;
                    lastLiteral = null;
                }
                segmentLength = index;
                segmentIndex--;
            }

            if (segmentLength != 0)
                return (segment.Segments[0] is ParameterSubsegment);
            
            return true;
        }
        private void MatchCatchAll(ContentSegment segment, IEnumerable<string> remainingSegments, 
            ValueDictionary defaults, ValueDictionary matchedValues)
        {
            object value;
            string remainingPart = String.Join(String.Empty, remainingSegments.ToArray());
            ParameterSubsegment parameter = (segment.Segments.FirstOrDefault() as ParameterSubsegment);

            if (remainingPart.Length > 0)
                value = remainingPart;
            else
                defaults.TryGetValue(parameter.ParameterName, out value);
            
            matchedValues.Add(parameter.ParameterName, value);
        }
        private BoundUrl BuildUrl(ValueDictionary defaults,
                                  ValueDictionary acceptedValues, HashSet <string> unusedValues)
        {
            StringBuilder pathBuilder    = new StringBuilder();
            StringBuilder segmentBuilder = new StringBuilder();
            bool          flush          = false;

            foreach (PathSegment segment in _segments)
            {
                SeparatorSegment separator = (segment as SeparatorSegment);
                ContentSegment   content   = (segment as ContentSegment);

                if (separator != null)
                {
                    if (flush && segmentBuilder.Length > 0)
                    {
                        pathBuilder.Append(segmentBuilder.ToString());
                        segmentBuilder.Length = 0;
                    }

                    flush = false;
                    segmentBuilder.Append(RouteParser.PathSeparator);
                }

                if (content != null)
                {
                    bool segmentEnd = false;

                    foreach (PathSubsegment subsegment in content.Segments)
                    {
                        LiteralSubsegment   literal   = (subsegment as LiteralSubsegment);
                        ParameterSubsegment parameter = (subsegment as ParameterSubsegment);

                        if (literal != null)
                        {
                            flush = true;
                            segmentBuilder.Append(Uri.EscapeUriString(literal.Literal));
                        }

                        if (parameter != null)
                        {
                            object acceptedValue;
                            object defaultValue;

                            if (flush && segmentBuilder.Length > 0)
                            {
                                pathBuilder.Append(segmentBuilder.ToString());
                                segmentBuilder.Length = 0;

                                segmentEnd = true;
                            }

                            flush = false;

                            if (acceptedValues.TryGetValue(parameter.ParameterName, out acceptedValue))
                            {
                                unusedValues.Remove(parameter.ParameterName);
                            }

                            defaults.TryGetValue(parameter.ParameterName, out defaultValue);
                            if (RoutePartsEqual(acceptedValue, defaultValue))
                            {
                                segmentBuilder.Append(Uri.EscapeUriString(
                                                          Convert.ToString(acceptedValue, CultureInfo.InvariantCulture)));
                                continue;
                            }

                            if (segmentBuilder.Length > 0)
                            {
                                pathBuilder.Append(segmentBuilder.ToString());
                                segmentBuilder.Length = 0;
                            }

                            pathBuilder.Append(Uri.EscapeUriString(Convert.ToString(acceptedValue, CultureInfo.InvariantCulture)));
                            segmentEnd = true;
                        }
                    }

                    if (segmentEnd && segmentBuilder.Length > 0)
                    {
                        pathBuilder.Append(segmentBuilder.ToString());
                        segmentBuilder.Length = 0;
                    }
                }
            }

            if (flush && segmentBuilder.Length > 0)
            {
                pathBuilder.Append(segmentBuilder.ToString());
            }

            if (unusedValues.Count > 0)
            {
                bool isFirst = true;
                foreach (string key in unusedValues)
                {
                    object value;
                    if (acceptedValues.TryGetValue(key, out value))
                    {
                        pathBuilder.Append(isFirst ? '?' : '&');
                        isFirst = false;

                        pathBuilder.Append(Uri.EscapeDataString(key.ToLowerInvariant()));
                        pathBuilder.Append('=');
                        pathBuilder.Append(Uri.EscapeDataString(
                                               Convert.ToString(value, CultureInfo.InvariantCulture)));
                    }
                }
            }
            return(new BoundUrl(pathBuilder.ToString(), acceptedValues));
        }
        private bool MatchContent(ContentSegment segment, string pathSegment,
                                  ValueDictionary defaults, ValueDictionary matchedValues)
        {
            if (String.IsNullOrEmpty(pathSegment))
            {
                if (segment.Segments.Count > 0)
                {
                    object value;
                    ParameterSubsegment ps = (segment.Segments.FirstOrDefault(
                                                  s => (s is ParameterSubsegment)) as ParameterSubsegment);

                    if (ps == null)
                    {
                        return(false);
                    }

                    if (defaults.TryGetValue(ps.ParameterName, out value))
                    {
                        matchedValues.Add(ps.ParameterName, value);
                        return(true);
                    }
                }
                return(false);
            }

            int segmentLength = pathSegment.Length;
            int segmentIndex  = (segment.Segments.Count - 1);

            ParameterSubsegment lastParameter = null;
            LiteralSubsegment   lastLiteral   = null;

            while (segmentIndex >= 0)
            {
                int index = segmentLength;
                ParameterSubsegment parameter = (segment.Segments[segmentIndex] as ParameterSubsegment);
                LiteralSubsegment   literal   = (segment.Segments[segmentIndex] as LiteralSubsegment);

                if (parameter != null)
                {
                    lastParameter = parameter;
                }

                if (literal != null)
                {
                    lastLiteral = literal;

                    int literalIndex = pathSegment.LastIndexOf(literal.Literal,
                                                               segmentLength - 1, StringComparison.OrdinalIgnoreCase);

                    if (literalIndex == -1)
                    {
                        return(false);
                    }

                    if ((segmentIndex == segment.Segments.Count - 1) &&
                        ((literalIndex + literal.Literal.Length) != pathSegment.Length))
                    {
                        return(false);
                    }

                    index = literalIndex;
                }

                if (lastParameter != null && ((lastLiteral != null &&
                                               parameter == null) || segmentIndex == 0))
                {
                    int startIndex;
                    int lastIndex;

                    if (lastLiteral == null)
                    {
                        startIndex = (segmentIndex == 0) ? 0 : index + lastLiteral.Literal.Length;
                        lastIndex  = segmentLength;
                    }
                    else if (segmentIndex == 0 && parameter != null)
                    {
                        startIndex = 0;
                        lastIndex  = segmentLength;
                    }
                    else
                    {
                        startIndex = index + lastLiteral.Literal.Length;
                        lastIndex  = segmentLength - startIndex;
                    }

                    string part = pathSegment.Substring(startIndex, lastIndex);

                    if (String.IsNullOrEmpty(part))
                    {
                        return(false);
                    }

                    matchedValues.Add(lastParameter.ParameterName, part);

                    lastParameter = null;
                    lastLiteral   = null;
                }
                segmentLength = index;
                segmentIndex--;
            }

            if (segmentLength != 0)
            {
                return(segment.Segments[0] is ParameterSubsegment);
            }

            return(true);
        }
Beispiel #10
0
        public ValueDictionary Match(string virtualPath,
                                     ValueDictionary variables, ValueDictionary defaults)
        {
            List <string> parts = new List <string>(RouteParser.SplitUrl(virtualPath));

            defaults  = defaults ?? new ValueDictionary();
            variables = variables ?? new ValueDictionary();

            ValueDictionary values = new ValueDictionary();

            bool hasAdditionalParameters = false;
            bool isCatchAll = false;

            AssignVariableValues(variables);

            for (int i = 0; i < _segments.Count; i++)
            {
                SeparatorSegment separator = (_segments[i] as SeparatorSegment);
                ContentSegment   content   = (_segments[i] as ContentSegment);

                if (parts.Count <= i)
                {
                    hasAdditionalParameters = true;
                }

                string part = (hasAdditionalParameters) ? null : parts[i];
                if (separator != null)
                {
                    if (!hasAdditionalParameters &&
                        !RouteParser.IsSeparator(part))
                    {
                        return(null);
                    }
                }

                if (content != null)
                {
                    if (content.IsCatchAll)
                    {
                        MatchCatchAll(content, parts.Skip(i), defaults, values);
                        isCatchAll = true;
                    }
                    else if (!MatchContent(content, part, defaults, values))
                    {
                        return(null);
                    }
                }
            }

            if (!isCatchAll && _segments.Count < parts.Count)
            {
                for (int j = _segments.Count; j < parts.Count; j++)
                {
                    if (!RouteParser.IsSeparator(parts[j]))
                    {
                        return(null);
                    }
                }
            }

            if (defaults != null)
            {
                foreach (KeyValuePair <string, object> kvp in defaults)
                {
                    if (!values.ContainsKey(kvp.Key))
                    {
                        values.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            return(values);
        }