Esempio n. 1
0
        /// <summary>
        /// Processes the file.
        /// </summary>
        /// <param name="pathInfo">The path info.</param>
        /// <param name="filePath">The file path.</param>
        void IFileCrawlerObserver.ProcessFile(FileFolderPath pathInfo, string filePath)
        {
            try
            {
                // Notify processing file:
                this.NotifyFileProcessing(filePath);

                // Read file:
                //string input = File.ReadAllText(filePath);
                string   input;
                Encoding detectedEncoding;
                using (StreamReader reader = new StreamReader(filePath, Encoding.Default))
                {
                    input            = reader.ReadToEnd();
                    detectedEncoding = reader.CurrentEncoding;
                }

                // Reset file tags:
                AllAvailableSmartTags.ResetFileTags();

                // Apply replacing:
                string output = input;
                int    count  = this.wrappedPatterns.Count;
                for (int i = 0; i < count; i++)
                {
                    output = this.wrappedPatterns[i].ProcessPattern(output);
                }

                // Notify file processed:
                bool shouldSave = this.NotifyFileProcessed(filePath, input, output);

                // Save file:
                if (shouldSave)
                {
                    //File.WriteAllText(filePath, output, Encoding.ASCII);
                    File.WriteAllText(filePath, output, detectedEncoding);
                }
            }
            catch (Exception e)
            {
                this.NotifyMessage(filePath, String.Format("Exception in file {0} : {1}", filePath, e));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Replaces the specified parameters.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        public void Replace(ReplaceParameters parameters)
        {
            // Validate:
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            if (parameters.FileCrawlerParameters == null)
            {
                throw new ArgumentException("parameters.FileCrawlerParameters is null");
            }

            if (parameters.FileCrawlerParameters.PathInfoList.Count == 0)
            {
                this.NotifyMessage(null, "Error: No files to crawl.");
                return;
            }

            if (parameters.ReplacePatterns.Count == 0)
            {
                this.NotifyMessage(null, "Error: No replace patterns specified.");
                return;
            }

            // Reset tags:
            AllAvailableSmartTags.Reset();

            // Wrap patterns:
            this.wrappedPatterns = parameters.ReplacePatterns
                                   .Select(p => new ReplacePatternWrapper(p))
                                   .ToList();

            // Process:
            this.crawler = new FileCrawler();
            this.crawler.AddObserver(this);
            this.crawler.Crawl(parameters.FileCrawlerParameters);
            this.crawler.RemoveObserver(this);

            // Clean up:
            this.wrappedPatterns = null;
        }