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.FileReader;
027: import java.io.OutputStreamWriter;
028: import java.util.List;
029:
030: /**
031: * Models the result of a spell check of a single word.
032: *<p>
033: */
034: public class FileSpellChecker {
035: private String _aspellExeFilename;
036: private AspellEngine _spellEngine = null;
037: private Validator _spellValidator = null;
038:
039: public FileSpellChecker(String aspellExeFilename) {
040: _aspellExeFilename = aspellExeFilename;
041: }
042:
043: public FileSpellChecker() {
044: this ("O:\\local\\aspell\\aspell.exe");
045: }
046:
047: public static void main(String[] args) {
048: int exitStatus;
049:
050: String inputFilename = "spellTest.txt";
051:
052: try {
053: BufferedReader input = new BufferedReader(new FileReader(
054: inputFilename));
055: BufferedWriter output = new BufferedWriter(
056: new OutputStreamWriter(System.out));
057:
058: FileSpellChecker checker = new FileSpellChecker();
059:
060: checker.checkFile(input, output);
061:
062: input.close();
063: output.close();
064:
065: exitStatus = 0;
066: } catch (Exception e) {
067: e.printStackTrace(System.err);
068: exitStatus = 1;
069: }
070:
071: System.exit(exitStatus);
072: }
073:
074: /**
075: * @return <i>true</i> if file completely checked and <i>false</i> if the user
076: * interupted the checking.
077: */
078: public boolean checkFile(BufferedReader input, BufferedWriter output)
079: throws SpellException {
080: try {
081: String line = input.readLine();
082:
083: while (line != null) {
084: String checkedLine;
085:
086: if (line.trim().equals("")) {
087: checkedLine = line;
088: } else {
089: List results = _getSpellEngine().checkLine(line);
090:
091: checkedLine = _getSpellValidator().validate(line,
092: results);
093:
094: if (checkedLine == null) {
095: return false;
096: }
097: }
098:
099: 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 e) {
109: stop();
110:
111: if (e instanceof SpellException) {
112: throw (SpellException) e;
113: } else {
114: throw new SpellException(
115: "Error communicating with the aspell subprocess",
116: e);
117: }
118: }
119:
120: return true;
121: }
122:
123: public String getAspellExeFilename() {
124: return _aspellExeFilename;
125: }
126:
127: public void stop() {
128: if (_spellEngine != null) {
129: _spellEngine.stop();
130: _spellEngine = null;
131: }
132: }
133:
134: private Engine _getSpellEngine() throws SpellException {
135: if (_spellEngine == null) {
136: String aSpellCommandLine = _aspellExeFilename + " pipe";
137: _spellEngine = new AspellEngine(aSpellCommandLine);
138: }
139:
140: return _spellEngine;
141: }
142:
143: private Validator _getSpellValidator() {
144: if (_spellValidator == null) {
145: _spellValidator = new Validator();
146: }
147:
148: return _spellValidator;
149: }
150: }
|