예제 #1
0
 public abstract void fReplace(TextReader rin, SearchType searchType, String replaceString);
예제 #2
0
 public abstract bool fSearch(TextReader rin, SearchType searchType);
예제 #3
0
 public XMLSearchReplace(string path, SearchType searchType, XMLPartEnum xmlPartEnum) : base(path, searchType)
 {
     this.xmlPartEnum = xmlPartEnum;
 }
예제 #4
0
        public override void fReplace(TextReader rin, SearchType searchType, String replaceString)
        {
            if (rin == null || searchType == null || string.IsNullOrEmpty(replaceString) || xmlPartEnum == XMLPartEnum.UNKNOWN)
            {
                return;
            }

            string newEndTag = null;
            string oldEndTag = null;

            switch (xmlPartEnum)
            {
            case XMLPartEnum.ELEMENT_NAME:
                if (!searchType.searchString.Contains("<") || !replaceString.Contains("<"))
                {
                    return;
                }
                StringBuilder sb = new StringBuilder(replaceString);
                sb.Insert(1, '/');
                newEndTag = sb.ToString();
                sb        = new StringBuilder(searchType.searchString);
                sb.Insert(1, '/');
                oldEndTag = sb.ToString();
                break;

            case XMLPartEnum.ATTRIBUTE:
                if (!searchType.searchString.Contains("=") || !replaceString.Contains("="))
                {
                    return;
                }
                break;

            default:
                break;
            }

            //Console.WriteLine("oldEndTab: " + oldEndTag + ", newEndTag: " + newEndTag);

            TypeEnum typeEnum = searchType.typeEnum;

            using (TextWriter tw = File.CreateText(getOutPath(path)))
            {
                while (rin.Peek() > -1)
                {
                    string line = rin.ReadLine();

                    switch (typeEnum)
                    {
                    case TypeEnum.PHRASE:
                        if (line.Contains(searchType.searchString))
                        {
                            phraseReplace(line, searchType.searchString, replaceString, tw);
                        }
                        else if (oldEndTag != null && newEndTag != null && line.Contains(oldEndTag))
                        {
                            phraseReplace(line, oldEndTag, newEndTag, tw);
                        }
                        else
                        {
                            lineReplace(line, tw);
                        }
                        break;

                    case TypeEnum.PATTERN:
                        Regex rgx = new Regex(searchType.searchString);
                        if (rgx.IsMatch(line))
                        {
                            patternReplace(line, searchType.searchString, replaceString, tw);
                        }
                        else if (oldEndTag != null && newEndTag != null)
                        {
                            Regex rx = new Regex(oldEndTag);
                            if (rx.IsMatch(line))
                            {
                                patternReplace(line, oldEndTag, newEndTag, tw);
                            }
                            else
                            {
                                lineReplace(line, tw);
                            }
                        }
                        else
                        {
                            lineReplace(line, tw);
                        }
                        break;

                    case TypeEnum.WILDCARD:
                        //TODO
                        return;

                    case TypeEnum.VARIABLE:
                        //TODO
                        return;

                    default:
                        break;
                    }
                }
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            string      path         = string.Empty;
            string      searchString = string.Empty;
            int         type         = 0;
            FileType    fileType     = FileType.UNKNOWN;
            XMLPartEnum xmlPartEnum  = XMLPartEnum.UNKNOWN;

            Console.WriteLine("Please input the file path:");
            while (string.IsNullOrWhiteSpace(path = Console.ReadLine()))
            {
                Console.WriteLine("Please input the file path:");
            }
            int    idx           = path.LastIndexOf('.');
            string extensionName = path.Substring(++idx);

            if (extensionName.ToLower().Equals("txt"))
            {
                fileType = FileType.TEXT;
            }
            else if (extensionName.ToLower().Equals("xml"))
            {
                fileType = FileType.XML;

                Console.WriteLine("Please input the XML part type(1: element name 2. attribute 3. text node), just input the number:");
                while (true)
                {
                    while (!Int32.TryParse(Console.ReadLine(), out type))
                    {
                        Console.WriteLine("Error! You must input a number: 1 or 2 or 3! Please input:");
                    }

                    if (type == 1 || type == 2 || type == 3)
                    {
                        switch (type)
                        {
                        case 1:
                            xmlPartEnum = XMLPartEnum.ELEMENT_NAME;
                            break;

                        case 2:
                            xmlPartEnum = XMLPartEnum.ATTRIBUTE;
                            break;

                        case 3:
                            xmlPartEnum = XMLPartEnum.TEXT_NODE;
                            break;

                        default:
                            break;
                        }
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please input the XML part type(1: element name 2. attribute 3. text node), just input the number:");
                    }
                }
            }

            Console.WriteLine("Please input the string you want to search:");
            while (true)
            {
                if (string.IsNullOrWhiteSpace(searchString = Console.ReadLine()))
                {
                    Console.WriteLine("Please input the string you want to search:");
                }
                else // verify the xml syntax
                {
                    if (xmlPartEnum == XMLPartEnum.ELEMENT_NAME && !searchString.Contains("<"))
                    {
                        Console.WriteLine("Error! The xml element name must be prefixed by a <");
                        Console.WriteLine("Please input the string you want to search:");
                    }
                    else if (xmlPartEnum == XMLPartEnum.ATTRIBUTE && !searchString.Contains("="))
                    {
                        Console.WriteLine("Error! The xml attribute must contain a =");
                        Console.WriteLine("Please input the string you want to search:");
                    }
                    else
                    {
                        break;
                    }
                }
            }

            Console.WriteLine("Please input the search type(1: Text phrease 2. Pattern), just input the number:");
            while (true)
            {
                if (!Int32.TryParse(Console.ReadLine(), out type))
                {
                    Console.WriteLine("Error! You must input a number either 1 or 2!");
                    continue;
                }

                if (type == 1 || type == 2)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Please input the search type(1: Text phrease 2. Pattern), just input the number:");
                }
            }
            Console.WriteLine("Path: " + path + ", searchString: " + searchString + ", type: " + type);

            SearchType searchType = new SearchType(type == 1 ? TypeEnum.PHRASE : TypeEnum.PATTERN, searchString);

            Console.WriteLine("Please input the replacement string(if you input no, that means you skip the replace operation):");
            string replaceString = Console.ReadLine();

            if (!replaceString.Equals("no"))
            {
                while (true)
                {
                    if (xmlPartEnum == XMLPartEnum.ELEMENT_NAME && !replaceString.Contains("<"))
                    {
                        Console.WriteLine("Error! The xml element name must be prefixed by a <");
                        Console.WriteLine("Please input the replacement string if you want:");
                        replaceString = Console.ReadLine();
                    }
                    else if (xmlPartEnum == XMLPartEnum.ATTRIBUTE && !replaceString.Contains("="))
                    {
                        Console.WriteLine("Error! The xml attribute must contain a =");
                        Console.WriteLine("Please input the replacement string if you want:");
                        replaceString = Console.ReadLine();
                    }
                    else
                    {
                        break;
                    }
                }
            }

            SearchReplace searchReplace = null;

            if (fileType == FileType.TEXT)
            {
                searchReplace = new TextSearchReplace(path, searchType);
            }
            else if (fileType == FileType.XML)
            {
                searchReplace = new XMLSearchReplace(path, searchType, xmlPartEnum);
            }

            bool found = false;

            using (TextReader tr = File.OpenText(path))
            {
                if (found = searchReplace.fSearch(tr, searchType))
                {
                    Console.WriteLine("\"" + searchType.searchString + "\" is found in file: " + path);
                }
                else
                {
                    Console.WriteLine("\"" + searchType.searchString + "\" is NOT found in file: " + path);
                }
            }

            if (found && !replaceString.Equals("no"))
            {
                using (TextReader tr = File.OpenText(path))
                {
                    searchReplace.fReplace(tr, searchType, replaceString);
                    Console.WriteLine("Replacement is done, please check the new generated file: " + searchReplace.getOutPath(path));
                }
            }
            //Console.ReadLine();
        }