Searcher.java :  » Source-Control » gruntspud » gruntspud » editor » Java Open Source

Java Open Source » Source Control » gruntspud 
gruntspud » gruntspud » editor » Searcher.java
/*
 *  Gruntspud
 *
 *  Copyright (C) 2002 Brett Smith.
 *
 *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package gruntspud.editor;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;

/**
 * Searches a text document for a given pattern.
 *
 * @author magicthize
 */
public class Searcher {
    private Element element;
    private int index;
    private Document document;
    private SearchCriteria criteria;
    private ElementIterator iterator;
    private int startIdx;

    /**
     * Creates a new Searcher object.
     *
     * @param doc text document to search
     */
    public Searcher(Document doc) {
        setDocument(doc);
    }

    /**
     * Get the next, or <code>null</code> null will be returned if there are no
     * more matches
     *
     * @return next match
     */
    public Match nextMatch() {
        while (true) {
            if (element == null) {
                index = -1;
                element = iterator.next();
            }

            if (element == null) {
                return null;
            }

            if (element.isLeaf()) {
                try {
                    String val = document.getText(element.getStartOffset(),
                                                  element.getEndOffset() -
                                                  element.getStartOffset());
                    int z = element.getStartOffset();

                    while (true) {
                        if (criteria.isCaseSensitive()) {
                            index = val.indexOf(criteria.getPattern(),
                                                (index == -1) ? 0 : (index + 1));
                        }
                        else {
                            index = val.toLowerCase().indexOf(criteria.
                                getPattern()
                                .toLowerCase(),
                                (index == -1) ? 0 : (index + 1));

                        }
                        if (index == -1) {
                            break;
                        }

                        if ( (index + element.getStartOffset()) >= startIdx) {
                            break;
                        }
                    }

                    if (index != -1) {
                        int start = element.getStartOffset() + index;
                        int len = criteria.getPattern().length();

                        if (criteria.isWholeWords()) {
                            char ch = (index == 0) ? '\000'
                                : val.charAt(index - 1);

                            if ( (ch == '\000') || (ch == '\r') || (ch == '\n') ||
                                (ch == ' ') || (ch == '"')) {
                                ch = ( (index + len) == val.length()) ? '\000'
                                    : val.charAt(index +
                                                 len);

                                if ( (ch == '\000') || (ch == ' ') ||
                                    (ch == ',') || (ch == '.') ||
                                    (ch == '?') || (ch == '!') ||
                                    (ch == '"') || (ch == '\r') ||
                                    (ch == '\n')) {
                                    return new Match(start, len);
                                }
                            }
                        }
                        else {

                            return new Match(start, len);
                        }
                    }
                    else {
                        element = null;
                    }
                }
                catch (BadLocationException bLE) {
                }
            }
            else {
                element = null;
            }
        }
    }

    /**
     * Set the text document to search
     *
     * @param doc text document to search
     */
    public void setDocument(Document doc) {
        document = doc;
    }

    /**
     * Set the search criteria
     *
     * @param criteria criteria
     */
    public void startSearch(SearchCriteria criteria) {
        startSearch(criteria, 0);
    }

    /**
     * Start searching.
     *
     * @param criteria search criteria
     * @param startIdx position in document to start searching
     */
    public void startSearch(SearchCriteria criteria, int startIdx) {
        iterator = new ElementIterator(document);
        this.startIdx = startIdx;
        element = null;
        this.criteria = criteria;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.