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