Ejemplo n.º 1
0
        /// <summary>
        /// Изменяет файл согласно заданным параметрам
        /// </summary>
        /// <param name="item">Элемент item из xml файла настроек, содержащий параметры поиска</param>
        /// <param name="itemLine">Строка, которую необходимо найти</param>
        private static void do_changes_by_direction_of_commenting(ItemFromXML item, ItemLine itemLine)
        {
            if (item.path_to_dir == null)
            {
                Program.MyWriteLine("ERRROR!!! " + " item.path_to_dir = null " + "\n");
                return;
            }

            String path_to_dir = item.path_to_dir;

            if (path_to_dir.Length > 0 && path_to_dir.LastIndexOf("/") != (path_to_dir.Length - 1))
            {
                MyWriteLine("/ - in path_to_dir: NOT EXIST !" + "\n");
                item.path_to_dir = path_to_dir + "/";
            }

            path_to_target_file = item.path_to_dir + item.name_file;
            to_comment_prefix   = item.to_comment_prefix;
            to_comment_postfix  = item.to_comment_postfix;

            DIRECTION_OF_COMMENTING dir_of_com = (DIRECTION_OF_COMMENTING)itemLine.direction_of_commenting;

            switch (dir_of_com)
            {
            // Коментирует, если ещё не коментировано:
            case DIRECTION_OF_COMMENTING.Comment:
                MyWriteLine("Case 1 " + "[ Commented if did not make comments ]" + "\n");

                do_1_for_line(itemLine.line);

                break;

            // Де-коментирует, если еще не де-коментировано:
            case DIRECTION_OF_COMMENTING.DeComment:
                MyWriteLine("Case 2 " + "[ De-commented, if not de-comment ]" + "\n");

                do_2_for_line(itemLine.line);

                break;

            // Заменяет строку line на line2:
            case DIRECTION_OF_COMMENTING.Replace:
                MyWriteLine("Case 3 " + "[ Replace line ]" + "\n");

                do_3_for_line(itemLine.line, itemLine.line2);

                break;

            default:
                MyWriteLine("Default case. " + "itemLine.direction_of_commenting: " + itemLine.direction_of_commenting.ToString() + "\n");
                break;
            }
        }
Ejemplo n.º 2
0
        static public List <ItemFromXML> ReadXML(String path)
        {
            if (path == null || path.Length == 0)
            {
                Program.MyWriteLine("ERRROR!!! " + "path == null || path.Length == 0");
                return(null);
            }

            XmlTextReader reader = null;

            try
            {
                if (File.Exists(path))
                {
                    reader = new XmlTextReader(path);
                }
                else
                {
                    Program.MyWriteLine("ERRROR!!! " + "File not exist with path: " + path);
                }
            } catch (Exception e)
            {
                Program.MyWriteLine("ERRROR!!! " + " new XmlTextReader(path) ");
                Program.MyWriteLine(e.Message);
                return(null);
            }

            if (reader == null)
            {
                Program.MyWriteLine("ERRROR!!! " + " reader = null ");
                return(null);
            }

            List <ItemFromXML> listOfItemFromXML = new List <ItemFromXML>();
            ItemFromXML        itemFromXML       = new ItemFromXML();

            bool do_loop = true;

            while (do_loop)
            {
                try
                {
                    do_loop = reader.Read();
                }
                catch (Exception e)
                {
                    Program.MyWriteLine("ERRROR!!! " + "XML Invalid");
                    Program.MyWriteLine(e.Message);
                    return(null);
                }

                // Program.MyWriteLine("reader.NodeType: " + reader.NodeType + "|  reader.Name = " + reader.Name);

                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "item")
                    {
                        itemFromXML = new ItemFromXML();
                        listOfItemFromXML.Add(itemFromXML);

                        itemFromXML.path_to_dir = reader.GetAttribute("path_to_dir");
                        itemFromXML.name_file   = reader.GetAttribute("name_file");

                        itemFromXML.to_comment_prefix = reader.GetAttribute("to_comment_prefix");
                        if (itemFromXML.to_comment_prefix == null)
                        {
                            itemFromXML.to_comment_prefix = "";
                        }

                        itemFromXML.to_comment_postfix = reader.GetAttribute("to_comment_postfix");
                        if (itemFromXML.to_comment_postfix == null)
                        {
                            itemFromXML.to_comment_postfix = "";
                        }
                    }

                    if (reader.Name == "sub")
                    {
                        ItemLine itemLine = new ItemLine();

                        try
                        {
                            itemLine.direction_of_commenting = int.Parse(reader.GetAttribute("direction_of_commenting"));
                        }
                        catch (Exception e)
                        {
                            Program.MyWriteLine("ERRROR!!! " + "XML is Invalid! " + "direction_of_commenting" + " is Invalid!");
                            Program.MyWriteLine(e.Message);
                        }

                        itemLine.line = reader.GetAttribute("line");
                        if (itemLine.line == null)
                        {
                            itemLine.line = "";
                        }

                        itemLine.line2 = reader.GetAttribute("line2");
                        if (itemLine.line2 == null)
                        {
                            itemLine.line2 = "";
                        }

                        itemFromXML.itemLines.Add(itemLine);
                    }
                }
            }

            reader.Close();

            Program.MyWriteLine("START READ XML FILE");
            Program.MyWriteLine("--------------------------------------");
            foreach (ItemFromXML item in listOfItemFromXML)
            {
                Program.MyWriteLine("name_file: " + item.name_file + " | Count Lines:" + item.itemLines.Count);
                foreach (ItemLine itemLine in item.itemLines)
                {
                    Program.MyWriteLine("   direction_of_commenting: " + itemLine.direction_of_commenting + " | line: " + itemLine.line);
                }
            }
            Program.MyWriteLine("--------------------------------------");
            Program.MyWriteLine("END READ XML FILE" + "\n");

            return(listOfItemFromXML);
        }