SimpleWebHandlerParser.cs 3.6 KB

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