1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.jazzy;
21  
22  import com.swabunga.spell.engine.Word;
23  import com.swabunga.spell.event.SpellCheckEvent;
24  import com.swabunga.spell.event.SpellCheckListener;
25  
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * <a href="BasicSpellCheckListener.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class BasicSpellCheckListener implements SpellCheckListener {
37  
38      public BasicSpellCheckListener(String text) {
39          _text = text;
40          _textCharArray = text.toCharArray();
41          _invalidWords = new ArrayList<InvalidWord>();
42      }
43  
44      public void spellingError(SpellCheckEvent event) {
45          List<String> suggestions = new ArrayList<String>();
46  
47          Iterator<Word> itr = event.getSuggestions().iterator();
48  
49          while (itr.hasNext()) {
50              Word word = itr.next();
51  
52              suggestions.add(word.getWord());
53          }
54  
55          int pos = event.getWordContextPosition();
56  
57          if (pos >= 0) {
58              if ((pos == 0) ||
59                  ((pos > 0) &&
60                   //(_text.charAt(pos - 1) != '<') &&
61                   (!_isInsideHtmlTag(pos)) &&
62                   (_text.charAt(pos - 1) != '&') &&
63                   (event.getInvalidWord().length() > 1))) {
64  
65                  _invalidWords.add(
66                      new InvalidWord(
67                          event.getInvalidWord(), suggestions,
68                          event.getWordContext(), pos));
69              }
70          }
71      }
72  
73      public List<InvalidWord> getInvalidWords() {
74          return _invalidWords;
75      }
76  
77      private boolean _isInsideHtmlTag(int pos) {
78          boolean insideHtmlTag = false;
79  
80          for (int i = pos; i >= 0; i--) {
81              if (_textCharArray[i] == '<') {
82                  insideHtmlTag = true;
83  
84                  break;
85              }
86              else if (_textCharArray[i] == '>') {
87                  break;
88              }
89          }
90  
91          if (insideHtmlTag) {
92              for (int i = pos; i < _textCharArray.length; i++) {
93                  if (_textCharArray[i] == '<') {
94                      insideHtmlTag = false;
95  
96                      break;
97                  }
98                  else if (_textCharArray[i] == '>') {
99                      break;
100                 }
101             }
102         }
103 
104         return insideHtmlTag;
105     }
106 
107     private String _text;
108     private char[] _textCharArray;
109     private List<InvalidWord> _invalidWords;
110 
111 }