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