1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
//
// System.Web.Compilation.AspGenerator
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
//
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web.UI;
namespace System.Web.Compilation
{
class BuilderLocation
{
public ControlBuilder Builder;
public ILocation Location;
public BuilderLocation (ControlBuilder builder, ILocation location)
{
this.Builder = builder;
this.Location = location;
}
}
class BuilderLocationStack : Stack
{
public override void Push (object o)
{
if (!(o is BuilderLocation))
throw new InvalidOperationException ();
base.Push (o);
}
public virtual void Push (ControlBuilder builder, ILocation location)
{
BuilderLocation bl = new BuilderLocation (builder, location);
Push (bl);
}
public new BuilderLocation Peek ()
{
return (BuilderLocation) base.Peek ();
}
public new BuilderLocation Pop ()
{
return (BuilderLocation) base.Pop ();
}
public ControlBuilder Builder {
get { return Peek ().Builder; }
}
}
class AspGenerator
{
string filename;
AspParser parser;
BuilderLocationStack stack;
TemplateParser tparser;
StringBuilder text;
RootBuilder rootBuilder;
bool inScript;
ILocation location;
static Hashtable emptyHash = new Hashtable ();
public AspGenerator (TemplateParser tparser)
{
this.tparser = tparser;
this.filename = Path.GetFullPath (tparser.InputFile);
tparser.AddDependency (tparser.InputFile);
text = new StringBuilder ();
stack = new BuilderLocationStack ();
rootBuilder = new RootBuilder (tparser);
stack.Push (rootBuilder, null);
tparser.RootBuilder = rootBuilder;
}
BaseCompiler GetCompilerFromType ()
{
Type type = tparser.GetType ();
if (type == typeof (PageParser))
return new PageCompiler ((PageParser) tparser);
if (type == typeof (ApplicationFileParser))
return new GlobalAsaxCompiler ((ApplicationFileParser) tparser);
if (type == typeof (UserControlParser))
return new UserControlCompiler ((UserControlParser) tparser);
throw new Exception ("Got type: " + type);
}
public Type GetCompiledType ()
{
//FIXME: use the encoding of the file or the one specified in the machine.config/web.config file.
StreamReader reader = new StreamReader (filename, Encoding.Default);
parser = new AspParser (filename, reader);
parser.Error += new ParseErrorHandler (ParseError);
parser.TagParsed += new TagParsedHandler (TagParsed);
parser.TextParsed += new TextParsedHandler (TextParsed);
parser.Parse ();
if (text.Length > 0)
FlushText ();
#if DEBUG
PrintTree (rootBuilder, 0);
#endif
if (stack.Count > 1)
throw new ParseException (stack.Builder.location,
"Expecting </" + stack.Builder.TagName + ">" + stack.Builder);
BaseCompiler compiler = GetCompilerFromType ();
return compiler.GetCompiledType ();
}
#if DEBUG
static void PrintTree (ControlBuilder builder, int indent)
{
if (builder == null)
return;
string i = new string ('\t', indent);
Console.Write (i);
Console.WriteLine ("b: {0} id: {1} type: {2} parent: {3}",
builder, builder.ID, builder.ControlType, builder.parentBuilder);
if (builder.Children != null)
foreach (object o in builder.Children) {
if (o is ControlBuilder)
PrintTree ((ControlBuilder) o, indent++);
}
}
#endif
static void PrintLocation (ILocation loc)
{
Console.WriteLine ("\tFile name: " + loc.Filename);
Console.WriteLine ("\tBegin line: " + loc.BeginLine);
Console.WriteLine ("\tEnd line: " + loc.EndLine);
Console.WriteLine ("\tBegin column: " + loc.BeginColumn);
Console.WriteLine ("\tEnd column: " + loc.EndColumn);
Console.WriteLine ("\tPlainText: " + loc.PlainText);
Console.WriteLine ();
}
void ParseError (ILocation location, string message)
{
throw new ParseException (location, message);
}
void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
{
this.location = new Location (location);
if (tparser != null)
tparser.Location = location;
if (text.Length != 0)
FlushText ();
if (0 == String.Compare (tagid, "script", true)) {
if (ProcessScript (tagtype, attributes))
return;
}
switch (tagtype) {
case TagType.Directive:
if (tagid == "")
tagid = tparser.DefaultDirectiveName;
tparser.AddDirective (tagid, attributes.GetDictionary (null));
break;
case TagType.Tag:
if (!ProcessTag (tagid, attributes))
TextParsed (location, location.PlainText);
break;
case TagType.Close:
if (!CloseControl (tagid))
TextParsed (location, location.PlainText);
break;
case TagType.SelfClosing:
int count = stack.Count;
if (!ProcessTag (tagid, attributes)) {
TextParsed (location, location.PlainText);
} else if (stack.Count != count) {
CloseControl (tagid);
}
break;
case TagType.DataBinding:
goto case TagType.CodeRender;
case TagType.CodeRenderExpression:
goto case TagType.CodeRender;
case TagType.CodeRender:
ProcessCode (tagtype, tagid, location);
break;
case TagType.Include:
string file = attributes ["virtual"] as string;
bool isvirtual = (file != null);
if (!isvirtual)
file = attributes ["file"] as string;
TextParsed (location, tparser.ProcessInclude (isvirtual, file));
break;
default:
break;
}
//PrintLocation (location);
}
void TextParsed (ILocation location, string text)
{
if (text.IndexOf ("<%") != -1) {
if (this.text.Length > 0)
FlushText ();
CodeRenderParser r = new CodeRenderParser (text, stack.Builder);
r.AddChildren ();
return;
}
this.text.Append (text);
//PrintLocation (location);
}
void FlushText ()
{
string t = text.ToString ();
text.Length = 0;
if (inScript) {
// TODO: store location
tparser.Scripts.Add (t);
return;
}
if (tparser.DefaultDirectiveName == "application" && t.Trim () != "")
throw new ParseException (location, "Content not valid for application file.");
stack.Builder.AppendLiteralString (t);
}
bool ProcessTag (string tagid, TagAttributes atts)
{
ControlBuilder parent = stack.Builder;
ControlBuilder builder = null;
BuilderLocation bl = null;
Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
if (stack.Count > 1)
builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
if (builder == null && atts != null && atts.IsRunAtServer ())
builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
if (builder == null)
return false;
builder.location = location;
builder.ID = htable ["id"] as string;
if (builder.HasBody ()) {
if (builder is TemplateBuilder) {
// push the id list
}
stack.Push (builder, location);
} else {
// FIXME:ObjectTags...
parent.AppendSubBuilder (builder);
builder.CloseControl ();
}
return true;
}
bool ProcessScript (TagType tagtype, TagAttributes attributes)
{
if (tagtype != TagType.Close && attributes != null && attributes.IsRunAtServer ()) {
if (tagtype == TagType.Tag) {
parser.VerbatimID = "script";
inScript = true;
} //else if (tagtype == TagType.SelfClosing)
// load script file here
return true;
}
bool result = inScript;
inScript = false;
return result;
}
bool CloseControl (string tagid)
{
ControlBuilder current = stack.Builder;
string btag = current.TagName;
if (0 != String.Compare (tagid, btag, true))
return false;
// if (current is TemplateBuilder)
// pop from the id list
current.CloseControl ();
stack.Pop ();
stack.Builder.AppendSubBuilder (current);
return true;
}
bool ProcessCode (TagType tagtype, string code, ILocation location)
{
ControlBuilder b = null;
if (tagtype == TagType.CodeRender)
b = new CodeRenderBuilder (code, false, location);
else if (tagtype == TagType.CodeRenderExpression)
b = new CodeRenderBuilder (code, true, location);
else if (tagtype == TagType.DataBinding)
b = new DataBindingBuilder (code, location);
else
throw new HttpException ("Should never happen");
stack.Builder.AppendSubBuilder (b);
return true;
}
public ILocation Location {
get { return location; }
}
// Used to get CodeRender tags in attribute values
class CodeRenderParser
{
string str;
ControlBuilder builder;
public CodeRenderParser (string str, ControlBuilder builder)
{
this.str = str;
this.builder = builder;
}
public void AddChildren ()
{
int index = str.IndexOf ("<%");
if (index > 0) {
TextParsed (null, str.Substring (0, index));
str = str.Substring (index);
}
AspParser parser = new AspParser ("@@inner_string@@", new StringReader (str));
parser.Error += new ParseErrorHandler (ParseError);
parser.TagParsed += new TagParsedHandler (TagParsed);
parser.TextParsed += new TextParsedHandler (TextParsed);
parser.Parse ();
}
void TagParsed (ILocation location, TagType tagtype, string tagid, TagAttributes attributes)
{
if (tagtype == TagType.CodeRender)
builder.AppendSubBuilder (new CodeRenderBuilder (tagid, false, location));
else if (tagtype == TagType.CodeRenderExpression)
builder.AppendSubBuilder (new CodeRenderBuilder (tagid, true, location));
else if (tagtype == TagType.DataBinding)
builder.AppendSubBuilder (new DataBindingBuilder (tagid, location));
else
builder.AppendLiteralString (location.PlainText);
}
void TextParsed (ILocation location, string text)
{
builder.AppendLiteralString (text);
}
void ParseError (ILocation location, string message)
{
throw new ParseException (location, message);
}
}
}
}
|