1 package org.roller.model; 2 3 import com.swabunga.spell.engine.SpellDictionary; 4 import com.swabunga.spell.event.SpellCheckEvent; 5 import com.swabunga.spell.event.SpellCheckListener; 6 import com.swabunga.spell.event.SpellChecker; 7 import com.swabunga.spell.event.StringWordTokenizer; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.roller.RollerException; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InputStreamReader ; 16 import java.util.ArrayList ; 17 18 28 public class RollerSpellCheck implements SpellCheckListener 29 { 30 private static Log mLogger = 31 LogFactory.getFactory().getInstance(RollerSpellCheck.class); 32 33 private static SpellDictionary dictionary = null; 34 35 private SpellChecker spellCheck = null; 36 private ArrayList spellCheckEvents = new ArrayList (); 37 38 47 public static void init(InputStream in) throws RollerException 48 { 49 try { 50 InputStreamReader inR = new InputStreamReader ( in ); 51 RollerSpellCheck.dictionary = new SpellDictionary( inR ); 52 53 } catch (IOException ioe) { 54 mLogger.error("RollerSpellCheck unable to load SpellDictionary", 55 ioe); 56 throw new RollerException(ioe); 57 } 58 } 59 60 64 private RollerSpellCheck() 65 { 66 spellCheck = new SpellChecker(dictionary); 67 spellCheck.addSpellCheckListener(this); 68 } 69 70 74 public static RollerSpellCheck getInstance() throws RollerException 75 { 76 if (RollerSpellCheck.dictionary == null) 77 { 78 throw new RollerException( 79 "RollerSpellCheck.SpellDictionary has not been defined"); 80 } 81 82 return new RollerSpellCheck(); 83 } 84 85 90 public void spellingError(SpellCheckEvent event) 91 { 92 spellCheckEvents.add( event ); 93 } 94 95 102 public ArrayList checkSpelling(String str) throws RollerException 103 { 104 spellCheck.checkSpelling( new StringWordTokenizer(str) ); 105 return spellCheckEvents; 106 } 107 108 112 public static ArrayList getSpellingErrors(String str) throws RollerException 113 { 114 RollerSpellCheck rCheck = RollerSpellCheck.getInstance(); 115 return rCheck.checkSpelling(str); 116 } 117 } | Popular Tags |