|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | + |
| 7 | +namespace Microsoft.AspNetCore.Dispatcher |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// A builder for producing a mapping of keys to <see cref="IDispatcherValueConstraint"/>. |
| 11 | + /// </summary> |
| 12 | + /// <remarks> |
| 13 | + /// <see cref="DispatcherValueConstraintBuilder"/> allows iterative building a set of route constraints, and will |
| 14 | + /// merge multiple entries for the same key. |
| 15 | + /// </remarks> |
| 16 | + public class DispatcherValueConstraintBuilder |
| 17 | + { |
| 18 | + private readonly IConstraintFactory _constraintFactory; |
| 19 | + private readonly string _rawText; |
| 20 | + private readonly Dictionary<string, List<IDispatcherValueConstraint>> _constraints; |
| 21 | + private readonly HashSet<string> _optionalParameters; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a new <see cref="DispatcherValueConstraintBuilder"/> instance. |
| 25 | + /// </summary> |
| 26 | + /// <param name="constraintFactory">The <see cref="IConstraintFactory"/>.</param> |
| 27 | + /// <param name="rawText">The display name (for use in error messages).</param> |
| 28 | + public DispatcherValueConstraintBuilder( |
| 29 | + IConstraintFactory constraintFactory, |
| 30 | + string rawText) |
| 31 | + { |
| 32 | + if (constraintFactory == null) |
| 33 | + { |
| 34 | + throw new ArgumentNullException(nameof(constraintFactory)); |
| 35 | + } |
| 36 | + |
| 37 | + if (rawText == null) |
| 38 | + { |
| 39 | + throw new ArgumentNullException(nameof(rawText)); |
| 40 | + } |
| 41 | + |
| 42 | + _constraintFactory = constraintFactory; |
| 43 | + _rawText = rawText; |
| 44 | + |
| 45 | + _constraints = new Dictionary<string, List<IDispatcherValueConstraint>>(StringComparer.OrdinalIgnoreCase); |
| 46 | + _optionalParameters = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Builds a mapping of constraints. |
| 51 | + /// </summary> |
| 52 | + /// <returns>An <see cref="IDictionary{String, IDispatcherValueConstraint}"/> of the constraints.</returns> |
| 53 | + public IDictionary<string, IDispatcherValueConstraint> Build() |
| 54 | + { |
| 55 | + var constraints = new Dictionary<string, IDispatcherValueConstraint>(StringComparer.OrdinalIgnoreCase); |
| 56 | + foreach (var kvp in _constraints) |
| 57 | + { |
| 58 | + IDispatcherValueConstraint constraint; |
| 59 | + if (kvp.Value.Count == 1) |
| 60 | + { |
| 61 | + constraint = kvp.Value[0]; |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + constraint = new CompositeDispatcherValueConstraint(kvp.Value.ToArray()); |
| 66 | + } |
| 67 | + |
| 68 | + if (_optionalParameters.Contains(kvp.Key)) |
| 69 | + { |
| 70 | + var optionalConstraint = new OptionalDispatcherValueConstraint(constraint); |
| 71 | + constraints.Add(kvp.Key, optionalConstraint); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + constraints.Add(kvp.Key, constraint); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return constraints; |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Adds a constraint instance for the given key. |
| 84 | + /// </summary> |
| 85 | + /// <param name="key">The key.</param> |
| 86 | + /// <param name="value"> |
| 87 | + /// The constraint instance. Must either be a string or an instance of <see cref="IDispatcherValueConstraint"/>. |
| 88 | + /// </param> |
| 89 | + /// <remarks> |
| 90 | + /// If the <paramref name="value"/> is a string, it will be converted to a <see cref="RegexDispatcherValueConstraint"/>. |
| 91 | + /// |
| 92 | + /// For example, the string <code>Product[0-9]+</code> will be converted to the regular expression |
| 93 | + /// <code>^(Product[0-9]+)</code>. See <see cref="System.Text.RegularExpressions.Regex"/> for more details. |
| 94 | + /// </remarks> |
| 95 | + public void AddConstraint(string key, object value) |
| 96 | + { |
| 97 | + if (key == null) |
| 98 | + { |
| 99 | + throw new ArgumentNullException(nameof(key)); |
| 100 | + } |
| 101 | + |
| 102 | + if (value == null) |
| 103 | + { |
| 104 | + throw new ArgumentNullException(nameof(value)); |
| 105 | + } |
| 106 | + |
| 107 | + var constraint = value as IDispatcherValueConstraint; |
| 108 | + if (constraint == null) |
| 109 | + { |
| 110 | + var regexPattern = value as string; |
| 111 | + if (regexPattern == null) |
| 112 | + { |
| 113 | + throw new InvalidOperationException( |
| 114 | + Resources.FormatDispatcherValueConstraintBuilder_ValidationMustBeStringOrCustomConstraint( |
| 115 | + key, |
| 116 | + value, |
| 117 | + _rawText, |
| 118 | + typeof(IDispatcherValueConstraint))); |
| 119 | + } |
| 120 | + |
| 121 | + var constraintsRegEx = "^(" + regexPattern + ")$"; |
| 122 | + constraint = new RegexDispatcherValueConstraint(constraintsRegEx); |
| 123 | + } |
| 124 | + |
| 125 | + Add(key, constraint); |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Adds a constraint for the given key, resolved by the <see cref="IConstraintFactory"/>. |
| 130 | + /// </summary> |
| 131 | + /// <param name="key">The key.</param> |
| 132 | + /// <param name="constraintText">The text to be resolved by <see cref="IConstraintFactory"/>.</param> |
| 133 | + /// <remarks> |
| 134 | + /// The <see cref="IConstraintFactory"/> can create <see cref="IDispatcherValueConstraint"/> instances |
| 135 | + /// based on <paramref name="constraintText"/>. See <see cref="DispatcherOptions.ConstraintMap"/> to register |
| 136 | + /// custom constraint types. |
| 137 | + /// </remarks> |
| 138 | + public void AddResolvedConstraint(string key, string constraintText) |
| 139 | + { |
| 140 | + if (key == null) |
| 141 | + { |
| 142 | + throw new ArgumentNullException(nameof(key)); |
| 143 | + } |
| 144 | + |
| 145 | + if (constraintText == null) |
| 146 | + { |
| 147 | + throw new ArgumentNullException(nameof(constraintText)); |
| 148 | + } |
| 149 | + |
| 150 | + var constraint = _constraintFactory.ResolveConstraint(constraintText); |
| 151 | + if (constraint == null) |
| 152 | + { |
| 153 | + throw new InvalidOperationException( |
| 154 | + Resources.FormatDispatcherValueConstraintBuilder_CouldNotResolveConstraint( |
| 155 | + key, |
| 156 | + constraintText, |
| 157 | + _rawText, |
| 158 | + _constraintFactory.GetType().Name)); |
| 159 | + } |
| 160 | + |
| 161 | + Add(key, constraint); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Sets the given key as optional. |
| 166 | + /// </summary> |
| 167 | + /// <param name="key">The key.</param> |
| 168 | + public void SetOptional(string key) |
| 169 | + { |
| 170 | + if (key == null) |
| 171 | + { |
| 172 | + throw new ArgumentNullException(nameof(key)); |
| 173 | + } |
| 174 | + |
| 175 | + _optionalParameters.Add(key); |
| 176 | + } |
| 177 | + |
| 178 | + private void Add(string key, IDispatcherValueConstraint constraint) |
| 179 | + { |
| 180 | + if (!_constraints.TryGetValue(key, out var list)) |
| 181 | + { |
| 182 | + list = new List<IDispatcherValueConstraint>(); |
| 183 | + _constraints.Add(key, list); |
| 184 | + } |
| 185 | + |
| 186 | + list.Add(constraint); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments