private static void VisitVariable(VariableSubsegment segment, ValueDictionary variables)
		{
			object value;
			if (!variables.TryGetValue(segment.VariableName, out value))
				throw Error.UndefinedRouteVariable(segment.VariableName);

			segment.Value = value;
		}
Exemplo n.º 2
0
        private static bool IsParameterRequired(ParameterSubsegment segment, 
            ValueDictionary defaults, out object value)
        {
            value = null;

            if (segment.IsCatchAll)
                return false;

            return !(defaults.TryGetValue(segment.ParameterName, out value));
        }
        public override ValueProviderResult GetValue(string key)
        {
            object value;

            if (_values.TryGetValue(key, out value))
            {
                return(new ValueProviderResult(value, Culture));
            }

            return(null);
        }
        private static void VisitVariable(VariableSubsegment segment, ValueDictionary variables)
        {
            object value;

            if (!variables.TryGetValue(segment.VariableName, out value))
            {
                throw Error.UndefinedRouteVariable(segment.VariableName);
            }

            segment.Value = value;
        }
Exemplo n.º 5
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            T val; result = null;

            if (ValueDictionary.TryGetValue(binder.Name, out val))
            {
                result = val;
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
        private static bool IsParameterRequired(ParameterSubsegment segment,
                                                ValueDictionary defaults, out object value)
        {
            value = null;

            if (segment.IsCatchAll)
            {
                return(false);
            }

            return(!(defaults.TryGetValue(segment.ParameterName, out value)));
        }
        protected virtual string CreateParameterKey(ValueDictionary parameters,
                                                    string parameterName)
        {
            object value;

            parameters.TryGetValue(parameterName, out value);

            string stringValue = (value == null) ? "<NULL>" : Convert.ToString(value, CultureInfo.InvariantCulture);
            string typeName    = (value == null) ? null : value.GetType().FullName;

            if (String.Equals(stringValue, typeName, StringComparison.OrdinalIgnoreCase))
            {
                return(String.Empty);
            }

            return(String.Concat("[", parameterName, "=", stringValue, "]"));
        }
Exemplo n.º 8
0
        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);
        }
        public bool Intercept(RouteDefinition route)
        {
            Precondition.Require(route, () => Error.ArgumentNull("route"));
            MatchCollection matches = _variablePattern.Matches(route.Url);
            string          url     = route.Url;

            foreach (Match match in matches)
            {
                string name = match.Groups["name"].Value;
                object value;

                if (!_variables.TryGetValue(name, out value))
                {
                    throw Error.UndefinedRouteVariable(name);
                }

                url = url.Replace(String.Format("[{0}]", name), Convert.ToString(value));
            }
            route.Url = url;
            return(true);
        }
Exemplo n.º 10
0
 public bool TryGetValue(string key, out object value)
 {
     return(_values.TryGetValue(key, out value));
 }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        public BoundUrl Bind(ValueDictionary currentValues, 
            ValueDictionary values, ValueDictionary variables, 
			ValueDictionary defaults)
        {
            currentValues = currentValues ?? new ValueDictionary();
            values = values ?? new ValueDictionary();
			variables = variables ?? new ValueDictionary();
			defaults = defaults ?? new ValueDictionary();

            ValueDictionary acceptedValues = new ValueDictionary();
            HashSet<string> unusedValues = new HashSet<string>(
                values.Keys, StringComparer.OrdinalIgnoreCase);

			AssignVariableValues(variables);

            ForEachParameter(_segments, segment => {
                object value;
                object currentValue;

                string parameterName = segment.ParameterName;
                bool hasValue = values.TryGetValue(parameterName, out value);
                
                if (hasValue)
                    unusedValues.Remove(parameterName);
                
                bool hasCurrentValue = currentValues.TryGetValue(parameterName, out currentValue);
                
                if (hasValue && hasCurrentValue && !RoutePartsEqual(currentValue, value))
                    return false;

                if (hasValue)
                {
                    if (IsRoutePartNonEmpty(value))
                        acceptedValues.Add(parameterName, value);
                }
                else if (hasCurrentValue)
                    acceptedValues.Add(parameterName, currentValue);
                
                return true;
            });

            foreach (KeyValuePair<string, object> kvp in values)
            {
                if (IsRoutePartNonEmpty(kvp.Value) && !acceptedValues.ContainsKey(kvp.Key))
                    acceptedValues.Add(kvp.Key, kvp.Value);
            }

            foreach (KeyValuePair<string, object> kvp in currentValues)
            {
                if (!acceptedValues.ContainsKey(kvp.Key) && 
                    GetParameterSubsegment(_segments, kvp.Key) == null)
                    acceptedValues.Add(kvp.Key, kvp.Value);
            }

            ForEachParameter(_segments, segment => {
                object value;
                
                if (!acceptedValues.ContainsKey(segment.ParameterName) && 
                    !IsParameterRequired(segment, defaults, out value))
                    acceptedValues.Add(segment.ParameterName, value);
                
                return true;
            });

            if (!ForEachParameter(_segments, segment => {
                object value;
                if (IsParameterRequired(segment, defaults, out value) && 
                    !acceptedValues.ContainsKey(segment.ParameterName))
                    return false;
                
                return true;
            })) 
            {
                return null;
            }

            ValueDictionary others = new ValueDictionary(
                (IDictionary<string, object>)defaults);
            ForEachParameter(_segments, segment => {
                others.Remove(segment.ParameterName);
                return true;
            });

            foreach (KeyValuePair<string, object> kvp in others)
            {
                object value;
                if (values.TryGetValue(kvp.Key, out value))
                {
                    unusedValues.Remove(kvp.Key);
                    if (!RoutePartsEqual(value, kvp.Value))
                        return null;
                }
            }

            return BuildUrl(defaults, acceptedValues, unusedValues);
        }
Exemplo n.º 14
0
        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;
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        public BoundUrl Bind(ValueDictionary currentValues,
                             ValueDictionary values, ValueDictionary variables,
                             ValueDictionary defaults)
        {
            currentValues = currentValues ?? new ValueDictionary();
            values        = values ?? new ValueDictionary();
            variables     = variables ?? new ValueDictionary();
            defaults      = defaults ?? new ValueDictionary();

            ValueDictionary  acceptedValues = new ValueDictionary();
            HashSet <string> unusedValues   = new HashSet <string>(
                values.Keys, StringComparer.OrdinalIgnoreCase);

            AssignVariableValues(variables);

            ForEachParameter(_segments, segment => {
                object value;
                object currentValue;

                string parameterName = segment.ParameterName;
                bool hasValue        = values.TryGetValue(parameterName, out value);

                if (hasValue)
                {
                    unusedValues.Remove(parameterName);
                }

                bool hasCurrentValue = currentValues.TryGetValue(parameterName, out currentValue);

                if (hasValue && hasCurrentValue && !RoutePartsEqual(currentValue, value))
                {
                    return(false);
                }

                if (hasValue)
                {
                    if (IsRoutePartNonEmpty(value))
                    {
                        acceptedValues.Add(parameterName, value);
                    }
                }
                else if (hasCurrentValue)
                {
                    acceptedValues.Add(parameterName, currentValue);
                }

                return(true);
            });

            foreach (KeyValuePair <string, object> kvp in values)
            {
                if (IsRoutePartNonEmpty(kvp.Value) && !acceptedValues.ContainsKey(kvp.Key))
                {
                    acceptedValues.Add(kvp.Key, kvp.Value);
                }
            }

            foreach (KeyValuePair <string, object> kvp in currentValues)
            {
                if (!acceptedValues.ContainsKey(kvp.Key) &&
                    GetParameterSubsegment(_segments, kvp.Key) == null)
                {
                    acceptedValues.Add(kvp.Key, kvp.Value);
                }
            }

            ForEachParameter(_segments, segment => {
                object value;

                if (!acceptedValues.ContainsKey(segment.ParameterName) &&
                    !IsParameterRequired(segment, defaults, out value))
                {
                    acceptedValues.Add(segment.ParameterName, value);
                }

                return(true);
            });

            if (!ForEachParameter(_segments, segment => {
                object value;
                if (IsParameterRequired(segment, defaults, out value) &&
                    !acceptedValues.ContainsKey(segment.ParameterName))
                {
                    return(false);
                }

                return(true);
            }))
            {
                return(null);
            }

            ValueDictionary others = new ValueDictionary(
                (IDictionary <string, object>)defaults);

            ForEachParameter(_segments, segment => {
                others.Remove(segment.ParameterName);
                return(true);
            });

            foreach (KeyValuePair <string, object> kvp in others)
            {
                object value;
                if (values.TryGetValue(kvp.Key, out value))
                {
                    unusedValues.Remove(kvp.Key);
                    if (!RoutePartsEqual(value, kvp.Value))
                    {
                        return(null);
                    }
                }
            }

            return(BuildUrl(defaults, acceptedValues, unusedValues));
        }
Exemplo n.º 17
0
        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));
        }