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