1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util.jazzy;
16  
17  import com.swabunga.spell.engine.Word;
18  import com.swabunga.spell.event.SpellCheckEvent;
19  import com.swabunga.spell.event.SpellCheckListener;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /**
26   * <a href="BasicSpellCheckListener.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class BasicSpellCheckListener implements SpellCheckListener {
31  
32      public BasicSpellCheckListener(String text) {
33          _text = text;
34          _textCharArray = text.toCharArray();
35          _invalidWords = new ArrayList<InvalidWord>();
36      }
37  
38      public void spellingError(SpellCheckEvent event) {
39          List<String> suggestions = new ArrayList<String>();
40  
41          Iterator<Word> itr = event.getSuggestions().iterator();
42  
43          while (itr.hasNext()) {
44              Word word = itr.next();
45  
46              suggestions.add(word.getWord());
47          }
48  
49          int pos = event.getWordContextPosition();
50  
51          if (pos >= 0) {
52              if ((pos == 0) ||
53                  ((pos > 0) &&
54                   //(_text.charAt(pos - 1) != '<') &&
55                   (!_isInsideHtmlTag(pos)) &&
56                   (_text.charAt(pos - 1) != '&') &&
57                   (event.getInvalidWord().length() > 1))) {
58  
59                  _invalidWords.add(
60                      new InvalidWord(
61                          event.getInvalidWord(), suggestions,
62                          event.getWordContext(), pos));
63              }
64          }
65      }
66  
67      public List<InvalidWord> getInvalidWords() {
68          return _invalidWords;
69      }
70  
71      private boolean _isInsideHtmlTag(int pos) {
72          boolean insideHtmlTag = false;
73  
74          for (int i = pos; i >= 0; i--) {
75              if (_textCharArray[i] == '<') {
76                  insideHtmlTag = true;
77  
78                  break;
79              }
80              else if (_textCharArray[i] == '>') {
81                  break;
82              }
83          }
84  
85          if (insideHtmlTag) {
86              for (int i = pos; i < _textCharArray.length; i++) {
87                  if (_textCharArray[i] == '<') {
88                      insideHtmlTag = false;
89  
90                      break;
91                  }
92                  else if (_textCharArray[i] == '>') {
93                      break;
94                  }
95              }
96          }
97  
98          return insideHtmlTag;
99      }
100 
101     private String _text;
102     private char[] _textCharArray;
103     private List<InvalidWord> _invalidWords;
104 
105 }