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.io.BufferedReader;
025: import java.io.BufferedWriter;
026: import java.io.IOException;
027: import java.io.InputStreamReader;
028: import java.io.OutputStreamWriter;
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: /**
033: * Models a spelling checker
034: *<p>
035: *
036: */
037: public class AspellEngine implements Engine {
038: BufferedReader _aSpellReader;
039: BufferedWriter _aSpellWriter;
040: String _aSpellWelcomeMsg;
041: Process _aSpellProcess;
042:
043: public AspellEngine(String aSpellCommandLine) throws SpellException {
044: try {
045: Runtime runtime = Runtime.getRuntime();
046: _aSpellProcess = runtime.exec(aSpellCommandLine);
047:
048: _aSpellReader = new BufferedReader(new InputStreamReader(
049: _aSpellProcess.getInputStream()));
050:
051: _aSpellWriter = new BufferedWriter(new OutputStreamWriter(
052: _aSpellProcess.getOutputStream()));
053:
054: _aSpellWelcomeMsg = _aSpellReader.readLine();
055: } catch (IOException e) {
056: String msg = "Cannot create aspell process.";
057: throw new SpellException(msg, e);
058: }
059: }
060:
061: /**
062: * Spell check a list of words
063: *<p>
064: * Spell checks the list of works in <code>words</code> and returns a list of
065: * {@link Result}s. There is one {@link Result} for each word in
066: * <code>words</code>.
067: *<p>
068: * @param words {@link String} with list of works to be spell checked.
069: * @return List of {@link Result}
070: */
071: public List checkLine(String line) throws SpellException {
072: try {
073: List results = new ArrayList();
074:
075: final String spellCheckLinePrefix = "^";
076: _aSpellWriter.write(spellCheckLinePrefix + line);
077: _aSpellWriter.newLine();
078: _aSpellWriter.flush();
079:
080: String response = _aSpellReader.readLine();
081:
082: while ((response != null) && !response.equals("")) {
083: Result result = new Result(response);
084: results.add(result);
085:
086: response = _aSpellReader.readLine();
087: }
088:
089: return results;
090: } catch (IOException e) {
091: String msg = "Cannot access aspell process.";
092: throw new SpellException(msg, e);
093: }
094: }
095:
096: public String getVersion() {
097: return _aSpellWelcomeMsg;
098: }
099:
100: public void stop() {
101: _aSpellProcess.destroy();
102: }
103: }
|