//===================================================================== /// <summary> /// Constructor /// </summary> public FileSpellChecker(GlobalDictionary dictionary) { this.dictionary = dictionary; }
//===================================================================== /// <summary> /// Spell check the current document and start showing suggestions for replacement /// </summary> private bool SpellCheckCurrentDocument() { bool success = false; try { Cursor.Current = Cursors.WaitCursor; // Use the language from the current project to do the spell checking var projectExplorer = this.FindProjectExplorerWindow(); if(dictionary == null || (projectExplorer != null && projectExplorer.CurrentProject.Language != dictionary.Language)) { dictionary = GlobalDictionary.CreateGlobalDictionary( (projectExplorer != null) ? projectExplorer.CurrentProject.Language : null); } if(dictionary == null) lblMisspelledWord.Text = "Unable to create dictionary!"; else { var speller = new FileSpellChecker(dictionary); speller.MisspelledWord += new EventHandler<SpellingEventArgs>(speller_MisspelledWord); speller.DoubledWord += new EventHandler<SpellingEventArgs>(speller_DoubledWord); string text = currentTopicWindow.GetAllText(); string ext = Path.GetExtension(currentTopicWindow.Filename).ToLowerInvariant(); // TODO: Need a config file to map extensions to parsers, add support for code files too switch(ext) { case ".aml": case ".ascx": case ".asp": case ".aspx": case ".config": case ".content": case ".htm": case ".html": case ".items": case ".sitemap": case ".snippets": case ".tokens": case ".topic": case ".xaml": case ".xml": case ".xsl": case ".xslt": case ".xamlcfg": using(MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(text))) { XmlReaderSettings rs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore }; speller.SpellCheckXmlReader(XmlReader.Create(ms, rs)); } break; default: speller.SpellCheckText(text); break; } success = true; } } catch(Exception ex) { System.Diagnostics.Debug.WriteLine(ex); lblMisspelledWord.Text = "Unable to spell check file: " + ex.Message; } finally { Cursor.Current = Cursors.Default; } return success; }
//===================================================================== /// <summary> /// Create a global dictionary for the specified culture /// </summary> /// <param name="culture">The language to use for the dictionary</param> /// <returns>The spell factory to use or null if one could not be created</returns> public static GlobalDictionary CreateGlobalDictionary(CultureInfo culture) { GlobalDictionary globalDictionary = null; try { if (globalDictionaries == null) { globalDictionaries = new Dictionary <string, GlobalDictionary>(); } // If no culture is specified, use the default culture if (culture == null) { culture = SpellCheckerConfiguration.DefaultLanguage; } // If not already loaded, create the dictionary and the thread-safe spell factory instance for // the given culture. if (!globalDictionaries.ContainsKey(culture.Name)) { string dllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (spellEngine == null) { Hunspell.NativeDllPath = dllPath; spellEngine = new SpellEngine(); } // Look in the configuration folder first for user-supplied dictionaries string dictionaryFile = Path.Combine(SpellCheckerConfiguration.ConfigurationFilePath, culture.Name.Replace("-", "_") + ".aff"); // If not found, default to the English dictionary supplied with the package. This can at // least clue us in that it didn't find the language-specific dictionary when the suggestions // are in English. if (!File.Exists(dictionaryFile)) { dictionaryFile = Path.Combine(dllPath, "Spelling", "en_US.aff"); } LanguageConfig lc = new LanguageConfig(); lc.LanguageCode = culture.Name; lc.HunspellAffFile = dictionaryFile; lc.HunspellDictFile = Path.ChangeExtension(dictionaryFile, ".dic"); spellEngine.AddLanguage(lc); globalDictionaries.Add(culture.Name, new GlobalDictionary(culture, spellEngine[culture.Name])); } globalDictionary = globalDictionaries[culture.Name]; } catch (Exception ex) { // Ignore exceptions. Not much we can do, we'll just not spell check anything. System.Diagnostics.Debug.WriteLine(ex); } return(globalDictionary); }