package echopointng.util;
/* 
 * This file is part of the Echo Point Project.  This project is a collection
 * of Components that have extended the Echo Web Application Framework.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */

/**
 * The TokenizerKit class is useful for break a String down 
 * into an array of "Token" strings. This class is more powerful 
 * than the standard java.util.StringTokenizer.
 */
public class TokenizerKit {

	/** not instantiable */
	private TokenizerKit() {
	}

	/**
	 * Tokenizes a string into an array of Strings based on the delimeters while 
	 * respecting the quote characters ' and " as well defining 
	 * whitespace characters 0 to 32 as delimeters.
	 * 
	 * @param str - the string to tokenize
	 * @param delims - the string of allowable delimeter characters
	 * @return a String[] of tokens
	 */
	public static String[] tokenize(String str, String delims) {
		return doTokenize(str,delims,0,32,33,0xFF,true, false, false);
	}

	/**
	 * Tokenizes a string into an array of Strings based on the delimeters while 
	 * respecting the quote characters ' and " but the common whitespace characters 
	 * space, tab, carriage return and newline are no longer whitespace.
	 * 
	 * @param str - the string to tokenize
	 * @param delims - the string of allowable delimeter characters
	 * @return a String[] of tokens
	 */
	public static String[] tokenizeStrict(String str, String delims) {
		return doTokenize(str,delims,0,32,33,0xFF,true,true,false);
	}
	
	/**
	 * Tokenizes a string into an array of Strings based on the delimeters while 
	 * respecting the quote characters ' and " as well defining 
	 * whitespace characters 0 to 32 as delimeters.
	 * 
	 * @param str - the string to tokenize
	 * @param delims - the string of allowable delimeter characters
	 * @param returnDelimeters - if true then delimeters will be returned in the tokens array
	 * @return a String[] of tokens
	 */
	public static String[] tokenize(String str, String delims, boolean returnDelimeters) {
		return doTokenize(str,delims,0,32,33,0xFF,true, false, returnDelimeters);
	}

	/**
	 * Tokenizes a string into an array of Strings based on the delimeters while 
	 * respecting the quote characters ' and " but the common whitespace characters 
	 * space, tab, carriage return and newline are no longer whitespace.
	 * 
	 * @param str - the string to tokenize
	 * @param delims - the string of allowable delimeter characters
	 * @param returnDelimeters - if true then delimeters will be returned in the tokens array
	 * @return a String[] of tokens
	 */
	public static String[] tokenizeStrict(String str, String delims, boolean returnDelimeters) {
		return doTokenize(str,delims,0,32,33,0xFF,true,true,returnDelimeters);
	}	
	/** Do our tokenisation */
	private static String[] doTokenize(String str, String delims, int loWhiteSpace, int hiWhiteSpace, int loWordChar, int hiWordChar, boolean useQuotes, boolean noCommonWhiteSpace, boolean returnDelims) {
		try {
			java.util.ArrayList tokenList = new java.util.ArrayList();
			java.io.StringReader sr = new java.io.StringReader(str);
			java.io.StreamTokenizer st = new java.io.StreamTokenizer(sr);

			st.resetSyntax();
			st.whitespaceChars(loWhiteSpace, hiWhiteSpace);
			st.wordChars(loWordChar, hiWordChar);
			for (int i = 0; i < delims.length(); i++) {
				st.ordinaryChar(delims.charAt(i));
			}
			if (useQuotes) {
				st.quoteChar('\'');
				st.quoteChar('"');
			}
			if (noCommonWhiteSpace) {
				st.wordChars(' ',' ');
				st.wordChars('\t','\t');
				st.wordChars('\n','\n');
				st.wordChars('\r','\r');
			}

			String token = "";
			int tt = st.nextToken();
			while (tt != java.io.StreamTokenizer.TT_EOF) {
				token = null;

				switch (tt) {
					case java.io.StreamTokenizer.TT_WORD :
						// A word was found; the value is in sval
						token = st.sval;
						break;
					case '"' :
						// A double-quoted string was found; sval contains the contents
						token = st.sval;
						break;
					case '\'' :
						// A single-quoted string was found; sval contains the contents
						token = st.sval;
						break;
					case java.io.StreamTokenizer.TT_EOF :
						// End of file has been reached
						break;
					default :
						// A regular character was found; the value is the token itself
						if (returnDelims) {
							token = new String(new char[] {(char)tt});
						} else {
							token = null;
						}
							
						break;
				}
				if (token != null)
					tokenList.add(token);
				tt = st.nextToken();
			}
			return (String[]) tokenList.toArray(new String[tokenList.size()]);

		} catch (java.io.IOException ieo) {
			return new String[0];
		}
	}
}
