|
1 | | -using System.Text.RegularExpressions; |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text.RegularExpressions; |
2 | 4 | using net.openstack.Core.Domain; |
3 | 5 |
|
4 | 6 | namespace net.openstack.Core |
5 | 7 | { |
| 8 | + /// <summary> |
| 9 | + /// A status parser for HTTP status codes. |
| 10 | + /// </summary> |
6 | 11 | public class HttpStatusCodeParser : IStatusParser |
7 | 12 | { |
8 | | - private const string RegEx = @"(?<StatusCode>\d*)\s*(?<Status>\w*)"; |
9 | | - public Status Parse(string value) |
| 13 | + /// <summary> |
| 14 | + /// The default regular expression to use for matching HTTP status codes. |
| 15 | + /// </summary> |
| 16 | + protected static readonly string DefaultPattern = @"(?<StatusCode>\d*)\s*(?<Status>\w*)"; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// A singleton instance of the default <see cref="HttpStatusCodeParser"/>. |
| 20 | + /// </summary> |
| 21 | + private static readonly HttpStatusCodeParser _default = new HttpStatusCodeParser(DefaultPattern); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The compiled regular expression to use for matching HTTP status codes. |
| 25 | + /// </summary> |
| 26 | + private readonly Regex _expression; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Constructs a new instance of <see cref="HttpStatusCodeParser"/> for the default regular |
| 30 | + /// expression. |
| 31 | + /// </summary> |
| 32 | + [Obsolete("Use HttpStatusCodeParser.Default instead.")] |
| 33 | + public HttpStatusCodeParser() |
| 34 | + : this(DefaultPattern) |
10 | 35 | { |
11 | | - var regex = new Regex(RegEx); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Constructs a new instance of <see cref="HttpStatusCodeParser"/> for the specified regular |
| 40 | + /// expression. |
| 41 | + /// </summary> |
| 42 | + /// <param name="pattern"> |
| 43 | + /// The regular expression pattern to use. |
| 44 | + /// |
| 45 | + /// <para><paramref name="pattern"/> should contain the named capturing grounds <c>StatusCode</c> and <c>status</c>.</para> |
| 46 | + /// </param> |
| 47 | + /// <exception cref="ArgumentNullException"><paramref name="pattern"/> is <c>null</c>.</exception> |
| 48 | + /// <exception cref="ArgumentException"> |
| 49 | + /// <paramref name="pattern"/> does not contain a capturing group named <c>StatusCode</c>. |
| 50 | + /// <para>-or-</para> |
| 51 | + /// <para><paramref name="pattern"/> does not contain a capturing group named <c>Status</c>.</para> |
| 52 | + /// </exception> |
| 53 | + protected HttpStatusCodeParser(string pattern) |
| 54 | + { |
| 55 | + if (pattern == null) |
| 56 | + throw new ArgumentNullException("pattern"); |
| 57 | + |
| 58 | + _expression = new Regex(pattern, RegexOptions.Compiled); |
12 | 59 |
|
13 | | - var match = regex.Match(value); |
| 60 | + string[] groupNames = _expression.GetGroupNames(); |
| 61 | + if (!groupNames.Contains("StatusCode", StringComparer.Ordinal)) |
| 62 | + throw new ArgumentException("The pattern does not contain a StatusCode named capturing group.", "pattern"); |
| 63 | + if (!groupNames.Contains("Status", StringComparer.Ordinal)) |
| 64 | + throw new ArgumentException("The pattern does not contain a Status named capturing group.", "pattern"); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets a default <see cref="HttpStatusCodeParser"/>. |
| 69 | + /// </summary> |
| 70 | + public static HttpStatusCodeParser Default |
| 71 | + { |
| 72 | + get |
| 73 | + { |
| 74 | + return _default; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public Status Parse(string value) |
| 79 | + { |
| 80 | + var match = _expression.Match(value); |
14 | 81 | if (!match.Success) |
15 | 82 | return null; |
16 | 83 |
|
|
0 commit comments