/**
* generate a random char from the single char list
*
* @returns - a randomly selscted character from the single char list
*
*/
private static Character randomSingle()
{
return (new Character(single[(int)((getFloat() * singlecount) - 1)]));
} /**
* generate a random character
*
* @param lower lower bound from which to get a random char
* @param upper upper bound from which to get a random char
*
* @returns - a randomly generated character
*
*/
private static Character randomChar(Character lower, Character upper)
{
int tempval;
char low = lower.charValue();
char up = upper.charValue(); // get a random number in the range lowlow - lowup
tempval = (int)((int)low + (getFloat() * ((int)(up - low)))); // return the random char
return (new Character((char) tempval));
} /**
* get the randomly created string for use with the
* &lt;jsp:getProperty name=<i>"id"</i> property="randomstr"/&gt;
*
* @return - randomly created string
*
*/
public static String getRandom()
{
randomstr = new String();
generaterandom(); // generate the first random string if (hmap != null)
{ while (hmap.containsKey(randomstr))
{
// random string has already been created generate a different one
generaterandom();
} hmap.put(randomstr, null); // add the new random string
} return randomstr;
} /**
* set the ranges from which to choose the characters for the random string
*
* @param low set of lower ranges
* @param up set of upper ranges
*
*/
public static void setRanges(ArrayList low, ArrayList up)
{
lower = low;
upper = up;
}
/**
* set the hashmap that is used to check the uniqueness of random strings
*
* @param map hashmap whose keys are used to insure uniqueness of random strgs
*
*/
public static void setHmap(HashMap map)
{
hmap = map;
} /**
* set the length of the random string
*
* @param value length of the random string
*
*/
public static void setLength(String value)
{
length = new Integer(value);
} /**
* set the algorithm name
*
* @param value name of the algorithm to use for a SecureRandom object
*
*/
public static void setAlgorithm(String value)
{
algorithm = value;
secure = true; // a SecureRandom object is to be used
} /**
* set the provider name
*
* @param value name of the package to check for the algorithm
*
*/
public static void setProvider(String value)
{
provider = value;
} /**
* set the allchars flag
*
* @param value boolean value of the allchars flag
*
*/
public static void setAllchars(boolean value)
{
allchars = value;
} /**
* set the array of single chars to choose from for this random string and the
* number of chars in the array
*
* @param chars the array of single chars
* @param value the number of single chars
*
*/
public static void setSingle(char[] chars, int value)
{
single = chars; // set the array of chars
singlecount = value; // set the number of chars in array single
singles = true; // set flag that single chars are in use
} public static void setCharset(String value)
{
// values tells the method whether or not to check for single chars
boolean more = true; // create the arraylists to hold the upper and lower bounds for the char
// ranges
lower = new ArrayList(3);
upper = new ArrayList(3); // user has chosen to use all possible characters in the random string
if (value.compareTo("all") == 0)
{
allchars = true; // set allchars flag
// all chars are to be used so there are no single chars to sort
// through
more = false;
}
else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\'))
{
// run through the ranges at most 3
while (more && (value.charAt(1) == '-'))
{
// check to make sure that the dash is not the single char
if (value.charAt(0) == '\\')
{
break;
}
else
{
// add upper and lower ranges to there list
lower.add(new Character(value.charAt(0)));
upper.add(new Character(value.charAt(2)));
} // check to see if there is more to the charset
if (value.length() <= 3)
{
more = false;
}
else
{
// create a new string so that the next range if there is one
// starts it
value = value.substring(3);
}
}
} // if more = false there are no single chars in the charset
if (more)
{
single = new char[30]; // create single // create a set of tokens from the string of single chars
StringTokenizer tokens = new StringTokenizer(value); while (tokens.hasMoreTokens())
{
// get the next token from the string
String token = tokens.nextToken(); if (token.length() > 1)
{
// char is a - add it to the list
single[singlecount++] = '-';
} // add the current char to the list
single[singlecount++] = token.charAt(0);
}
}
if ((lower == null) && (single == null))
setCharset("a-zA-Z0-9");
}
}