1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.jazzy;
24  
25  import com.swabunga.spell.engine.Word;
26  import com.swabunga.spell.event.SpellCheckEvent;
27  import com.swabunga.spell.event.SpellCheckListener;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="BasicSpellCheckListener.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class BasicSpellCheckListener implements SpellCheckListener {
40  
41      public BasicSpellCheckListener(String text) {
42          _text = text;
43          _textCharArray = text.toCharArray();
44          _invalidWords = new ArrayList<InvalidWord>();
45      }
46  
47      public void spellingError(SpellCheckEvent event) {
48          List<String> suggestions = new ArrayList<String>();
49  
50          Iterator<Word> itr = event.getSuggestions().iterator();
51  
52          while (itr.hasNext()) {
53              Word word = itr.next();
54  
55              suggestions.add(word.getWord());
56          }
57  
58          int pos = event.getWordContextPosition();
59  
60          if (pos >= 0) {
61              if ((pos == 0) ||
62                  ((pos > 0) &&
63                   //(_text.charAt(pos - 1) != '<') &&
64                   (!_isInsideHtmlTag(pos)) &&
65                   (_text.charAt(pos - 1) != '&') &&
66                   (event.getInvalidWord().length() > 1))) {
67  
68                  _invalidWords.add(
69                      new InvalidWord(
70                          event.getInvalidWord(), suggestions,
71                          event.getWordContext(), pos));
72              }
73          }
74      }
75  
76      public List<InvalidWord> getInvalidWords() {
77          return _invalidWords;
78      }
79  
80      private boolean _isInsideHtmlTag(int pos) {
81          boolean insideHtmlTag = false;
82  
83          for (int i = pos; i >= 0; i--) {
84              if (_textCharArray[i] == '<') {
85                  insideHtmlTag = true;
86  
87                  break;
88              }
89              else if (_textCharArray[i] == '>') {
90                  break;
91              }
92          }
93  
94          if (insideHtmlTag) {
95              for (int i = pos; i < _textCharArray.length; i++) {
96                  if (_textCharArray[i] == '<') {
97                      insideHtmlTag = false;
98  
99                      break;
100                 }
101                 else if (_textCharArray[i] == '>') {
102                     break;
103                 }
104             }
105         }
106 
107         return insideHtmlTag;
108     }
109 
110     private String _text;
111     private char[] _textCharArray;
112     private List<InvalidWord> _invalidWords;
113 
114 }