001    /**
002     * Copyright (c) 2000-2013 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.List;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class BasicSpellCheckListener implements SpellCheckListener {
028    
029            public BasicSpellCheckListener(String text) {
030                    _text = text;
031                    _textCharArray = text.toCharArray();
032                    _invalidWords = new ArrayList<InvalidWord>();
033            }
034    
035            public List<InvalidWord> getInvalidWords() {
036                    return _invalidWords;
037            }
038    
039            public void spellingError(SpellCheckEvent event) {
040                    List<String> suggestions = new ArrayList<String>();
041    
042                    for (Word word : (List<Word>)event.getSuggestions()) {
043                            suggestions.add(word.getWord());
044                    }
045    
046                    int pos = event.getWordContextPosition();
047    
048                    if (pos >= 0) {
049                            if ((pos == 0) ||
050                                    ((pos > 0) &&
051                                     //(_text.charAt(pos - 1) != '<') &&
052                                     (!_isInsideHtmlTag(pos)) &&
053                                     (_text.charAt(pos - 1) != '&') &&
054                                     (event.getInvalidWord().length() > 1))) {
055    
056                                    _invalidWords.add(
057                                            new InvalidWord(
058                                                    event.getInvalidWord(), suggestions,
059                                                    event.getWordContext(), pos));
060                            }
061                    }
062            }
063    
064            private boolean _isInsideHtmlTag(int pos) {
065                    boolean insideHtmlTag = false;
066    
067                    for (int i = pos; i >= 0; i--) {
068                            if (_textCharArray[i] == '<') {
069                                    insideHtmlTag = true;
070    
071                                    break;
072                            }
073                            else if (_textCharArray[i] == '>') {
074                                    break;
075                            }
076                    }
077    
078                    if (insideHtmlTag) {
079                            for (int i = pos; i < _textCharArray.length; i++) {
080                                    if (_textCharArray[i] == '<') {
081                                            insideHtmlTag = false;
082    
083                                            break;
084                                    }
085                                    else if (_textCharArray[i] == '>') {
086                                            break;
087                                    }
088                            }
089                    }
090    
091                    return insideHtmlTag;
092            }
093    
094            private List<InvalidWord> _invalidWords;
095            private String _text;
096            private char[] _textCharArray;
097    
098    }