/// <summary>
        /// Given a path, attempts to match the next part of it to the current segment.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="path">The path.</param>
        /// <returns>
        /// An object that indicates whether the path was successfully matched.
        /// </returns>
        public override SegmentPathMatch MatchPath(IRoute route, PathIterator path)
        {
            var values = new RouteValueDictionary();
            var next = path.Next();
            
            if (next.Length == 0)
            {
                if (defaultValue != UrlParameter.NotSpecified)
                {
                    values[parameterName] = defaultValue;
                }
                else
                {
                    return SegmentPathMatch.Failure(string.Format("The path does not contain a segment for parameter '{0}'", parameterName));   
                }
            }
            else
            {
                values[parameterName] = next;

                if (constraint != null && !constraint.IsValid(route, next, parameterName))
                {
                    return SegmentPathMatch.Failure(string.Format("Segment '{0}' did not match the constraint on parameter '{1}'", next, parameterName));
                }
            }
            return SegmentPathMatch.Successful(values);
        }
示例#2
0
 /// <summary>
 /// Given a path, attempts to match the next part of it to the current segment.
 /// </summary>
 /// <param name="route">The route.</param>
 /// <param name="path">The path.</param>
 /// <returns>
 /// An object that indicates whether the path was successfully matched.
 /// </returns>
 public override SegmentPathMatch MatchPath(IRoute route, PathIterator path)
 {
     var next = path.Next();
     return String.Equals(next, literal, StringComparison.InvariantCultureIgnoreCase)
         ? SegmentPathMatch.Successful()
         : SegmentPathMatch.Failure(string.Format("Expected segment '{0}'; got '{1}'", literal, next));
 }
        /// <summary>
        /// Given a path, attempts to match the next part of it to the current segment.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="path">The path.</param>
        /// <returns>
        /// An object that indicates whether the path was successfully matched.
        /// </returns>
        public override SegmentPathMatch MatchPath(IRoute route, PathIterator path)
        {
            var values = new RouteValueDictionary();
            var next   = path.Next();

            if (next.Length == 0)
            {
                if (defaultValue != UrlParameter.NotSpecified)
                {
                    values[parameterName] = defaultValue;
                }
                else
                {
                    return(SegmentPathMatch.Failure(string.Format("The path does not contain a segment for parameter '{0}'", parameterName)));
                }
            }
            else
            {
                values[parameterName] = next;

                if (constraint != null && !constraint.IsValid(route, next, parameterName))
                {
                    return(SegmentPathMatch.Failure(string.Format("Segment '{0}' did not match the constraint on parameter '{1}'", next, parameterName)));
                }
            }
            return(SegmentPathMatch.Successful(values));
        }
示例#4
0
        /// <summary>
        /// Given a path, attempts to match the next part of it to the current segment.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="path">The path.</param>
        /// <returns>
        /// An object that indicates whether the path was successfully matched.
        /// </returns>
        public override SegmentPathMatch MatchPath(IRoute route, PathIterator path)
        {
            var next = path.Next();

            return(String.Equals(next, literal, StringComparison.InvariantCultureIgnoreCase)
                ? SegmentPathMatch.Successful()
                : SegmentPathMatch.Failure(string.Format("Expected segment '{0}'; got '{1}'", literal, next)));
        }
 public void ConsumeTest()
 {
     var eater = new PathIterator("foo/bar/baz");
     Assert.AreEqual("foo", eater.Next());
     Assert.IsFalse(eater.IsAtEnd);
     Assert.AreEqual("bar", eater.Next());
     Assert.AreEqual("baz", eater.Next());
     Assert.IsTrue(eater.IsAtEnd);
 }
示例#6
0
        /// <summary>
        /// Matches the path.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public override SegmentPathMatch MatchPath(IRoute route, PathIterator reader)
        {
            var values = new RouteValueDictionary();
            var path   = reader.ReadAll();

            values[parameterName] = path;
            if (path.Length == 0)
            {
                if (defaultValue != UrlParameter.NotSpecified)
                {
                    values[parameterName] = defaultValue;
                }
            }
            else if (constraint != null && !constraint.IsValid(route, path, parameterName))
            {
                return(SegmentPathMatch.Failure(string.Format("Segment '{0}' did not match constraint for parameter '{1}'", path, parameterName)));
            }
            return(SegmentPathMatch.Successful(values));
        }
        /// <summary>
        /// Matches the path.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        public override SegmentPathMatch MatchPath(IRoute route, PathIterator reader)
        {
            var values = new RouteValueDictionary();
            var path = reader.ReadAll();

            values[parameterName] = path;
            if (path.Length == 0)
            {
                if (defaultValue != UrlParameter.NotSpecified)
                {
                    values[parameterName] = defaultValue;
                }
            }
            else if (constraint != null && !constraint.IsValid(route, path, parameterName))
            {
                return SegmentPathMatch.Failure(string.Format("Segment '{0}' did not match constraint for parameter '{1}'", path, parameterName));
            }
            return SegmentPathMatch.Successful(values);
        }
示例#8
0
        /// <summary>
        /// Attempts to matche a path to this parsed route.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="request">The request.</param>
        /// <returns>An object indicating the success or failure of the attempt to match the path.</returns>
        public RouteMatch MatchPathToRoute(IRoute route, string request)
        {
            var values   = new RouteValueDictionary();
            var iterator = new PathIterator(request);

            foreach (var segment in Segments)
            {
                var match = segment.MatchPath(route, iterator);
                if (match.Success)
                {
                    values.AddRange(match.Values);
                }
                else
                {
                    return(RouteMatch.Failure(route, match.FailReason));
                }
            }

            return(iterator.IsAtEnd
                ? RouteMatch.Successful(route, values)
                : RouteMatch.Failure(route, "Route was initially matched, but the request contains additional unexpected segments"));
        }
示例#9
0
        /// <summary>
        /// Attempts to matche a path to this parsed route.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <param name="request">The request.</param>
        /// <returns>An object indicating the success or failure of the attempt to match the path.</returns>
        public RouteMatch MatchPathToRoute(IRoute route, string request)
        {
            var values = new RouteValueDictionary();
            var iterator = new PathIterator(request);

            foreach (var segment in Segments)
            {
                var match = segment.MatchPath(route, iterator);
                if (match.Success)
                {
                    values.AddRange(match.Values);
                }
                else
                {
                    return RouteMatch.Failure(route, match.FailReason);
                }
            }

            return iterator.IsAtEnd
                ? RouteMatch.Successful(route, values)
                : RouteMatch.Failure(route, "Route was initially matched, but the request contains additional unexpected segments");
        }
示例#10
0
 public void ReadAllConsumesPath()
 {
     var eater = new PathIterator("/foo//bar////baz");
     Assert.AreEqual("foo/bar/baz", eater.ReadAll());
     Assert.IsTrue(eater.IsAtEnd);
 }
示例#11
0
 public void ConsumeEmptyTest()
 {
     var eater = new PathIterator("");
     Assert.AreEqual("", eater.Next());
     Assert.IsTrue(eater.IsAtEnd);
 }