Exemple #1
0
        private void OnOpen(object sender, EventArgs e)
        {
            DeleteAllFilesAndNodesFromMemory();
            var dialog = new OpenFileDialog()
            {
                Filter = @"Yaml files (*.yaml)|*.yaml|All files (*.*)|*.*", DefaultExt = "yaml"
            };

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;

                System.Diagnostics.Trace.WriteLine($"Filename: {dialog.FileName}");
                Directory.SetCurrentDirectory(Path.GetDirectoryName(dialog.FileName) ?? "");

                MyYamlFileFactory.CreateMyYamlFile(dialog.FileName);//Loads the file

                //Switches the configuration file to first place
                MyYamlFile configuration = MyYamlFile.all_files[MyYamlFile.all_files.Count - 1];
                int        counter       = MyYamlFile.all_files.Count - 1;
                while (counter > -1)
                {
                    if (counter == 0)
                    {
                        MyYamlFile.all_files[counter] = configuration;
                    }
                    else
                    {
                        MyYamlFile.all_files[counter] = MyYamlFile.all_files[counter - 1];
                    }

                    counter--;
                }

                PopulateTreeView(mainTreeView, Path.GetDirectoryName(dialog.FileName));
            }

            Cursor.Current = Cursors.Default;
        }
Exemple #2
0
        /// <summary>
        /// Saves the files in a temporary folder and loads them into a YamlStream to validate the changes made
        /// </summary>
        private void toolStripButton_Validate_Click(object sender, EventArgs e)
        {
            if (mainTreeView.Nodes.Count == 0)
            {
                Logger.Instance.WriteLine("There are no files open, cannot validate changes.");
            }
            else
            {
                bool errorfound = false;

                string temporarydirectory = GetTemporaryDirectory("YamlEditor");
                Directory.CreateDirectory(temporarydirectory);                  //creates a YamlEditor folder in the operating systems temporary foder
                string basedirectory = MyYamlFile.all_files[0].directory;       //the first file is always the configuration.yaml
                basedirectory = basedirectory.Remove(basedirectory.Length - 1); //removes the '/' at the end of the directory

                MyYamlFile.SaveAllFilesInNewDirectory(basedirectory, temporarydirectory);

                List <String> folders = new List <string>(); //creates a list of subfolders and an empty folder to represent the base folder
                folders.Add("");
                foreach (var subfolder in Directory.GetDirectories(temporarydirectory))
                {
                    folders.Add(subfolder);
                }

                foreach (string subfolder in folders)
                {
                    string path = subfolder;
                    if (subfolder == "")
                    {
                        path = temporarydirectory + "\\" + subfolder;
                    }

                    DirectoryInfo d     = new DirectoryInfo(Path.GetFullPath(path)); //Assuming Test is your Folder
                    FileInfo[]    Files = d.GetFiles("*.yaml");                      //Getting Text files
                    foreach (FileInfo file in Files)
                    {
                        YamlStream yaml = new YamlStream();
                        try
                        {
                            using (var stream = new StreamReader(file.FullName))
                            {
                                yaml.Load(stream);
                            }
                            Logger.Instance.WriteLine($"Validating:  '\"{file.FullName}\"'.");
                        }
                        catch (Exception exception)
                        {
                            Logger.Instance.WriteLine(exception.Message + " In file '" + file.FullName + "'");
                            errorfound = true;
                        }
                    }
                }

                Directory.Delete(temporarydirectory, true); //deletes the YamlEditor folder created in the operating systems temporary foder

                if (errorfound)
                {
                    Logger.Instance.WriteLine("VALIDATION: Errors were found while validating the files.");
                }
                else
                {
                    Logger.Instance.WriteLine("VALIDATION: No errors were found while validating the files.");
                }
            }
        }
Exemple #3
0
        private void PopulateNodes(TreeNode parent, MyYamlNode yamlnode)
        {
            if (yamlnode is MyYamlScalarNode)
            {
                MyYamlScalarNode yamlnodeAsScalar = (MyYamlScalarNode)yamlnode;
                if (yamlnode.name == "")
                {
                    parent.Nodes.Add(CreateTreeNode(yamlnodeAsScalar.value, yamlnode));
                }
                else
                {
                    parent.Nodes.Add(CreateTreeNode(yamlnode.name, yamlnode));
                }
            }
            else if (yamlnode is MyYamlMappingNode)
            {
                TreeNode new_parent = new TreeNode();

                if (yamlnode.name == "")
                {
                    try
                    {
                        MyYamlMappingNode parentAsMyYamlNode = (MyYamlMappingNode)parent.Tag;
                        new_parent = CreateTreeNode(parent.Name, yamlnode);
                        parent.Nodes.Add(new_parent);
                    }
                    catch (Exception) { }

                    try
                    {
                        MyYamlSequenceNode parentAsMyYamlNode = (MyYamlSequenceNode)parent.Tag;
                        new_parent = CreateTreeNode(parent.Name, yamlnode);
                        parent.Nodes.Add(new_parent);
                    }
                    catch (Exception) { }

                    try
                    {
                        MyYamlFile parentAsMyYamlNode = (MyYamlFile)parent.Tag;
                        new_parent = CreateTreeNode(parent.Name, yamlnode);
                        parent.Nodes.Add(new_parent);
                    }
                    catch (Exception) { }
                }
                else if (yamlnode.name == "-")
                {
                    new_parent = CreateTreeNode(yamlnode.name, yamlnode);
                    parent.Nodes.Add(new_parent);
                }
                else
                {
                    new_parent = CreateTreeNode(yamlnode.name, yamlnode);
                    parent.Nodes.Add(new_parent);
                }

                foreach (MyYamlNode child in yamlnode.nodes)
                {
                    PopulateNodes(new_parent, child);
                }
            }
            else if (yamlnode is MyYamlSequenceNode)
            {
                TreeNode new_parent = CreateTreeNode(yamlnode.name, yamlnode);
                parent.Nodes.Add(new_parent);
                foreach (MyYamlNode child in yamlnode.nodes)
                {
                    PopulateNodes(new_parent, child);
                }
            }
        }