PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/SmartCodeGenerator.WPF/Uility/TextTemplatingEngineHost.cs

#
C# | 313 lines | 173 code | 5 blank | 135 comment | 16 complexity | b6eaa77a313c96f03103d3cc68a49d0e MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using Microsoft.VisualStudio.TextTemplating;
  8. using System.CodeDom.Compiler;
  9. using System.IO;
  10. namespace SmartCodeGenerator
  11. {
  12. public class TextTemplatingEngineHost : ITextTemplatingEngineHost, ITextTemplatingSessionHost
  13. {
  14. #region ITextTemplatingEngineHost
  15. //the path and file name of the text template that is being processed
  16. //---------------------------------------------------------------------
  17. internal string TemplateFileValue;
  18. public string TemplateFile
  19. {
  20. get { return TemplateFileValue; }
  21. }
  22. //This will be the extension of the generated text output file.
  23. //The host can provide a default by setting the value of the field here.
  24. //The engine can change this value based on the optional output directive
  25. //if the user specifies it in the text template.
  26. //---------------------------------------------------------------------
  27. private string fileExtensionValue = ".txt";
  28. public string FileExtension
  29. {
  30. get { return fileExtensionValue; }
  31. }
  32. //This will be the encoding of the generated text output file.
  33. //The host can provide a default by setting the value of the field here.
  34. //The engine can change this value based on the optional output directive
  35. //if the user specifies it in the text template.
  36. //---------------------------------------------------------------------
  37. private Encoding fileEncodingValue = Encoding.UTF8;
  38. public Encoding FileEncoding
  39. {
  40. get { return fileEncodingValue; }
  41. }
  42. //These are the errors that occur when the engine processes a template.
  43. //The engine passes the errors to the host when it is done processing,
  44. //and the host can decide how to display them. For example, the host
  45. //can display the errors in the UI or write them to a file.
  46. //---------------------------------------------------------------------
  47. private CompilerErrorCollection errorsValue;
  48. public CompilerErrorCollection Errors
  49. {
  50. get { return errorsValue; }
  51. }
  52. //The host can provide standard assembly references.
  53. //The engine will use these references when compiling and
  54. //executing the generated transformation class.
  55. //--------------------------------------------------------------
  56. public IList<string> StandardAssemblyReferences
  57. {
  58. get
  59. {
  60. return new string[]
  61. {
  62. //If this host searches standard paths and the GAC,
  63. //we can specify the assembly name like this.
  64. //---------------------------------------------------------
  65. //"System"
  66. //Because this host only resolves assemblies from the
  67. //fully qualified path and name of the assembly,
  68. //this is a quick way to get the code to give us the
  69. //fully qualified path and name of the System assembly.
  70. //---------------------------------------------------------
  71. typeof(System.Uri).Assembly.Location,
  72. typeof(System.Data.DataTable).Assembly.Location,
  73. typeof(System.Xml.XmlDocument ).Assembly.Location,
  74. typeof(System.Text.StringBuilder ).Assembly.Location,
  75. typeof(Parameter).Assembly.Location
  76. };
  77. }
  78. }
  79. //The host can provide standard imports or using statements.
  80. //The engine will add these statements to the generated
  81. //transformation class.
  82. //--------------------------------------------------------------
  83. public IList<string> StandardImports
  84. {
  85. get
  86. {
  87. return new string[]
  88. {
  89. "System",
  90. "System.Data",
  91. "System.Xml",
  92. "System.Text",
  93. "SmartCodeGenerator"
  94. };
  95. }
  96. }
  97. //The engine calls this method based on the optional include directive
  98. //if the user has specified it in the text template.
  99. //This method can be called 0, 1, or more times.
  100. //---------------------------------------------------------------------
  101. //The included text is returned in the context parameter.
  102. //If the host searches the registry for the location of include files,
  103. //or if the host searches multiple locations by default, the host can
  104. //return the final path of the include file in the location parameter.
  105. //---------------------------------------------------------------------
  106. public bool LoadIncludeText(string requestFileName, out string content, out string location)
  107. {
  108. content = System.String.Empty;
  109. location = System.String.Empty;
  110. //If the argument is the fully qualified path of an existing file,
  111. //then we are done.
  112. //----------------------------------------------------------------
  113. if (File.Exists(requestFileName))
  114. {
  115. content = File.ReadAllText(requestFileName);
  116. return true;
  117. }
  118. //This can be customized to search specific paths for the file.
  119. //This can be customized to accept paths to search as command line
  120. //arguments.
  121. //----------------------------------------------------------------
  122. else
  123. {
  124. return false;
  125. }
  126. }
  127. //Called by the Engine to enquire about
  128. //the processing options you require.
  129. //If you recognize that option, return an
  130. //appropriate value.
  131. //Otherwise, pass back NULL.
  132. //--------------------------------------------------------------------
  133. public object GetHostOption(string optionName)
  134. {
  135. object returnObject;
  136. switch (optionName)
  137. {
  138. case "CacheAssemblies":
  139. returnObject = true;
  140. break;
  141. default:
  142. returnObject = null;
  143. break;
  144. }
  145. return returnObject;
  146. }
  147. //The engine calls this method to resolve assembly references used in
  148. //the generated transformation class project and for the optional
  149. //assembly directive if the user has specified it in the text template.
  150. //This method can be called 0, 1, or more times.
  151. //---------------------------------------------------------------------
  152. public string ResolveAssemblyReference(string assemblyReference)
  153. {
  154. //If the argument is the fully qualified path of an existing file,
  155. //then we are done. (This does not do any work.)
  156. //----------------------------------------------------------------
  157. if (File.Exists(assemblyReference))
  158. {
  159. return assemblyReference;
  160. }
  161. //Maybe the assembly is in the same folder as the text template that
  162. //called the directive.
  163. //----------------------------------------------------------------
  164. string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), assemblyReference);
  165. if (File.Exists(candidate))
  166. {
  167. return candidate;
  168. }
  169. //This can be customized to search specific paths for the file
  170. //or to search the GAC.
  171. //----------------------------------------------------------------
  172. //This can be customized to accept paths to search as command line
  173. //arguments.
  174. //----------------------------------------------------------------
  175. //If we cannot do better, return the original file name.
  176. return "";
  177. }
  178. //The engine calls this method based on the directives the user has
  179. //specified in the text template.
  180. //This method can be called 0, 1, or more times.
  181. //---------------------------------------------------------------------
  182. public Type ResolveDirectiveProcessor(string processorName)
  183. {
  184. //This host will not resolve any specific processors.
  185. //Check the processor name, and if it is the name of a processor the
  186. //host wants to support, return the type of the processor.
  187. //---------------------------------------------------------------------
  188. if (string.Compare(processorName, "XYZ", StringComparison.OrdinalIgnoreCase) == 0)
  189. {
  190. //return typeof();
  191. }
  192. //This can be customized to search specific paths for the file
  193. //or to search the GAC
  194. //If the directive processor cannot be found, throw an error.
  195. throw new Exception("Directive Processor not found");
  196. }
  197. //A directive processor can call this method if a file name does not
  198. //have a path.
  199. //The host can attempt to provide path information by searching
  200. //specific paths for the file and returning the file and path if found.
  201. //This method can be called 0, 1, or more times.
  202. //---------------------------------------------------------------------
  203. public string ResolvePath(string fileName)
  204. {
  205. if (fileName == null)
  206. {
  207. throw new ArgumentNullException("the file name cannot be null");
  208. }
  209. //If the argument is the fully qualified path of an existing file,
  210. //then we are done
  211. //----------------------------------------------------------------
  212. if (File.Exists(fileName))
  213. {
  214. return fileName;
  215. }
  216. //Maybe the file is in the same folder as the text template that
  217. //called the directive.
  218. //----------------------------------------------------------------
  219. string candidate = Path.Combine(Path.GetDirectoryName(this.TemplateFile), fileName);
  220. if (File.Exists(candidate))
  221. {
  222. return candidate;
  223. }
  224. //Look more places.
  225. //----------------------------------------------------------------
  226. //More code can go here...
  227. //If we cannot do better, return the original file name.
  228. return fileName;
  229. }
  230. //If a call to a directive in a text template does not provide a value
  231. //for a required parameter, the directive processor can try to get it
  232. //from the host by calling this method.
  233. //This method can be called 0, 1, or more times.
  234. //---------------------------------------------------------------------
  235. public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
  236. {
  237. if (directiveId == null)
  238. {
  239. throw new ArgumentNullException("the directiveId cannot be null");
  240. }
  241. if (processorName == null)
  242. {
  243. throw new ArgumentNullException("the processorName cannot be null");
  244. }
  245. if (parameterName == null)
  246. {
  247. throw new ArgumentNullException("the parameterName cannot be null");
  248. }
  249. //Code to provide "hard-coded" parameter values goes here.
  250. //This code depends on the directive processors this host will interact with.
  251. //If we cannot do better, return the empty string.
  252. return String.Empty;
  253. }
  254. //The engine calls this method to change the extension of the
  255. //generated text output file based on the optional output directive
  256. //if the user specifies it in the text template.
  257. //---------------------------------------------------------------------
  258. public void SetFileExtension(string extension)
  259. {
  260. //The parameter extension has a '.' in front of it already.
  261. //--------------------------------------------------------
  262. fileExtensionValue = extension;
  263. }
  264. //The engine calls this method to change the encoding of the
  265. //generated text output file based on the optional output directive
  266. //if the user specifies it in the text template.
  267. //----------------------------------------------------------------------
  268. public void SetOutputEncoding(System.Text.Encoding encoding, bool fromOutputDirective)
  269. {
  270. fileEncodingValue = encoding;
  271. }
  272. //The engine calls this method when it is done processing a text
  273. //template to pass any errors that occurred to the host.
  274. //The host can decide how to display them.
  275. //---------------------------------------------------------------------
  276. public void LogErrors(CompilerErrorCollection errors)
  277. {
  278. errorsValue = errors;
  279. }
  280. //This is the application domain that is used to compile and run
  281. //the generated transformation class to create the generated text output.
  282. //----------------------------------------------------------------------
  283. public AppDomain ProvideTemplatingAppDomain(string content)
  284. {
  285. //return AppDomain.CurrentDomain;
  286. return AppDomain.CreateDomain("Generation App Domain");
  287. }
  288. #endregion
  289. #region ITextTemplatingSessionHost
  290. /// <summary>
  291. ///
  292. /// </summary>
  293. /// <returns></returns>
  294. public ITextTemplatingSession CreateSession()
  295. {
  296. return Session;
  297. }
  298. /// <summary>
  299. ///
  300. /// </summary>
  301. public ITextTemplatingSession Session
  302. {
  303. get;
  304. set;
  305. }
  306. #endregion
  307. }
  308. }