SimpleWebHandlerParser.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // System.Web.UI.SimpleWebHandlerParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. using System.Web;
  14. using System.Web.Compilation;
  15. using System.Web.Util;
  16. namespace System.Web.UI
  17. {
  18. public abstract class SimpleWebHandlerParser
  19. {
  20. HttpContext context;
  21. string vPath;
  22. string physPath;
  23. string className;
  24. string codeBehind;
  25. bool debug;
  26. string language;
  27. string program;
  28. protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
  29. {
  30. this.context = context;
  31. this.vPath = virtualPath;
  32. this.physPath = physicalPath;
  33. GetDirectiveAndContent ();
  34. }
  35. private void GetDirectiveAndContent ()
  36. {
  37. StreamReader reader = new StreamReader (File.OpenRead (physPath));
  38. string line;
  39. bool directiveFound = false;
  40. StringBuilder content = new StringBuilder ();
  41. while ((line = reader.ReadLine ()) != null) {
  42. string trimmed = line.Trim ();
  43. if (!directiveFound && trimmed != String.Empty)
  44. continue;
  45. if (!directiveFound) {
  46. ParseDirective (trimmed);
  47. directiveFound = true;
  48. continue;
  49. }
  50. content.Append (line + "\n");
  51. content.Append (reader.ReadToEnd ());
  52. }
  53. this.program = content.ToString ();
  54. reader.Close ();
  55. }
  56. private void ParseDirective (string line)
  57. {
  58. MemoryStream st = new MemoryStream (WebEncoding.Encoding.GetBytes (line));
  59. AspParser parser = new AspParser (physPath, st);
  60. parser.Parse ();
  61. ArrayList elems = parser.Elements;
  62. if (elems.Count != 1)
  63. throw new ApplicationException ("Error looking for WebService directive.");
  64. Directive directive = elems [0] as Directive;
  65. if (directive == null)
  66. throw new ApplicationException ("Error looking for WebService directive.");
  67. if (0 != String.Compare (directive.TagID, DefaultDirectiveName, false))
  68. throw new ApplicationException ("Expecting @WebService. Got: " +
  69. directive.TagID);
  70. TagAttributes ta = directive.Attributes;
  71. className = ta ["class"] as string;
  72. if (className == null)
  73. throw new ApplicationException ("No Class attribute found.");
  74. string d = ta ["debug"] as string;
  75. if (d != null)
  76. debug = Convert.ToBoolean (d);
  77. language = ta ["language"] as string;
  78. if (language != null) {
  79. if (0 != String.Compare (language, "C#", false))
  80. throw new ApplicationException ("Only C# language is supported.");
  81. }
  82. codeBehind = ta ["codebehind"] as string;
  83. if (codeBehind != null) {
  84. string ext = Path.GetExtension (codeBehind);
  85. if (0 != String.Compare (ext, "cs", false) &&
  86. 0 != String.Compare (ext, "dll", false))
  87. throw new ApplicationException ("Unknown file type in CodeBehind.");
  88. }
  89. }
  90. protected abstract string DefaultDirectiveName { get; }
  91. internal HttpContext Context
  92. {
  93. get {
  94. return context;
  95. }
  96. }
  97. internal string VirtualPath
  98. {
  99. get {
  100. return vPath;
  101. }
  102. }
  103. internal string PhysicalPath
  104. {
  105. get {
  106. return physPath;
  107. }
  108. }
  109. internal string ClassName
  110. {
  111. get {
  112. return className;
  113. }
  114. set {
  115. className = value;
  116. }
  117. }
  118. internal string CodeBehind
  119. {
  120. get {
  121. return codeBehind;
  122. }
  123. set {
  124. codeBehind = value;
  125. }
  126. }
  127. internal bool Debug
  128. {
  129. get {
  130. return debug;
  131. }
  132. set {
  133. debug = value;
  134. }
  135. }
  136. internal string Language
  137. {
  138. get {
  139. return language;
  140. }
  141. set {
  142. language = value;
  143. }
  144. }
  145. internal string Program
  146. {
  147. get {
  148. return program;
  149. }
  150. set {
  151. program = value;
  152. }
  153. }
  154. }
  155. }