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            @Override
040            public void spellingError(SpellCheckEvent event) {
041                    List<String> suggestions = new ArrayList<String>();
042    
043                    for (Word word : (List<Word>)event.getSuggestions()) {
044                            suggestions.add(word.getWord());
045                    }
046    
047                    int pos = event.getWordContextPosition();
048    
049                    if (pos >= 0) {
050                            if ((pos == 0) ||
051                                    ((pos > 0) &&
052                                     //(_text.charAt(pos - 1) != '<') &&
053                                     (!_isInsideHtmlTag(pos)) &&
054                                     (_text.charAt(pos - 1) != '&') &&
055                                     (event.getInvalidWord().length() > 1))) {
056    
057                                    _invalidWords.add(
058                                            new InvalidWord(
059                                                    event.getInvalidWord(), suggestions,
060                                                    event.getWordContext(), pos));
061                            }
062                    }
063            }
064    
065            private boolean _isInsideHtmlTag(int pos) {
066                    boolean insideHtmlTag = false;
067    
068                    for (int i = pos; i >= 0; i--) {
069                            if (_textCharArray[i] == '<') {
070                                    insideHtmlTag = true;
071    
072                                    break;
073                            }
074                            else if (_textCharArray[i] == '>') {
075                                    break;
076                            }
077                    }
078    
079                    if (insideHtmlTag) {
080                            for (int i = pos; i < _textCharArray.length; i++) {
081                                    if (_textCharArray[i] == '<') {
082                                            insideHtmlTag = false;
083    
084                                            break;
085                                    }
086                                    else if (_textCharArray[i] == '>') {
087                                            break;
088                                    }
089                            }
090                    }
091    
092                    return insideHtmlTag;
093            }
094    
095            private List<InvalidWord> _invalidWords;
096            private String _text;
097            private char[] _textCharArray;
098    
099    }