001    /**
002     * Implements the CFML Function gettoken
003     */
004    package railo.runtime.functions.string;
005    
006    import java.util.StringTokenizer;
007    
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.ExpressionException;
010    import railo.runtime.exp.FunctionException;
011    import railo.runtime.ext.function.Function;
012    
013    public final class GetToken implements Function {
014            public static String call(PageContext pc , String str, double index) throws ExpressionException {
015                    return call(pc,str,index,null);
016            }
017            public static String call(PageContext pc , String str, double index, String delimiters) throws ExpressionException {
018                    if(delimiters==null) delimiters="\r\n\t ";
019                    
020                    if(index<1)
021                            throw new FunctionException(pc,"getToken",2,"index","index must be a positive number now ("+((int)index)+")");
022                    
023                    StringTokenizer tokens=new StringTokenizer(str,delimiters);
024                    int count=0;
025                    while(tokens.hasMoreTokens()) {
026                            if(++count==index)return tokens.nextToken();
027                            tokens.nextToken();
028                    }
029                    return "";
030            }
031    }