Exemplo n.º 1
0
        public static IGrepEngine GetSearchEngine(string fileName, GrepEngineInitParams param, FileFilter filter)
        {
            Debug.Assert(param != null);
            Debug.Assert(filter != null);

            loadPlugins();

            string fileExtension = Path.GetExtension(fileName).ToLower();

            if (fileExtension.Length > 1)
            {
                fileExtension = fileExtension.Substring(1);
            }

            if (!fileTypeEngines.ContainsKey(fileExtension))
            {
                foreach (GrepPlugin plugin in plugings)
                {
                    if (plugin.Extensions.Contains(fileExtension))
                    {
                        fileTypeEngines[fileExtension] = plugin;
                    }
                }
            }
            GrepEnginePlainText plainTextEngine = new GrepEnginePlainText();

            plainTextEngine.Initialize(param, filter);

            if (fileTypeEngines.ContainsKey(fileExtension) && fileTypeEngines[fileExtension].Enabled)
            {
                if (FrameworkVersionsAreCompatible(fileTypeEngines[fileExtension].Engine.FrameworkVersion, plainTextEngine.FrameworkVersion))
                {
                    if (fileTypeEngines[fileExtension].Engine.Initialize(param, filter))
                    {
                        return(fileTypeEngines[fileExtension].Engine);
                    }
                    else
                    {
                        failedEngines[fileTypeEngines[fileExtension].Engine.GetType().Name] = "Failed to initialize the plugin. See error log for details.";
                        return(plainTextEngine);
                    }
                }
                else
                {
                    failedEngines[fileTypeEngines[fileExtension].Engine.GetType().Name] = "Plugin developed under outdated framework. Please update the plugin.";
                    return(plainTextEngine);
                }
            }
            else
            {
                return(plainTextEngine);
            }
        }
Exemplo n.º 2
0
        public static IGrepEngine GetSearchEngine(string fileName, GrepEngineInitParams param)
        {
            loadPlugins();

            string fileExtension = Path.GetExtension(fileName).ToLower();
            if (fileExtension.Length > 1)
                fileExtension = fileExtension.Substring(1);

            if (!fileTypeEngines.ContainsKey(fileExtension)) 
            {
                foreach (GrepPlugin plugin in plugings)
                {
                    if (plugin.Extensions.Contains(fileExtension))
                    {
                        fileTypeEngines[fileExtension] = plugin;
                    }
                }
            }
            GrepEnginePlainText plainTextEngine = new GrepEnginePlainText();
            plainTextEngine.Initialize(param);

            if (fileTypeEngines.ContainsKey(fileExtension) && fileTypeEngines[fileExtension].Enabled)
            {
                if (FrameworkVersionsAreCompatible(fileTypeEngines[fileExtension].Engine.FrameworkVersion, plainTextEngine.FrameworkVersion))
                {
                    if (fileTypeEngines[fileExtension].Engine.Initialize(param))
                    {
                        return fileTypeEngines[fileExtension].Engine;
                    }
                    else
                    {
                        failedEngines[fileTypeEngines[fileExtension].Engine.GetType().Name] = "Failed to initialize the plugin. See error log for details.";
                        return plainTextEngine;
                    }
                }
                else
                {
                    failedEngines[fileTypeEngines[fileExtension].Engine.GetType().Name] = "Plugin developed under outdated framework. Please update the plugin.";
                    return plainTextEngine;
                }
            }
            else
                return plainTextEngine;
        }
Exemplo n.º 3
0
        private static IGrepEngine GetPlainTextEngine(string fileExtension, GrepEngineInitParams param, FileFilter filter)
        {
            if (!poolKeys.ContainsKey(fileExtension))
            {
                poolKeys.Add(fileExtension, "GrepEnginePlainText");
            }

            IGrepEngine poolEngine = FetchFromPool(fileExtension);

            if (poolEngine != null)
            {
                poolEngine.Initialize(param, filter);
                return(poolEngine);
            }

            IGrepEngine engine = new GrepEnginePlainText();

            loadedEngines.Add(engine);
            engine.Initialize(param, filter);
            return(engine);
        }
Exemplo n.º 4
0
        private void replace()
        {
            GrepEnginePlainText engine = new GrepEnginePlainText();
            engine.Initialize(new GrepEngineInitParams(GrepSettings.Instance.Get<bool>(GrepSettings.Key.ShowLinesInContext),
                GrepSettings.Instance.Get<int>(GrepSettings.Key.ContextLinesBefore),
                GrepSettings.Instance.Get<int>(GrepSettings.Key.ContextLinesAfter),
                GrepSettings.Instance.Get<double>(GrepSettings.Key.FuzzyMatchThreshold)));
            List<GrepSearchResult> results = new List<GrepSearchResult>();

            GrepSearchOption searchOptions = GrepSearchOption.None;
            if (Multiline)
                searchOptions |= GrepSearchOption.Multiline;
            if (CaseSensitive)
                searchOptions |= GrepSearchOption.CaseSensitive;
            if (Singleline)
                searchOptions |= GrepSearchOption.SingleLine;
            if (WholeWord)
                searchOptions |= GrepSearchOption.WholeWord;

            string replacedString = "";
            using (Stream inputStream = new MemoryStream(Encoding.Default.GetBytes(SampleText)))
            using (Stream writeStream = new MemoryStream())
            {
                engine.Replace(inputStream, writeStream, SearchFor, ReplaceWith, TypeOfSearch,
                    searchOptions, Encoding.Default);
                writeStream.Position = 0;
                StreamReader reader = new StreamReader(writeStream);
                replacedString = reader.ReadToEnd();
            }
            SearchResults.Clear();
            SearchResults.AddRange(results);
            Paragraph paragraph = new Paragraph();
            paragraph.Inlines.Add(new Run(replacedString));
            TestOutput = paragraph.Inlines;
            TestOutputText = replacedString;
        }
Exemplo n.º 5
0
        private void search()
        {
            if (SampleText == null)
                SampleText = string.Empty;
            GrepEnginePlainText engine = new GrepEnginePlainText();
            engine.Initialize(new GrepEngineInitParams(GrepSettings.Instance.Get<bool>(GrepSettings.Key.ShowLinesInContext),
                GrepSettings.Instance.Get<int>(GrepSettings.Key.ContextLinesBefore),
                GrepSettings.Instance.Get<int>(GrepSettings.Key.ContextLinesAfter),
                GrepSettings.Instance.Get<double>(GrepSettings.Key.FuzzyMatchThreshold)));
            List<GrepSearchResult> results = new List<GrepSearchResult>();
            GrepSearchOption searchOptions = GrepSearchOption.None;
            if (Multiline)
                searchOptions |= GrepSearchOption.Multiline;
            if (CaseSensitive)
                searchOptions |= GrepSearchOption.CaseSensitive;
            if (Singleline)
                searchOptions |= GrepSearchOption.SingleLine;
            if (WholeWord)
                searchOptions |= GrepSearchOption.WholeWord;
            using (Stream inputStream = new MemoryStream(Encoding.Default.GetBytes(SampleText)))
            {
                try
                {
                    results = engine.Search(inputStream, "test.txt", SearchFor, TypeOfSearch,
                        searchOptions, Encoding.Default);
                    if (results != null)
                    {
                        using (StringReader reader = new StringReader(SampleText))
                        {
                            foreach (var result in results)
                            {
                                if (!result.HasSearchResults)
                                    result.SearchResults = Utils.GetLinesEx(reader, result.Matches, 0, 0);
                            }
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show("Incorrect pattern: " + ex.Message, "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            SearchResults.Clear();
            SearchResults.AddRange(results);
            Paragraph paragraph = new Paragraph();
            if (SearchResults.Count == 1)
            {
                SearchResults[0].FormattedLines.Load(false);
                foreach (FormattedGrepLine line in SearchResults[0].FormattedLines)
                {
                    // Copy children Inline to a temporary array.
                    paragraph.Inlines.AddRange(line.FormattedText.ToList());

                    paragraph.Inlines.Add(new LineBreak());
                    paragraph.Inlines.Add(new Run("================================="));
                    paragraph.Inlines.Add(new LineBreak());
                }
            }
            else
            {
                paragraph.Inlines.Add(new Run("No matches found"));
            }
            TestOutput = paragraph.Inlines;
        }