/*
 *
 * Copyright (c) 1998 The Java Apache Project. ; All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Every modification must be notified to the Java Apache Project
 *    and redistribution of the modified code without prior notification
 *    is not permitted in any form.
 *
 * 4. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the Java Apache Project
 *    (http://java.apache.org/)."
 *
 * 5. The names "Jetspeed", "Apache Jetspeed" and "Apache Jetspeed
 *    Project" must not be used to endorse or promote products
 *    derived from this software without prior written permission.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the Java Apache Project
 *    (http://java.apache.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * License version 1.0
 *
 */
package org.apache.maven.jxr.util;

import java.util.*;

/**
 * This is a small and fast word tokenizer. It has different characteristics
 * from the normal Java tokenizer. It only considers clear words that are only
 * ended with spaces as strings. EX: "Flight" would be a word but "Flight()"
 * would not.
 */
public class SimpleWordTokenizer
{

    /** Description of the Field */
    public final static char[] BREAKERS = {'(', ')', '[', ' ', '{', '}'};

    /** Break the given line into multiple StringUtils  */
    public static StringEntry[] tokenize(String line)
    {

        /*
        determine where to start processing this String... this could
        either be the start of the line or just keep going until the first
        */
        int start = getStart(line);

        //find the first non-BREAKER char and assume that is where you want to start

        if (line == null ||
            line.length() == 0 ||
            start == -1)
        {
            return new StringEntry[0];
        }

        return tokenize(line, start);
    }


    /**
     * Tokenize the given line but only return StringUtils that match the parameter
     * find.
     *
     * @param line String to search in
     * @param find String to match.
     */
    public static StringEntry[] tokenize(String line, String find)
    {

        Vector v = new Vector();

        StringEntry[] se = tokenize(line);

        for (int i = 0; i < se.length; ++i)
        {

            if (se[i].toString().equals(find))
            {
                v.addElement(se[i]);
            }

        }

        StringEntry[] found = new StringEntry[v.size()];
        Collections.sort(v);
        v.copyInto(found);
        return found;
    }

    /** Internal impl. Specify the start and end.  */
    private static StringEntry[] tokenize(String line, int start)
    {

        Vector words = new Vector();

        //algorithm works like this... break the line out into segments
        //that are separated by spaces, and if the entire String doesn't contain
        //a non-Alpha char then assume it is a word.
        while (true)
        {

            int next = getNextBreak(line, start);

            if (next < 0 ||
                next <= start)
            {
                break;
            }

            String word = line.substring(start, next);

            if (isWord(word))
            {
                words.addElement(new StringEntry(word, start));
            }

            start = next + 1;
        }

        StringEntry[] found = new StringEntry[words.size()];
        words.copyInto(found);
        return found;
    }


    /**
     * Go through the entire String and if any character is not a Letter( a, b,
     * c, d, etc) then return false.
     */
    private static boolean isWord(String string)
    {

        if (string == null ||
            string.length() == 0)
        {

            return false;
        }

        for (int i = 0; i < string.length(); ++i)
        {

            char c = string.charAt(i);

            if (Character.isLetter(c) == false &&
                c != '.')
            {
                return false;
            }

        }

        return true;
    }

    /** Go through the list of BREAKERS and find the closes one.  */
    private static int getNextBreak(String string, int start)
    {

        int breakPoint = -1;

        for (int i = 0; i < BREAKERS.length; ++i)
        {

            int next = string.indexOf(BREAKERS[i], start);

            if (breakPoint == -1 ||
                next < breakPoint &&
                next != -1)
            {

                breakPoint = next;

            }

        }

        //if the breakPoint is still -1 go to the end of the string
        if (breakPoint == -1)
        {
            breakPoint = string.length();
        }

        return breakPoint;
    }

    /** Go through the list of BREAKERS and find the closes one.  */
    private static int getStart(String string)
    {

        for (int i = 0; i < string.length(); ++i)
        {

            if (isBreaker(string.charAt(i)) == false)
            {
                return i;
            }

        }

        return -1;
    }


    /** Return true if the given char is considered a breaker.  */
    private static boolean isBreaker(char c)
    {

        for (int i = 0; i < BREAKERS.length; ++i)
        {

            if (BREAKERS[i] == c)
            {
                return true;
            }

        }

        return false;
    }

}

