BaseParser.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #if !NET_2_0
  48. CompilationConfiguration compilationConfig;
  49. #endif
  50. internal string MapPath (string path)
  51. {
  52. return MapPath (path, true);
  53. }
  54. internal string MapPath (string path, bool allowCrossAppMapping)
  55. {
  56. if (context == null)
  57. throw new HttpException ("context is null!!");
  58. return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
  59. }
  60. internal string PhysicalPath (string path)
  61. {
  62. if (Path.DirectorySeparatorChar != '/')
  63. path = path.Replace ('/', '\\');
  64. return Path.Combine (BaseDir, path);
  65. }
  66. internal bool GetBool (Hashtable hash, string key, bool deflt)
  67. {
  68. string val = hash [key] as string;
  69. if (val == null)
  70. return deflt;
  71. hash.Remove (key);
  72. bool result = false;
  73. if (String.Compare (val, "true", true) == 0)
  74. result = true;
  75. else if (String.Compare (val, "false", true) != 0)
  76. ThrowParseException ("Invalid value for " + key);
  77. return result;
  78. }
  79. internal static string GetString (Hashtable hash, string key, string deflt)
  80. {
  81. string val = hash [key] as string;
  82. if (val == null)
  83. return deflt;
  84. hash.Remove (key);
  85. return val;
  86. }
  87. internal void ThrowParseException (string message, params object[] parms)
  88. {
  89. if (parms == null)
  90. throw new ParseException (location, message);
  91. throw new ParseException (location, String.Format (message, parms));
  92. }
  93. internal void ThrowParseException (string message, Exception inner, params object[] parms)
  94. {
  95. throw new ParseException (location, String.Format (message, parms), inner);
  96. }
  97. internal void ThrowParseFileNotFound (string path, params object[] parms)
  98. {
  99. ThrowParseException ("The file '" + path + "' does not exist", parms);
  100. }
  101. internal ILocation Location {
  102. get { return location; }
  103. set { location = value; }
  104. }
  105. internal HttpContext Context {
  106. get { return context; }
  107. set { context = value; }
  108. }
  109. internal string BaseDir {
  110. get {
  111. if (baseDir == null)
  112. baseDir = MapPath (BaseVirtualDir, false);
  113. return baseDir;
  114. }
  115. }
  116. internal virtual string BaseVirtualDir {
  117. get {
  118. if (baseVDir == null)
  119. baseVDir = VirtualPathUtility.GetDirectory (context.Request.FilePath);
  120. return baseVDir;
  121. }
  122. set {
  123. if (VirtualPathUtility.IsRooted (value))
  124. baseVDir = VirtualPathUtility.ToAbsolute (value);
  125. else
  126. baseVDir = value;
  127. }
  128. }
  129. #if NET_2_0
  130. internal CompilationSection CompilationConfig {
  131. get {
  132. return WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
  133. }
  134. }
  135. #else
  136. internal CompilationConfiguration CompilationConfig {
  137. get {
  138. if (compilationConfig == null)
  139. compilationConfig = CompilationConfiguration.GetInstance (context);
  140. return compilationConfig;
  141. }
  142. }
  143. #endif
  144. }
  145. }