BaseParser.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. using System;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Web;
  14. using System.Web.Compilation;
  15. using System.Web.Util;
  16. namespace System.Web.UI
  17. {
  18. public class BaseParser
  19. {
  20. HttpContext context;
  21. string baseDir;
  22. string baseVDir;
  23. ILocation location;
  24. internal string MapPath (string path)
  25. {
  26. return MapPath (path, true);
  27. }
  28. internal string MapPath (string path, bool allowCrossAppMapping)
  29. {
  30. if (context == null)
  31. throw new HttpException ("context is null!!");
  32. return context.Request.MapPath (path, BaseVirtualDir, allowCrossAppMapping);
  33. }
  34. internal string PhysicalPath (string path)
  35. {
  36. if (Path.DirectorySeparatorChar != '/')
  37. path = path.Replace ('/', '\\');
  38. return Path.Combine (BaseDir, path);
  39. }
  40. internal bool GetBool (Hashtable hash, string key, bool deflt)
  41. {
  42. string val = hash [key] as string;
  43. if (val == null)
  44. return deflt;
  45. hash.Remove (key);
  46. bool result = false;
  47. if (String.Compare (val, "true", true) == 0)
  48. result = true;
  49. else if (String.Compare (val, "false", true) != 0)
  50. ThrowParseException ("Invalid value for " + key);
  51. return result;
  52. }
  53. internal static string GetString (Hashtable hash, string key, string deflt)
  54. {
  55. string val = hash [key] as string;
  56. if (val == null)
  57. return deflt;
  58. hash.Remove (key);
  59. return val;
  60. }
  61. internal void ThrowParseException (string message)
  62. {
  63. throw new ParseException (location, message);
  64. }
  65. internal void ThrowParseException (string message, Exception inner)
  66. {
  67. throw new ParseException (location, message, inner);
  68. }
  69. internal ILocation Location {
  70. get { return location; }
  71. set { location = value; }
  72. }
  73. internal HttpContext Context {
  74. get { return context; }
  75. set { context = value; }
  76. }
  77. internal string BaseDir {
  78. get {
  79. if (baseDir == null)
  80. baseDir = MapPath (BaseVirtualDir, false);
  81. return baseDir;
  82. }
  83. }
  84. internal virtual string BaseVirtualDir {
  85. get {
  86. if (baseVDir == null)
  87. baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
  88. return baseVDir;
  89. }
  90. }
  91. }
  92. }