2 // System.Web.Compilation.AspParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.ComponentModel;
32 using System.Collections;
33 using System.Globalization;
36 using System.Web.Util;
37 using System.Security.Cryptography;
39 namespace System.Web.Compilation
41 delegate void ParseErrorHandler (ILocation location, string message);
42 delegate void TextParsedHandler (ILocation location, string text);
43 delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
45 class AspParser : ILocation
47 static readonly object errorEvent = new object ();
48 static readonly object tagParsedEvent = new object ();
49 static readonly object textParsedEvent = new object ();
54 AspTokenizer tokenizer;
55 int beginLine, endLine;
56 int beginColumn, endColumn;
57 int beginPosition, endPosition;
62 EventHandlerList events = new EventHandlerList ();
64 public event ParseErrorHandler Error {
65 add { events.AddHandler (errorEvent, value); }
66 remove { events.RemoveHandler (errorEvent, value); }
69 public event TagParsedHandler TagParsed {
70 add { events.AddHandler (tagParsedEvent, value); }
71 remove { events.RemoveHandler (tagParsedEvent, value); }
74 public event TextParsedHandler TextParsed {
75 add { events.AddHandler (textParsedEvent, value); }
76 remove { events.RemoveHandler (textParsedEvent, value); }
79 public AspParser (string filename, TextReader input)
81 this.filename = filename;
82 fileText = input.ReadToEnd ();
84 MD5 md5 = MD5.Create ();
85 md5checksum = md5.ComputeHash (Encoding.UTF8.GetBytes (fileText));
87 StringReader reader = new StringReader (fileText);
88 tokenizer = new AspTokenizer (reader);
92 public byte[] MD5Checksum {
93 get { return md5checksum; }
97 public int BeginLine {
98 get { return beginLine; }
101 public int BeginColumn {
102 get { return beginColumn; }
106 get { return endLine; }
109 public int EndColumn {
110 get { return endColumn; }
113 public string FileText {
114 get { return fileText; }
117 public string PlainText {
119 if (beginPosition >= endPosition)
122 return fileText.Substring (beginPosition, endPosition - beginPosition);
126 public string Filename {
127 get { return filename; }
130 public string VerbatimID {
132 tokenizer.Verbatim = true;
137 bool Eat (int expected_token)
139 if (tokenizer.get_token () != expected_token) {
140 tokenizer.put_back ();
144 endLine = tokenizer.EndLine;
145 endColumn = tokenizer.EndColumn;
151 beginLine = tokenizer.BeginLine;
152 beginColumn = tokenizer.BeginColumn;
153 beginPosition = tokenizer.Position - 1;
158 endLine = tokenizer.EndLine;
159 endColumn = tokenizer.EndColumn;
160 endPosition = tokenizer.Position;
167 TagAttributes attributes;
168 TagType tagtype = TagType.Text;
169 StringBuilder text = new StringBuilder ();
171 while ((token = tokenizer.get_token ()) != Token.EOF) {
174 if (tokenizer.Verbatim){
175 string end_verbatim = "</" + verbatimID + ">";
176 string verbatim_text = GetVerbatim (token, end_verbatim);
178 if (verbatim_text == null)
179 OnError ("Unexpected EOF processing " + verbatimID);
181 tokenizer.Verbatim = false;
184 endPosition -= end_verbatim.Length;
185 OnTextParsed (verbatim_text);
186 beginPosition = endPosition;
187 endPosition += end_verbatim.Length;
188 OnTagParsed (TagType.Close, verbatimID, null);
193 GetTag (out tagtype, out id, out attributes);
195 if (tagtype == TagType.ServerComment)
198 if (tagtype == TagType.Text)
201 OnTagParsed (tagtype, id, attributes);
206 if (tokenizer.Value.Trim () == "" && tagtype == TagType.Directive) {
212 text.Append (tokenizer.Value);
213 token = tokenizer.get_token ();
214 } while (token != '<' && token != Token.EOF);
216 tokenizer.put_back ();
218 OnTextParsed (text.ToString ());
222 bool GetInclude (string str, out string pathType, out string filename)
226 str = str.Substring (2).Trim ();
227 int len = str.Length;
228 int lastQuote = str.LastIndexOf ('"');
229 if (len < 10 || lastQuote != len - 1)
232 if (!StrUtils.StartsWith (str, "#include ", true))
235 str = str.Substring (9).Trim ();
236 bool isfile = (StrUtils.StartsWith (str ,"file", true));
237 if (!isfile && !StrUtils.StartsWith (str, "virtual", true))
240 pathType = (isfile) ? "file" : "virtual";
241 if (str.Length < pathType.Length + 3)
244 str = str.Substring (pathType.Length).Trim ();
245 if (str.Length < 3 || str [0] != '=')
249 for (; index < str.Length; index++) {
250 if (Char.IsWhiteSpace (str [index]))
252 else if (str [index] == '"')
256 if (index == str.Length || index == lastQuote)
259 str = str.Substring (index);
260 if (str.Length == 2) { // only quotes
261 OnError ("Empty file name.");
265 filename = str.Trim ().Substring (index, str.Length - 2);
266 if (filename.LastIndexOf ('"') != -1)
267 return false; // file=""" -> no error
272 void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
274 int token = tokenizer.get_token ();
276 tagtype = TagType.ServerComment;
281 GetServerTag (out tagtype, out id, out attributes);
284 if (!Eat (Token.IDENTIFIER))
285 OnError ("expecting TAGNAME");
287 id = tokenizer.Value;
289 OnError ("expecting '>'. Got '" + id + "'");
291 tagtype = TagType.Close;
294 bool double_dash = Eat (Token.DOUBLEDASH);
296 tokenizer.put_back ();
298 tokenizer.Verbatim = true;
299 string end = double_dash ? "-->" : ">";
300 string comment = GetVerbatim (tokenizer.get_token (), end);
301 tokenizer.Verbatim = false;
303 OnError ("Unfinished HTML comment/DTD");
305 string pathType, filename;
306 if (double_dash && GetInclude (comment, out pathType, out filename)) {
307 tagtype = TagType.Include;
308 attributes = new TagAttributes ();
309 attributes.Add (pathType, filename);
311 tagtype = TagType.Text;
312 id = "<!" + comment + end;
315 case Token.IDENTIFIER:
316 if (this.filename == "@@inner_string@@") {
317 // Actually not tag but "xxx < yyy" stuff in inner_string!
318 tagtype = TagType.Text;
319 tokenizer.InTag = false;
320 id = "<" + tokenizer.Odds + tokenizer.Value;
322 id = tokenizer.Value;
324 attributes = GetAttributes ();
325 } catch (Exception e) {
330 tagtype = TagType.Tag;
331 if (Eat ('/') && Eat ('>')) {
332 tagtype = TagType.SelfClosing;
333 } else if (!Eat ('>')) {
334 if (attributes.IsRunAtServer ()) {
335 OnError ("The server tag is not well formed.");
338 tokenizer.Verbatim = true;
339 attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
340 tokenizer.Verbatim = false;
346 tagtype = TagType.Text;
347 tokenizer.InTag = false;
348 id = "<" + tokenizer.Value;
353 TagAttributes GetAttributes ()
356 TagAttributes attributes;
358 bool wellFormedForServer = true;
360 attributes = new TagAttributes ();
361 while ((token = tokenizer.get_token ()) != Token.EOF){
362 if (token == '<' && Eat ('%')) {
363 tokenizer.Verbatim = true;
364 attributes.Add ("", "<%" +
365 GetVerbatim (tokenizer.get_token (), "%>") + "%>");
366 tokenizer.Verbatim = false;
367 tokenizer.InTag = true;
371 if (token != Token.IDENTIFIER)
374 id = tokenizer.Value;
376 if (Eat (Token.ATTVALUE)){
377 attributes.Add (id, tokenizer.Value);
378 wellFormedForServer &= tokenizer.AlternatingQuotes;
379 } else if (Eat ('<') && Eat ('%')) {
380 tokenizer.Verbatim = true;
381 attributes.Add (id, "<%" +
382 GetVerbatim (tokenizer.get_token (), "%>") + "%>");
383 tokenizer.Verbatim = false;
384 tokenizer.InTag = true;
386 OnError ("expected ATTVALUE");
390 attributes.Add (id, null);
394 tokenizer.put_back ();
396 if (attributes.IsRunAtServer () && !wellFormedForServer) {
397 OnError ("The server tag is not well formed.");
404 string GetVerbatim (int token, string end)
406 StringBuilder vb_text = new StringBuilder ();
407 StringBuilder tmp = new StringBuilder ();
410 if (tokenizer.Value.Length > 1){
411 // May be we have a put_back token that is not a single character
412 vb_text.Append (tokenizer.Value);
413 token = tokenizer.get_token ();
416 end = end.ToLower (CultureInfo.InvariantCulture);
418 for (int k = 0; k < end.Length; k++)
419 if (end [0] == end [k])
422 while (token != Token.EOF){
423 if (Char.ToLower ((char) token, CultureInfo.InvariantCulture) == end [i]){
424 if (++i >= end.Length)
426 tmp.Append ((char) token);
427 token = tokenizer.get_token ();
430 if (repeated > 1 && i == repeated && (char) token == end [0]) {
431 vb_text.Append ((char) token);
432 token = tokenizer.get_token ();
435 vb_text.Append (tmp.ToString ());
436 tmp.Remove (0, tmp.Length);
440 vb_text.Append ((char) token);
441 token = tokenizer.get_token ();
444 if (token == Token.EOF)
445 OnError ("Expecting " + end + " and got EOF.");
447 return RemoveComments (vb_text.ToString ());
450 string RemoveComments (string text)
453 int start = text.IndexOf ("<%--");
455 while (start != -1) {
456 end = text.IndexOf ("--%>");
457 if (end == -1 || end <= start + 1)
460 text = text.Remove (start, end - start + 4);
461 start = text.IndexOf ("<%--");
467 void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
470 bool old = tokenizer.ExpectAttrValue;
472 tokenizer.ExpectAttrValue = false;
474 tokenizer.ExpectAttrValue = old;
475 tagtype = TagType.Directive;
477 if (Eat (Token.DIRECTIVE))
478 id = tokenizer.Value;
480 attributes = GetAttributes ();
481 if (!Eat ('%') || !Eat ('>'))
482 OnError ("expecting '%>'");
487 if (Eat (Token.DOUBLEDASH)) {
488 tokenizer.ExpectAttrValue = old;
489 tokenizer.Verbatim = true;
490 inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
491 tokenizer.Verbatim = false;
494 tagtype = TagType.ServerComment;
498 tokenizer.ExpectAttrValue = old;
502 databinding = !varname && Eat ('#');
504 tokenizer.Verbatim = true;
505 inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
506 tokenizer.Verbatim = false;
509 tagtype = (databinding ? TagType.DataBinding :
510 (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
513 void OnError (string msg)
515 ParseErrorHandler eh = events [errorEvent] as ParseErrorHandler;
520 void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
522 TagParsedHandler eh = events [tagParsedEvent] as TagParsedHandler;
524 eh (this, tagtype, id, attributes);
527 void OnTextParsed (string text)
529 TextParsedHandler eh = events [textParsedEvent] as TextParsedHandler;