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