KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spellcheck > ASpellInterface


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 import org.columba.mail.spellcheck.cswilly.FileSpellChecker;
25 import org.columba.mail.spellcheck.cswilly.SpellException;
26
27
28 public class ASpellInterface {
29     
30     private static final java.util.logging.Logger JavaDoc LOG =
31         java.util.logging.Logger.getLogger("org.columba.mail.spellcheck"); //$NON-NLS-1$
32

33     private static FileSpellChecker fileSpellChecker = null;
34     private static String JavaDoc aspellExeFilename;
35
36     public static String JavaDoc checkBuffer(String JavaDoc buffer) {
37         String JavaDoc checkedBuffer;
38         FileSpellChecker checker = null;
39
40         try {
41             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new StringReader JavaDoc(buffer));
42             StringWriter JavaDoc stringWriter = new StringWriter JavaDoc(buffer.length());
43             BufferedWriter JavaDoc output = new BufferedWriter JavaDoc(stringWriter);
44
45             checker = getFileSpellChecker();
46
47             boolean checkingNotCanceled = checker.checkFile(input, output);
48
49             input.close();
50             output.close();
51
52             if (checkingNotCanceled) {
53                 checkedBuffer = stringWriter.toString();
54             } else {
55                 checkedBuffer = null;
56             }
57         } catch (SpellException e) {
58             String JavaDoc msg = "Cannot check selection.\nError (Aspell) is: " +
59                 e.getMessage();
60             LOG.info(msg);
61             checkedBuffer = null;
62         } catch (IOException JavaDoc e) {
63             String JavaDoc msg = "Cannot check selection.\nError (Interface) is: " +
64                 e.getMessage();
65             LOG.info(msg);
66             checkedBuffer = null;
67         }
68
69         return checkedBuffer;
70     }
71
72     private static FileSpellChecker getFileSpellChecker() {
73         String JavaDoc aspellExeFilename = getAspellExeFilename();
74
75         if (fileSpellChecker == null) {
76             fileSpellChecker = new FileSpellChecker(aspellExeFilename);
77         } else if (!aspellExeFilename.equals(
78                     fileSpellChecker.getAspellExeFilename())) {
79             fileSpellChecker.stop();
80             fileSpellChecker = new FileSpellChecker(aspellExeFilename);
81         }
82
83         return fileSpellChecker;
84     }
85
86     public static String JavaDoc getAspellExeFilename() {
87         if ((aspellExeFilename == null) || aspellExeFilename.equals("")) {
88             aspellExeFilename = "aspell.exe";
89         }
90
91         return aspellExeFilename;
92     }
93
94     public static void setAspellExeFilename(String JavaDoc exeFilename) {
95         aspellExeFilename = exeFilename;
96     }
97 }
98
Popular Tags