1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
//------------------------------------------------------------------------------
// <copyright file="ResourceExpressionBuilder.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Compilation {
using System;
using System.Security.Permissions;
using System.Collections;
using System.Collections.Specialized;
using System.CodeDom;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Web.Caching;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.Util;
using System.Web.UI;
[ExpressionPrefix("Resources")]
[ExpressionEditor("System.Web.UI.Design.ResourceExpressionEditor, " + AssemblyRef.SystemDesign)]
public class ResourceExpressionBuilder : ExpressionBuilder {
private static ResourceProviderFactory s_resourceProviderFactory;
public override bool SupportsEvaluate {
get {
return true;
}
}
public static ResourceExpressionFields ParseExpression(string expression) {
return ParseExpressionInternal(expression);
}
public override object ParseExpression(string expression, Type propertyType, ExpressionBuilderContext context) {
ResourceExpressionFields fields = null;
try {
fields = ParseExpressionInternal(expression);
}
catch {
}
// If the parsing failed for any reason throw an error
if (fields == null) {
throw new HttpException(
SR.GetString(SR.Invalid_res_expr, expression));
}
// The resource expression was successfully parsed. We now need to check whether
// the resource object actually exists in the neutral culture
// If we don't have a virtual path, we can't check that the resource exists.
// This happens in the designer.
if (context.VirtualPathObject != null) {
IResourceProvider resourceProvider = GetResourceProvider(fields, VirtualPath.Create(context.VirtualPath));
object o = null;
if (resourceProvider != null) {
try {
o = resourceProvider.GetObject(fields.ResourceKey, CultureInfo.InvariantCulture);
}
catch {}
}
// If it doesn't throw an exception
if (o == null) {
throw new HttpException(
SR.GetString(SR.Res_not_found, fields.ResourceKey));
}
}
return fields;
}
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
object parsedData, ExpressionBuilderContext context) {
// Retrieve our parsed data
ResourceExpressionFields fields = (ResourceExpressionFields) parsedData;
// If there is no classKey, it's a page-level resource
if (fields.ClassKey.Length == 0) {
return GetPageResCodeExpression(fields.ResourceKey, entry);
}
// Otherwise, it's a global resource
return GetAppResCodeExpression(fields.ClassKey, fields.ResourceKey, entry);
}
public override object EvaluateExpression(object target, BoundPropertyEntry entry,
object parsedData, ExpressionBuilderContext context) {
// Retrieve our parsed data
ResourceExpressionFields fields = (ResourceExpressionFields) parsedData;
IResourceProvider resourceProvider = GetResourceProvider(fields, context.VirtualPathObject);
if (entry.Type == typeof(string))
return GetResourceObject(resourceProvider, fields.ResourceKey, null /*culture*/);
// If the property is not of type string, pass in extra information
// so that the resource value will be converted to the right type
return GetResourceObject(resourceProvider, fields.ResourceKey, null /*culture*/,
entry.DeclaringType, entry.PropertyInfo.Name);
}
private CodeExpression GetAppResCodeExpression(string classKey, string resourceKey, BoundPropertyEntry entry) {
// We generate the following
// this.GetGlobalResourceObject(classKey)
CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression();
expr.Method.TargetObject = new CodeThisReferenceExpression();
expr.Method.MethodName = "GetGlobalResourceObject";
expr.Parameters.Add(new CodePrimitiveExpression(classKey));
expr.Parameters.Add(new CodePrimitiveExpression(resourceKey));
// If the property is not of type string, it will need to be converted
if (entry.Type != typeof(string) && entry.Type != null) {
expr.Parameters.Add(new CodeTypeOfExpression(entry.DeclaringType));
expr.Parameters.Add(new CodePrimitiveExpression(entry.PropertyInfo.Name));
}
return expr;
}
private CodeExpression GetPageResCodeExpression(string resourceKey, BoundPropertyEntry entry) {
// We generate of the following
// this.GetLocalResourceObject(resourceKey)
CodeMethodInvokeExpression expr = new CodeMethodInvokeExpression();
expr.Method.TargetObject = new CodeThisReferenceExpression();
expr.Method.MethodName = "GetLocalResourceObject";
expr.Parameters.Add(new CodePrimitiveExpression(resourceKey));
// If the property is not of type string, it will need to be converted
if (entry.Type != typeof(string) && entry.Type != null) {
expr.Parameters.Add(new CodeTypeOfExpression(entry.DeclaringType));
expr.Parameters.Add(new CodePrimitiveExpression(entry.PropertyInfo.Name));
}
return expr;
}
internal static object GetGlobalResourceObject(string classKey, string resourceKey) {
return GetGlobalResourceObject(classKey, resourceKey, null /*objType*/, null /*propName*/, null /*culture*/);
}
internal static object GetGlobalResourceObject(string classKey,
string resourceKey, Type objType, string propName, CultureInfo culture) {
IResourceProvider resourceProvider = GetGlobalResourceProvider(classKey);
return GetResourceObject(resourceProvider, resourceKey, culture,
objType, propName);
}
internal static object GetResourceObject(IResourceProvider resourceProvider,
string resourceKey, CultureInfo culture) {
return GetResourceObject(resourceProvider, resourceKey, culture,
null /*objType*/, null /*propName*/);
}
internal static object GetResourceObject(IResourceProvider resourceProvider,
string resourceKey, CultureInfo culture, Type objType, string propName) {
if (resourceProvider == null)
return null;
object o = resourceProvider.GetObject(resourceKey, culture);
// If no objType/propName was provided, return the object as is
if (objType == null)
return o;
// Also, if the object from the resource is not a string, return it as is
string s = o as String;
if (s == null)
return o;
// If they were provided, perform the appropriate conversion
return ObjectFromString(s, objType, propName);
}
private static object ObjectFromString(string value, Type objType, string propName) {
// Get the PropertyDescriptor for the property
PropertyDescriptor pd = TypeDescriptor.GetProperties(objType)[propName];
Debug.Assert(pd != null);
if (pd == null) return null;
// Get its type descriptor
TypeConverter converter = pd.Converter;
Debug.Assert(converter != null);
if (converter == null) return null;
// Perform the conversion
return converter.ConvertFromInvariantString(value);
}
// The following syntaxes are accepted for the expression
// resourceKey
// classKey, resourceKey
//
private static ResourceExpressionFields ParseExpressionInternal(string expression) {
string classKey = null;
string resourceKey = null;
int len = expression.Length;
if (len == 0) {
return new ResourceExpressionFields(classKey, resourceKey);
}
// Split the comma separated string
string[] parts = expression.Split(',');
int numOfParts = parts.Length;
if (numOfParts > 2) return null;
if (numOfParts == 1) {
resourceKey = parts[0].Trim();
}
else {
classKey = parts[0].Trim();
resourceKey = parts[1].Trim();
}
return new ResourceExpressionFields(classKey, resourceKey);
}
private static IResourceProvider GetResourceProvider(ResourceExpressionFields fields,
VirtualPath virtualPath) {
// If there is no classKey, it's a page-level resource
if (fields.ClassKey.Length == 0) {
return GetLocalResourceProvider(virtualPath);
}
// Otherwise, it's a global resource
return GetGlobalResourceProvider(fields.ClassKey);
}
private static void EnsureResourceProviderFactory() {
if (s_resourceProviderFactory != null)
return;
Type t = null;
GlobalizationSection globConfig = RuntimeConfig.GetAppConfig().Globalization;
t = globConfig.ResourceProviderFactoryTypeInternal;
// If we got a type from config, use it. Otherwise, use default factory
if (t == null) {
s_resourceProviderFactory = new ResXResourceProviderFactory();
}
else {
s_resourceProviderFactory = (ResourceProviderFactory) HttpRuntime.CreatePublicInstance(t);
}
}
private static IResourceProvider GetGlobalResourceProvider(string classKey) {
string fullClassName = BaseResourcesBuildProvider.DefaultResourcesNamespace +
"." + classKey;
// If we have it cached, return it
CacheStoreProvider cacheInternal = System.Web.HttpRuntime.Cache.InternalCache;
string cacheKey = CacheInternal.PrefixResourceProvider + fullClassName;
IResourceProvider resourceProvider = cacheInternal.Get(cacheKey) as IResourceProvider;
if (resourceProvider != null) {
return resourceProvider;
}
EnsureResourceProviderFactory();
resourceProvider = s_resourceProviderFactory.CreateGlobalResourceProvider(classKey);
// Cache it
cacheInternal.Insert(cacheKey, resourceProvider, null);
return resourceProvider;
}
// Get the page-level IResourceProvider
internal static IResourceProvider GetLocalResourceProvider(TemplateControl templateControl) {
return GetLocalResourceProvider(templateControl.VirtualPath);
}
// Get the page-level IResourceProvider
internal static IResourceProvider GetLocalResourceProvider(VirtualPath virtualPath) {
// If we have it cached, return it (it may be null if there are no local resources)
CacheStoreProvider cacheInternal = System.Web.HttpRuntime.Cache.InternalCache;
string cacheKey = CacheInternal.PrefixResourceProvider + virtualPath.VirtualPathString;
IResourceProvider resourceProvider = cacheInternal.Get(cacheKey) as IResourceProvider;
if (resourceProvider != null) {
return resourceProvider;
}
EnsureResourceProviderFactory();
resourceProvider = s_resourceProviderFactory.CreateLocalResourceProvider(virtualPath.VirtualPathString);
// Cache it
cacheInternal.Insert(cacheKey, resourceProvider, null);
return resourceProvider;
}
// Create a ResourceExpressionFields without actually parsing any string. This
// is used for 'automatic' resources, where we already have the pieces of
// relevant data.
internal static object GetParsedData(string resourceKey) {
return new ResourceExpressionFields(String.Empty, resourceKey);
}
}
// Holds the fields parsed from a resource expression (e.g. classKey, resourceKey)
public sealed class ResourceExpressionFields {
private string _classKey;
private string _resourceKey;
internal ResourceExpressionFields(string classKey, string resourceKey) {
_classKey = classKey;
_resourceKey = resourceKey;
}
public string ClassKey {
get {
if (_classKey == null) {
return String.Empty;
}
return _classKey;
}
}
public string ResourceKey {
get {
if (_resourceKey == null) {
return String.Empty;
}
return _resourceKey;
}
}
}
}
|