1 12 13 package com.openedit.modules.spell; 14 15 import java.io.File ; 16 import java.io.FileNotFoundException ; 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.Set ; 22 23 import com.swabunga.spell.engine.Configuration; 24 import com.swabunga.spell.engine.GenericSpellDictionary; 25 import com.swabunga.spell.engine.SpellDictionary; 26 import com.swabunga.spell.event.SpellCheckEvent; 27 import com.swabunga.spell.event.SpellCheckListener; 28 import com.swabunga.spell.event.SpellChecker; 29 import com.swabunga.spell.event.StringWordTokenizer; 30 31 36 public class WebSpellChecker extends SpellChecker implements SpellCheckListener 37 { 38 protected Set fieldIgnoreList; 39 protected List fieldErrors; 40 public final static String KEY ="WebSpellChecker"; 41 42 public WebSpellChecker(String inPath) throws FileNotFoundException , IOException 43 { 44 this(new GenericSpellDictionary(new File (inPath))); 46 } 47 48 53 public WebSpellChecker(SpellDictionary dictionary) 54 { 55 super(dictionary); 56 addSpellCheckListener(this); 57 getConfiguration().setBoolean( Configuration.SPELL_IGNORESENTENCECAPITALIZATION, true ); 58 getConfiguration().setBoolean( Configuration.SPELL_IGNOREINTERNETADDRESSES, true ); 59 getConfiguration().setBoolean( Configuration.SPELL_IGNOREUPPERCASE, true ); 60 getConfiguration().setBoolean( Configuration.SPELL_IGNOREDIGITWORDS, true ); 61 } 62 63 public List checkAllWords(String inWords) 64 { 65 fieldErrors = new ArrayList (); 66 67 inWords = inWords.replaceAll("(?m)(?s)<[^>]*>", " "); 69 StringWordTokenizer words = new StringWordTokenizer(inWords); 70 checkSpelling(words); 71 return getErrors(); 72 } 73 74 75 80 public void addIgnore(SpellCheckEvent inEvent) 81 { 82 getIgnoreList().add(inEvent.getInvalidWord()); 83 } 84 85 88 public void spellingError(SpellCheckEvent event) 89 { 90 if (!getIgnoreList().contains(event.getInvalidWord()) && 91 areAllLetters(event.getInvalidWord())) 92 { 93 getErrors().add(event); 94 } 95 } 96 97 protected void setIgnoreList(Set inIgnoreList) 98 { 99 fieldIgnoreList = inIgnoreList; 100 } 101 102 protected Set getIgnoreList() 103 { 104 if (fieldIgnoreList == null) 105 { 106 fieldIgnoreList = new HashSet (); 107 } 108 109 return fieldIgnoreList; 110 } 111 112 protected boolean areAllLetters(String inError) 113 { 114 for (int i = 0; i < inError.length(); i++) 115 { 116 if (!Character.isLetter(inError.charAt(i))) 117 { 118 return false; 119 } 120 } 121 122 return true; 123 } 124 125 128 public List getErrors() 129 { 130 return fieldErrors; 131 } 132 133 134 } 135 | Popular Tags |