From 55a1fab053ff3a9fdf15d7027bc9b9628bbec2ff Mon Sep 17 00:00:00 2001 From: Pranav K Date: Thu, 8 Oct 2015 10:42:40 -0700 Subject: [PATCH] Replace NotNullAttribute with thrown exceptions --- src/CultureInfoGenerator/Program.cs | 3 +- ...eptLanguageHeaderRequestCultureProvider.cs | 11 ++- .../ApplicationBuilderExtensions.cs | 27 +++++-- .../CookieRequestCultureProvider.cs | 19 +++-- .../CustomRequestCultureProvider.cs | 19 +++-- .../QueryStringRequestCultureProvider.cs | 11 ++- .../RequestCulture.cs | 19 +++-- .../RequestCultureFeature.cs | 9 ++- .../RequestLocalizationMiddleware.cs | 23 ++++-- .../project.json | 9 ++- .../project.json | 3 +- .../LocalizedString.cs | 19 +++-- .../StringLocalizerExtensions.cs | 49 ++++++++++--- .../StringLocalizerOfT.cs | 36 ++++++++-- .../project.json | 7 +- .../Internal/AssemblyWrapper.cs | 9 ++- ...LocalizationServiceCollectionExtensions.cs | 15 +++- .../ResourceManagerStringLocalizer.cs | 70 +++++++++++++++---- .../ResourceManagerStringLocalizerFactory.cs | 29 ++++++-- ...sourceManagerWithCultureStringLocalizer.cs | 50 +++++++++++-- .../project.json | 7 +- 21 files changed, 349 insertions(+), 95 deletions(-) diff --git a/src/CultureInfoGenerator/Program.cs b/src/CultureInfoGenerator/Program.cs index 76882d3ce0..1e1da87b91 100644 --- a/src/CultureInfoGenerator/Program.cs +++ b/src/CultureInfoGenerator/Program.cs @@ -5,7 +5,8 @@ using System; using System.Globalization; using System.IO; using System.Linq; -using Microsoft.Dnx.Runtime;using Microsoft.Win32; +using Microsoft.Dnx.Runtime; +using Microsoft.Win32; namespace CultureInfoGenerator { diff --git a/src/Microsoft.AspNet.Localization/AcceptLanguageHeaderRequestCultureProvider.cs b/src/Microsoft.AspNet.Localization/AcceptLanguageHeaderRequestCultureProvider.cs index 1368efb4ed..adf41adc9a 100644 --- a/src/Microsoft.AspNet.Localization/AcceptLanguageHeaderRequestCultureProvider.cs +++ b/src/Microsoft.AspNet.Localization/AcceptLanguageHeaderRequestCultureProvider.cs @@ -1,11 +1,11 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.Extensions.Globalization; -using Microsoft.Extensions.Internal; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Localization @@ -23,8 +23,13 @@ namespace Microsoft.AspNet.Localization public int MaximumAcceptLanguageHeaderValuesToTry { get; set; } = 3; /// - public override Task DetermineRequestCulture([NotNull] HttpContext httpContext) + public override Task DetermineRequestCulture(HttpContext httpContext) { + if (httpContext == null) + { + throw new ArgumentNullException(nameof(httpContext)); + } + var acceptLanguageHeader = httpContext.Request.GetTypedHeaders().AcceptLanguage; if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0) @@ -40,7 +45,7 @@ namespace Microsoft.AspNet.Localization // attempt to parse as a CultureInfo to mitigate potentially spinning CPU on lots of parse attempts. languages = languages.Take(MaximumAcceptLanguageHeaderValuesToTry); } - + var orderedLanguages = languages.OrderByDescending(h => h, StringWithQualityHeaderValueComparer.QualityComparer) .ToList(); diff --git a/src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs b/src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs index 37080d6dc2..6d49afbb66 100644 --- a/src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Localization/ApplicationBuilderExtensions.cs @@ -1,8 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using Microsoft.AspNet.Localization; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Builder { @@ -17,8 +17,13 @@ namespace Microsoft.AspNet.Builder /// /// The . /// The . - public static IApplicationBuilder UseRequestLocalization([NotNull] this IApplicationBuilder builder) + public static IApplicationBuilder UseRequestLocalization(this IApplicationBuilder builder) { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + var options = new RequestLocalizationOptions(); return UseRequestLocalization(builder, options); @@ -32,8 +37,20 @@ namespace Microsoft.AspNet.Builder /// The options to configure the middleware with. /// The . public static IApplicationBuilder UseRequestLocalization( - [NotNull] this IApplicationBuilder builder, - [NotNull] RequestLocalizationOptions options) - => builder.UseMiddleware(options); + this IApplicationBuilder builder, + RequestLocalizationOptions options) + { + if (builder == null) + { + throw new ArgumentNullException(nameof(builder)); + } + + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } + + return builder.UseMiddleware(options); + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Localization/CookieRequestCultureProvider.cs b/src/Microsoft.AspNet.Localization/CookieRequestCultureProvider.cs index 305f0796db..22c98c1e15 100644 --- a/src/Microsoft.AspNet.Localization/CookieRequestCultureProvider.cs +++ b/src/Microsoft.AspNet.Localization/CookieRequestCultureProvider.cs @@ -5,7 +5,6 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.Extensions.Globalization; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Localization { @@ -17,7 +16,7 @@ namespace Microsoft.AspNet.Localization private static readonly char[] _cookieSeparator = new[] { '|' }; private static readonly string _culturePrefix = "c="; private static readonly string _uiCulturePrefix = "uic="; - + /// /// Represent the default cookie name used to track the user's preferred culture information, which is "ASPNET_CULTURE". /// @@ -30,8 +29,13 @@ namespace Microsoft.AspNet.Localization public string CookieName { get; set; } = DefaultCookieName; /// - public override Task DetermineRequestCulture([NotNull] HttpContext httpContext) + public override Task DetermineRequestCulture(HttpContext httpContext) { + if (httpContext == null) + { + throw new ArgumentNullException(nameof(httpContext)); + } + var cookie = httpContext.Request.Cookies[CookieName]; if (cookie == null) @@ -51,8 +55,13 @@ namespace Microsoft.AspNet.Localization /// /// The . /// The cookie value. - public static string MakeCookieValue([NotNull] RequestCulture requestCulture) + public static string MakeCookieValue(RequestCulture requestCulture) { + if (requestCulture == null) + { + throw new ArgumentNullException(nameof(requestCulture)); + } + var seperator = _cookieSeparator[0].ToString(); return string.Join(seperator, @@ -66,7 +75,7 @@ namespace Microsoft.AspNet.Localization /// /// The cookie value to parse. /// The or null if parsing fails. - public static RequestCulture ParseCookieValue([NotNull] string value) + public static RequestCulture ParseCookieValue(string value) { if (string.IsNullOrWhiteSpace(value)) { diff --git a/src/Microsoft.AspNet.Localization/CustomRequestCultureProvider.cs b/src/Microsoft.AspNet.Localization/CustomRequestCultureProvider.cs index 112123b847..4af0faabda 100644 --- a/src/Microsoft.AspNet.Localization/CustomRequestCultureProvider.cs +++ b/src/Microsoft.AspNet.Localization/CustomRequestCultureProvider.cs @@ -4,7 +4,6 @@ using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Localization { @@ -19,13 +18,25 @@ namespace Microsoft.AspNet.Localization /// Creates a new using the specified delegate. /// /// The provider delegate. - public CustomRequestCultureProvider([NotNull] Func> provider) + public CustomRequestCultureProvider(Func> provider) { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + _provider = provider; } /// - public override Task DetermineRequestCulture([NotNull] HttpContext httpContext) - => _provider(httpContext); + public override Task DetermineRequestCulture(HttpContext httpContext) + { + if (httpContext == null) + { + throw new ArgumentNullException(nameof(httpContext)); + } + + return _provider(httpContext); + } } } diff --git a/src/Microsoft.AspNet.Localization/QueryStringRequestCultureProvider.cs b/src/Microsoft.AspNet.Localization/QueryStringRequestCultureProvider.cs index bdf6dc6c1f..ea1398ed9f 100644 --- a/src/Microsoft.AspNet.Localization/QueryStringRequestCultureProvider.cs +++ b/src/Microsoft.AspNet.Localization/QueryStringRequestCultureProvider.cs @@ -1,10 +1,10 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.Extensions.Globalization; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Localization { @@ -27,8 +27,13 @@ namespace Microsoft.AspNet.Localization public string UIQueryStringKey { get; set; } = "ui-culture"; /// - public override Task DetermineRequestCulture([NotNull] HttpContext httpContext) + public override Task DetermineRequestCulture(HttpContext httpContext) { + if (httpContext == null) + { + throw new ArgumentNullException(nameof(httpContext)); + } + var request = httpContext.Request; if (!request.QueryString.HasValue) { @@ -65,7 +70,7 @@ namespace Microsoft.AspNet.Localization // Value for UI culture but not for culture so default to UI culture value for both queryCulture = queryUICulture; } - + var culture = CultureInfoCache.GetCultureInfo(queryCulture); var uiCulture = CultureInfoCache.GetCultureInfo(queryUICulture); diff --git a/src/Microsoft.AspNet.Localization/RequestCulture.cs b/src/Microsoft.AspNet.Localization/RequestCulture.cs index 31cf028760..175a05ec0b 100644 --- a/src/Microsoft.AspNet.Localization/RequestCulture.cs +++ b/src/Microsoft.AspNet.Localization/RequestCulture.cs @@ -1,8 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Globalization; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Localization { @@ -16,10 +16,9 @@ namespace Microsoft.AspNet.Localization /// properties set to the same value. /// /// The for the request. - public RequestCulture([NotNull] CultureInfo culture) - : this (culture, culture) + public RequestCulture(CultureInfo culture) + : this(culture, culture) { - } /// @@ -28,8 +27,18 @@ namespace Microsoft.AspNet.Localization /// /// The for the request to be used for formatting. /// The for the request to be used for text, i.e. language. - public RequestCulture([NotNull] CultureInfo culture, [NotNull] CultureInfo uiCulture) + public RequestCulture(CultureInfo culture, CultureInfo uiCulture) { + if (culture == null) + { + throw new ArgumentNullException(nameof(culture)); + } + + if (uiCulture == null) + { + throw new ArgumentNullException(nameof(uiCulture)); + } + Culture = culture; UICulture = uiCulture; } diff --git a/src/Microsoft.AspNet.Localization/RequestCultureFeature.cs b/src/Microsoft.AspNet.Localization/RequestCultureFeature.cs index 0b952984fb..1fb00c9895 100644 --- a/src/Microsoft.AspNet.Localization/RequestCultureFeature.cs +++ b/src/Microsoft.AspNet.Localization/RequestCultureFeature.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.Extensions.Internal; +using System; namespace Microsoft.AspNet.Localization { @@ -15,8 +15,13 @@ namespace Microsoft.AspNet.Localization /// /// The . /// The . - public RequestCultureFeature([NotNull] RequestCulture requestCulture, IRequestCultureProvider provider) + public RequestCultureFeature(RequestCulture requestCulture, IRequestCultureProvider provider) { + if (requestCulture == null) + { + throw new ArgumentNullException(nameof(requestCulture)); + } + RequestCulture = requestCulture; Provider = provider; } diff --git a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs index a5df668ca5..825e7c4f8c 100644 --- a/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs +++ b/src/Microsoft.AspNet.Localization/RequestLocalizationMiddleware.cs @@ -1,13 +1,13 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Globalization; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; -using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Localization { @@ -19,14 +19,24 @@ namespace Microsoft.AspNet.Localization { private readonly RequestDelegate _next; private readonly RequestLocalizationOptions _options; - + /// /// Creates a new . /// /// The representing the next middleware in the pipeline. /// The representing the options for the . - public RequestLocalizationMiddleware([NotNull] RequestDelegate next, [NotNull] RequestLocalizationOptions options) + public RequestLocalizationMiddleware(RequestDelegate next, RequestLocalizationOptions options) { + if (next == null) + { + throw new ArgumentNullException(nameof(next)); + } + + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } + _next = next; _options = options; } @@ -36,8 +46,13 @@ namespace Microsoft.AspNet.Localization /// /// The . /// A that completes when the middleware has completed processing. - public async Task Invoke([NotNull] HttpContext context) + public async Task Invoke(HttpContext context) { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + var requestCulture = _options.DefaultRequestCulture ?? new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture); diff --git a/src/Microsoft.AspNet.Localization/project.json b/src/Microsoft.AspNet.Localization/project.json index 6b2f05db67..450bf4c204 100644 --- a/src/Microsoft.AspNet.Localization/project.json +++ b/src/Microsoft.AspNet.Localization/project.json @@ -5,14 +5,13 @@ "type": "git", "url": "https://github.com/aspnet/localization" }, + "compilationOptions": { + "warningsAsErrors": true + }, "dependencies": { "Microsoft.AspNet.Http.Extensions": "1.0.0-*", "Microsoft.Extensions.Globalization.CultureInfoCache": "1.0.0-*", - "Microsoft.Extensions.Localization.Abstractions": "1.0.0-*", - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - } + "Microsoft.Extensions.Localization.Abstractions": "1.0.0-*" }, "frameworks": { "dnx451": { }, diff --git a/src/Microsoft.Extensions.Globalization.CultureInfoCache/project.json b/src/Microsoft.Extensions.Globalization.CultureInfoCache/project.json index 8bb390e713..bcab9ffe4d 100644 --- a/src/Microsoft.Extensions.Globalization.CultureInfoCache/project.json +++ b/src/Microsoft.Extensions.Globalization.CultureInfoCache/project.json @@ -5,7 +5,8 @@ "type": "git", "url": "https://github.com/aspnet/localization" }, - "dependencies": { + "compilationOptions": { + "warningsAsErrors": true }, "frameworks": { "dnx451": { }, diff --git a/src/Microsoft.Extensions.Localization.Abstractions/LocalizedString.cs b/src/Microsoft.Extensions.Localization.Abstractions/LocalizedString.cs index a8db327609..378d5edb21 100644 --- a/src/Microsoft.Extensions.Localization.Abstractions/LocalizedString.cs +++ b/src/Microsoft.Extensions.Localization.Abstractions/LocalizedString.cs @@ -1,7 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.Extensions.Internal; +using System; namespace Microsoft.Extensions.Localization { @@ -15,10 +15,9 @@ namespace Microsoft.Extensions.Localization /// /// The name of the string in the resource it was loaded from. /// The actual string. - public LocalizedString([NotNull] string name, [NotNull] string value) + public LocalizedString(string name, string value) : this(name, value, resourceNotFound: false) { - } /// @@ -27,14 +26,24 @@ namespace Microsoft.Extensions.Localization /// The name of the string in the resource it was loaded from. /// The actual string. /// Whether the string was found in a resource. Set this to false to indicate an alternate string value was used. - public LocalizedString([NotNull] string name, [NotNull] string value, bool resourceNotFound) + public LocalizedString(string name, string value, bool resourceNotFound) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } + Name = name; Value = value; ResourceNotFound = resourceNotFound; } - public static implicit operator string (LocalizedString localizedString) + public static implicit operator string(LocalizedString localizedString) { return localizedString.Value; } diff --git a/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerExtensions.cs b/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerExtensions.cs index ff48da00e1..73d254bab9 100644 --- a/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerExtensions.cs +++ b/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerExtensions.cs @@ -1,8 +1,8 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; -using Microsoft.Extensions.Internal; namespace Microsoft.Extensions.Localization { @@ -15,8 +15,21 @@ namespace Microsoft.Extensions.Localization /// The name of the string resource. /// The string resource as a . public static LocalizedString GetString( - [NotNull] this IStringLocalizer stringLocalizer, - [NotNull] string name) => stringLocalizer[name]; + this IStringLocalizer stringLocalizer, + string name) + { + if (stringLocalizer == null) + { + throw new ArgumentNullException(nameof(stringLocalizer)); + } + + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return stringLocalizer[name]; + } /// /// Gets the string resource with the given name and formatted with the supplied arguments. @@ -26,16 +39,36 @@ namespace Microsoft.Extensions.Localization /// The values to format the string with. /// The formatted string resource as a . public static LocalizedString GetString( - [NotNull] this IStringLocalizer stringLocalizer, - [NotNull] string name, - params object[] arguments) => stringLocalizer[name, arguments]; + this IStringLocalizer stringLocalizer, + string name, + params object[] arguments) + { + if (stringLocalizer == null) + { + throw new ArgumentNullException(nameof(stringLocalizer)); + } + + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return stringLocalizer[name, arguments]; + } /// /// Gets all string resources including those for ancestor cultures. /// /// The . /// The string resources. - public static IEnumerable GetAllStrings([NotNull] this IStringLocalizer stringLocalizer) => - stringLocalizer.GetAllStrings(includeAncestorCultures: true); + public static IEnumerable GetAllStrings(this IStringLocalizer stringLocalizer) + { + if (stringLocalizer == null) + { + throw new ArgumentNullException(nameof(stringLocalizer)); + } + + return stringLocalizer.GetAllStrings(includeAncestorCultures: true); + } } } diff --git a/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerOfT.cs b/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerOfT.cs index 2c19e7fbaa..7007122b67 100644 --- a/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerOfT.cs +++ b/src/Microsoft.Extensions.Localization.Abstractions/StringLocalizerOfT.cs @@ -1,9 +1,9 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Globalization; -using Microsoft.Extensions.Internal; namespace Microsoft.Extensions.Localization { @@ -19,8 +19,13 @@ namespace Microsoft.Extensions.Localization /// Creates a new . /// /// The to use. - public StringLocalizer([NotNull] IStringLocalizerFactory factory) + public StringLocalizer(IStringLocalizerFactory factory) { + if (factory == null) + { + throw new ArgumentNullException(nameof(factory)); + } + _localizer = factory.Create(typeof(TResourceSource)); } @@ -28,11 +33,32 @@ namespace Microsoft.Extensions.Localization public virtual IStringLocalizer WithCulture(CultureInfo culture) => _localizer.WithCulture(culture); /// - public virtual LocalizedString this[[NotNull] string name] => _localizer[name]; + public virtual LocalizedString this[string name] + { + get + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer[name]; + } + } /// - public virtual LocalizedString this[[NotNull] string name, params object[] arguments] => - _localizer[name, arguments]; + public virtual LocalizedString this[string name, params object[] arguments] + { + get + { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + + return _localizer[name, arguments]; + } + } /// public IEnumerable GetAllStrings(bool includeAncestorCultures) => diff --git a/src/Microsoft.Extensions.Localization.Abstractions/project.json b/src/Microsoft.Extensions.Localization.Abstractions/project.json index 323c5af77e..7a35dfaf2e 100644 --- a/src/Microsoft.Extensions.Localization.Abstractions/project.json +++ b/src/Microsoft.Extensions.Localization.Abstractions/project.json @@ -5,11 +5,8 @@ "type": "git", "url": "https://github.com/aspnet/localization" }, - "dependencies": { - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - } + "compilationOptions": { + "warningsAsErrors": true }, "frameworks": { "dnx451": { }, diff --git a/src/Microsoft.Extensions.Localization/Internal/AssemblyWrapper.cs b/src/Microsoft.Extensions.Localization/Internal/AssemblyWrapper.cs index be32e83867..b0c3c2bce1 100644 --- a/src/Microsoft.Extensions.Localization/Internal/AssemblyWrapper.cs +++ b/src/Microsoft.Extensions.Localization/Internal/AssemblyWrapper.cs @@ -1,16 +1,21 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.IO; using System.Reflection; -using Microsoft.Extensions.Internal; namespace Microsoft.Extensions.Localization.Internal { public class AssemblyWrapper { - public AssemblyWrapper([NotNull] Assembly assembly) + public AssemblyWrapper(Assembly assembly) { + if (assembly == null) + { + throw new ArgumentNullException(nameof(assembly)); + } + Assembly = assembly; } diff --git a/src/Microsoft.Extensions.Localization/LocalizationServiceCollectionExtensions.cs b/src/Microsoft.Extensions.Localization/LocalizationServiceCollectionExtensions.cs index f09d4ec5d3..25e619293d 100644 --- a/src/Microsoft.Extensions.Localization/LocalizationServiceCollectionExtensions.cs +++ b/src/Microsoft.Extensions.Localization/LocalizationServiceCollectionExtensions.cs @@ -3,7 +3,6 @@ using System; using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.Localization; namespace Microsoft.Extensions.DependencyInjection @@ -18,8 +17,13 @@ namespace Microsoft.Extensions.DependencyInjection /// /// The to add the services to. /// The . - public static IServiceCollection AddLocalization([NotNull] this IServiceCollection services) + public static IServiceCollection AddLocalization(this IServiceCollection services) { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + return AddLocalization(services, setupAction: null); } @@ -30,9 +34,14 @@ namespace Microsoft.Extensions.DependencyInjection /// An action to configure the . /// The . public static IServiceCollection AddLocalization( - [NotNull] this IServiceCollection services, + this IServiceCollection services, Action setupAction) { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + services.TryAdd(new ServiceDescriptor( typeof(IStringLocalizerFactory), typeof(ResourceManagerStringLocalizerFactory), diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs index a7d6101c23..46139de4c0 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizer.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; @@ -34,24 +35,47 @@ namespace Microsoft.Extensions.Localization /// The base name of the embedded resource in the that contains the strings. /// Cache of the list of strings for a given resource assembly name. public ResourceManagerStringLocalizer( - [NotNull] ResourceManager resourceManager, - [NotNull] Assembly resourceAssembly, - [NotNull] string baseName, - [NotNull] IResourceNamesCache resourceNamesCache) + ResourceManager resourceManager, + Assembly resourceAssembly, + string baseName, + IResourceNamesCache resourceNamesCache) : this(resourceManager, new AssemblyWrapper(resourceAssembly), baseName, resourceNamesCache) { - + if (resourceAssembly == null) + { + throw new ArgumentNullException(nameof(resourceAssembly)); + } } /// /// Intended for testing purposes only. /// public ResourceManagerStringLocalizer( - [NotNull] ResourceManager resourceManager, - [NotNull] AssemblyWrapper resourceAssemblyWrapper, - [NotNull] string baseName, - [NotNull] IResourceNamesCache resourceNamesCache) + ResourceManager resourceManager, + AssemblyWrapper resourceAssemblyWrapper, + string baseName, + IResourceNamesCache resourceNamesCache) { + if (resourceManager == null) + { + throw new ArgumentNullException(nameof(resourceManager)); + } + + if (resourceAssemblyWrapper == null) + { + throw new ArgumentNullException(nameof(resourceAssemblyWrapper)); + } + + if (baseName == null) + { + throw new ArgumentNullException(nameof(baseName)); + } + + if (resourceNamesCache == null) + { + throw new ArgumentNullException(nameof(resourceNamesCache)); + } + _resourceAssemblyWrapper = resourceAssemblyWrapper; _resourceManager = resourceManager; _resourceBaseName = baseName; @@ -59,20 +83,30 @@ namespace Microsoft.Extensions.Localization } /// - public virtual LocalizedString this[[NotNull] string name] + public virtual LocalizedString this[string name] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + var value = GetStringSafely(name, null); return new LocalizedString(name, value ?? name, resourceNotFound: value == null); } } /// - public virtual LocalizedString this[[NotNull] string name, params object[] arguments] + public virtual LocalizedString this[string name, params object[] arguments] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + var format = GetStringSafely(name, null); var value = string.Format(format ?? name, arguments); return new LocalizedString(name, value, resourceNotFound: format == null); @@ -110,8 +144,13 @@ namespace Microsoft.Extensions.Localization /// /// The to get strings for. /// The strings. - protected IEnumerable GetAllStrings(bool includeAncestorCultures, [NotNull] CultureInfo culture) + protected IEnumerable GetAllStrings(bool includeAncestorCultures, CultureInfo culture) { + if (culture == null) + { + throw new ArgumentNullException(nameof(culture)); + } + var resourceNames = includeAncestorCultures ? GetResourceNamesFromCultureHierarchy(culture) : GetResourceNamesForCulture(culture); @@ -130,8 +169,13 @@ namespace Microsoft.Extensions.Localization /// The name of the string resource. /// The to get the string for. /// The resource string, or null if none was found. - protected string GetStringSafely([NotNull] string name, CultureInfo culture) + protected string GetStringSafely(string name, CultureInfo culture) { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + var cacheKey = $"name={name}&culture={(culture ?? CultureInfo.CurrentUICulture).Name}"; if (_missingManifestCache.ContainsKey(cacheKey)) diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs index 75cac08fde..a77d3a7431 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs @@ -6,7 +6,6 @@ using System.IO; using System.Reflection; using System.Resources; using Microsoft.Dnx.Runtime; -using Microsoft.Extensions.Internal; using Microsoft.Extensions.OptionsModel; namespace Microsoft.Extensions.Localization @@ -28,9 +27,19 @@ namespace Microsoft.Extensions.Localization /// The . /// The . public ResourceManagerStringLocalizerFactory( - [NotNull] IApplicationEnvironment applicationEnvironment, - [NotNull] IOptions localizationOptions) + IApplicationEnvironment applicationEnvironment, + IOptions localizationOptions) { + if (applicationEnvironment == null) + { + throw new ArgumentNullException(nameof(applicationEnvironment)); + } + + if (localizationOptions == null) + { + throw new ArgumentNullException(nameof(localizationOptions)); + } + _applicationEnvironment = applicationEnvironment; _resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty; if (!string.IsNullOrEmpty(_resourcesRelativePath)) @@ -46,8 +55,13 @@ namespace Microsoft.Extensions.Localization /// /// The . /// The . - public IStringLocalizer Create([NotNull] Type resourceSource) + public IStringLocalizer Create(Type resourceSource) { + if (resourceSource == null) + { + throw new ArgumentNullException(nameof(resourceSource)); + } + var typeInfo = resourceSource.GetTypeInfo(); var assembly = typeInfo.Assembly; var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + resourceSource.Name; @@ -65,8 +79,13 @@ namespace Microsoft.Extensions.Localization /// The base name of the resource to load strings from. /// The location to load resources from. /// The . - public IStringLocalizer Create([NotNull] string baseName, string location) + public IStringLocalizer Create(string baseName, string location) { + if (baseName == null) + { + throw new ArgumentNullException(nameof(baseName)); + } + var rootPath = location ?? _applicationEnvironment.ApplicationName; var assembly = Assembly.Load(new AssemblyName(rootPath)); baseName = rootPath + "." + _resourcesRelativePath + baseName; diff --git a/src/Microsoft.Extensions.Localization/ResourceManagerWithCultureStringLocalizer.cs b/src/Microsoft.Extensions.Localization/ResourceManagerWithCultureStringLocalizer.cs index e077bcd682..dda581d5eb 100644 --- a/src/Microsoft.Extensions.Localization/ResourceManagerWithCultureStringLocalizer.cs +++ b/src/Microsoft.Extensions.Localization/ResourceManagerWithCultureStringLocalizer.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Globalization; using System.Reflection; @@ -26,31 +27,66 @@ namespace Microsoft.Extensions.Localization /// Cache of the list of strings for a given resource assembly name. /// The specific to use. public ResourceManagerWithCultureStringLocalizer( - [NotNull] ResourceManager resourceManager, - [NotNull] Assembly resourceAssembly, - [NotNull] string baseName, - [NotNull] IResourceNamesCache resourceNamesCache, - [NotNull] CultureInfo culture) + ResourceManager resourceManager, + Assembly resourceAssembly, + string baseName, + IResourceNamesCache resourceNamesCache, + CultureInfo culture) : base(resourceManager, resourceAssembly, baseName, resourceNamesCache) { + if (resourceManager == null) + { + throw new ArgumentNullException(nameof(resourceManager)); + } + + if (resourceAssembly == null) + { + throw new ArgumentNullException(nameof(resourceAssembly)); + } + + if (baseName == null) + { + throw new ArgumentNullException(nameof(baseName)); + } + + if (resourceNamesCache == null) + { + throw new ArgumentNullException(nameof(resourceNamesCache)); + } + + if (culture == null) + { + throw new ArgumentNullException(nameof(culture)); + } + _culture = culture; } /// - public override LocalizedString this[[NotNull] string name] + public override LocalizedString this[string name] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + var value = GetStringSafely(name, _culture); return new LocalizedString(name, value ?? name); } } /// - public override LocalizedString this[[NotNull] string name, params object[] arguments] + public override LocalizedString this[string name, params object[] arguments] { get { + if (name == null) + { + throw new ArgumentNullException(nameof(name)); + } + var format = GetStringSafely(name, _culture); var value = string.Format(_culture, format ?? name, arguments); return new LocalizedString(name, value ?? name, resourceNotFound: format == null); diff --git a/src/Microsoft.Extensions.Localization/project.json b/src/Microsoft.Extensions.Localization/project.json index 632e56f762..d4ca2308d4 100644 --- a/src/Microsoft.Extensions.Localization/project.json +++ b/src/Microsoft.Extensions.Localization/project.json @@ -5,14 +5,13 @@ "type": "git", "url": "https://github.com/aspnet/localization" }, + "compilationOptions": { + "warningsAsErrors": true + }, "dependencies": { "Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*", "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-*", "Microsoft.Extensions.Localization.Abstractions": "1.0.0-*", - "Microsoft.Extensions.NotNullAttribute.Sources": { - "type": "build", - "version": "1.0.0-*" - }, "Microsoft.Extensions.OptionsModel": "1.0.0-*" }, "frameworks": {