KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > spell > WebSpellChecker


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.spell;
14
15 import java.io.File JavaDoc;
16 import java.io.FileNotFoundException JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Set JavaDoc;
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 /**
32  * DOCUMENT ME!
33  *
34  * @author cburkey
35  */

36 public class WebSpellChecker extends SpellChecker implements SpellCheckListener
37 {
38     protected Set JavaDoc fieldIgnoreList;
39     protected List JavaDoc fieldErrors;
40     public final static String JavaDoc KEY ="WebSpellChecker";
41     
42     public WebSpellChecker(String JavaDoc inPath) throws FileNotFoundException JavaDoc, IOException JavaDoc
43     {
44         //dict/english.0
45
this(new GenericSpellDictionary(new File JavaDoc(inPath)));
46     }
47
48     /**
49      * Constructor for WebSpellChecker.
50      *
51      * @param dictionary
52      */

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 JavaDoc checkAllWords(String JavaDoc inWords)
64     {
65         fieldErrors = new ArrayList JavaDoc();
66
67         //clean up the words
68
inWords = inWords.replaceAll("(?m)(?s)<[^>]*>", " ");
69         StringWordTokenizer words = new StringWordTokenizer(inWords);
70         checkSpelling(words);
71         return getErrors();
72     }
73
74
75     /**
76      * Method addIgnore.
77      *
78      * @param oldChanges
79      */

80     public void addIgnore(SpellCheckEvent inEvent)
81     {
82         getIgnoreList().add(inEvent.getInvalidWord());
83     }
84
85 /**
86  * @see com.swabunga.spell.event.SpellCheckListener#spellingError(SpellCheckEvent)
87  */

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 JavaDoc inIgnoreList)
98 {
99     fieldIgnoreList = inIgnoreList;
100 }
101
102 protected Set JavaDoc getIgnoreList()
103 {
104     if (fieldIgnoreList == null)
105     {
106         fieldIgnoreList = new HashSet JavaDoc();
107     }
108
109     return fieldIgnoreList;
110 }
111
112 protected boolean areAllLetters(String JavaDoc 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 /**
126  * @return
127  */

128 public List JavaDoc getErrors()
129 {
130     return fieldErrors;
131 }
132
133
134 }
135
Popular Tags