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