TemplateBuildProvider.cs 11 KB

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