Example #1
0
        public void SaveFile(FileDescriptionTemplate newfile, string content, string binaryFileName)
        {
            string parsedFileName = StringParser.Parse(newfile.Name);
            // Parse twice so that tags used in included standard header are parsed
            string parsedContent = StringParser.Parse(StringParser.Parse(content));

            if (parsedContent != null)
            {
                if (EditorControlService.GlobalOptions.IndentationString != "\t")
                {
                    parsedContent = parsedContent.Replace("\t", EditorControlService.GlobalOptions.IndentationString);
                }
            }


            // when newFile.Name is "${Path}/${FileName}", there might be a useless '/' in front of the file name
            // if the file is created when no project is opened. So we remove single '/' or '\', but not double
            // '\\' (project is saved on network share).
            if (parsedFileName.StartsWith("/") && !parsedFileName.StartsWith("//") ||
                parsedFileName.StartsWith("\\") && !parsedFileName.StartsWith("\\\\"))
            {
                parsedFileName = parsedFileName.Substring(1);
            }

            if (newfile.IsDependentFile && Path.IsPathRooted(parsedFileName))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
                if (!String.IsNullOrEmpty(binaryFileName))
                {
                    File.Copy(binaryFileName, parsedFileName);
                }
                else
                {
                    File.WriteAllText(parsedFileName, parsedContent, ParserService.DefaultFileEncoding);
                }
                ParserService.ParseFile(parsedFileName, new StringTextBuffer(parsedContent));
            }
            else
            {
                if (!String.IsNullOrEmpty(binaryFileName))
                {
                    LoggingService.Warn("binary file was skipped");
                    return;
                }
                IViewContent viewContent = FileService.NewFile(Path.GetFileName(parsedFileName), parsedContent);
                if (viewContent == null)
                {
                    return;
                }
                if (Path.IsPathRooted(parsedFileName))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
                    viewContent.PrimaryFile.SaveToDisk(parsedFileName);
                }
            }
            createdFiles.Add(new KeyValuePair <string, FileDescriptionTemplate>(parsedFileName, newfile));
        }
Example #2
0
        public override void Load(XmlElement filenode)
        {
            //pull out the main area
            XmlElement fileText = filenode ["FileText"];

            if (fileText == null)
            {
                throw new InvalidOperationException("Invalid ASP.NET template: FileText element not found.");
            }
            content = fileText.InnerText;

            //collect all of the code substitution areas
            foreach (XmlNode xn in filenode.GetElementsByTagName("CodeTranslationFile"))
            {
                XmlElement xe = xn as XmlElement;
                if (xe == null)
                {
                    continue;
                }

                string name = xe.GetAttribute("TagName");

                if ((name == null) || (name.Length == 0))
                {
                    throw new InvalidOperationException("Invalid ASP.NET template: CodeTranslationFile must have valid TagName.");
                }

                //This is overzealous, but better safe than sorry
                char [] forbiddenChars = "`-=[];'#,./\\¬!\"£$%^&*()_+{}:@~|<>?".ToCharArray();
                if (name.IndexOfAny(forbiddenChars) > -1)
                {
                    throw new InvalidOperationException("Invalid ASP.NET template: TagName must be alphanumeric.");
                }

                if (codeAreas.ContainsKey(name))
                {
                    throw new InvalidOperationException("Invalid ASP.NET template: all TagNames must be unique within the AspNetFile.");
                }

                CodeTranslationFileDescriptionTemplate templ =
                    FileDescriptionTemplate.CreateTemplate(xe) as CodeTranslationFileDescriptionTemplate;

                if (templ == null)
                {
                    throw new InvalidOperationException("Invalid ASP.NET template: invalid CodeTranslationFile.");
                }

                codeAreas [name] = templ;
            }

            base.Load(filenode);
        }
        static void CreateFileFromTemplate(Project project, SolutionItem policyItem, FilePath templateSourceDirectory, string fileTemplateName)
        {
            string templateFileName = templateSourceDirectory.Combine(fileTemplateName + ".xft.xml");

            using (Stream stream = File.OpenRead(templateFileName)) {
                var document = new XmlDocument();
                document.Load(stream);

                foreach (XmlElement templateElement in document.DocumentElement["TemplateFiles"].ChildNodes.OfType <XmlElement> ())
                {
                    var template = FileDescriptionTemplate.CreateTemplate(templateElement, templateSourceDirectory);
                    template.AddToProject(policyItem, project, "C#", project.BaseDirectory, null);
                }
            }
        }
Example #4
0
        public void SaveFile(FileDescriptionTemplate newfile, string content, byte[] binaryContent)
        {
            string parsedFileName = StringParser.Parse(newfile.Name);
            // Parse twice so that tags used in included standard header are parsed
            string parsedContent = StringParser.Parse(StringParser.Parse(content));

            // when newFile.Name is "${Path}/${FileName}", there might be a useless '/' in front of the file name
            // if the file is created when no project is opened. So we remove single '/' or '\', but not double
            // '\\' (project is saved on network share).
            if (parsedFileName.StartsWith("/") && !parsedFileName.StartsWith("//") ||
                parsedFileName.StartsWith("\\") && !parsedFileName.StartsWith("\\\\"))
            {
                parsedFileName = parsedFileName.Substring(1);
            }

            if (newfile.IsDependentFile && Path.IsPathRooted(parsedFileName))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
                if (binaryContent != null)
                {
                    File.WriteAllBytes(parsedFileName, binaryContent);
                }
                else
                {
                    File.WriteAllText(parsedFileName, parsedContent, ParserService.DefaultFileEncoding);
                }
                ParserService.ParseFile(parsedFileName, parsedContent);
            }
            else
            {
                if (binaryContent != null)
                {
                    LoggingService.Warn("binary file was skipped");
                    return;
                }
                IWorkbenchWindow window = FileService.NewFile(Path.GetFileName(parsedFileName), StringParser.Parse(newfile.Language), parsedContent);
                if (window == null)
                {
                    return;
                }
                if (Path.IsPathRooted(parsedFileName))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(parsedFileName));
                    window.ViewContent.Save(parsedFileName);
                }
            }
            createdFiles.Add(new KeyValuePair <string, FileDescriptionTemplate>(parsedFileName, newfile));
        }
            public FileTemplate(string filename)
            {
                XmlDocument doc = new XmlDocument();

                doc.Load(filename);

                XmlElement config = doc.DocumentElement["TemplateConfiguration"];

                originator   = doc.DocumentElement.Attributes["Originator"].InnerText;
                created      = doc.DocumentElement.Attributes["Created"].InnerText;
                lastmodified = doc.DocumentElement.Attributes["LastModified"].InnerText;

                name         = config["Name"].InnerText;
                category     = config["Category"].InnerText;
                languagename = config["LanguageName"].InnerText;

                if (config["Description"] != null)
                {
                    description = config["Description"].InnerText;
                }

                if (config["Icon"] != null)
                {
                    icon = config["Icon"].InnerText;
                }

                if (config["Wizard"] != null)
                {
                    wizardpath = config["Wizard"].Attributes["path"].InnerText;
                }

                fileoptions = doc.DocumentElement["FileOptions"];

                // load the files
                XmlElement  files = doc.DocumentElement["TemplateFiles"];
                XmlNodeList nodes = files.ChildNodes;

                foreach (XmlElement filenode in nodes)
                {
                    FileDescriptionTemplate template = new FileDescriptionTemplate(filenode.Attributes["DefaultName"].InnerText + filenode.Attributes["DefaultExtension"].InnerText, filenode.InnerText);
                    this.files.Add(template);
                }
            }
Example #6
0
        public override void Load(XmlElement filenode, FilePath baseDirectory)
        {
            foreach (XmlNode node in filenode.ChildNodes)
            {
                XmlElement elem = node as XmlElement;
                if (elem == null)
                {
                    continue;
                }

                if (elem.Name == "SteticTemplate")
                {
                    if (steticTemplate != null)
                    {
                        throw new InvalidOperationException("Widget templates can't contain more than one SteticTemplate element");
                    }
                    steticTemplate = elem;
                }
                else if (fileTemplate == null)
                {
                    fileTemplate = FileDescriptionTemplate.CreateTemplate(elem, baseDirectory) as SingleFileDescriptionTemplate;
                    if (fileTemplate == null)
                    {
                        throw new InvalidOperationException("Widget templates can only contain single-file and stetic templates.");
                    }
                }
            }
            if (fileTemplate == null)
            {
                throw new InvalidOperationException("File template not found in widget template.");
            }
            if (steticTemplate == null)
            {
                throw new InvalidOperationException("Stetic template not found in widget template.");
            }
        }
			public FileTemplate(string filename)
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(filename);
			
				XmlElement config = doc.DocumentElement["TemplateConfiguration"];
			
				originator   = doc.DocumentElement.Attributes["Originator"].InnerText;
				created      = doc.DocumentElement.Attributes["Created"].InnerText;
				lastmodified = doc.DocumentElement.Attributes["LastModified"].InnerText;
			
				name         = config["Name"].InnerText;
				category     = config["Category"].InnerText;
				languagename = config["LanguageName"].InnerText;
			
				if (config["Description"] != null) 
				{
					description  = config["Description"].InnerText;
				}
			
				if (config["Icon"] != null) 
				{
					icon         = config["Icon"].InnerText;
				}
			
				if (config["Wizard"] != null) 
				{
					wizardpath = config["Wizard"].Attributes["path"].InnerText;
				}
			
				fileoptions = doc.DocumentElement["FileOptions"];
			
				// load the files
				XmlElement files  = doc.DocumentElement["TemplateFiles"];
				XmlNodeList nodes = files.ChildNodes;
				foreach (XmlElement filenode in nodes) 
				{
					FileDescriptionTemplate template = new FileDescriptionTemplate(filenode.Attributes["DefaultName"].InnerText + filenode.Attributes["DefaultExtension"].InnerText, filenode.InnerText);
					this.files.Add(template);
				}
			}