001    /**
002     * Copyright (c) 2000-2010 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 void spellingError(SpellCheckEvent event) {
037                    List<String> suggestions = new ArrayList<String>();
038    
039                    Iterator<Word> itr = event.getSuggestions().iterator();
040    
041                    while (itr.hasNext()) {
042                            Word word = itr.next();
043    
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            public List<InvalidWord> getInvalidWords() {
066                    return _invalidWords;
067            }
068    
069            private boolean _isInsideHtmlTag(int pos) {
070                    boolean insideHtmlTag = false;
071    
072                    for (int i = pos; i >= 0; i--) {
073                            if (_textCharArray[i] == '<') {
074                                    insideHtmlTag = true;
075    
076                                    break;
077                            }
078                            else if (_textCharArray[i] == '>') {
079                                    break;
080                            }
081                    }
082    
083                    if (insideHtmlTag) {
084                            for (int i = pos; i < _textCharArray.length; i++) {
085                                    if (_textCharArray[i] == '<') {
086                                            insideHtmlTag = false;
087    
088                                            break;
089                                    }
090                                    else if (_textCharArray[i] == '>') {
091                                            break;
092                                    }
093                            }
094                    }
095    
096                    return insideHtmlTag;
097            }
098    
099            private String _text;
100            private char[] _textCharArray;
101            private List<InvalidWord> _invalidWords;
102    
103    }