KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.BufferedReader JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.List JavaDoc;
31
32
33 /**
34  * Models a spelling checker
35  *<p>
36  *
37  */

38 public class AspellEngine implements Engine {
39     BufferedReader JavaDoc _aSpellReader;
40     BufferedWriter JavaDoc _aSpellWriter;
41     String JavaDoc _aSpellWelcomeMsg;
42     Process JavaDoc _aSpellProcess;
43
44     public AspellEngine(String JavaDoc aSpellCommandLine) throws SpellException {
45         try {
46             Runtime JavaDoc runtime = Runtime.getRuntime();
47             _aSpellProcess = runtime.exec(aSpellCommandLine);
48
49             _aSpellReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
50                         _aSpellProcess.getInputStream()));
51
52             _aSpellWriter = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(
53                         _aSpellProcess.getOutputStream()));
54
55             _aSpellWelcomeMsg = _aSpellReader.readLine();
56         } catch (IOException JavaDoc e) {
57             String JavaDoc msg = "Cannot create aspell process.";
58             throw new SpellException(msg, e);
59         }
60     }
61
62     /**
63  * Spell check a list of words
64  *<p>
65  * Spell checks the list of works in <code>words</code> and returns a list of
66  * {@link Result}s. There is one {@link Result} for each word in
67  * <code>words</code>.
68  *<p>
69  * @param words {@link String} with list of works to be spell checked.
70  * @return List of {@link Result}
71  */

72     public List JavaDoc checkLine(String JavaDoc line) throws SpellException {
73         try {
74             List JavaDoc results = new ArrayList JavaDoc();
75
76             final String JavaDoc spellCheckLinePrefix = "^";
77             _aSpellWriter.write(spellCheckLinePrefix + line);
78             _aSpellWriter.newLine();
79             _aSpellWriter.flush();
80
81             String JavaDoc response = _aSpellReader.readLine();
82
83             while ((response != null) && !response.equals("")) {
84                 Result result = new Result(response);
85                 results.add(result);
86
87                 response = _aSpellReader.readLine();
88             }
89
90             return results;
91         } catch (IOException JavaDoc e) {
92             String JavaDoc msg = "Cannot access aspell process.";
93             throw new SpellException(msg, e);
94         }
95     }
96
97     public String JavaDoc getVersion() {
98         return _aSpellWelcomeMsg;
99     }
100
101     public void stop() {
102         _aSpellProcess.destroy();
103     }
104 }
105
Popular Tags