KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileReader JavaDoc;
27 import java.io.OutputStreamWriter JavaDoc;
28 import java.util.List JavaDoc;
29
30
31 /**
32  * Models the result of a spell check of a single word.
33  *<p>
34  */

35 public class FileSpellChecker {
36     private String JavaDoc _aspellExeFilename;
37     private AspellEngine _spellEngine = null;
38     private Validator _spellValidator = null;
39
40     public FileSpellChecker(String JavaDoc aspellExeFilename) {
41         _aspellExeFilename = aspellExeFilename;
42     }
43
44     public FileSpellChecker() {
45         this("O:\\local\\aspell\\aspell.exe");
46     }
47
48     public static void main(String JavaDoc[] args) {
49         int exitStatus;
50
51         String JavaDoc inputFilename = "spellTest.txt";
52
53         try {
54             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new FileReader JavaDoc(
55                         inputFilename));
56             BufferedWriter JavaDoc output = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(
57                         System.out));
58
59             FileSpellChecker checker = new FileSpellChecker();
60
61             checker.checkFile(input, output);
62
63             input.close();
64             output.close();
65
66             exitStatus = 0;
67         } catch (Exception JavaDoc e) {
68             e.printStackTrace(System.err);
69             exitStatus = 1;
70         }
71
72         System.exit(exitStatus);
73     }
74
75     /**
76  * @return <i>true</i> if file completely checked and <i>false</i> if the user
77  * interupted the checking.
78  */

79     public boolean checkFile(BufferedReader JavaDoc input, BufferedWriter JavaDoc output)
80         throws SpellException {
81         try {
82             String JavaDoc line = input.readLine();
83
84             while (line != null) {
85                 String JavaDoc checkedLine;
86
87                 if (line.trim().equals("")) {
88                     checkedLine = line;
89                 } else {
90                     List JavaDoc results = _getSpellEngine().checkLine(line);
91
92                     checkedLine = _getSpellValidator().validate(line, results);
93
94                     if (checkedLine == null) {
95                         return false;
96                     }
97                 }
98
99                 output.write(checkedLine);
100
101                 line = input.readLine();
102
103                 // Force that the last line in buffer does NOT have a newline
104
if (line != null) {
105                     output.write('\n');
106                 }
107             }
108         } catch (Exception JavaDoc e) {
109             stop();
110
111             if (e instanceof SpellException) {
112                 throw (SpellException) e;
113             } else {
114                 throw new SpellException("Error communicating with the aspell subprocess",
115                     e);
116             }
117         }
118
119         return true;
120     }
121
122     public String JavaDoc getAspellExeFilename() {
123         return _aspellExeFilename;
124     }
125
126     public void stop() {
127         if (_spellEngine != null) {
128             _spellEngine.stop();
129             _spellEngine = null;
130         }
131     }
132
133     private Engine _getSpellEngine() throws SpellException {
134         if (_spellEngine == null) {
135             String JavaDoc aSpellCommandLine = _aspellExeFilename + " pipe";
136             _spellEngine = new AspellEngine(aSpellCommandLine);
137         }
138
139         return _spellEngine;
140     }
141
142     private Validator _getSpellValidator() {
143         if (_spellValidator == null) {
144             _spellValidator = new Validator();
145         }
146
147         return _spellValidator;
148     }
149 }
150
Popular Tags