KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spellcheck > cswilly > Validator


1 /*
2  * $Revision: 1.1 $
3  * $Date: 2006/06/10 13:32:32 $
4  * $Author: fdietz $
5  *
6  * Copyright (C) 2001 C. Scott Willy
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22 package org.columba.mail.spellcheck.cswilly;
23
24
25 //import java.io.*;
26
import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29
30
31 /**
32  * A validator of a spell check results
33  *<p>
34  * After a spell check engine runs, its results must be validated (normally by
35  * a user). The {@link Validator} class provides this service.
36  */

37 public class Validator {
38     private final HashMap JavaDoc _changeAllMap = new HashMap JavaDoc();
39     private final HashSet JavaDoc _ignoreAllSet = new HashSet JavaDoc();
40
41     /**
42  * Validate a line of words that have the <code>results</code> of a spell
43  * check.
44  *<p>
45  * @param line String with a line of words that are to be corrected
46  * @param results List of {@link Result} of a spell check
47  * @return new line with all corrected words validated
48  */

49     public String JavaDoc validate(String JavaDoc line, List JavaDoc results) {
50         String JavaDoc checkedLine = line;
51
52         for (int ii = results.size() - 1; ii >= 0; ii--) {
53             Result result = (Result) results.get(ii);
54
55             if (result.getType() != Result.OK) {
56                 String JavaDoc replacementWord;
57
58                 if (_changeAllMap.containsKey(result.getOriginalWord())) {
59                     replacementWord = (String JavaDoc) _changeAllMap.get(result.getOriginalWord());
60                 } else if (_ignoreAllSet.contains(result.getOriginalWord())) {
61                     replacementWord = result.getOriginalWord();
62                 } else {
63                     replacementWord = validate(result);
64
65                     if (replacementWord == null) {
66                         checkedLine = null;
67
68                         break;
69                     }
70                 }
71
72                 if (replacementWord != null) {
73                     checkedLine = replaceWord(checkedLine,
74                             result.getOriginalWord(), result.getOffset(),
75                             replacementWord);
76                 }
77             }
78         }
79
80         return checkedLine;
81     }
82
83     /**
84  * Validates a single correction
85  *<p>
86  *
87  *<p>
88  * @param result A {@link Result} of spell checking one word
89  * @return validated correction (this is the replacement word). <i>null</i>
90  * is returned if the operation is cancelled. The replacement word
91  * maybe the same or different from the original word in
92  * <code>result</code>.
93  */

94     public String JavaDoc validate(Result result) {
95         String JavaDoc replacementWord = null;
96
97         ValidationDialog validationDialog;
98         validationDialog = new ValidationDialog(result.getOriginalWord(),
99                 result.getSuggestions());
100         validationDialog.setVisible(true);
101
102         ValidationDialog.UserAction userAction = validationDialog.getUserAction();
103
104         if (userAction == ValidationDialog.CANCEL) {
105             replacementWord = null;
106         } else if (userAction == ValidationDialog.CHANGE_ALL) {
107             if (_changeAllMap.containsKey(result.getOriginalWord())) {
108                 System.err.println(
109                     "Validator error: Change all twice same word: " +
110                     result.getOriginalWord());
111             }
112
113             _changeAllMap.put(result.getOriginalWord(),
114                 validationDialog.getSelectedWord());
115             replacementWord = validationDialog.getSelectedWord();
116         } else if (userAction == ValidationDialog.CHANGE) {
117             replacementWord = validationDialog.getSelectedWord();
118         } else if (userAction == ValidationDialog.IGNORE_ALL) {
119             if (_ignoreAllSet.contains(result.getOriginalWord())) {
120                 System.err.println(
121                     "Validator error: Ignore all twice same word: " +
122                     result.getOriginalWord());
123             }
124
125             _ignoreAllSet.add(result.getOriginalWord());
126             replacementWord = result.getOriginalWord();
127         } else if (userAction == ValidationDialog.IGNORE) {
128             replacementWord = result.getOriginalWord();
129         }
130
131         return replacementWord;
132     }
133
134     /**
135  * Helper method to replace the original word with the correction in the line
136  */

137     protected String JavaDoc replaceWord(String JavaDoc originalLine, String JavaDoc originalWord,
138         int originalIndex, String JavaDoc replacementWord) {
139         String JavaDoc leftText = originalLine.substring(0, originalIndex - 1);
140         String JavaDoc rightText = originalLine.substring((originalIndex +
141                 originalWord.length()) - 1);
142
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         buf.append(leftText);
145         buf.append(replacementWord);
146         buf.append(rightText);
147
148         return buf.toString();
149     }
150 }
151
Popular Tags