AspParser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // System.Web.Compilation.AspParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.Text;
  34. using System.Web.Util;
  35. using System.Security.Cryptography;
  36. namespace System.Web.Compilation
  37. {
  38. delegate void ParseErrorHandler (ILocation location, string message);
  39. delegate void TextParsedHandler (ILocation location, string text);
  40. delegate void TagParsedHandler (ILocation location, TagType tagtype, string id, TagAttributes attributes);
  41. class AspParser : ILocation
  42. {
  43. AspTokenizer tokenizer;
  44. int beginLine, endLine;
  45. int beginColumn, endColumn;
  46. int beginPosition, endPosition;
  47. byte[] md5checksum;
  48. string filename;
  49. string fileText;
  50. string verbatimID;
  51. public AspParser (string filename, TextReader input)
  52. {
  53. this.filename = filename;
  54. fileText = input.ReadToEnd ();
  55. MD5 md5 = MD5.Create ();
  56. md5checksum = md5.ComputeHash (Encoding.UTF8.GetBytes (fileText));
  57. StringReader reader = new StringReader (fileText);
  58. tokenizer = new AspTokenizer (reader);
  59. }
  60. public byte[] MD5Checksum {
  61. get { return md5checksum; }
  62. }
  63. public int BeginLine {
  64. get { return beginLine; }
  65. }
  66. public int BeginColumn {
  67. get { return beginColumn; }
  68. }
  69. public int EndLine {
  70. get { return endLine; }
  71. }
  72. public int EndColumn {
  73. get { return endColumn; }
  74. }
  75. public string FileText {
  76. get { return fileText; }
  77. }
  78. public string PlainText {
  79. get {
  80. if (beginPosition >= endPosition)
  81. return null;
  82. return fileText.Substring (beginPosition, endPosition - beginPosition);
  83. }
  84. }
  85. public string Filename {
  86. get { return filename; }
  87. }
  88. public string VerbatimID {
  89. set {
  90. tokenizer.Verbatim = true;
  91. verbatimID = value;
  92. }
  93. }
  94. bool Eat (int expected_token)
  95. {
  96. if (tokenizer.get_token () != expected_token) {
  97. tokenizer.put_back ();
  98. return false;
  99. }
  100. endLine = tokenizer.EndLine;
  101. endColumn = tokenizer.EndColumn;
  102. return true;
  103. }
  104. void BeginElement ()
  105. {
  106. beginLine = tokenizer.BeginLine;
  107. beginColumn = tokenizer.BeginColumn;
  108. beginPosition = tokenizer.Position - 1;
  109. }
  110. void EndElement ()
  111. {
  112. endLine = tokenizer.EndLine;
  113. endColumn = tokenizer.EndColumn;
  114. endPosition = tokenizer.Position;
  115. }
  116. public void Parse ()
  117. {
  118. int token;
  119. string id;
  120. TagAttributes attributes;
  121. TagType tagtype = TagType.Text;
  122. StringBuilder text = new StringBuilder ();
  123. while ((token = tokenizer.get_token ()) != Token.EOF) {
  124. BeginElement ();
  125. if (tokenizer.Verbatim){
  126. string end_verbatim = "</" + verbatimID + ">";
  127. string verbatim_text = GetVerbatim (token, end_verbatim);
  128. if (verbatim_text == null)
  129. OnError ("Unexpected EOF processing " + verbatimID);
  130. tokenizer.Verbatim = false;
  131. EndElement ();
  132. endPosition -= end_verbatim.Length;
  133. OnTextParsed (verbatim_text);
  134. beginPosition = endPosition;
  135. endPosition += end_verbatim.Length;
  136. OnTagParsed (TagType.Close, verbatimID, null);
  137. continue;
  138. }
  139. if (token == '<') {
  140. GetTag (out tagtype, out id, out attributes);
  141. EndElement ();
  142. if (tagtype == TagType.ServerComment)
  143. continue;
  144. if (tagtype == TagType.Text)
  145. OnTextParsed (id);
  146. else
  147. OnTagParsed (tagtype, id, attributes);
  148. continue;
  149. }
  150. if (tokenizer.Value.Trim () == "" && tagtype == TagType.Directive) {
  151. continue;
  152. }
  153. text.Length = 0;
  154. do {
  155. text.Append (tokenizer.Value);
  156. token = tokenizer.get_token ();
  157. } while (token != '<' && token != Token.EOF);
  158. tokenizer.put_back ();
  159. EndElement ();
  160. OnTextParsed (text.ToString ());
  161. }
  162. }
  163. bool GetInclude (string str, out string pathType, out string filename)
  164. {
  165. pathType = null;
  166. filename = null;
  167. str = str.Substring (2).Trim ();
  168. int len = str.Length;
  169. int lastQuote = str.LastIndexOf ('"');
  170. if (len < 10 || lastQuote != len - 1)
  171. return false;
  172. if (!StrUtils.StartsWith (str, "#include ", true))
  173. return false;
  174. str = str.Substring (9).Trim ();
  175. bool isfile = (StrUtils.StartsWith (str ,"file", true));
  176. if (!isfile && !StrUtils.StartsWith (str, "virtual", true))
  177. return false;
  178. pathType = (isfile) ? "file" : "virtual";
  179. if (str.Length < pathType.Length + 3)
  180. return false;
  181. str = str.Substring (pathType.Length).Trim ();
  182. if (str.Length < 3 || str [0] != '=')
  183. return false;
  184. int index = 1;
  185. for (; index < str.Length; index++) {
  186. if (Char.IsWhiteSpace (str [index]))
  187. continue;
  188. else if (str [index] == '"')
  189. break;
  190. }
  191. if (index == str.Length || index == lastQuote)
  192. return false;
  193. str = str.Substring (index);
  194. if (str.Length == 2) { // only quotes
  195. OnError ("Empty file name.");
  196. return false;
  197. }
  198. filename = str.Trim ().Substring (index, str.Length - 2);
  199. if (filename.LastIndexOf ('"') != -1)
  200. return false; // file=""" -> no error
  201. return true;
  202. }
  203. void GetTag (out TagType tagtype, out string id, out TagAttributes attributes)
  204. {
  205. int token = tokenizer.get_token ();
  206. tagtype = TagType.ServerComment;
  207. id = null;
  208. attributes = null;
  209. switch (token){
  210. case '%':
  211. GetServerTag (out tagtype, out id, out attributes);
  212. break;
  213. case '/':
  214. if (!Eat (Token.IDENTIFIER))
  215. OnError ("expecting TAGNAME");
  216. id = tokenizer.Value;
  217. if (!Eat ('>'))
  218. OnError ("expecting '>'. Got '" + id + "'");
  219. tagtype = TagType.Close;
  220. break;
  221. case '!':
  222. bool double_dash = Eat (Token.DOUBLEDASH);
  223. if (double_dash)
  224. tokenizer.put_back ();
  225. tokenizer.Verbatim = true;
  226. string end = double_dash ? "-->" : ">";
  227. string comment = GetVerbatim (tokenizer.get_token (), end);
  228. tokenizer.Verbatim = false;
  229. if (comment == null)
  230. OnError ("Unfinished HTML comment/DTD");
  231. string pathType, filename;
  232. if (double_dash && GetInclude (comment, out pathType, out filename)) {
  233. tagtype = TagType.Include;
  234. attributes = new TagAttributes ();
  235. attributes.Add (pathType, filename);
  236. } else {
  237. tagtype = TagType.Text;
  238. id = "<!" + comment + end;
  239. }
  240. break;
  241. case Token.IDENTIFIER:
  242. if (this.filename == "@@inner_string@@") {
  243. // Actually not tag but "xxx < yyy" stuff in inner_string!
  244. tagtype = TagType.Text;
  245. tokenizer.InTag = false;
  246. id = "<" + tokenizer.Odds + tokenizer.Value;
  247. } else {
  248. id = tokenizer.Value;
  249. try {
  250. attributes = GetAttributes ();
  251. } catch (Exception e) {
  252. OnError (e.Message);
  253. break;
  254. }
  255. tagtype = TagType.Tag;
  256. if (Eat ('/') && Eat ('>')) {
  257. tagtype = TagType.SelfClosing;
  258. } else if (!Eat ('>')) {
  259. if (attributes.IsRunAtServer ()) {
  260. OnError ("The server tag is not well formed.");
  261. break;
  262. }
  263. tokenizer.Verbatim = true;
  264. attributes.Add ("", GetVerbatim (tokenizer.get_token (), ">") + ">");
  265. tokenizer.Verbatim = false;
  266. }
  267. }
  268. break;
  269. default:
  270. tagtype = TagType.Text;
  271. tokenizer.InTag = false;
  272. id = "<" + tokenizer.Value;
  273. break;
  274. }
  275. }
  276. TagAttributes GetAttributes ()
  277. {
  278. int token;
  279. TagAttributes attributes;
  280. string id;
  281. bool wellFormedForServer = true;
  282. attributes = new TagAttributes ();
  283. while ((token = tokenizer.get_token ()) != Token.EOF){
  284. if (token == '<' && Eat ('%')) {
  285. tokenizer.Verbatim = true;
  286. attributes.Add ("", "<%" +
  287. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  288. tokenizer.Verbatim = false;
  289. tokenizer.InTag = true;
  290. continue;
  291. }
  292. if (token != Token.IDENTIFIER)
  293. break;
  294. id = tokenizer.Value;
  295. if (Eat ('=')){
  296. if (Eat (Token.ATTVALUE)){
  297. attributes.Add (id, tokenizer.Value);
  298. wellFormedForServer &= tokenizer.AlternatingQuotes;
  299. } else if (Eat ('<') && Eat ('%')) {
  300. tokenizer.Verbatim = true;
  301. attributes.Add (id, "<%" +
  302. GetVerbatim (tokenizer.get_token (), "%>") + "%>");
  303. tokenizer.Verbatim = false;
  304. tokenizer.InTag = true;
  305. } else {
  306. OnError ("expected ATTVALUE");
  307. return null;
  308. }
  309. } else {
  310. attributes.Add (id, null);
  311. }
  312. }
  313. tokenizer.put_back ();
  314. if (attributes.IsRunAtServer () && !wellFormedForServer) {
  315. OnError ("The server tag is not well formed.");
  316. return null;
  317. }
  318. return attributes;
  319. }
  320. string GetVerbatim (int token, string end)
  321. {
  322. StringBuilder vb_text = new StringBuilder ();
  323. int i = 0;
  324. if (tokenizer.Value.Length > 1){
  325. // May be we have a put_back token that is not a single character
  326. vb_text.Append (tokenizer.Value);
  327. token = tokenizer.get_token ();
  328. }
  329. end = end.ToLower (CultureInfo.InvariantCulture);
  330. while (token != Token.EOF){
  331. if (Char.ToLower ((char) token, CultureInfo.InvariantCulture) == end [i]){
  332. if (++i >= end.Length)
  333. break;
  334. token = tokenizer.get_token ();
  335. continue;
  336. } else if (i > 0) {
  337. for (int j = 0; j < i; j++)
  338. vb_text.Append (end [j]);
  339. i = 0;
  340. }
  341. vb_text.Append ((char) token);
  342. token = tokenizer.get_token ();
  343. }
  344. if (token == Token.EOF)
  345. OnError ("Expecting " + end + " and got EOF.");
  346. return RemoveComments (vb_text.ToString ());
  347. }
  348. string RemoveComments (string text)
  349. {
  350. int end;
  351. int start = text.IndexOf ("<%--");
  352. while (start != -1) {
  353. end = text.IndexOf ("--%>");
  354. if (end == -1 || end <= start + 1)
  355. break;
  356. text = text.Remove (start, end - start + 4);
  357. start = text.IndexOf ("<%--");
  358. }
  359. return text;
  360. }
  361. void GetServerTag (out TagType tagtype, out string id, out TagAttributes attributes)
  362. {
  363. string inside_tags;
  364. bool old = tokenizer.ExpectAttrValue;
  365. tokenizer.ExpectAttrValue = false;
  366. if (Eat ('@')){
  367. tokenizer.ExpectAttrValue = old;
  368. tagtype = TagType.Directive;
  369. id = "";
  370. if (Eat (Token.DIRECTIVE))
  371. id = tokenizer.Value;
  372. attributes = GetAttributes ();
  373. if (!Eat ('%') || !Eat ('>'))
  374. OnError ("expecting '%>'");
  375. return;
  376. }
  377. if (Eat (Token.DOUBLEDASH)) {
  378. tokenizer.ExpectAttrValue = old;
  379. tokenizer.Verbatim = true;
  380. inside_tags = GetVerbatim (tokenizer.get_token (), "--%>");
  381. tokenizer.Verbatim = false;
  382. id = null;
  383. attributes = null;
  384. tagtype = TagType.ServerComment;
  385. return;
  386. }
  387. tokenizer.ExpectAttrValue = old;
  388. bool varname;
  389. bool databinding;
  390. varname = Eat ('=');
  391. databinding = !varname && Eat ('#');
  392. tokenizer.Verbatim = true;
  393. inside_tags = GetVerbatim (tokenizer.get_token (), "%>");
  394. tokenizer.Verbatim = false;
  395. id = inside_tags;
  396. attributes = null;
  397. tagtype = (databinding ? TagType.DataBinding :
  398. (varname ? TagType.CodeRenderExpression : TagType.CodeRender));
  399. }
  400. public event ParseErrorHandler Error;
  401. public event TagParsedHandler TagParsed;
  402. public event TextParsedHandler TextParsed;
  403. void OnError (string msg)
  404. {
  405. if (Error != null)
  406. Error (this, msg);
  407. }
  408. void OnTagParsed (TagType tagtype, string id, TagAttributes attributes)
  409. {
  410. if (TagParsed != null)
  411. TagParsed (this, tagtype, id, attributes);
  412. }
  413. void OnTextParsed (string text)
  414. {
  415. if (TextParsed != null)
  416. TextParsed (this, text);
  417. }
  418. }
  419. }