BaseParser.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // System.Web.UI.BaseParser.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.IO;
  32. using System.Security.Permissions;
  33. using System.Web.Compilation;
  34. using System.Web.Configuration;
  35. using System.Web.Util;
  36. namespace System.Web.UI
  37. {
  38. // CAS
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. public class BaseParser
  42. {
  43. HttpContext context;
  44. string baseDir;
  45. string baseVDir;
  46. ILocation location;
  47. CompilationConfiguration compilationConfig;
  48. internal string MapPath (string path)
  49. {
  50. return MapPath (path, true);
  51. }
  52. internal string MapPath (string path, bool allowCrossAppMapping)
  53. {
  54. if (context == null)
  55. throw new HttpException ("context is null!!");
  56. return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
  57. }
  58. internal string PhysicalPath (string path)
  59. {
  60. if (Path.DirectorySeparatorChar != '/')
  61. path = path.Replace ('/', '\\');
  62. return Path.Combine (BaseDir, path);
  63. }
  64. internal bool GetBool (Hashtable hash, string key, bool deflt)
  65. {
  66. string val = hash [key] as string;
  67. if (val == null)
  68. return deflt;
  69. hash.Remove (key);
  70. bool result = false;
  71. if (String.Compare (val, "true", true) == 0)
  72. result = true;
  73. else if (String.Compare (val, "false", true) != 0)
  74. ThrowParseException ("Invalid value for " + key);
  75. return result;
  76. }
  77. internal static string GetString (Hashtable hash, string key, string deflt)
  78. {
  79. string val = hash [key] as string;
  80. if (val == null)
  81. return deflt;
  82. hash.Remove (key);
  83. return val;
  84. }
  85. internal void ThrowParseException (string message)
  86. {
  87. throw new ParseException (location, message);
  88. }
  89. internal void ThrowParseException (string message, Exception inner)
  90. {
  91. throw new ParseException (location, message, inner);
  92. }
  93. internal ILocation Location {
  94. get { return location; }
  95. set { location = value; }
  96. }
  97. internal HttpContext Context {
  98. get { return context; }
  99. set { context = value; }
  100. }
  101. internal string BaseDir {
  102. get {
  103. if (baseDir == null)
  104. baseDir = MapPath (BaseVirtualDir, false);
  105. return baseDir;
  106. }
  107. }
  108. internal virtual string BaseVirtualDir {
  109. get {
  110. if (baseVDir == null)
  111. baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
  112. return baseVDir;
  113. }
  114. set { baseVDir = value; }
  115. }
  116. internal CompilationConfiguration CompilationConfig {
  117. get {
  118. if (compilationConfig == null)
  119. compilationConfig = CompilationConfiguration.GetInstance (context);
  120. return compilationConfig;
  121. }
  122. }
  123. }
  124. }