1 22 package org.columba.mail.spellcheck.cswilly; 23 24 import java.io.BufferedReader ; 25 import java.io.BufferedWriter ; 26 import java.io.FileReader ; 27 import java.io.OutputStreamWriter ; 28 import java.util.List ; 29 30 31 35 public class FileSpellChecker { 36 private String _aspellExeFilename; 37 private AspellEngine _spellEngine = null; 38 private Validator _spellValidator = null; 39 40 public FileSpellChecker(String aspellExeFilename) { 41 _aspellExeFilename = aspellExeFilename; 42 } 43 44 public FileSpellChecker() { 45 this("O:\\local\\aspell\\aspell.exe"); 46 } 47 48 public static void main(String [] args) { 49 int exitStatus; 50 51 String inputFilename = "spellTest.txt"; 52 53 try { 54 BufferedReader input = new BufferedReader (new FileReader ( 55 inputFilename)); 56 BufferedWriter output = new BufferedWriter (new OutputStreamWriter ( 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 e) { 68 e.printStackTrace(System.err); 69 exitStatus = 1; 70 } 71 72 System.exit(exitStatus); 73 } 74 75 79 public boolean checkFile(BufferedReader input, BufferedWriter output) 80 throws SpellException { 81 try { 82 String line = input.readLine(); 83 84 while (line != null) { 85 String checkedLine; 86 87 if (line.trim().equals("")) { 88 checkedLine = line; 89 } else { 90 List 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 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("Error communicating with the aspell subprocess", 115 e); 116 } 117 } 118 119 return true; 120 } 121 122 public String 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 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 |