2
0

BaseParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. //
  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;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Web;
  34. using System.Web.Compilation;
  35. using System.Web.Configuration;
  36. using System.Web.Util;
  37. namespace System.Web.UI
  38. {
  39. public class BaseParser
  40. {
  41. HttpContext context;
  42. string baseDir;
  43. string baseVDir;
  44. ILocation location;
  45. CompilationConfiguration compilationConfig;
  46. internal string MapPath (string path)
  47. {
  48. return MapPath (path, true);
  49. }
  50. internal string MapPath (string path, bool allowCrossAppMapping)
  51. {
  52. if (context == null)
  53. throw new HttpException ("context is null!!");
  54. return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
  55. }
  56. internal string PhysicalPath (string path)
  57. {
  58. if (Path.DirectorySeparatorChar != '/')
  59. path = path.Replace ('/', '\\');
  60. return Path.Combine (BaseDir, path);
  61. }
  62. internal bool GetBool (Hashtable hash, string key, bool deflt)
  63. {
  64. string val = hash [key] as string;
  65. if (val == null)
  66. return deflt;
  67. hash.Remove (key);
  68. bool result = false;
  69. if (String.Compare (val, "true", true) == 0)
  70. result = true;
  71. else if (String.Compare (val, "false", true) != 0)
  72. ThrowParseException ("Invalid value for " + key);
  73. return result;
  74. }
  75. internal static string GetString (Hashtable hash, string key, string deflt)
  76. {
  77. string val = hash [key] as string;
  78. if (val == null)
  79. return deflt;
  80. hash.Remove (key);
  81. return val;
  82. }
  83. internal void ThrowParseException (string message)
  84. {
  85. throw new ParseException (location, message);
  86. }
  87. internal void ThrowParseException (string message, Exception inner)
  88. {
  89. throw new ParseException (location, message, inner);
  90. }
  91. internal ILocation Location {
  92. get { return location; }
  93. set { location = value; }
  94. }
  95. internal HttpContext Context {
  96. get { return context; }
  97. set { context = value; }
  98. }
  99. internal string BaseDir {
  100. get {
  101. if (baseDir == null)
  102. baseDir = MapPath (BaseVirtualDir, false);
  103. return baseDir;
  104. }
  105. }
  106. internal virtual string BaseVirtualDir {
  107. get {
  108. if (baseVDir == null)
  109. baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
  110. return baseVDir;
  111. }
  112. set { baseVDir = value; }
  113. }
  114. internal CompilationConfiguration CompilationConfig {
  115. get {
  116. if (compilationConfig == null)
  117. compilationConfig = CompilationConfiguration.GetInstance (context);
  118. return compilationConfig;
  119. }
  120. }
  121. }
  122. }