1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text.spelling.engine;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.Locale;
19 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jdt.ui.PreferenceConstants;
27 * Default spell checker for standard text.
31 public class DefaultSpellChecker implements ISpellChecker {
33 /** Array of URL prefixes */
34 public static final String[] URL_PREFIXES= new String[] { "http://", "https://", "www.", "ftp://", "ftps://", "news://", "mailto://" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$
37 * Does this word contain digits?
39 * @param word the word to check
40 * @return <code>true</code> iff this word contains digits, <code>false></code> otherwise
42 protected static boolean isDigits(final String word) {
44 for (int index= 0; index < word.length(); index++) {
46 if (Character.isDigit(word.charAt(index)))
53 * Does this word contain mixed-case letters?
58 * <code>true</code> iff the specified word starts a new
59 * sentence, <code>false</code> otherwise
60 * @return <code>true</code> iff the contains mixed-case letters, <code>false</code>
63 protected static boolean isMixedCase(final String word, final boolean sentence) {
65 final int length= word.length();
66 boolean upper= Character.isUpperCase(word.charAt(0));
68 if (sentence && upper && (length > 1))
69 upper= Character.isUpperCase(word.charAt(1));
73 for (int index= length - 1; index > 0; index--) {
74 if (Character.isLowerCase(word.charAt(index)))
79 for (int index= length - 1; index > 0; index--) {
80 if (Character.isUpperCase(word.charAt(index)))
88 * Does this word contain upper-case letters only?
92 * @return <code>true</code> iff this word only contains upper-case
93 * letters, <code>false</code> otherwise
95 protected static boolean isUpperCase(final String word) {
97 for (int index= word.length() - 1; index >= 0; index--) {
99 if (Character.isLowerCase(word.charAt(index)))
106 * Does this word look like an URL?
110 * @return <code>true</code> iff this word looks like an URL, <code>false</code>
113 protected static boolean isUrl(final String word) {
115 for (int index= 0; index < URL_PREFIXES.length; index++) {
117 if (word.startsWith(URL_PREFIXES[index]))
124 * The dictionaries to use for spell checking. Synchronized to avoid
125 * concurrent modifications.
127 private final Set<ISpellDictionary> fDictionaries= Collections.synchronizedSet(new HashSet<ISpellDictionary>());
130 * The words to be ignored. Synchronized to avoid concurrent modifications.
132 private final Set<String> fIgnored= Collections.synchronizedSet(new HashSet<String>());
135 * The preference store. Assumes the <code>IPreferenceStore</code>
136 * implementation is thread safe.
138 private final IPreferenceStore fPreferences;
141 * The locale of this checker.
144 private Locale fLocale;
147 * Creates a new default spell checker.
149 * @param store the preference store for this spell checker
150 * @param locale the locale
152 public DefaultSpellChecker(IPreferenceStore store, Locale locale) {
153 Assert.isLegal(store != null);
154 Assert.isLegal(locale != null);
161 * @see org.eclipse.spelling.done.ISpellChecker#addDictionary(org.eclipse.spelling.done.ISpellDictionary)
163 public final void addDictionary(final ISpellDictionary dictionary) {
164 // synchronizing is necessary as this is a write access
165 fDictionaries.add(dictionary);
169 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellChecker#acceptsWords()
171 public boolean acceptsWords() {
172 // synchronizing might not be needed here since acceptWords is
173 // a read-only access and only called in the same thread as
174 // the modifying methods add/checkWord (?)
175 Set<ISpellDictionary> copy;
176 synchronized (fDictionaries) {
177 copy= new HashSet<ISpellDictionary>(fDictionaries);
180 ISpellDictionary dictionary= null;
181 for (final Iterator<ISpellDictionary> iterator= copy.iterator(); iterator.hasNext();) {
183 dictionary= iterator.next();
184 if (dictionary.acceptsWords())
191 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker#addWord(java.lang.String)
193 public void addWord(final String word) {
194 // synchronizing is necessary as this is a write access
195 Set<ISpellDictionary> copy;
196 synchronized (fDictionaries) {
197 copy= new HashSet<ISpellDictionary>(fDictionaries);
200 final String addable= word.toLowerCase();
201 for (final Iterator<ISpellDictionary> iterator= copy.iterator(); iterator.hasNext();) {
202 ISpellDictionary dictionary= iterator.next();
203 if (dictionary.acceptsWords())
204 dictionary.addWord(addable);
210 * @see org.eclipse.jdt.ui.text.spelling.engine.ISpellChecker#checkWord(java.lang.String)
212 public final void checkWord(final String word) {
213 // synchronizing is necessary as this is a write access
214 fIgnored.remove(word.toLowerCase());
218 * @see org.eclipse.spelling.done.ISpellChecker#execute(org.eclipse.spelling.ISpellCheckTokenizer)
220 public void execute(final ISpellEventListener listener, final ISpellCheckIterator iterator) {
222 final boolean ignoreDigits= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_DIGITS);
223 final boolean ignoreMixed= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_MIXED);
224 final boolean ignoreSentence= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_SENTENCE);
225 final boolean ignoreUpper= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_UPPER);
226 final boolean ignoreURLS= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_URLS);
227 final boolean ignoreNonLetters= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_NON_LETTERS);
228 final boolean ignoreSingleLetters= fPreferences.getBoolean(PreferenceConstants.SPELLING_IGNORE_SINGLE_LETTERS);
229 final int problemsThreshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROBLEMS_THRESHOLD);
231 iterator.setIgnoreSingleLetters(ignoreSingleLetters);
233 Iterator<ISpellDictionary> iter= fDictionaries.iterator();
234 while (iter.hasNext())
235 iter.next().setStripNonLetters(ignoreNonLetters);
238 boolean starts= false;
241 while (problemCount <= problemsThreshold && iterator.hasNext()) {
243 word= iterator.next();
246 // synchronizing is necessary as this is called inside the reconciler
247 if (!fIgnored.contains(word)) {
249 starts= iterator.startsSentence();
250 if (!isCorrect(word)) {
252 boolean isMixed= isMixedCase(word, true);
253 boolean isUpper= isUpperCase(word);
254 boolean isDigits= isDigits(word);
255 boolean isURL= isUrl(word);
257 if ( !ignoreMixed && isMixed || !ignoreUpper && isUpper || !ignoreDigits && isDigits || !ignoreURLS && isURL || !(isMixed || isUpper || isDigits || isURL)) {
258 listener.handle(new SpellEvent(this, word, iterator.getBegin(), iterator.getEnd(), starts, false));
264 if (!ignoreSentence && starts && Character.isLowerCase(word.charAt(0))) {
265 listener.handle(new SpellEvent(this, word, iterator.getBegin(), iterator.getEnd(), true, true));
275 * @see org.eclipse.spelling.done.ISpellChecker#getProposals(java.lang.String,boolean)
277 public Set<RankedWordProposal> getProposals(final String word, final boolean sentence) {
279 // synchronizing might not be needed here since getProposals is
280 // a read-only access and only called in the same thread as
281 // the modifing methods add/removeDictionary (?)
282 Set<ISpellDictionary> copy;
283 synchronized (fDictionaries) {
284 copy= new HashSet<ISpellDictionary>(fDictionaries);
287 ISpellDictionary dictionary= null;
288 final HashSet<RankedWordProposal> proposals= new HashSet<RankedWordProposal>();
290 for (final Iterator<ISpellDictionary> iterator= copy.iterator(); iterator.hasNext();) {
292 dictionary= iterator.next();
293 proposals.addAll(dictionary.getProposals(word, sentence));
299 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker#ignoreWord(java.lang.String)
301 public final void ignoreWord(final String word) {
302 // synchronizing is necessary as this is a write access
303 fIgnored.add(word.toLowerCase());
307 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker#isCorrect(java.lang.String)
309 public final boolean isCorrect(final String word) {
310 // synchronizing is necessary as this is called from execute
311 Set<ISpellDictionary> copy;
312 synchronized (fDictionaries) {
313 copy= new HashSet<ISpellDictionary>(fDictionaries);
316 if (fIgnored.contains(word.toLowerCase()))
319 ISpellDictionary dictionary= null;
320 for (final Iterator<ISpellDictionary> iterator= copy.iterator(); iterator.hasNext();) {
322 dictionary= iterator.next();
323 if (dictionary.isCorrect(word))
330 * @see org.eclipse.spelling.done.ISpellChecker#removeDictionary(org.eclipse.spelling.done.ISpellDictionary)
332 public final void removeDictionary(final ISpellDictionary dictionary) {
333 // synchronizing is necessary as this is a write access
334 fDictionaries.remove(dictionary);
338 * @see org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker#getLocale()
341 public Locale getLocale() {