001: /*
002: * $Revision: 1.1 $
003: * $Date: 2006/06/10 13:32:32 $
004: * $Author: fdietz $
005: *
006: * Copyright (C) 2001 C. Scott Willy
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package org.columba.mail.spellcheck.cswilly;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: /**
029: * Models the result of a spell check of a single word.
030: *<p>
031: * Features of a spell check result are:</p>
032: * <ul>
033: * <li>contains the result of checking one word</li>
034: * <li>has the original word</li>
035: * <li>has a type of result (i.e. OK, ERROR, NONE, SUGGESTION)</li>
036: * <li>has a list of suggested replacement words (if type is SUGGESTION)</li>
037: * <li>has an offset into the line where the original word was found</li>
038: * </ul>
039: * <p>The possible result types are:
040: * <dl>
041: * <dt>OK</dt>
042: * <dd>the original word was found in the dictionary.</dd>
043: * <dt>ERROR</dt>
044: * <dd>Internal error (e.g. aspell process died, wrong version of aspell, phase
045: * of moon wrong).</dd>
046: * <dt>NONE</dt>
047: * <dd>the original word was not found in the dictionary and no suggestions were
048: * found.</dd>
049: * <dt>SUGGESTION</dt>
050: * <dd>the original word was not found in the dictionary and one or more
051: * suggested replacements were found.</dd>
052: * </dl>
053: */
054: public class Result {
055: public static final Type ERROR = new Type("Error");
056: public static final Type OK = new Type("OK ");
057: public static final Type NONE = new Type("None");
058: public static final Type SUGGESTION = new Type("Suggestion");
059: private int _offset;
060: private Type _type;
061: private List _suggestions;
062: private String _originalWord;
063:
064: public Result(String line) {
065: if ((line == null) || (line.length() <= 0)) {
066: processError(line);
067: } else if (line.charAt(0) == '*') {
068: processOk(line);
069: } else if (line.charAt(0) == '&') {
070: processSuggestion(line);
071: } else if (line.charAt(0) == '#') {
072: processNone(line);
073: } else {
074: processError(line);
075: }
076: }
077:
078: public int getOffset() {
079: return _offset;
080: }
081:
082: public Type getType() {
083: return _type;
084: }
085:
086: public List getSuggestions() {
087: return _suggestions;
088: }
089:
090: public String getOriginalWord() {
091: return _originalWord;
092: }
093:
094: public String toString() {
095: StringBuffer buff = new StringBuffer();
096: buff.append("[type:");
097: buff.append(_type);
098: buff.append(",originalWord:");
099: buff.append(_originalWord);
100: buff.append(",offset:");
101: buff.append(_offset);
102: buff.append(",suggestions:");
103: buff.append(_suggestions);
104:
105: return buff.toString();
106: }
107:
108: private void processError(String line) {
109: _offset = 0;
110: _type = ERROR;
111: _suggestions = new ArrayList();
112: _originalWord = "";
113: }
114:
115: private void processOk(String line) {
116: _offset = 0;
117: _type = OK;
118: _suggestions = new ArrayList();
119: _originalWord = "";
120: }
121:
122: private void processNone(String line) {
123: _type = NONE;
124: _suggestions = new ArrayList();
125:
126: StringTokenizer st = new StringTokenizer(line);
127: st.nextToken(); // skip '#'
128: _originalWord = st.nextToken();
129: _offset = Integer.parseInt(st.nextToken());
130: }
131:
132: private void processSuggestion(String line) {
133: _type = SUGGESTION;
134:
135: StringTokenizer st = new StringTokenizer(line);
136: st.nextToken(); // skip '#'
137: _originalWord = st.nextToken();
138:
139: int count = Integer.parseInt(st.nextToken().trim());
140: _suggestions = new ArrayList(count);
141: _offset = Integer.parseInt(st.nextToken(":").trim());
142:
143: st = new StringTokenizer(st.nextToken(":"), ",");
144:
145: while (st.hasMoreTokens()) {
146: String suggestion = st.nextToken().trim();
147: _suggestions.add(suggestion);
148: }
149: }
150:
151: private static class Type {
152: private final String _typeName;
153:
154: Type(String typeName) {
155: _typeName = typeName;
156: }
157:
158: public String toString() {
159: return _typeName;
160: }
161: }
162: }
|