/*
 * This file is part of Mapyrus, software for plotting maps.
 * Copyright (C) 2003, 2004, 2005 Simon Chenery.
 *
 * Mapyrus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Mapyrus 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Mapyrus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/*
 * @(#) $Id$
 */
package org.mapyrus.function;

import org.mapyrus.Argument;
import org.mapyrus.ContextStack;
import org.mapyrus.MapyrusException;

/**
 * Function splitting string into array, delimited by a regular expression.
 * For example, split("foo:bar", ":") = [1] -> "foo", [2] -> "bar".
 */
public class Split extends Function
{
	/*
	 * Pre-defined hashmap keys for split function.
	 */
	private static String mSplitIndexes[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};

	/**
	 * @see org.mapyrus.function.Function#evaluate(org.mapyrus.ContextStack, org.mapyrus.Argument, org.mapyrus.Argument)
	 */
	public Argument evaluate(ContextStack context, Argument arg1, Argument arg2)
		throws MapyrusException
	{
		/*
		 * Split string on regular expression and assign as hashmap entries
		 * with keys, "1", "2", "3", ...
		 */
		String []split = arg1.toString().split(arg2.toString());
		String key;
		Argument retval = new Argument();
		for (int i = 0; i < split.length; i++)
		{
			/*
			 * Use pre-allocated strings to reduce object creation.
			 */
			if (i < mSplitIndexes.length)
				key = mSplitIndexes[i];
			else
				key = String.valueOf(i + 1);
			retval.addHashMapEntry(key, new Argument(Argument.STRING, split[i]));
		}
		return(retval);
	}

	/**
	 * @see org.mapyrus.function.Function#getMaxArgumentCount()
	 */
	public int getMaxArgumentCount()
	{
		return(2);
	}

	/**
	 * @see org.mapyrus.function.Function#getMinArgumentCount()
	 */
	public int getMinArgumentCount()
	{
		return(2);
	}

	/**
	 * @see org.mapyrus.function.Function#getName()
	 */
	public String getName()
	{
		return("split");
	}
}

@@ -882,16 +882,51 @@
 ction;%0A%0A
+import java.util.StringTokenizer;%0A%0A
 import o
@@ -1354,16 +1354,356 @@
  %229%22%7D;%0A%0A
+%09private static Argument WHITESPACE_PATTERN = new Argument(Argument.STRING, %22 %22);%0A%0A%09/**%0A%09 * @see org.mapyrus.function.Function#evaluate(org.mapyrus.ContextStack, org.mapyrus.Argument)%0A%09 */%0A%09public Argument evaluate(ContextStack context, Argument arg1)%0A%09%09throws MapyrusException%0A%09%7B%0A%09%09return(evaluate(context, arg1, WHITESPACE_PATTERN));%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -2051,29 +2051,99 @@
 /%0A%09%09
-String %5B%5Dsplit
+Argument retval = new Argument();%0A%09%09String %5B%5Dsplit;%0A%09%09String key;%0A%09%09String delimiter
  = arg
-1
+2
 .toS
@@ -2153,19 +2153,164 @@
 ng()
-.split
+;%0A%09%09if (delimiter.equals(WHITESPACE_PATTERN.toString()))%0A%09%09%7B%0A%09%09%09/*%0A%09%09%09 * Simple split on whitespace.%0A%09%09%09 */%0A%09%09%09StringTokenizer st = new StringTokenizer
 (arg
-2
+1
 .toS
@@ -2325,55 +2325,235 @@
 ;%0A%09%09
+%09
-String key;%0A%09%09Argument retval = new Argument();
+split = new String%5Bst.countTokens()%5D;%0A%09%09%09for (int i = 0; i %3C split.length; i++)%0A%09%09%09%09split%5Bi%5D = st.nextToken();%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09/*%0A%09%09%09 * Split using regular expression.%0A%09%09%09 */%0A%09%09%09split = arg1.toString().split(delimiter);%0A%09%09%7D%0A
 %0A%09%09f
@@ -3101,33 +3101,33 @@
 t()%0A%09%7B%0A%09%09return(
-2
+1
 );%0A%09%7D%0A%0A%09/**%0A%09 * 

Add splitting on white space if only a single argument given.