|
| 1 | +#region License |
| 2 | +/*---------------------------------------------------------------------------------*\ |
| 3 | +
|
| 4 | + Distributed under the terms of an MIT-style license: |
| 5 | +
|
| 6 | + The MIT License |
| 7 | +
|
| 8 | + Copyright (c) 2006-2009 Stephen M. McKamey |
| 9 | +
|
| 10 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + of this software and associated documentation files (the "Software"), to deal |
| 12 | + in the Software without restriction, including without limitation the rights |
| 13 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + copies of the Software, and to permit persons to whom the Software is |
| 15 | + furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | + The above copyright notice and this permission notice shall be included in |
| 18 | + all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + THE SOFTWARE. |
| 27 | +
|
| 28 | +\*---------------------------------------------------------------------------------*/ |
| 29 | +#endregion License |
| 30 | + |
| 31 | +using System; |
| 32 | +using System.Collections.Generic; |
| 33 | +using System.IO; |
| 34 | + |
| 35 | +namespace JsonFx.Json |
| 36 | +{ |
| 37 | + public interface IDataWriterProvider |
| 38 | + { |
| 39 | + IDataWriter DefaultDataWriter { get; } |
| 40 | + |
| 41 | + IDataWriter Find(string extension); |
| 42 | + |
| 43 | + IDataWriter Find(string acceptHeader, string contentTypeHeader); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Provides lookup capabilities for finding an IDataWriter |
| 48 | + /// </summary> |
| 49 | + public class DataWriterProvider : IDataWriterProvider |
| 50 | + { |
| 51 | + #region Fields |
| 52 | + |
| 53 | + private readonly IDataWriter DefaultWriter; |
| 54 | + private readonly IDictionary<string, IDataWriter> WritersByExt = new Dictionary<string, IDataWriter>(StringComparer.OrdinalIgnoreCase); |
| 55 | + private readonly IDictionary<string, IDataWriter> WritersByMime = new Dictionary<string, IDataWriter>(StringComparer.OrdinalIgnoreCase); |
| 56 | + |
| 57 | + #endregion Fields |
| 58 | + |
| 59 | + #region Init |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Ctor |
| 63 | + /// </summary> |
| 64 | + /// <param name="writers">inject with all possible writers</param> |
| 65 | + public DataWriterProvider(IEnumerable<IDataWriter> writers) |
| 66 | + { |
| 67 | + if (writers != null) |
| 68 | + { |
| 69 | + foreach (IDataWriter writer in writers) |
| 70 | + { |
| 71 | + if (this.DefaultWriter == null) |
| 72 | + { |
| 73 | + // TODO: decide less arbitrary way to choose default |
| 74 | + // without hardcoding value into IDataWriter |
| 75 | + this.DefaultWriter = writer; |
| 76 | + } |
| 77 | + |
| 78 | + if (!String.IsNullOrEmpty(writer.ContentType)) |
| 79 | + { |
| 80 | + this.WritersByMime[writer.ContentType] = writer; |
| 81 | + } |
| 82 | + |
| 83 | + if (!String.IsNullOrEmpty(writer.ContentType)) |
| 84 | + { |
| 85 | + string ext = DataWriterProvider.NormalizeExtension(writer.FileExtension); |
| 86 | + this.WritersByExt[ext] = writer; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + #endregion Init |
| 93 | + |
| 94 | + #region Properties |
| 95 | + |
| 96 | + public IDataWriter DefaultDataWriter |
| 97 | + { |
| 98 | + get { return this.DefaultWriter; } |
| 99 | + } |
| 100 | + |
| 101 | + #endregion Properties |
| 102 | + |
| 103 | + #region Methods |
| 104 | + |
| 105 | + public IDataWriter Find(string extension) |
| 106 | + { |
| 107 | + extension = DataWriterProvider.NormalizeExtension(extension); |
| 108 | + |
| 109 | + if (this.WritersByExt.ContainsKey(extension)) |
| 110 | + { |
| 111 | + return WritersByExt[extension]; |
| 112 | + } |
| 113 | + |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + public IDataWriter Find(string acceptHeader, string contentTypeHeader) |
| 118 | + { |
| 119 | + foreach (string type in DataWriterProvider.ParseHeaders(acceptHeader, contentTypeHeader)) |
| 120 | + { |
| 121 | + if (this.WritersByMime.ContainsKey(type)) |
| 122 | + { |
| 123 | + return WritersByMime[type]; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + #endregion Methods |
| 131 | + |
| 132 | + #region Utility Methods |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Parses HTTP headers for Media-Types |
| 136 | + /// </summary> |
| 137 | + /// <param name="accept">HTTP Accept header</param> |
| 138 | + /// <param name="contentType">HTTP Content-Type header</param> |
| 139 | + /// <returns>sequence of Media-Types</returns> |
| 140 | + /// <remarks> |
| 141 | + /// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html |
| 142 | + /// </remarks> |
| 143 | + public static IEnumerable<string> ParseHeaders(string accept, string contentType) |
| 144 | + { |
| 145 | + string mime; |
| 146 | + |
| 147 | + // check for a matching accept type |
| 148 | + foreach (string type in DataWriterProvider.SplitTrim(accept, ',')) |
| 149 | + { |
| 150 | + mime = DataWriterProvider.ParseMediaType(type); |
| 151 | + if (!String.IsNullOrEmpty(mime)) |
| 152 | + { |
| 153 | + yield return mime; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // fallback on content-type |
| 158 | + mime = DataWriterProvider.ParseMediaType(contentType); |
| 159 | + if (!String.IsNullOrEmpty(mime)) |
| 160 | + { |
| 161 | + yield return mime; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// |
| 167 | + /// </summary> |
| 168 | + /// <param name="type"></param> |
| 169 | + /// <returns></returns> |
| 170 | + public static string ParseMediaType(string type) |
| 171 | + { |
| 172 | + foreach (string mime in DataWriterProvider.SplitTrim(type, ';')) |
| 173 | + { |
| 174 | + // only return first part |
| 175 | + return mime; |
| 176 | + } |
| 177 | + |
| 178 | + // if no parts then was empty |
| 179 | + return String.Empty; |
| 180 | + } |
| 181 | + |
| 182 | + private static IEnumerable<string> SplitTrim(string source, char ch) |
| 183 | + { |
| 184 | + if (String.IsNullOrEmpty(source)) |
| 185 | + { |
| 186 | + yield break; |
| 187 | + } |
| 188 | + |
| 189 | + int length = source.Length; |
| 190 | + for (int prev=0, next=0; prev<length && next>=0; prev=next+1) |
| 191 | + { |
| 192 | + next = source.IndexOf(ch, prev); |
| 193 | + if (next < 0) |
| 194 | + { |
| 195 | + next = length; |
| 196 | + } |
| 197 | + |
| 198 | + string part = source.Substring(prev, next-prev).Trim(); |
| 199 | + if (part.Length > 0) |
| 200 | + { |
| 201 | + yield return part; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static string NormalizeExtension(string extension) |
| 207 | + { |
| 208 | + if (String.IsNullOrEmpty(extension)) |
| 209 | + { |
| 210 | + return String.Empty; |
| 211 | + } |
| 212 | + |
| 213 | + // ensure is only extension with leading dot |
| 214 | + return Path.GetExtension(extension); |
| 215 | + } |
| 216 | + |
| 217 | + #endregion Utility Methods |
| 218 | + } |
| 219 | +} |
0 commit comments