001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.modules.spell;
014:
015: import java.io.File;
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.util.ArrayList;
019: import java.util.HashSet;
020: import java.util.List;
021: import java.util.Set;
022:
023: import com.swabunga.spell.engine.Configuration;
024: import com.swabunga.spell.engine.GenericSpellDictionary;
025: import com.swabunga.spell.engine.SpellDictionary;
026: import com.swabunga.spell.event.SpellCheckEvent;
027: import com.swabunga.spell.event.SpellCheckListener;
028: import com.swabunga.spell.event.SpellChecker;
029: import com.swabunga.spell.event.StringWordTokenizer;
030:
031: /**
032: * DOCUMENT ME!
033: *
034: * @author cburkey
035: */
036: public class WebSpellChecker extends SpellChecker implements
037: SpellCheckListener {
038: protected Set fieldIgnoreList;
039: protected List fieldErrors;
040: public final static String KEY = "WebSpellChecker";
041:
042: public WebSpellChecker(String inPath) throws FileNotFoundException,
043: IOException {
044: //dict/english.0
045: this (new GenericSpellDictionary(new File(inPath)));
046: }
047:
048: /**
049: * Constructor for WebSpellChecker.
050: *
051: * @param dictionary
052: */
053: public WebSpellChecker(SpellDictionary dictionary) {
054: super (dictionary);
055: addSpellCheckListener(this );
056: getConfiguration().setBoolean(
057: Configuration.SPELL_IGNORESENTENCECAPITALIZATION, true);
058: getConfiguration().setBoolean(
059: Configuration.SPELL_IGNOREINTERNETADDRESSES, true);
060: getConfiguration().setBoolean(
061: Configuration.SPELL_IGNOREUPPERCASE, true);
062: getConfiguration().setBoolean(
063: Configuration.SPELL_IGNOREDIGITWORDS, true);
064: }
065:
066: public List checkAllWords(String inWords) {
067: fieldErrors = new ArrayList();
068:
069: //clean up the words
070: inWords = inWords.replaceAll("(?m)(?s)<[^>]*>", " ");
071: StringWordTokenizer words = new StringWordTokenizer(inWords);
072: checkSpelling(words);
073: return getErrors();
074: }
075:
076: /**
077: * Method addIgnore.
078: *
079: * @param oldChanges
080: */
081: public void addIgnore(SpellCheckEvent inEvent) {
082: getIgnoreList().add(inEvent.getInvalidWord());
083: }
084:
085: /**
086: * @see com.swabunga.spell.event.SpellCheckListener#spellingError(SpellCheckEvent)
087: */
088: public void spellingError(SpellCheckEvent event) {
089: if (!getIgnoreList().contains(event.getInvalidWord())
090: && areAllLetters(event.getInvalidWord())) {
091: getErrors().add(event);
092: }
093: }
094:
095: protected void setIgnoreList(Set inIgnoreList) {
096: fieldIgnoreList = inIgnoreList;
097: }
098:
099: protected Set getIgnoreList() {
100: if (fieldIgnoreList == null) {
101: fieldIgnoreList = new HashSet();
102: }
103:
104: return fieldIgnoreList;
105: }
106:
107: protected boolean areAllLetters(String inError) {
108: for (int i = 0; i < inError.length(); i++) {
109: if (!Character.isLetter(inError.charAt(i))) {
110: return false;
111: }
112: }
113:
114: return true;
115: }
116:
117: /**
118: * @return
119: */
120: public List getErrors() {
121: return fieldErrors;
122: }
123:
124: }
|