/*******************************************************************************
 * Copyright (c), 2001, 2002 N2 Broadband, Inc.  All Rights Reserved.
 *
 * This module contains unpublished, confidential, proprietary
 * material.  The use and dissemination of this material are
 * governed by a license.  The above copyright notice does not
 * evidence any actual or intended publication of this material.
 *
 * Author:  Drake H. Henderson
 * Created:  11-12-01
 *
 ******************************************************************************/
package com.n2bb.util;

public class Match {

  public static boolean isMatch(String originalStr, String searchStr) {

    // .printMessage(5, "isMatch  orig - " + originalStr + " search - " + searchStr);

    if ((searchStr == null) || (searchStr.equals("")) || (originalStr.equals(searchStr)))
      return true;

    if (searchStr.indexOf('+') != -1) {
      int index = searchStr.indexOf('+');
      String first = searchStr.substring(0, index);
      String second = searchStr.substring(index + 1);

      if (!isMatch(originalStr, first)) {
        return isMatch(originalStr, second);
      }
      return true;
    }

    int oLength = originalStr.length();
    int sLength = searchStr.length();
    int oCount = 0;
    int sCount = 0;

    // fast forward through any matching characters at the beginning of the strings
    while (sCount < sLength && oCount < oLength) {
      if (searchStr.charAt(sCount) == originalStr.charAt(oCount)) {
        oCount++;
        sCount++;
      } else if (searchStr.charAt(sCount) == '?') {
        oCount++;
        sCount++;
      } else {
        break;
      }
    }

    // .printMessage(searchStr + "  sCount - " + sCount + " sLength - " + sLength);
    // .printMessage(originalStr + "  oCount - " + oCount + " oLength - " + oLength);

    // at the end of the search string?
    if (sCount == sLength) {
      // beginning characters matched but orig string continues
      if (oCount < oLength) {
        return false;
      } // beginning characters matched and both strings end
      return true;
    }

    if (searchStr.charAt(sCount) == '*') {
      // .printMessage(" have a *");
      
      // fast forward through all stars      
      while ((sCount < sLength) && (searchStr.charAt(sCount) == '*'))
        sCount++;

      // if search string has nothing after the star(s)  
      if (sCount == sLength)
        return true;

      return isMatchStar(originalStr.substring(oCount), searchStr.substring(sCount));
    }

    // handle trailing question mark wildcards
    boolean result = false;
    if (oCount == oLength) {
      // if remainder of search string consists of '?', consider it a match
      while ((sCount < sLength) && (searchStr.charAt(sCount) == '?'))
        sCount++;
      if (sCount == sLength)
        result = true;

    } else if (oCount < oLength) {
      while ((sCount < sLength) && (searchStr.charAt(sCount) == '?')) {
        oCount++;
        sCount++;
      }
      if ((oCount >= oLength) && (sCount == sLength))
        result = true;
    }
    return result;
  }

  public static boolean isMatchStar(String originalStr, String searchStr) {
    // .printMessage(5, "isMatchStar - " + originalStr + " - " + searchStr);

    int oLength = originalStr.length();
    int sLength = searchStr.length();
    int oCount = 0;
    int sCount = 0;

    int startNo = 0;

    // step through orig string looking for occurrences of search string
    // need to progressively step through orig looking for the next occurrence of the search string
    while (true) {

      // .printMessage(5, "startNo - " + startNo);

      // no remaining occurrence of search string in orig to provide possible match
      // break and return false
      if ((startNo < 0) || (startNo >= oLength))
        break;

      // look for search string within orig string 
      // have to step through search string one character at a time when looking for substring
      //   so that the below isMatch call can handle wildcard characters -- if do not progressively 
      //   step through search string, subsequent wildcard characters will not be treated as 
      //   wildcards
      int count = 0;
      while (true) {
        // .printMessage(5, "count - " + count);

        // isMatch was false and the end of the search string has been reached
        // no match at this point in the orig string
        // break and try to find a match farther along in the orig string
        if (count + 1 > sLength)
          break;

        // look for the search string up to the count index in the orig string
        String matchString = searchStr.substring(0, count + 1);

        int index = originalStr.indexOf(matchString, startNo);
        // .printMessage(5, "oString - " + originalStr + "  matchString - " + matchString +
        //        "  index - " + index);
                
        // search substring not found in orig starting at startNo
        // break and try to find a match farther along in the orig string
        if (index == -1) {
          break;
        }

        // substring was found in orig
        // cut off orig string up to the beginning of the search string and look for match
        if (isMatch(originalStr.substring(index), searchStr)) {
          // .printMessage(5, "isMatch = true");
          return true;
        } // else
          // .printMessage(5, "isMatch = false");

        count++;
      }
      
      // look for next occurrence of search string in orig 
      // perhaps it will lead to a match
      startNo = originalStr.indexOf(searchStr.charAt(0), startNo + 1);
    }
    return false;
  }

  /*
  	public static void main(String args[]){

  		String originalStr = args[0];
  		String searchStr = args[1];

  		if(isMatch(originalStr, searchStr))
  			System.out.println("Matched");
  		else
  			System.out.println("Not Matched");
  	}
  */

}

