KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > model > RollerSpellCheck


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 JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 /**
19  * Since this class cannot be thread-safe due to the
20  * SpellCheckListener interface, first a "canonical"
21  * SpellDictionary must be instantiated with the path
22  * to the dictionary. Subsequently, call getInstance()
23  * which will return a new instance of RollerSpellCheck
24  * using the "precompiled" SpellDictionary, or will throw
25  * a NullPointerException. A better way can
26  * probably be found.
27  **/

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 JavaDoc spellCheckEvents = new ArrayList JavaDoc();
37
38     /**
39      * Initializer which takes an InputStream for
40      * /WEB-INF/english.0 to load the SpellDictionary.
41      * Building the SpellDictionary, and thus the SpellChecker
42      * is an expensive operation.
43      * You can get this InputStream with ServletContext.getResource(
44      * "/WEB-INF/english.0"). Throws a RollerException if
45      * SpellDictionary cannot be instantiated.
46      **/

47     public static void init(InputStream JavaDoc in) throws RollerException
48     {
49         try {
50             InputStreamReader JavaDoc inR = new InputStreamReader JavaDoc( in );
51             RollerSpellCheck.dictionary = new SpellDictionary( inR );
52
53         } catch (IOException JavaDoc ioe) {
54             mLogger.error("RollerSpellCheck unable to load SpellDictionary",
55                 ioe);
56             throw new RollerException(ioe);
57         }
58     }
59
60     /**
61      * Private constructor taking a prebuilt SpellChecker
62      * as an argument.
63      **/

64     private RollerSpellCheck()
65     {
66         spellCheck = new SpellChecker(dictionary);
67         spellCheck.addSpellCheckListener(this);
68     }
69
70     /**
71      * Returns a new instance of RollerSpellCheck, using
72      * the (hopefully) prebuilt SpellChecker.
73      **/

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     /**
86      * Fulfills interface SpellCheckListener.
87      * SpellCheckEvent is placed into the ArrayList
88      * spellCheckEvents held by this RollerSpellCheck.
89      **/

90     public void spellingError(SpellCheckEvent event)
91     {
92         spellCheckEvents.add( event );
93     }
94
95     /**
96      * This is the method to check spelling.
97      * The submitted String is "parsed" by SpellChecker,
98      * SpellCheckEvents are placed into an ArrayList, which
99      * is returned to the caller. A SpellCheckEvent contains
100      * the "suspect" word, and a LinkedList of suggested replacements.
101      */

102     public ArrayList JavaDoc checkSpelling(String JavaDoc str) throws RollerException
103     {
104         spellCheck.checkSpelling( new StringWordTokenizer(str) );
105         return spellCheckEvents;
106     }
107
108     /**
109      * Convenience method. Creates a RollerSpellCheck object
110      * and calls checkSpelling(str) on it, returning the ArrayList.
111      */

112     public static ArrayList JavaDoc getSpellingErrors(String JavaDoc str) throws RollerException
113     {
114         RollerSpellCheck rCheck = RollerSpellCheck.getInstance();
115         return rCheck.checkSpelling(str);
116     }
117 }
Popular Tags