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