Esempio n. 1
0
        static void Main(string[] args)
        {
            try
            {
                string path  = string.Empty;
                string xpath = string.Empty;

                Console.OutputEncoding = utf8;
                //removed input encoding utf8 because character like "é" are treated like "\0" when entered in cmd and in windows terminal.
                //Console.InputEncoding = utf8;

                NamespaceHelper.Instance.LoadNamespaces();

                if (args.Length > 0)
                {
                    path = args[0];
                    _startWithParameter = true;
                }

                DisplayHelp();

                #region "file mode"
                while (true)
                {
                    CConsole.Write("File path > ", MODE_COLOR);

                    if (_startWithParameter)
                    {
                        CConsole.WriteLine(path);
                        _startWithParameter = false;
                    }
                    else
                    {
                        path = Console.ReadLine();
                    }

                    path = path.Trim(CommandHelper.TRIMMABLE.ToCharArray());

                    if (string.IsNullOrWhiteSpace(path))
                    {
                        continue;
                    }

                    if (CommandHelper.IsExitKeyword(path) || CommandHelper.IsExitAllKeyword(path))
                    {
                        break;
                    }

                    if (CommandHelper.IsHelpKeyword(path))
                    {
                        DisplayHelp();
                        continue;
                    }

                    if (CommandHelper.IsNamespacesCommand(path))
                    {
                        EExitMode nsExitMode = new NamespaceMgtMode().Start();
                        //if (nsExitMode == EExitMode.ExitMode) continue;
                        if (nsExitMode == EExitMode.ExitApplication)
                        {
                            break;
                        }

                        //use continue for EExitMode.None (is usefull ?) and EExitMode.ExitMode
                        continue;
                    }

                    if (!File.Exists(path))
                    {
                        CConsole.WriteLine($"Path '{path}' doesn't exists!", ConsoleColor.Red);
                        CConsole.WriteLine();
                        continue;
                    }

                    try
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(path);

                        Console.WriteLine();
                        XPathMode mode     = new XPathMode(doc);
                        EExitMode exitMode = mode.Start();

                        if (exitMode == EExitMode.ExitApplication)
                        {
                            break;
                        }
                    }
                    catch (XmlException ex)
                    {
                        CConsole.WriteLine("Error when loading file!", ConsoleColor.Red);
                        CConsole.WriteLine($"Message: {ex.Message}", ConsoleColor.Red);
                        Console.WriteLine();
                    }
                }
                #endregion "file mode"
            }
            catch (Exception ex)
            {
                CConsole.WriteLine(ex.ToString(), ConsoleColor.Red);
                CConsole.WriteLine();
                CConsole.WriteLine("Press a key to exit ...");
                Console.ReadKey();
            }
        }
Esempio n. 2
0
        public EExitMode Start()
        {
            Console.WriteLine();
            DisplayHelp();

            while (true)
            {
                string prompt = "Namespaces > ";
                if (_hasModificationsNotSaved)
                {
                    prompt = "Namespaces * > ";
                }

                CConsole.Write(prompt, MODE_COLOR);
                command = Console.ReadLine();

                if (string.IsNullOrWhiteSpace(command))
                {
                    continue;
                }

                if (CommandHelper.IsExitAllKeyword(command))
                {
                    _exitMode = EExitMode.ExitApplication;
                    break;
                }

                if (CommandHelper.IsExitKeyword(command))
                {
                    _exitMode = EExitMode.ExitMode;
                    break;
                }

                if (CommandHelper.IsHelpKeyword(command))
                {
                    DisplayHelp();
                    continue;
                }

                if (CommandHelper.IsNsDisplayCommand(command))
                {
                    DisplayCustomNamespaces();
                    continue;
                }

                if (CommandHelper.IsNsAddCommand(command))
                {
                    AddNamespace(command);
                    continue;
                }

                if (CommandHelper.IsNsDeleteCommand(command))
                {
                    DeleteNamespace(command);
                    continue;
                }

                if (CommandHelper.IsNsSaveCommand(command))
                {
                    SaveNewNamespaces();
                    continue;
                }
            }

            return(_exitMode);
        }
Esempio n. 3
0
        public EExitMode Start()
        {
            Console.WriteLine("DocumentElement:");
            Console.WriteLine(_document.DocumentElement.OuterXml);
            Console.WriteLine();

            AddDocumentNamespaces();
            Console.WriteLine();

            string nsPrefix = _nsManager.LookupPrefix(_document.DocumentElement.NamespaceURI);

            if (string.IsNullOrWhiteSpace(nsPrefix))
            {
                DisplayDefaultNamespaceError(_document.DocumentElement.NamespaceURI);
                //return EExitMode.ExitMode;
            }
            else
            {
                DisplayDefaultNamespace(_document.DocumentElement.NamespaceURI, nsPrefix);
            }

            DisplayHelp();

            XmlNodeList nodeList = null;

            while (true)
            {
                string prompt = "XPath";
                _activeColor = XPATH_MODE_COLOR;
                _workNode    = _document.DocumentElement;

                if (_selectionStatus == ESelectionModeStatus.In)
                {
                    prompt       = "XPath (selection)";
                    _activeColor = SELECTION_MODE_COLOR;
                    _workNode    = _selectedNode;
                }

                if (Settings.Default.DisplayCurrentNode)
                {
                    CConsole.Write($"{prompt} [", _activeColor);
                    CConsole.Write(FormatNodeName(_workNode));
                    CConsole.Write("] > ", _activeColor);
                }
                else
                {
                    CConsole.Write($"{prompt} > ", _activeColor);
                }

                command = Console.ReadLine();

                //Gestion de l'entrée utilisateur et des commandes
                if (string.IsNullOrWhiteSpace(command))
                {
                    continue;
                }
                if (CommandHelper.IsExitAllKeyword(command))
                {
                    _exitMode = EExitMode.ExitApplication;
                    break;
                }

                if (CommandHelper.IsExitKeyword(command))
                {
                    if (_selectionStatus == ESelectionModeStatus.In)
                    {
                        _selectionStatus = ESelectionModeStatus.None;
                        continue;
                    }

                    _exitMode = EExitMode.ExitMode;
                    break;
                }

                if (CommandHelper.IsHelpKeyword(command))
                {
                    DisplayHelp();
                    continue;
                }

                if (CommandHelper.IsShowCommand(command))
                {
                    DisplayNode(_workNode);
                    CConsole.WriteLine();
                    continue;
                }

                if (CommandHelper.IsSelectCommand(command))
                {
                    command = command.Replace(CommandHelper.SELECT_COMMAND, "").TrimStart();
                    if (string.IsNullOrWhiteSpace(command))
                    {
                        CConsole.WriteLine($"Usage: {CommandHelper.SELECT_COMMAND} <xpath>", ConsoleColor.Yellow);
                        continue;
                    }
                    if (command == "..")
                    {
                        if (_workNode != _document.DocumentElement)
                        {
                            _selectedNode = _selectedNode.ParentNode;
                            DisplayNode(_selectedNode);
                        }
                        else
                        {
                            CConsole.WriteLine("Can not select parent of the document node", ConsoleColor.Yellow);
                        }
                        continue;
                    }
                    _selectionStatus = ESelectionModeStatus.Entering;
                }

                if (CommandHelper.IsNodesCommand(command))
                {
                    DisplayChildNodeList(_workNode);
                    continue;
                }

                if (CommandHelper.IsAttributesCommand(command))
                {
                    DisplayAttributesList(_workNode);
                    continue;
                }

                try
                {
                    nodeList = _workNode.SelectNodes(command, _nsManager);

                    if (_selectionStatus == ESelectionModeStatus.Entering && nodeList.Count != 1)
                    {
                        CConsole.WriteLine("Exiting selection mode. You must select only 1 node!", ConsoleColor.Yellow);
                        Console.WriteLine();
                        _selectionStatus = ESelectionModeStatus.None;
                    }

                    DisplayNodeList(nodeList, command);

                    if (_selectionStatus == ESelectionModeStatus.Entering && nodeList.Count > 0)
                    {
                        _selectedNode    = nodeList[0];
                        _selectionStatus = ESelectionModeStatus.In;
                    }
                }
                catch (XPathException ex)
                {
                    CConsole.Write("Error with the xpath expression: '", ConsoleColor.Red);
                    CConsole.Write(command);
                    CConsole.WriteLine("'!", ConsoleColor.Red);
                    CConsole.WriteLine($"Message: {ex.Message}", ConsoleColor.Red);
                    Console.WriteLine();
                }
            } //end while

            return(_exitMode);
        }