001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.jazzy;
016    
017    import com.swabunga.spell.engine.Word;
018    import com.swabunga.spell.event.SpellCheckEvent;
019    import com.swabunga.spell.event.SpellCheckListener;
020    
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class BasicSpellCheckListener implements SpellCheckListener {
029    
030            public BasicSpellCheckListener(String text) {
031                    _text = text;
032                    _textCharArray = text.toCharArray();
033                    _invalidWords = new ArrayList<InvalidWord>();
034            }
035    
036            public List<InvalidWord> getInvalidWords() {
037                    return _invalidWords;
038            }
039    
040            public void spellingError(SpellCheckEvent event) {
041                    List<String> suggestions = new ArrayList<String>();
042    
043                    Iterator<Word> itr = event.getSuggestions().iterator();
044    
045                    while (itr.hasNext()) {
046                            Word word = itr.next();
047    
048                            suggestions.add(word.getWord());
049                    }
050    
051                    int pos = event.getWordContextPosition();
052    
053                    if (pos >= 0) {
054                            if ((pos == 0) ||
055                                    ((pos > 0) &&
056                                     //(_text.charAt(pos - 1) != '<') &&
057                                     (!_isInsideHtmlTag(pos)) &&
058                                     (_text.charAt(pos - 1) != '&') &&
059                                     (event.getInvalidWord().length() > 1))) {
060    
061                                    _invalidWords.add(
062                                            new InvalidWord(
063                                                    event.getInvalidWord(), suggestions,
064                                                    event.getWordContext(), pos));
065                            }
066                    }
067            }
068    
069            private boolean _isInsideHtmlTag(int pos) {
070                    boolean insideHtmlTag = false;
071    
072                    for (int i = pos; i >= 0; i--) {
073                            if (_textCharArray[i] == '<') {
074                                    insideHtmlTag = true;
075    
076                                    break;
077                            }
078                            else if (_textCharArray[i] == '>') {
079                                    break;
080                            }
081                    }
082    
083                    if (insideHtmlTag) {
084                            for (int i = pos; i < _textCharArray.length; i++) {
085                                    if (_textCharArray[i] == '<') {
086                                            insideHtmlTag = false;
087    
088                                            break;
089                                    }
090                                    else if (_textCharArray[i] == '>') {
091                                            break;
092                                    }
093                            }
094                    }
095    
096                    return insideHtmlTag;
097            }
098    
099            private List<InvalidWord> _invalidWords;
100            private String _text;
101            private char[] _textCharArray;
102    
103    }