PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Main/Base/Project/Src/Internal/Templates/File/FileTemplate.cs

https://github.com/eusebiu/SharpDevelop
C# | 321 lines | 275 code | 40 blank | 6 comment | 27 complexity | d69c752275e5156b2296d39fae876ec8 MD5 | raw file
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Xml;
  10. using ICSharpCode.Core;
  11. using ICSharpCode.SharpDevelop.Project;
  12. namespace ICSharpCode.SharpDevelop.Internal.Templates
  13. {
  14. public class TemplateProperty
  15. {
  16. string name;
  17. string localizedName;
  18. string type;
  19. string category;
  20. string description;
  21. string defaultValue;
  22. public string Name {
  23. get {
  24. return name;
  25. }
  26. }
  27. public string LocalizedName {
  28. get {
  29. return localizedName;
  30. }
  31. }
  32. public string Type {
  33. get {
  34. return type;
  35. }
  36. }
  37. public string Category {
  38. get {
  39. return category;
  40. }
  41. }
  42. public string Description {
  43. get {
  44. return description;
  45. }
  46. }
  47. public string DefaultValue {
  48. get {
  49. return defaultValue;
  50. }
  51. }
  52. public TemplateProperty(XmlElement propertyElement)
  53. {
  54. name = propertyElement.GetAttribute("name");
  55. localizedName= propertyElement.GetAttribute("localizedName");
  56. type = propertyElement.GetAttribute("type");
  57. category = propertyElement.GetAttribute("category");
  58. description = propertyElement.GetAttribute("description");
  59. defaultValue = propertyElement.GetAttribute("defaultValue");
  60. }
  61. }
  62. public class TemplateType
  63. {
  64. string name;
  65. Hashtable pairs = new Hashtable();
  66. public string Name {
  67. get {
  68. return name;
  69. }
  70. }
  71. public Hashtable Pairs {
  72. get {
  73. return pairs;
  74. }
  75. }
  76. public TemplateType(XmlElement enumType)
  77. {
  78. name = enumType.GetAttribute("name");
  79. foreach (XmlElement node in enumType.ChildNodes) {
  80. pairs[node.GetAttribute("name")] = node.GetAttribute("value");
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// This class defines and holds the new file templates.
  86. /// </summary>
  87. public class FileTemplate : IComparable
  88. {
  89. public static List<FileTemplate> FileTemplates = new List<FileTemplate>();
  90. string author = null;
  91. string name = null;
  92. string category = null;
  93. string languagename = null;
  94. string icon = null;
  95. string description = null;
  96. string wizardpath = null;
  97. string defaultName = null;
  98. string subcategory = null;
  99. bool newFileDialogVisible = true;
  100. List<FileDescriptionTemplate> files = new List<FileDescriptionTemplate>();
  101. List<TemplateProperty> properties = new List<TemplateProperty>();
  102. List<TemplateType> customTypes = new List<TemplateType>();
  103. List<ReferenceProjectItem> requiredAssemblyReferences = new List<ReferenceProjectItem>();
  104. XmlElement fileoptions = null;
  105. int IComparable.CompareTo(object other)
  106. {
  107. FileTemplate pt = other as FileTemplate;
  108. if (pt == null) return -1;
  109. int res = category.CompareTo(pt.category);
  110. if (res != 0) return res;
  111. return name.CompareTo(pt.name);
  112. }
  113. public string Author {
  114. get {
  115. return author;
  116. }
  117. }
  118. public string Name {
  119. get {
  120. return name;
  121. }
  122. }
  123. public string Category {
  124. get {
  125. return category;
  126. }
  127. }
  128. public string Subcategory {
  129. get {
  130. return subcategory;
  131. }
  132. }
  133. public string LanguageName {
  134. get {
  135. return languagename;
  136. }
  137. }
  138. public string Icon {
  139. get {
  140. return icon;
  141. }
  142. }
  143. public string Description {
  144. get {
  145. return description;
  146. }
  147. }
  148. public string WizardPath {
  149. get {
  150. return wizardpath;
  151. }
  152. }
  153. public string DefaultName {
  154. get {
  155. return defaultName;
  156. }
  157. }
  158. public XmlElement Fileoptions {
  159. get {
  160. return fileoptions;
  161. }
  162. }
  163. public bool NewFileDialogVisible {
  164. get {
  165. return newFileDialogVisible;
  166. }
  167. }
  168. public List<FileDescriptionTemplate> FileDescriptionTemplates {
  169. get {
  170. return files;
  171. }
  172. }
  173. public List<TemplateProperty> Properties {
  174. get {
  175. return properties;
  176. }
  177. }
  178. public List<TemplateType> CustomTypes {
  179. get {
  180. return customTypes;
  181. }
  182. }
  183. public List<ReferenceProjectItem> RequiredAssemblyReferences {
  184. get { return requiredAssemblyReferences; }
  185. }
  186. public bool HasProperties {
  187. get {
  188. return properties != null && properties.Count > 0;
  189. }
  190. }
  191. public FileTemplate(string filename)
  192. {
  193. XmlDocument doc = new XmlDocument();
  194. doc.Load(filename);
  195. author = doc.DocumentElement.GetAttribute("author");
  196. XmlElement config = doc.DocumentElement["Config"];
  197. name = config.GetAttribute("name");
  198. icon = config.GetAttribute("icon");
  199. category = config.GetAttribute("category");
  200. defaultName = config.GetAttribute("defaultname");
  201. languagename = config.GetAttribute("language");
  202. if (config.HasAttribute("subcategory")) {
  203. subcategory = config.GetAttribute("subcategory");
  204. }
  205. string newFileDialogVisibleAttr = config.GetAttribute("newfiledialogvisible");
  206. if (newFileDialogVisibleAttr != null && newFileDialogVisibleAttr.Length != 0) {
  207. if (newFileDialogVisibleAttr.Equals("false", StringComparison.OrdinalIgnoreCase))
  208. newFileDialogVisible = false;
  209. }
  210. if (doc.DocumentElement["Description"] != null) {
  211. description = doc.DocumentElement["Description"].InnerText;
  212. }
  213. if (config["Wizard"] != null) {
  214. wizardpath = config["Wizard"].Attributes["path"].InnerText;
  215. }
  216. if (doc.DocumentElement["Properties"] != null) {
  217. XmlNodeList propertyList = doc.DocumentElement["Properties"].SelectNodes("Property");
  218. foreach (XmlElement propertyElement in propertyList) {
  219. properties.Add(new TemplateProperty(propertyElement));
  220. }
  221. }
  222. if (doc.DocumentElement["Types"] != null) {
  223. XmlNodeList typeList = doc.DocumentElement["Types"].SelectNodes("Type");
  224. foreach (XmlElement typeElement in typeList) {
  225. customTypes.Add(new TemplateType(typeElement));
  226. }
  227. }
  228. if (doc.DocumentElement["References"] != null) {
  229. XmlNodeList references = doc.DocumentElement["References"].SelectNodes("Reference");
  230. foreach (XmlElement reference in references) {
  231. if (!reference.HasAttribute("include"))
  232. throw new InvalidDataException("Reference without 'include' attribute!");
  233. ReferenceProjectItem item = new ReferenceProjectItem(null, reference.GetAttribute("include"));
  234. item.SetMetadata("HintPath", reference.GetAttribute("hintPath"));
  235. var requiredTargetFramework = reference.GetElementsByTagName("RequiredTargetFramework").OfType<XmlElement>().FirstOrDefault();
  236. if (requiredTargetFramework != null) {
  237. item.SetMetadata("RequiredTargetFramework", requiredTargetFramework.Value);
  238. }
  239. requiredAssemblyReferences.Add(item);
  240. }
  241. }
  242. fileoptions = doc.DocumentElement["AdditionalOptions"];
  243. doc.DocumentElement.SetAttribute("fileName", filename); // used for template loading warnings
  244. // load the files
  245. XmlElement files = doc.DocumentElement["Files"];
  246. XmlNodeList nodes = files.ChildNodes;
  247. foreach (XmlNode filenode in nodes) {
  248. if (filenode is XmlElement) {
  249. this.files.Add(new FileDescriptionTemplate((XmlElement)filenode, Path.GetDirectoryName(filename)));
  250. }
  251. }
  252. }
  253. public static void UpdateTemplates()
  254. {
  255. string dataTemplateDir = Path.Combine(PropertyService.DataDirectory, "templates", "file");
  256. List<string> files = FileUtility.SearchDirectory(dataTemplateDir, "*.xft");
  257. foreach (string templateDirectory in AddInTree.BuildItems<string>(ProjectTemplate.TemplatePath, null, false)) {
  258. if (!Directory.Exists(templateDirectory))
  259. Directory.CreateDirectory(templateDirectory);
  260. files.AddRange(FileUtility.SearchDirectory(templateDirectory, "*.xft"));
  261. }
  262. FileTemplates.Clear();
  263. foreach (string file in files) {
  264. try {
  265. FileTemplates.Add(new FileTemplate(file));
  266. } catch (XmlException ex) {
  267. MessageService.ShowError("Error loading template file " + file + ":\n" + ex.Message);
  268. } catch (TemplateLoadException ex) {
  269. MessageService.ShowError("Error loading template file " + file + ":\n" + ex.ToString());
  270. } catch(Exception e) {
  271. MessageService.ShowException(e, "Error loading template file " + file + ".");
  272. }
  273. }
  274. FileTemplates.Sort();
  275. }
  276. static FileTemplate()
  277. {
  278. UpdateTemplates();
  279. }
  280. }
  281. }