KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > edit > SpellCheckerModule


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.edit;
14
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.net.URLDecoder JavaDoc;
18 import java.util.List JavaDoc;
19
20 import com.openedit.OpenEditException;
21 import com.openedit.WebPageRequest;
22 import com.openedit.modules.BaseModule;
23 import com.openedit.modules.html.EditorSession;
24 import com.openedit.modules.spell.WebSpellChecker;
25 import com.openedit.page.Page;
26
27
28 /**
29  * This command must be called multiple times for each paragraph that is checked
30  *
31  * @author Christopher Burkey
32  */

33 public class SpellCheckerModule extends BaseModule
34 {
35     
36     public SpellCheckerModule()
37     {
38         super();
39     }
40
41     public void checkAllSpellings(WebPageRequest inReq) throws Exception JavaDoc
42     {
43
44         String JavaDoc content = inReq.getRequestParameter("textinputs[]");
45
46         String JavaDoc text = URLDecoder.decode(content);
47
48         WebSpellChecker checker = getSpellChecker( inReq );
49         List JavaDoc allwords = checker.checkAllWords(text);
50         
51         inReq.putPageValue("errors",allwords);
52         String JavaDoc javascript = new EditorSession().createVariable(text);
53
54         inReq.putPageValue("textinputs",javascript);
55     }
56     
57
58     protected WebSpellChecker getSpellChecker( WebPageRequest inContext ) throws OpenEditException
59     {
60         WebSpellChecker spellChecker = (WebSpellChecker)inContext.getSessionValue( WebSpellChecker.KEY );
61         if (spellChecker == null)
62         {
63             try
64             {
65                 //TODO: Should we keep this in memory? nah
66
Page dic = getPageManager().getPage("/openedit/editors/spell/dictionary/all.dic");
67                 spellChecker = new WebSpellChecker(
68                         getRoot().getAbsolutePath() + dic.getContentItem().getPath() );
69                 inContext.putSessionValue( WebSpellChecker.KEY, spellChecker );
70             }
71             catch (FileNotFoundException JavaDoc e)
72             {
73                 throw new OpenEditException("Spellchecker dictionary file not found.", e);
74             }
75             catch (IOException JavaDoc e)
76             {
77                 throw new OpenEditException("Failed to load spellchecker dictionary.", e);
78             }
79         }
80
81         return spellChecker;
82     }
83
84 }
85
Popular Tags