示例#1
0
        private void AddDocumentNamespaces()
        {
            var attributes = _document.DocumentElement.Attributes;

            foreach (XmlAttribute attr in attributes)
            {
                if (!attr.Name.StartsWith(XML_NAMESPACE))
                {
                    continue;
                }

                string prefix = attr.Name.Replace(XML_NAMESPACE, "");
                if (string.IsNullOrWhiteSpace(prefix))
                {
                    continue;
                }

                _nsManager.AddNamespace(prefix, attr.Value);
                CConsole.Write("Adding namespace '");
                CConsole.Write(attr.Value, _activeColor);
                CConsole.Write("' with prefix '");
                CConsole.Write(prefix, _activeColor);
                CConsole.WriteLine("' from document.");
            }
        }
示例#2
0
 private void DisplayNode(XmlNode node)
 {
     Console.Write("Node '");
     CConsole.Write(FormatNodeName(node), _activeColor);
     Console.WriteLine("' :");
     Console.WriteLine(node.OuterXml);
     Console.WriteLine();
     Console.WriteLine(separator);
 }
示例#3
0
 private void DisplayDefaultNamespaceError(string uri)
 {
     CConsole.Write("Can't find a prefix for default namespace uri '", ConsoleColor.Red);
     CConsole.Write(string.IsNullOrWhiteSpace(uri) ? "(empty)" : uri, _activeColor);
     CConsole.WriteLine("' in namespace manager.", ConsoleColor.Red);
     CConsole.WriteLine($"Add that namespace uri with a prefix in the '{NamespaceHelper.NAMESPACES_FILE_NAME}' file.", ConsoleColor.Red);
     CConsole.WriteLine("Maybe this file doesn't need the use of namespaces.", ConsoleColor.Yellow);
     Console.WriteLine();
 }
示例#4
0
 private static void DisplayHelp()
 {
     CConsole.WriteLine("Enter a path to a xml file.", MODE_COLOR);
     CConsole.WriteLine("Available commands :", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.HELP_KEYWORD} : display this message", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.EXIT_KEYWORD} : exit the application", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.NAMESPACES_COMMAND} or {CommandHelper.NAMESPACES_COMMAND_SHORT} : enter namespaces mode", MODE_COLOR);
     CConsole.WriteLine();
 }
示例#5
0
 private void DisplayDefaultNamespace(string uri, string prefix)
 {
     Console.Write("Default namespace uri '");
     CConsole.Write(uri, _activeColor);
     Console.Write("' has prefix '");
     CConsole.Write(prefix, _activeColor);
     Console.WriteLine("'.");
     Console.WriteLine();
 }
示例#6
0
 private void DisplayNodes(XmlNodeList nodeList)
 {
     Console.WriteLine(separator);
     foreach (XmlNode node in nodeList)
     {
         DisplayNode(node);
     }
     CConsole.WriteLine($"Found {nodeList.Count} nodes.", _activeColor);
     Console.WriteLine();
 }
示例#7
0
 private void DisplayAttribute(XmlAttribute attribute)
 {
     Console.Write("Attribute '");
     CConsole.Write(FormatNodeName(attribute), _activeColor);
     CConsole.WriteLine("' :");
     CConsole.Write("Value : '");
     CConsole.Write(attribute.InnerText, _activeColor);
     CConsole.WriteLine("'");
     CConsole.WriteLine();
     CConsole.WriteLine(separator);
 }
示例#8
0
 private void DisplayHelp()
 {
     CConsole.WriteLine("Enter a XPath string.", _activeColor);
     CConsole.WriteLine("Available commands :", _activeColor);
     CConsole.WriteLine($" * {CommandHelper.HELP_KEYWORD} : display this message", _activeColor);
     CConsole.WriteLine($" * {CommandHelper.EXIT_KEYWORD} : exit the current mode", _activeColor);
     CConsole.WriteLine($" * {CommandHelper.NODES_COMMAND} : list child nodes.", _activeColor);
     CConsole.WriteLine($" * {CommandHelper.ATTRIBUTES_COMMAND} or {CommandHelper.ATTRIBUTES_COMMAND_SHORT} : list current node's attributes", _activeColor);
     CConsole.WriteLine($" * {CommandHelper.SELECT_COMMAND} <xpath> : select a specific node to work with.", _activeColor);
     Console.WriteLine();
 }
示例#9
0
        private void SaveNewNamespaces()
        {
            bool saved = NamespaceHelper.Instance.SaveNewNamespaces();

            if (saved)
            {
                CConsole.WriteLine("Namespaces saved!", MODE_COLOR);
                _hasModificationsNotSaved = false;
            }
            CConsole.WriteLine();
        }
示例#10
0
 private void DisplayHelp()
 {
     CConsole.WriteLine("Enter a command.", MODE_COLOR);
     CConsole.WriteLine("Available commands :", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.HELP_KEYWORD} : display this message", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.EXIT_KEYWORD} : exit the current mode", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.NS_DISPLAY_COMMAND} : display loaded custom namespaces", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.NS_ADD_COMMAND} <prefix> <uri> : add a custom namespace", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.NS_DELETE_COMMAND} <prefix> : delete the custom namespace associated with <prefix>", MODE_COLOR);
     CConsole.WriteLine($" * {CommandHelper.NS_SAVE_COMMAND} : save added namespaces", MODE_COLOR);
     CConsole.WriteLine();
 }
示例#11
0
        private void DisplayNodeList(XmlNodeList nodeList, string xpath)
        {
            if (nodeList.Count == 0)
            {
                CConsole.Write("No nodes for XPath '", ConsoleColor.Yellow);
                CConsole.Write(xpath, _activeColor);
                CConsole.WriteLine("'.", ConsoleColor.Yellow);
                Console.WriteLine();
                return;
            }

            DisplayNodes(nodeList);
        }
示例#12
0
 private void AddNamespace(string command)
 {
     string[] parts = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
     if (parts.Length != 3)
     {
         CConsole.WriteLine($"Usage: {CommandHelper.NS_ADD_COMMAND} <prefix> <uri>", ConsoleColor.Yellow);
     }
     else
     {
         bool added = NamespaceHelper.Instance.AddNamespace(parts[1], parts[2]);
         _hasModificationsNotSaved |= added;
     }
     CConsole.WriteLine();
 }
示例#13
0
        private void DisplayChildNodeList(XmlNode workNode)
        {
            var nodeList = workNode.ChildNodes;

            if (nodeList.Count == 0)
            {
                CConsole.Write("No child nodes for node '", ConsoleColor.Yellow);
                CConsole.Write(FormatNodeName(workNode), _activeColor);
                CConsole.WriteLine("'!", ConsoleColor.Yellow);
                Console.WriteLine();
                return;
            }

            DisplayNodes(nodeList);
        }
示例#14
0
        private void DeleteNamespace(string command)
        {
            string[] parts = command.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
            {
                CConsole.WriteLine($"Usage: {CommandHelper.NS_DELETE_COMMAND} <prefix>", ConsoleColor.Yellow);
            }
            else
            {
                bool removed = NamespaceHelper.Instance.DeleteNamespace(parts[1]);
                _hasModificationsNotSaved |= removed;

                CConsole.Write("Prefix '", MODE_COLOR);
                CConsole.Write(parts[1]);
                CConsole.WriteLine("' was removed.", MODE_COLOR);
            }
            CConsole.WriteLine();
        }
示例#15
0
        private void DisplayCustomNamespaces()
        {
            string status = string.Empty;

            CConsole.WriteLine("Loaded custom namespaces :", MODE_COLOR);
            foreach (KeyValuePair <string, string> kv in NamespaceHelper.Instance.GetCustomNamespaces())
            {
                status = string.Empty;
                if (string.IsNullOrWhiteSpace(kv.Key) || string.IsNullOrWhiteSpace(kv.Value))
                {
                    status = " (unusable)";
                }
                CConsole.Write(" * Namespace '", MODE_COLOR);
                CConsole.Write(kv.Value);
                CConsole.Write("' with prefix '", MODE_COLOR);
                CConsole.Write(kv.Key);
                CConsole.WriteLine("'" + status, MODE_COLOR);
            }
            CConsole.WriteLine();
        }
示例#16
0
        private void DisplayAttributesList(XmlNode workNode)
        {
            var nodeList = workNode.Attributes;

            if (nodeList.Count == 0)
            {
                CConsole.Write("No attributes for node '", ConsoleColor.Yellow);
                CConsole.Write(FormatNodeName(workNode), _activeColor);
                CConsole.WriteLine("'!", ConsoleColor.Yellow);
                Console.WriteLine();
                return;
            }

            Console.WriteLine(separator);
            foreach (XmlAttribute attribute in nodeList)
            {
                DisplayAttribute(attribute);
            }
            CConsole.WriteLine($"Found {nodeList.Count} attributes.", _activeColor);
            Console.WriteLine();
        }
示例#17
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);
        }
示例#18
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();
            }
        }
示例#19
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);
        }