TemplateBuildProvider.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // System.Web.Compilation.TemplateBuildProvider
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007 Novell, Inc
  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. #if NET_2_0
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Text.RegularExpressions;
  38. using System.Web;
  39. using System.Web.Hosting;
  40. using System.Web.UI;
  41. using System.Web.Util;
  42. namespace System.Web.Compilation
  43. {
  44. internal abstract class TemplateBuildProvider : GenericBuildProvider <TemplateParser>
  45. {
  46. delegate void ExtractDirectiveDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp);
  47. static Regex directiveRegex = new Regex (@"<%\s*@(\s*(?<attrname>\w[\w:]*(?=\W))(\s*(?<equal>=)\s*""(?<attrval>[^""]*)""|\s*(?<equal>=)\s*'(?<attrval>[^']*)'|\s*(?<equal>=)\s*(?<attrval>[^\s%>]*)|(?<equal>)(?<attrval>\s*?)))*\s*?%>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  48. static SortedDictionary <string, ExtractDirectiveDependencies> directiveAttributes;
  49. static char[] directiveValueTrimChars = {' ', '\t', '\r', '\n', '"', '\''};
  50. SortedDictionary <string, bool> dependencies;
  51. string compilationLanguage;
  52. internal override string LanguageName {
  53. get {
  54. if (String.IsNullOrEmpty (compilationLanguage)) {
  55. ExtractDependencies ();
  56. if (String.IsNullOrEmpty (compilationLanguage))
  57. compilationLanguage = base.LanguageName;
  58. }
  59. return compilationLanguage;
  60. }
  61. }
  62. static TemplateBuildProvider ()
  63. {
  64. directiveAttributes = new SortedDictionary <string, ExtractDirectiveDependencies> (StringComparer.InvariantCultureIgnoreCase);
  65. directiveAttributes.Add ("Control", ExtractControlDependencies);
  66. directiveAttributes.Add ("Master", ExtractPageOrMasterDependencies);
  67. directiveAttributes.Add ("MasterType", ExtractPreviousPageTypeOrMasterTypeDependencies);
  68. directiveAttributes.Add ("Page", ExtractPageOrMasterDependencies);
  69. directiveAttributes.Add ("PreviousPageType", ExtractPreviousPageTypeOrMasterTypeDependencies);
  70. directiveAttributes.Add ("Reference", ExtractReferenceDependencies);
  71. directiveAttributes.Add ("Register", ExtractRegisterDependencies);
  72. directiveAttributes.Add ("WebHandler", ExtractLanguage);
  73. directiveAttributes.Add ("WebService", ExtractLanguage);
  74. }
  75. static string ExtractDirectiveAttribute (string baseDirectory, string name, CaptureCollection names, CaptureCollection values)
  76. {
  77. return ExtractDirectiveAttribute (baseDirectory, name, names, values, true);
  78. }
  79. static string ExtractDirectiveAttribute (string baseDirectory, string name, CaptureCollection names, CaptureCollection values, bool isPath)
  80. {
  81. if (names.Count == 0)
  82. return String.Empty;
  83. int index = 0;
  84. int valuesCount = values.Count;
  85. foreach (Capture c in names) {
  86. if (String.Compare (c.Value, name, StringComparison.OrdinalIgnoreCase) != 0) {
  87. index++;
  88. continue;
  89. }
  90. if (index > valuesCount)
  91. return String.Empty;
  92. if (isPath)
  93. return new VirtualPath (values [index].Value.Trim (directiveValueTrimChars), baseDirectory).Absolute;
  94. else
  95. return values [index].Value.Trim ();
  96. }
  97. return String.Empty;
  98. }
  99. static void ExtractControlDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  100. {
  101. ExtractLanguage (baseDirectory, names, values, bp);
  102. ExtractCodeBehind (baseDirectory, names, values, bp);
  103. }
  104. static void ExtractLanguage (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  105. {
  106. string value = ExtractDirectiveAttribute (baseDirectory, "Language", names, values, false);
  107. if (String.IsNullOrEmpty (value))
  108. return;
  109. bp.compilationLanguage = value;
  110. ExtractCodeBehind (baseDirectory, names, values, bp);
  111. }
  112. static void ExtractPageOrMasterDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  113. {
  114. ExtractLanguage (baseDirectory, names, values, bp);
  115. string value = ExtractDirectiveAttribute (baseDirectory, "MasterPageFile", names, values);
  116. if (!String.IsNullOrEmpty (value)) {
  117. if (!bp.dependencies.ContainsKey (value))
  118. bp.dependencies.Add (value, true);
  119. }
  120. ExtractCodeBehind (baseDirectory, names, values, bp);
  121. }
  122. static void ExtractCodeBehind (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  123. {
  124. string[] varray = new string [2];
  125. varray [0] = ExtractDirectiveAttribute (baseDirectory, "CodeFile", names, values);
  126. varray [1] = ExtractDirectiveAttribute (baseDirectory, "Src", names, values);
  127. foreach (string value in varray) {
  128. if (!String.IsNullOrEmpty (value)) {
  129. if (!bp.dependencies.ContainsKey (value))
  130. bp.dependencies.Add (value, true);
  131. }
  132. }
  133. }
  134. static void ExtractRegisterDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  135. {
  136. string src = ExtractDirectiveAttribute (baseDirectory, "Src", names, values);
  137. if (String.IsNullOrEmpty (src))
  138. return;
  139. string value = ExtractDirectiveAttribute (baseDirectory, "TagName", names, values);
  140. if (String.IsNullOrEmpty (value))
  141. return;
  142. value = ExtractDirectiveAttribute (baseDirectory, "TagPrefix", names, values);
  143. if (String.IsNullOrEmpty (value))
  144. return;
  145. if (bp.dependencies.ContainsKey (src))
  146. return;
  147. bp.dependencies.Add (src, true);
  148. }
  149. static void ExtractPreviousPageTypeOrMasterTypeDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  150. {
  151. string value = ExtractDirectiveAttribute (baseDirectory, "VirtualPath", names, values);
  152. if (String.IsNullOrEmpty (value))
  153. return;
  154. if (bp.dependencies.ContainsKey (value))
  155. return;
  156. bp.dependencies.Add (value, true);
  157. }
  158. static void ExtractReferenceDependencies (string baseDirectory, CaptureCollection names, CaptureCollection values, TemplateBuildProvider bp)
  159. {
  160. string control = ExtractDirectiveAttribute (baseDirectory, "Control", names, values);
  161. string virtualPath = ExtractDirectiveAttribute (baseDirectory, "VirtualPath", names, values);
  162. string page = ExtractDirectiveAttribute (baseDirectory, "Page", names, values);
  163. bool controlEmpty = String.IsNullOrEmpty (control);
  164. bool virtualPathEmpty = String.IsNullOrEmpty (virtualPath);
  165. bool pageEmpty = String.IsNullOrEmpty (page);
  166. if (controlEmpty && virtualPathEmpty && pageEmpty)
  167. return;
  168. if ((controlEmpty ? 1 : 0) + (virtualPathEmpty ? 1 : 0) + (pageEmpty ? 1 : 0) != 2)
  169. return;
  170. string value;
  171. if (!controlEmpty)
  172. value = control;
  173. else if (!virtualPathEmpty)
  174. value = virtualPath;
  175. else
  176. value = page;
  177. if (bp.dependencies.ContainsKey (value))
  178. return;
  179. bp.dependencies.Add (value, true);
  180. }
  181. internal override IDictionary <string, bool> ExtractDependencies ()
  182. {
  183. if (dependencies != null) {
  184. if (dependencies.Count == 0)
  185. return null;
  186. return dependencies;
  187. }
  188. string vpath = VirtualPath;
  189. if (String.IsNullOrEmpty (vpath))
  190. return null;
  191. VirtualPathProvider vpp = HostingEnvironment.VirtualPathProvider;
  192. if (!vpp.FileExists (vpath))
  193. return null;
  194. VirtualFile vf = vpp.GetFile (vpath);
  195. if (vf == null)
  196. return null;
  197. string input;
  198. using (Stream st = vf.Open ()) {
  199. if (st == null || !st.CanRead)
  200. return null;
  201. using (StreamReader sr = new StreamReader (st, WebEncoding.FileEncoding)) {
  202. input = sr.ReadToEnd ();
  203. }
  204. }
  205. if (String.IsNullOrEmpty (input))
  206. return null;
  207. MatchCollection matches = directiveRegex.Matches (input);
  208. if (matches == null || matches.Count == 0)
  209. return null;
  210. dependencies = new SortedDictionary <string, bool> (StringComparer.InvariantCultureIgnoreCase);
  211. CaptureCollection ccNames;
  212. GroupCollection groups;
  213. string directiveName;
  214. ExtractDirectiveDependencies edd;
  215. string baseDirectory = VirtualPathUtility.GetDirectory (vpath);
  216. foreach (Match match in matches) {
  217. groups = match.Groups;
  218. if (groups.Count < 6)
  219. continue;
  220. ccNames = groups [3].Captures;
  221. directiveName = ccNames [0].Value;
  222. if (!directiveAttributes.TryGetValue (directiveName, out edd))
  223. continue;
  224. edd (baseDirectory, ccNames, groups [5].Captures, this);
  225. }
  226. if (dependencies.Count == 0)
  227. return null;
  228. return dependencies;
  229. }
  230. protected override string GetClassType (BaseCompiler compiler, TemplateParser parser)
  231. {
  232. if (compiler != null)
  233. return compiler.MainClassType;
  234. return null;
  235. }
  236. protected override ICollection GetParserDependencies (TemplateParser parser)
  237. {
  238. if (parser != null)
  239. return parser.Dependencies;
  240. return null;
  241. }
  242. protected override string GetParserLanguage (TemplateParser parser)
  243. {
  244. if (parser != null)
  245. return parser.Language;
  246. return null;
  247. }
  248. protected override string GetCodeBehindSource (TemplateParser parser)
  249. {
  250. if (parser != null) {
  251. string codeBehind = parser.CodeBehindSource;
  252. if (String.IsNullOrEmpty (codeBehind))
  253. return null;
  254. return parser.CodeBehindSource;
  255. }
  256. return null;
  257. }
  258. protected override AspGenerator CreateAspGenerator (TemplateParser parser)
  259. {
  260. if (parser != null)
  261. return new AspGenerator (parser);
  262. return null;
  263. }
  264. protected override List <string> GetReferencedAssemblies (TemplateParser parser)
  265. {
  266. if (parser == null)
  267. return null;
  268. ArrayList al = parser.Assemblies;
  269. if (al == null || al.Count == 0)
  270. return null;
  271. List <string> ret = new List <string> ();
  272. string loc;
  273. foreach (object o in al) {
  274. loc = o as string;
  275. if (loc == null)
  276. continue;
  277. if (ret.Contains (loc))
  278. continue;
  279. ret.Add (loc);
  280. }
  281. return ret;
  282. }
  283. }
  284. }
  285. #endif