/// <summary> /// Crawls the specified parameters. /// </summary> /// <param name="parameters">The parameters.</param> public void Crawl(FileCrawlerParameters parameters) { // Reset cancelled flag: this.ResetCancelled(); // Don't crawl if no observers exist: if (this.observers.Count > 0) { List <FileFolderPath> paths = parameters.PathInfoList; int pathsCount = paths.Count; for (int i = 0; i < pathsCount; i++) { // Check cancelled: if (this.IsCancelled()) { break; } FileFolderPath path = paths[i]; // Notify crawling started: this.NotifyFileCrawlingStarted(path); try { // Check if the specified path is actually a file: if (File.Exists(path.RootPath)) { this.NotifyProcessFile(path, path.RootPath); } else if (Directory.Exists(path.RootPath)) { // Create regex parser if required: Regex regex = null; if (!String.IsNullOrWhiteSpace(path.Pattern)) { regex = new Regex(path.Pattern); } // Get directory info: DirectoryInfo info = new DirectoryInfo(path.RootPath); this.ProcessDirectory(path, info, regex); } } catch (Exception exc) { // Notify error: this.NotifyErrorOccurred(path, exc); } finally { // Notify path crawling finished: this.NotifyFileCrawlingFinished(path); } } } }
/// <summary> /// Notifies the file crawling finished. /// </summary> /// <param name="pathInfo">The path info.</param> private void NotifyFileCrawlingFinished(FileFolderPath pathInfo) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].FileCrawlingFinished(pathInfo); } }
/// <summary> /// Notifies the error occurred. /// </summary> /// <param name="pathInfo">The path info.</param> /// <param name="exc">The exc.</param> private void NotifyErrorOccurred(FileFolderPath pathInfo, Exception exc) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].ErrorOccurred(pathInfo, exc); } }
/// <summary> /// Notifies the process file. /// </summary> /// <param name="pathInfo">The path info.</param> /// <param name="filePath">The file path.</param> private void NotifyProcessFile(FileFolderPath pathInfo, string filePath) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].ProcessFile(pathInfo, filePath); } }
/// <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)); } }
/// <summary> /// Processes the directory. /// </summary> /// <param name="path">The path.</param> /// <param name="info">The info.</param> /// <param name="regex">The regex.</param> private void ProcessDirectory(FileFolderPath path, DirectoryInfo info, Regex regex) { // Iterate files: FileInfo[] files = info.GetFiles(); foreach (FileInfo fileInfo in files) { // Check cancelled: if (this.IsCancelled()) break; try { if (regex != null) { // Resolve correct file name: string fileName = String.Empty; switch (path.PatternType) { case FileNamePatternType.FileName: fileName = fileInfo.Name; break; case FileNamePatternType.FileFullName: fileName = fileInfo.FullName; break; } // Check against regex: if (!string.IsNullOrEmpty(fileName) && !regex.Match(fileName).Success) continue; } // Process file: this.NotifyProcessFile(path, fileInfo.FullName); } catch (Exception exc) { // Notify error: this.NotifyErrorOccurred(path, exc); } } // Iterate folders: DirectoryInfo[] directories = info.GetDirectories(); foreach (DirectoryInfo directory in directories) { // Check cancelled: if (this.IsCancelled()) break; try { // Include/Exclude folders that have ".svn" or "_svn" as their name. if (path.IncludeSvnFolders || (String.Compare(directory.Name, ".svn", true) != 0 && String.Compare(directory.Name, "_svn", true) != 0)) { if (regex != null) { // Resolve correct folder name: string folderName = String.Empty; switch (path.PatternType) { case FileNamePatternType.FolderName: folderName = directory.Name; break; case FileNamePatternType.FolderFullPath: folderName = directory.FullName; break; } // Check against regex: if (!string.IsNullOrEmpty(folderName) && !regex.Match(folderName).Success) continue; } // Crawl through child folder: this.ProcessDirectory(path, directory, regex); } } catch (Exception exc) { // Notify error: this.NotifyErrorOccurred(path, exc); } } }
/// <summary> /// Notifies the process file. /// </summary> /// <param name="pathInfo">The path info.</param> /// <param name="filePath">The file path.</param> private void NotifyProcessFile(FileFolderPath pathInfo, string filePath) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].ProcessFile(pathInfo, filePath); } }
/// <summary> /// Notifies the file crawling started. /// </summary> /// <param name="pathInfo">The path info.</param> private void NotifyFileCrawlingStarted(FileFolderPath pathInfo) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].FileCrawlingStarted(pathInfo); } }
/// <summary> /// Notifies the error occurred. /// </summary> /// <param name="pathInfo">The path info.</param> /// <param name="exc">The exc.</param> private void NotifyErrorOccurred(FileFolderPath pathInfo, Exception exc) { int count = this.observers.Count; for (int i = 0; i < count; i++) { this.observers[i].ErrorOccurred(pathInfo, exc); } }
/// <summary> /// Processes the directory. /// </summary> /// <param name="path">The path.</param> /// <param name="info">The info.</param> /// <param name="regex">The regex.</param> private void ProcessDirectory(FileFolderPath path, DirectoryInfo info, Regex regex) { // Iterate files: FileInfo[] files = info.GetFiles(); foreach (FileInfo fileInfo in files) { // Check cancelled: if (this.IsCancelled()) { break; } try { if (regex != null) { // Resolve correct file name: string fileName = String.Empty; switch (path.PatternType) { case FileNamePatternType.FileName: fileName = fileInfo.Name; break; case FileNamePatternType.FileFullName: fileName = fileInfo.FullName; break; } // Check against regex: if (!string.IsNullOrEmpty(fileName) && !regex.Match(fileName).Success) { continue; } } // Process file: this.NotifyProcessFile(path, fileInfo.FullName); } catch (Exception exc) { // Notify error: this.NotifyErrorOccurred(path, exc); } } // Iterate folders: DirectoryInfo[] directories = info.GetDirectories(); foreach (DirectoryInfo directory in directories) { // Check cancelled: if (this.IsCancelled()) { break; } try { // Include/Exclude folders that have ".svn" or "_svn" as their name. if (path.IncludeSvnFolders || (String.Compare(directory.Name, ".svn", true) != 0 && String.Compare(directory.Name, "_svn", true) != 0)) { if (regex != null) { // Resolve correct folder name: string folderName = String.Empty; switch (path.PatternType) { case FileNamePatternType.FolderName: folderName = directory.Name; break; case FileNamePatternType.FolderFullPath: folderName = directory.FullName; break; } // Check against regex: if (!string.IsNullOrEmpty(folderName) && !regex.Match(folderName).Success) { continue; } } // Crawl through child folder: this.ProcessDirectory(path, directory, regex); } } catch (Exception exc) { // Notify error: this.NotifyErrorOccurred(path, exc); } } }
/// <summary> /// Files the crawling started. /// </summary> /// <param name="pathInfo">The path info.</param> void IFileCrawlerObserver.FileCrawlingStarted(FileFolderPath pathInfo) { this.NotifyMessage(pathInfo.RootPath, "Crawling started."); }
/// <summary> /// Notifies that exception has occurred. /// </summary> /// <param name="pathInfo">The path info.</param> /// <param name="exc">The exc.</param> void IFileCrawlerObserver.ErrorOccurred(FileFolderPath pathInfo, Exception exc) { this.NotifyMessage(pathInfo.RootPath, exc); }