SimpleWebHandlerParser.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ArrayList elements;
  29. protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
  30. {
  31. this.context = context;
  32. this.vPath = virtualPath;
  33. this.physPath = physicalPath;
  34. GetDirectiveAndContent ();
  35. }
  36. private void GetDirectiveAndContent ()
  37. {
  38. StreamReader reader = new StreamReader (File.OpenRead (physPath));
  39. string line;
  40. bool directiveFound = false;
  41. StringBuilder content = new StringBuilder ();
  42. while ((line = reader.ReadLine ()) != null) {
  43. string trimmed = line.Trim ();
  44. if (!directiveFound && trimmed != String.Empty)
  45. continue;
  46. if (!directiveFound) {
  47. ParseDirective (trimmed);
  48. directiveFound = true;
  49. continue;
  50. }
  51. content.Append (line + "\n");
  52. content.Append (reader.ReadToEnd ());
  53. }
  54. this.program = content.ToString ();
  55. reader.Close ();
  56. }
  57. void ParseError (string msg, int line, int col)
  58. {
  59. string exc = String.Format ("parse error: {0}", msg);
  60. throw new HttpException (exc);
  61. }
  62. /* void TagParsed (Tag tag, int line, int col)
  63. {
  64. elements.Add (tag);
  65. }*/
  66. void TextParsed (string msg, int line, int col)
  67. {
  68. //elements.Add (msg);
  69. }
  70. private void ParseDirective (string line)
  71. {
  72. /*byte [] bytes = WebEncoding.Encoding.GetBytes (line);
  73. //AspTokenizer tok = new AspTokenizer (physPath, new MemoryStream (bytes));
  74. //AspParser parser = new AspParser (tok);
  75. elements = new ArrayList ();
  76. parser.Error += new ParseErrorHandler (ParseError);
  77. parser.TagParsed += new TagParsedHandler (TagParsed);
  78. parser.TextParsed += new TextParsedHandler (TextParsed);
  79. parser.Parse ();
  80. if (elements.Count != 1)
  81. throw new ApplicationException ("Error looking for WebService directive.");
  82. Directive directive = elements [0] as Directive;
  83. if (directive == null)
  84. throw new ApplicationException ("Error looking for WebService directive.");
  85. if (0 != String.Compare (directive.TagID, DefaultDirectiveName, false))
  86. throw new ApplicationException ("Expecting @WebService. Got: " +
  87. directive.TagID);
  88. TagAttributes ta = directive.Attributes;
  89. className = ta ["class"] as string;
  90. if (className == null)
  91. throw new ApplicationException ("No Class attribute found.");
  92. string d = ta ["debug"] as string;
  93. if (d != null)
  94. debug = Convert.ToBoolean (d);
  95. language = ta ["language"] as string;
  96. if (language != null) {
  97. if (0 != String.Compare (language, "C#", false))
  98. throw new ApplicationException ("Only C# language is supported.");
  99. }
  100. codeBehind = ta ["codebehind"] as string;
  101. if (codeBehind != null) {
  102. string ext = Path.GetExtension (codeBehind);
  103. if (0 != String.Compare (ext, "cs", false) &&
  104. 0 != String.Compare (ext, "dll", false))
  105. throw new ApplicationException ("Unknown file type in CodeBehind.");
  106. }
  107. */
  108. }
  109. protected abstract string DefaultDirectiveName { get; }
  110. internal HttpContext Context
  111. {
  112. get {
  113. return context;
  114. }
  115. }
  116. internal string VirtualPath
  117. {
  118. get {
  119. return vPath;
  120. }
  121. }
  122. internal string PhysicalPath
  123. {
  124. get {
  125. return physPath;
  126. }
  127. }
  128. internal string ClassName
  129. {
  130. get {
  131. return className;
  132. }
  133. set {
  134. className = value;
  135. }
  136. }
  137. internal string CodeBehind
  138. {
  139. get {
  140. return codeBehind;
  141. }
  142. set {
  143. codeBehind = value;
  144. }
  145. }
  146. internal bool Debug
  147. {
  148. get {
  149. return debug;
  150. }
  151. set {
  152. debug = value;
  153. }
  154. }
  155. internal string Language
  156. {
  157. get {
  158. return language;
  159. }
  160. set {
  161. language = value;
  162. }
  163. }
  164. internal string Program
  165. {
  166. get {
  167. return program;
  168. }
  169. set {
  170. program = value;
  171. }
  172. }
  173. }
  174. }