TemplateBuildProvider.cs 11 KB

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