ResourceExpressionBuilder.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // System.Web.Compilation.ResourceExpressionBuilder
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2006-2010 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.CodeDom;
  31. using System.ComponentModel;
  32. using System.Reflection;
  33. using System.Web;
  34. using System.Web.UI;
  35. namespace System.Web.Compilation
  36. {
  37. [ExpressionEditor("System.Web.UI.Design.ResourceExpressionEditor, " + Consts.AssemblySystem_Design)]
  38. [ExpressionPrefix("Resources")]
  39. public class ResourceExpressionBuilder : ExpressionBuilder
  40. {
  41. public ResourceExpressionBuilder ()
  42. {
  43. }
  44. public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  45. {
  46. ResourceExpressionFields fields = parsedData as ResourceExpressionFields;
  47. return HttpContext.GetGlobalResourceObject (fields.ClassKey, fields.ResourceKey);
  48. }
  49. public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  50. {
  51. ResourceExpressionFields fields = parsedData as ResourceExpressionFields;
  52. CodeExpression[] expr;
  53. // TODO: check what MS runtime does in this situation
  54. if (entry == null)
  55. return null;
  56. if (!String.IsNullOrEmpty (fields.ClassKey)) {
  57. if (! (entry.PropertyInfo is PropertyInfo))
  58. return null; // TODO: check what MS runtime does here
  59. expr = new CodeExpression [] {
  60. new CodePrimitiveExpression (fields.ClassKey),
  61. new CodePrimitiveExpression (fields.ResourceKey)
  62. };
  63. CodeMethodInvokeExpression getgro = new CodeMethodInvokeExpression (new CodeThisReferenceExpression (), "GetGlobalResourceObject", expr);
  64. return new CodeCastExpression (entry.PropertyInfo.PropertyType, getgro);
  65. } else
  66. return CreateGetLocalResourceObject (entry, fields.ResourceKey);
  67. }
  68. public static ResourceExpressionFields ParseExpression (string expression)
  69. {
  70. int comma = expression.IndexOf (',');
  71. if (comma == -1)
  72. return new ResourceExpressionFields (expression.Trim ());
  73. else
  74. return new ResourceExpressionFields (expression.Substring (0, comma).Trim (),
  75. expression.Substring (comma + 1).Trim ());
  76. }
  77. public override object ParseExpression (string expression, Type propertyType, ExpressionBuilderContext context)
  78. {
  79. //FIXME: not sure what the propertyType should be used for
  80. return ParseExpression (expression);
  81. }
  82. public override bool SupportsEvaluate {
  83. get { return true; }
  84. }
  85. internal static CodeExpression CreateGetLocalResourceObject (BoundPropertyEntry bpe, string resname)
  86. {
  87. if (bpe == null || String.IsNullOrEmpty (resname))
  88. return null;
  89. if (bpe.UseSetAttribute)
  90. return CreateGetLocalResourceObject (bpe.Type, typeof (string), null, resname);
  91. else
  92. return CreateGetLocalResourceObject (bpe.PropertyInfo, resname);
  93. }
  94. internal static CodeExpression CreateGetLocalResourceObject (MemberInfo mi, string resname)
  95. {
  96. if (String.IsNullOrEmpty (resname))
  97. return null;
  98. Type member_type = null;
  99. if (mi is PropertyInfo)
  100. member_type = ((PropertyInfo)mi).PropertyType;
  101. else if (mi is FieldInfo)
  102. member_type = ((FieldInfo)mi).FieldType;
  103. else
  104. return null; // an "impossible" case
  105. return CreateGetLocalResourceObject (member_type, mi.DeclaringType, mi.Name, resname);
  106. }
  107. static CodeExpression CreateGetLocalResourceObject (Type member_type, Type declaringType, string memberName, string resname)
  108. {
  109. TypeConverter converter;
  110. if (!String.IsNullOrEmpty (memberName))
  111. converter = TypeDescriptor.GetProperties (declaringType) [memberName].Converter;
  112. else
  113. converter = null;
  114. if (member_type != typeof (System.Drawing.Color) &&
  115. (converter == null || converter.CanConvertFrom (typeof (String)))) {
  116. CodeMethodInvokeExpression getlro = new CodeMethodInvokeExpression (
  117. new CodeThisReferenceExpression (),
  118. "GetLocalResourceObject",
  119. new CodeExpression [] { new CodePrimitiveExpression (resname) });
  120. return TemplateControlCompiler.CreateConvertToCall (Type.GetTypeCode (member_type), getlro);
  121. } else if (!String.IsNullOrEmpty (memberName)) {
  122. CodeMethodInvokeExpression getlro = new CodeMethodInvokeExpression (
  123. new CodeThisReferenceExpression (),
  124. "GetLocalResourceObject",
  125. new CodeExpression [] {
  126. new CodePrimitiveExpression (resname),
  127. new CodeTypeOfExpression (new CodeTypeReference (declaringType)),
  128. new CodePrimitiveExpression (memberName)
  129. }
  130. );
  131. return new CodeCastExpression (member_type, getlro);
  132. } else
  133. return null;
  134. }
  135. }
  136. }