01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.mail.spellcheck;
17:
18: import java.io.BufferedReader;
19: import java.io.BufferedWriter;
20: import java.io.IOException;
21: import java.io.StringReader;
22: import java.io.StringWriter;
23:
24: import org.columba.mail.spellcheck.cswilly.FileSpellChecker;
25: import org.columba.mail.spellcheck.cswilly.SpellException;
26:
27: public class ASpellInterface {
28:
29: private static final java.util.logging.Logger LOG = java.util.logging.Logger
30: .getLogger("org.columba.mail.spellcheck"); //$NON-NLS-1$
31:
32: private static FileSpellChecker fileSpellChecker = null;
33: private static String aspellExeFilename;
34:
35: public static String checkBuffer(String buffer) {
36: String checkedBuffer;
37: FileSpellChecker checker = null;
38:
39: try {
40: BufferedReader input = new BufferedReader(new StringReader(
41: buffer));
42: StringWriter stringWriter = new StringWriter(buffer
43: .length());
44: BufferedWriter output = new BufferedWriter(stringWriter);
45:
46: checker = getFileSpellChecker();
47:
48: boolean checkingNotCanceled = checker.checkFile(input,
49: output);
50:
51: input.close();
52: output.close();
53:
54: if (checkingNotCanceled) {
55: checkedBuffer = stringWriter.toString();
56: } else {
57: checkedBuffer = null;
58: }
59: } catch (SpellException e) {
60: String msg = "Cannot check selection.\nError (Aspell) is: "
61: + e.getMessage();
62: LOG.info(msg);
63: checkedBuffer = null;
64: } catch (IOException e) {
65: String msg = "Cannot check selection.\nError (Interface) is: "
66: + e.getMessage();
67: LOG.info(msg);
68: checkedBuffer = null;
69: }
70:
71: return checkedBuffer;
72: }
73:
74: private static FileSpellChecker getFileSpellChecker() {
75: String aspellExeFilename = getAspellExeFilename();
76:
77: if (fileSpellChecker == null) {
78: fileSpellChecker = new FileSpellChecker(aspellExeFilename);
79: } else if (!aspellExeFilename.equals(fileSpellChecker
80: .getAspellExeFilename())) {
81: fileSpellChecker.stop();
82: fileSpellChecker = new FileSpellChecker(aspellExeFilename);
83: }
84:
85: return fileSpellChecker;
86: }
87:
88: public static String getAspellExeFilename() {
89: if ((aspellExeFilename == null) || aspellExeFilename.equals("")) {
90: aspellExeFilename = "aspell.exe";
91: }
92:
93: return aspellExeFilename;
94: }
95:
96: public static void setAspellExeFilename(String exeFilename) {
97: aspellExeFilename = exeFilename;
98: }
99: }
|