| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 |
- //
- // System.Web.Compilation.AspGenerator
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
- // Copyright (c) 2004,2006 Novell, Inc (http://www.novell.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.CodeDom.Compiler;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using System.Web.Caching;
- using System.Web.Configuration;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.Util;
- 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 ParserStack
- {
- Hashtable files;
- Stack parsers;
- AspParser current;
- public ParserStack ()
- {
- files = new Hashtable (); // may be this should be case sensitive for windows
- parsers = new Stack ();
- }
-
- public bool Push (AspParser parser)
- {
- if (files.Contains (parser.Filename))
- return false;
- files [parser.Filename] = true;
- parsers.Push (parser);
- current = parser;
- return true;
- }
- public AspParser Pop ()
- {
- if (parsers.Count == 0)
- return null;
- files.Remove (current.Filename);
- AspParser result = (AspParser) parsers.Pop ();
- if (parsers.Count > 0)
- current = (AspParser) parsers.Peek ();
- else
- current = null;
- return result;
- }
- public int Count {
- get { return parsers.Count; }
- }
-
- public AspParser Parser {
- get { return current; }
- }
- public string Filename {
- get { return current.Filename; }
- }
- }
- class TagStack
- {
- Stack tags;
- public TagStack ()
- {
- tags = new Stack ();
- }
-
- public void Push (string tagid)
- {
- tags.Push (tagid);
- }
- public string Pop ()
- {
- if (tags.Count == 0)
- return null;
- return (string) tags.Pop ();
- }
- public bool CompareTo (string tagid)
- {
- if (tags.Count == 0)
- return false;
- return 0 == String.Compare (tagid, (string) tags.Peek (), true, CultureInfo.InvariantCulture);
- }
-
- public int Count {
- get { return tags.Count; }
- }
- public string Current {
- get { return (string) tags.Peek (); }
- }
- }
- class AspGenerator
- {
- ParserStack pstack;
- BuilderLocationStack stack;
- TemplateParser tparser;
- StringBuilder text;
- RootBuilder rootBuilder;
- bool inScript, javascript, ignore_text;
- ILocation location;
- bool isApplication;
- StringBuilder tagInnerText = new StringBuilder ();
- static Hashtable emptyHash = new Hashtable ();
- bool inForm;
- bool useOtherTags;
- public AspGenerator (TemplateParser tparser)
- {
- this.tparser = tparser;
- text = new StringBuilder ();
- stack = new BuilderLocationStack ();
- rootBuilder = new RootBuilder (tparser);
- stack.Push (rootBuilder, null);
- tparser.RootBuilder = rootBuilder;
- pstack = new ParserStack ();
- }
- public RootBuilder RootBuilder {
- get { return tparser.RootBuilder; }
- }
- public AspParser Parser {
- get { return pstack.Parser; }
- }
-
- public string Filename {
- get { return pstack.Filename; }
- }
-
- 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);
- #if NET_2_0
- if (type == typeof(MasterPageParser))
- return new MasterPageCompiler ((MasterPageParser) tparser);
- #endif
- throw new Exception ("Got type: " + type);
- }
-
- void InitParser (string filename)
- {
- StreamReader reader = new StreamReader (filename, WebEncoding.FileEncoding);
- AspParser parser = new AspParser (filename, reader);
- reader.Close ();
- parser.Error += new ParseErrorHandler (ParseError);
- parser.TagParsed += new TagParsedHandler (TagParsed);
- parser.TextParsed += new TextParsedHandler (TextParsed);
- if (!pstack.Push (parser))
- throw new ParseException (Location, "Infinite recursion detected including file: " + filename);
- tparser.AddDependency (filename);
- }
- public void Parse (string file)
- {
- InitParser (file);
- pstack.Parser.Parse ();
- if (text.Length > 0)
- FlushText ();
- pstack.Pop ();
- #if DEBUG
- PrintTree (rootBuilder, 0);
- #endif
- if (stack.Count > 1 && pstack.Count == 0)
- throw new ParseException (stack.Builder.location,
- "Expecting </" + stack.Builder.TagName + "> " + stack.Builder);
- }
- public void Parse ()
- {
- Parse (Path.GetFullPath (tparser.InputFile));
- }
- public Type GetCompiledType ()
- {
- Type type = (Type) HttpRuntime.Cache.Get ("@@Type" + tparser.InputFile);
- if (type != null) {
- return type;
- }
- isApplication = tparser.DefaultDirectiveName == "application";
- Parse ();
- BaseCompiler compiler = GetCompilerFromType ();
- type = compiler.GetCompiledType ();
- CacheDependency cd = new CacheDependency ((string[])
- tparser.Dependencies.ToArray (typeof (string)));
- HttpRuntime.Cache.InsertPrivate ("@@Type" + tparser.InputFile, type, cd);
- return type;
- }
- #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++);
- }
- }
-
- 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 ();
- }
- #endif
- 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;
- // MS ignores tbody/thead
- if ((attributes == null || !attributes.IsRunAtServer ())) {
- if (String.Compare (tagid, "tbody", true, CultureInfo.InvariantCulture) == 0 ||
- String.Compare (tagid, "thead", true, CultureInfo.InvariantCulture) == 0)
- return;
- }
- if (text.Length != 0)
- FlushText ();
- if (0 == String.Compare (tagid, "script", true, CultureInfo.InvariantCulture)) {
- bool in_script = (inScript || ignore_text);
- if (in_script || (tagtype != TagType.Close && attributes != null)) {
- if ((in_script || attributes.IsRunAtServer ()) && 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, tagtype)) {
- useOtherTags = true;
- break;
- }
- if (useOtherTags) {
- stack.Builder.EnsureOtherTags ();
- stack.Builder.OtherTags.Add (tagid);
- }
- TextParsed (location, location.PlainText);
- break;
- case TagType.Close:
- bool notServer = (useOtherTags && TryRemoveTag (tagid, stack.Builder.OtherTags));
- if (!notServer && CloseControl (tagid))
- break;
-
- TextParsed (location, location.PlainText);
- break;
- case TagType.SelfClosing:
- int count = stack.Count;
- if (!ProcessTag (tagid, attributes, tagtype)) {
- 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:
- if (isApplication)
- throw new ParseException (location, "Invalid content for application file.");
-
- ProcessCode (tagtype, tagid, location);
- break;
- case TagType.Include:
- if (isApplication)
- throw new ParseException (location, "Invalid content for application file.");
-
- string file = attributes ["virtual"] as string;
- bool isvirtual = (file != null);
- if (!isvirtual)
- file = attributes ["file"] as string;
- if (isvirtual) {
- file = tparser.MapPath (file);
- } else {
- file = GetIncludeFilePath (tparser.BaseDir, file);
- }
- Parse (file);
- break;
- default:
- break;
- }
- //PrintLocation (location);
- }
- static bool TryRemoveTag (string tagid, ArrayList otags)
- {
- if (otags == null || otags.Count == 0)
- return false;
- for (int idx = otags.Count - 1; idx >= 0; idx--) {
- string otagid = (string) otags [idx];
- if (0 == String.Compare (tagid, otagid, true, CultureInfo.InvariantCulture)) {
- do {
- otags.RemoveAt (idx);
- } while (otags.Count - 1 >= idx);
- return true;
- }
- }
- return false;
- }
- static string GetIncludeFilePath (string basedir, string filename)
- {
- if (Path.DirectorySeparatorChar == '/')
- filename = filename.Replace ("\\", "/");
- return Path.GetFullPath (Path.Combine (basedir, filename));
- }
-
- void TextParsed (ILocation location, string text)
- {
- if (ignore_text)
- return;
- if (text.IndexOf ("<%") != -1 && !inScript) {
- 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.");
- ControlBuilder current = stack.Builder;
- current.AppendLiteralString (t);
- if (current.NeedsTagInnerText ()) {
- tagInnerText.Append (t);
- }
- }
- bool ProcessTag (string tagid, TagAttributes atts, TagType tagtype)
- {
- if (isApplication) {
- if (String.Compare (tagid, "object", true, CultureInfo.InvariantCulture) != 0)
- throw new ParseException (location, "Invalid tag for application file.");
- }
- ControlBuilder parent = stack.Builder;
- ControlBuilder builder = null;
- Hashtable htable = (atts != null) ? atts.GetDictionary (null) : emptyHash;
- if (stack.Count > 1) {
- try {
- builder = parent.CreateSubBuilder (tagid, htable, null, tparser, location);
- } catch (TypeLoadException e) {
- throw new ParseException (Location, "Type not found.", e);
- } catch (Exception e) {
- throw new ParseException (Location, e.Message, e);
- }
- }
- if (builder == null && atts != null && atts.IsRunAtServer ()) {
- string id = htable ["id"] as string;
- if (id != null && !CodeGenerator.IsValidLanguageIndependentIdentifier (id))
- throw new ParseException (Location, "'" + id + "' is not a valid identifier");
-
- try {
- builder = rootBuilder.CreateSubBuilder (tagid, htable, null, tparser, location);
- } catch (TypeLoadException e) {
- throw new ParseException (Location, "Type not found.", e);
- } catch (Exception e) {
- throw new ParseException (Location, e.Message, e);
- }
- }
-
- if (builder == null)
- return false;
- builder.location = location;
- builder.ID = htable ["id"] as string;
- if (typeof (HtmlForm).IsAssignableFrom (builder.ControlType)) {
- if (inForm)
- throw new ParseException (location, "Only one <form> allowed.");
- inForm = true;
- }
- if (builder.HasBody () && !(builder is ObjectTagBuilder)) {
- if (builder is TemplateBuilder) {
- // push the id list
- }
- stack.Push (builder, location);
- } else {
- if (!isApplication && builder is ObjectTagBuilder) {
- ObjectTagBuilder ot = (ObjectTagBuilder) builder;
- if (ot.Scope != null && ot.Scope != "")
- throw new ParseException (location, "Scope not allowed here");
- if (tagtype == TagType.Tag) {
- stack.Push (builder, location);
- return true;
- }
- }
-
- parent.AppendSubBuilder (builder);
- builder.CloseControl ();
- }
- return true;
- }
- string ReadFile (string filename)
- {
- string realpath = tparser.MapPath (filename);
- using (StreamReader sr = new StreamReader (realpath, WebEncoding.FileEncoding)) {
- string content = sr.ReadToEnd ();
- return content;
- }
- }
- bool ProcessScript (TagType tagtype, TagAttributes attributes)
- {
- if (tagtype != TagType.Close) {
- if (attributes != null && attributes.IsRunAtServer ()) {
- CheckLanguage ((string) attributes ["language"]);
- string src = (string) attributes ["src"];
- if (src != null) {
- if (src == "")
- throw new ParseException (Parser,
- "src cannot be an empty string");
- string content = ReadFile (src);
- inScript = true;
- TextParsed (Parser, content);
- FlushText ();
- inScript = false;
- if (tagtype != TagType.SelfClosing) {
- ignore_text = true;
- Parser.VerbatimID = "script";
- }
- } else if (tagtype == TagType.Tag) {
- Parser.VerbatimID = "script";
- inScript = true;
- }
- return true;
- } else {
- if (tagtype != TagType.SelfClosing) {
- Parser.VerbatimID = "script";
- javascript = true;
- }
- TextParsed (location, location.PlainText);
- return true;
- }
- }
- bool result;
- if (inScript) {
- result = inScript;
- inScript = false;
- } else if (!ignore_text) {
- result = javascript;
- javascript = false;
- TextParsed (location, location.PlainText);
- } else {
- ignore_text = false;
- result = true;
- }
- return result;
- }
- bool CloseControl (string tagid)
- {
- ControlBuilder current = stack.Builder;
- string btag = current.TagName;
- if (String.Compare (btag, "tbody", true, CultureInfo.InvariantCulture) != 0 &&
- String.Compare (tagid, "tbody", true, CultureInfo.InvariantCulture) == 0) {
- if (!current.ChildrenAsProperties) {
- try {
- TextParsed (location, location.PlainText);
- FlushText ();
- } catch {}
- }
- return true;
- }
-
- if (0 != String.Compare (tagid, btag, true, CultureInfo.InvariantCulture))
- return false;
- // if (current is TemplateBuilder)
- // pop from the id list
- if (current.NeedsTagInnerText ()) {
- try {
- current.SetTagInnerText (tagInnerText.ToString ());
- } catch (Exception e) {
- throw new ParseException (current.location, e.Message, e);
- }
- tagInnerText.Length = 0;
- }
- if (typeof (HtmlForm).IsAssignableFrom (current.ControlType)) {
- inForm = false;
- }
- 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; }
- }
- void CheckLanguage (string lang)
- {
- if (lang == null || lang == "")
- return;
- if (String.Compare (lang, tparser.Language, true, CultureInfo.InvariantCulture) == 0)
- return;
- #if NET_2_0
- CompilationSection section = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
- if (section.Compilers[tparser.Language] != section.Compilers[lang])
- #else
- CompilationConfiguration cfg = CompilationConfiguration.GetInstance (HttpContext.Current);
- if (!cfg.Compilers.CompareLanguages (tparser.Language, lang))
- #endif
- throw new ParseException (Location,
- String.Format ("Trying to mix language '{0}' and '{1}'.",
- tparser.Language, lang));
- }
- // 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);
- }
- }
- }
- }
|