KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28
29 /**
30  * Models the result of a spell check of a single word.
31  *<p>
32  * Features of a spell check result are:</p>
33  * <ul>
34  * <li>contains the result of checking one&nbsp; word</li>
35  * <li>has the original word</li>
36  * <li>has a type of result (i.e. OK, ERROR, NONE, SUGGESTION)</li>
37  * <li>has a list of suggested replacement words (if type is SUGGESTION)</li>
38  * <li>has an offset into the line where the original word was found</li>
39  * </ul>
40  * <p>The possible result types are:
41  * <dl>
42  * <dt>OK</dt>
43  * <dd>the original word was found in the dictionary.</dd>
44  * <dt>ERROR</dt>
45  * <dd>Internal error (e.g. aspell process died, wrong version of aspell, phase
46  * of moon wrong).</dd>
47  * <dt>NONE</dt>
48  * <dd>the original word was not found in the dictionary and no suggestions were
49  * found.</dd>
50  * <dt>SUGGESTION</dt>
51  * <dd>the original word was not found in the dictionary and one or more
52  * suggested replacements were found.</dd>
53  * </dl>
54  */

55 public class Result {
56     public static final Type ERROR = new Type("Error");
57     public static final Type OK = new Type("OK ");
58     public static final Type NONE = new Type("None");
59     public static final Type SUGGESTION = new Type("Suggestion");
60     private int _offset;
61     private Type _type;
62     private List JavaDoc _suggestions;
63     private String JavaDoc _originalWord;
64
65     public Result(String JavaDoc line) {
66         if ((line == null) || (line.length() <= 0)) {
67             processError(line);
68         } else if (line.charAt(0) == '*') {
69             processOk(line);
70         } else if (line.charAt(0) == '&') {
71             processSuggestion(line);
72         } else if (line.charAt(0) == '#') {
73             processNone(line);
74         } else {
75             processError(line);
76         }
77     }
78
79     public int getOffset() {
80         return _offset;
81     }
82
83     public Type getType() {
84         return _type;
85     }
86
87     public List JavaDoc getSuggestions() {
88         return _suggestions;
89     }
90
91     public String JavaDoc getOriginalWord() {
92         return _originalWord;
93     }
94
95     public String JavaDoc toString() {
96         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
97         buff.append("[type:");
98         buff.append(_type);
99         buff.append(",originalWord:");
100         buff.append(_originalWord);
101         buff.append(",offset:");
102         buff.append(_offset);
103         buff.append(",suggestions:");
104         buff.append(_suggestions);
105
106         return buff.toString();
107     }
108
109     private void processError(String JavaDoc line) {
110         _offset = 0;
111         _type = ERROR;
112         _suggestions = new ArrayList JavaDoc();
113         _originalWord = "";
114     }
115
116     private void processOk(String JavaDoc line) {
117         _offset = 0;
118         _type = OK;
119         _suggestions = new ArrayList JavaDoc();
120         _originalWord = "";
121     }
122
123     private void processNone(String JavaDoc line) {
124         _type = NONE;
125         _suggestions = new ArrayList JavaDoc();
126
127         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line);
128         st.nextToken(); // skip '#'
129
_originalWord = st.nextToken();
130         _offset = Integer.parseInt(st.nextToken());
131     }
132
133     private void processSuggestion(String JavaDoc line) {
134         _type = SUGGESTION;
135
136         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line);
137         st.nextToken(); // skip '#'
138
_originalWord = st.nextToken();
139
140         int count = Integer.parseInt(st.nextToken().trim());
141         _suggestions = new ArrayList JavaDoc(count);
142         _offset = Integer.parseInt(st.nextToken(":").trim());
143
144         st = new StringTokenizer JavaDoc(st.nextToken(":"), ",");
145
146         while (st.hasMoreTokens()) {
147             String JavaDoc suggestion = st.nextToken().trim();
148             _suggestions.add(suggestion);
149         }
150     }
151
152     private static class Type {
153         private final String JavaDoc _typeName;
154
155         Type(String JavaDoc typeName) {
156             _typeName = typeName;
157         }
158
159         public String JavaDoc toString() {
160             return _typeName;
161         }
162     }
163 }
164
Popular Tags