RandomTag.java revision 998747bfaaa3c6b28bbfaf0e282e6c0ccbf46bc0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2006-2008 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
*/
/**
* This class defines a tag that may be used to generate random values. It has
* a number of subtypes based on the type of information that should be
* generated, including:
* <UL>
* <LI>alpha:length</LI>
* <LI>alpha:minlength:maxlength</LI>
* <LI>numeric:length</LI>
* <LI>numeric:minvalue:maxvalue</LI>
* <LI>numeric:minvalue:maxvalue:format</LI>
* <LI>alphanumeric:length</LI>
* <LI>alphanumeric:minlength:maxlength</LI>
* <LI>chars:characters:length</LI>
* <LI>chars:characters:minlength:maxlength</LI>
* <LI>hex:length</LI>
* <LI>hex:minlength:maxlength</LI>
* <LI>base64:length</LI>
* <LI>base64:minlength:maxlength</LI>
* <LI>month</LI>
* <LI>month:maxlength</LI>
* <LI>telephone</LI>
* </UL>
*/
public class RandomTag
extends Tag
{
/**
* The value that indicates that the value is to be generated from a fixed
* number of characters from a given character set.
*/
public static final int RANDOM_TYPE_CHARS_FIXED = 1;
/**
* The value that indicates that the value is to be generated from a variable
* number of characters from a given character set.
*/
public static final int RANDOM_TYPE_CHARS_VARIABLE = 2;
/**
* The value that indicates that the value should be a random number.
*/
public static final int RANDOM_TYPE_NUMERIC = 3;
/**
* The value that indicates that the value should be a random month.
*/
public static final int RANDOM_TYPE_MONTH = 4;
/**
* The value that indicates that the value should be a telephone number.
*/
public static final int RANDOM_TYPE_TELEPHONE = 5;
/**
* The character set that will be used for alphabetic characters.
*/
public static final char[] ALPHA_CHARS =
"abcdefghijklmnopqrstuvwxyz".toCharArray();
/**
* The character set that will be used for numeric characters.
*/
/**
* The character set that will be used for alphanumeric characters.
*/
public static final char[] ALPHANUMERIC_CHARS =
"abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
/**
* The character set that will be used for hexadecimal characters.
*/
/**
* The character set that will be used for base64 characters.
*/
public static final char[] BASE64_CHARS =
("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
"01234567890+/").toCharArray();
/**
* The set of month names that will be used.
*/
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
// The character set that should be used to generate the values.
private char[] characterSet;
// The decimal format used to format numeric values.
private DecimalFormat decimalFormat;
// The number of characters between the minimum and maximum length
// (inclusive).
private int lengthRange;
// The maximum number of characters to include in the value.
private int maxLength;
// The minimum number of characters to include in the value.
private int minLength;
// The type of random value that should be generated.
private int randomType;
// The maximum numeric value that should be generated.
private long maxValue;
// The minimum numeric value that should be generated.
private long minValue;
// The number of values between the minimum and maximum value (inclusive).
private long valueRange;
// The random number generator for this tag.
/**
* Creates a new instance of this random tag.
*/
public RandomTag()
{
characterSet = null;
lengthRange = 1;
maxLength = 0;
minLength = 0;
randomType = 0;
maxValue = 0L;
minValue = 0L;
valueRange = 1L;
}
/**
* Retrieves the name for this tag.
*
* @return The name for this tag.
*/
{
return "Random";
}
/**
* Indicates whether this tag is allowed for use in the extra lines for
* branches.
*
* @return <CODE>true</CODE> if this tag may be used in branch definitions,
* or <CODE>false</CODE> if not.
*/
public boolean allowedInBranch()
{
return true;
}
/**
* Performs any initialization for this tag that may be needed while parsing
* a branch definition.
*
* @param templateFile The template file in which this tag is used.
* @param branch The branch in which this tag is used.
* @param arguments The set of arguments provided for this tag.
* @param lineNumber The line number on which this tag appears in the
* template file.
* @param warnings A list into which any appropriate warning messages
* may be placed.
*
* @throws InitializationException If a problem occurs while initializing
* this tag.
*/
throws InitializationException
{
}
/**
* Performs any initialization for this tag that may be needed while parsing
* a template definition.
*
* @param templateFile The template file in which this tag is used.
* @param template The template in which this tag is used.
* @param arguments The set of arguments provided for this tag.
* @param lineNumber The line number on which this tag appears in the
* template file.
* @param warnings A list into which any appropriate warning messages
* may be placed.
*
* @throws InitializationException If a problem occurs while initializing
* this tag.
*/
throws InitializationException
{
}
/**
* Performs any initialization for this tag that may be needed while parsing
* either a branch or template definition.
*
* @param templateFile The template file in which this tag is used.
* @param arguments The set of arguments provided for this tag.
* @param lineNumber The line number on which this tag appears in the
* template file.
* @param warnings A list into which any appropriate warning messages
* may be placed.
*
* @throws InitializationException If a problem occurs while initializing
* this tag.
*/
throws InitializationException
{
// There must be at least one argument, to specify the type of random value
// to generate.
{
throw new InitializationException(message);
}
{
}
{
if (numArgs == 2)
{
try
{
if (minLength < 0)
{
throw new InitializationException(message);
}
else if (minLength == 0)
{
}
}
catch (NumberFormatException nfe)
{
}
}
{
if (numArgs == 4)
{
try
{
}
catch (Exception e)
{
throw new InitializationException(message, e);
}
}
else
{
}
try
{
}
catch (NumberFormatException nfe)
{
}
try
{
{
throw new InitializationException(message);
}
}
catch (NumberFormatException nfe)
{
}
}
else
{
throw new InitializationException(message);
}
}
{
}
{
{
throw new InitializationException(message);
}
}
{
}
{
}
{
if (numArgs == 1)
{
maxLength = 0;
}
else if (numArgs == 2)
{
try
{
if (maxLength <= 0)
{
throw new InitializationException(message);
}
}
catch (NumberFormatException nfe)
{
}
}
else
{
throw new InitializationException(message);
}
}
{
}
else
{
throw new InitializationException(message);
}
}
/**
* Decodes the information in the provided argument list as either a single
* integer specifying the number of characters, or two integers specifying the
* minimum and maximum number of characters.
*
* @param arguments The set of arguments to be processed.
* @param startPos The position at which the first legth value should
* appear in the argument list.
* @param lineNumber The line number on which the tag appears in the
* template file.
* @param warnings A list into which any appropriate warning messages may
* be placed.
*/
throws InitializationException
{
if (numArgs == 2)
{
// There is a fixed number of characters in the value.
try
{
if (minLength < 0)
{
throw new InitializationException(message);
}
else if (minLength == 0)
{
}
}
catch (NumberFormatException nfe)
{
}
}
else if (numArgs == 3)
{
// There are minimum and maximum lengths.
try
{
if (minLength < 0)
{
throw new InitializationException(message);
}
}
catch (NumberFormatException nfe)
{
}
try
{
{
throw new InitializationException(message);
}
else if (maxLength == 0)
{
}
}
catch (NumberFormatException nfe)
{
}
}
else
{
throw new InitializationException(message);
}
}
/**
* Generates the content for this tag by appending it to the provided tag.
*
* @param templateEntry The entry for which this tag is being generated.
* @param templateValue The template value to which the generated content
* should be appended.
*
* @return The result of generating content for this tag.
*/
{
switch (randomType)
{
case RANDOM_TYPE_CHARS_FIXED:
for (int i=0; i < minLength; i++)
{
}
break;
for (int i=0; i < numChars; i++)
{
}
break;
case RANDOM_TYPE_NUMERIC:
long randomValue =
if (decimalFormat == null)
{
}
else
{
}
break;
case RANDOM_TYPE_MONTH:
{
}
else
{
}
break;
case RANDOM_TYPE_TELEPHONE:
for (int i=0; i < 3; i++)
{
}
for (int i=0; i < 3; i++)
{
}
for (int i=0; i < 4; i++)
{
}
break;
}
return TagResult.SUCCESS_RESULT;
}
}