TemplateTag.java revision 66be6c035cb58baccacb6831f7403d5c2247f4b2
/*
* 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-2009 Sun Microsystems, Inc.
* Portions Copyright 2013 ForgeRock AS
*/
/**
* Represents a tag that may be used in a template line when generating entries.
* It can be used to generate content.
*
* @see EntryGenerator
*/
abstract class TemplateTag {
/**
* Retrieves the name for this tag.
*/
/**
* Indicates whether this tag is allowed for use in the extra lines for
* branches.
*/
abstract boolean allowedInBranch();
/**
* Performs any initialization for this tag that may be needed while parsing
* a branch definition.
*
* @param schema
* The schema used to create attributes.
* @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 DecodeException
* If tag cannot be initialized.
*/
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
// No implementation required by default.
}
/**
* Performs any initialization for this tag that may be needed while parsing
* a template definition.
*
* @param schema
* The schema used to create attributes.
* @param templateFile
* The template file in which this tag is used.
* @param template
* The template in which this tag is used.
* @param tagArguments
* 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.
*/
/**
* Performs any initialization for this tag that may be needed when starting
* to generate entries below a new parent.
*
* @param parentEntry
* The entry below which the new entries will be generated.
*/
// No implementation required by default.
}
/**
* Check for an attribute type in a branch or in a template.
*
* @param attrType
* The attribute type to check for.
* @param branch
* The branch that contains the type, or {@code null}
* @param template
* The template that contains the type, or {@code null}
* @return true if either the branch or the template has the provided
* attribute
*/
}
/**
* 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.
*/
/**
* Represents the result of tag generation.
*/
static enum TagResult {
}
/**
* Tag used to reference the value of a specified attribute
* already defined in the entry.
*/
static class AttributeValueTag extends TemplateTag {
/** The attribute type that specifies which value should be used. */
private AttributeType attributeType;
/** The maximum number of characters to include from the value. */
private int numCharacters;
numCharacters = 0;
}
return "AttributeValue";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
void initializeForTemplate(Schema schema, TemplateFile templateFile, Template template, String[] arguments,
}
private void initialize(Schema schema, Branch branch, Template template, String[] arguments, int lineNumber)
throws DecodeException {
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
}
try {
if (numCharacters < 0) {
}
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
} else {
numCharacters = 0;
}
}
// This is fine -- we just won't append anything.
}
if (numCharacters > 0) {
} else {
}
} else {
}
}
}
/**
* Tag used to include the DN of the current entry in the attribute value.
*/
static class DNTag extends TemplateTag {
/** The number of DN components to include. */
private int numComponents;
return "DN";
}
final boolean allowedInBranch() {
return true;
}
final void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
throws DecodeException {
numComponents = 0;
try {
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
} else {
}
}
}
}
if (numComponents == 0) {
// Return the DN of the entry
} else if (numComponents > 0) {
// Return the first numComponents RDNs of the DN
} else {
// numComponents is negative
// Return the last numComponents RDNs of the DN
}
// If expected separator is not standard separator
// Then substitute expected to standard
}
}
}
/**
* Tag used to provide values from a text file. The file should have one
* value per line. Access to the values may be either at random or in
* sequential order.
*/
static class FileTag extends TemplateTag {
/**
* Indicates whether the values should be selected sequentially or at
* random.
*/
private boolean isSequential = false;
/** The index used for sequential access. */
private int nextIndex;
/** The random number generator for this tag. */
/** The lines read from the file. */
return "File";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
throws DecodeException {
// There must be at least one argument, and possibly two.
}
// The first argument should be the path to the file.
try {
if (dataReader == null) {
}
// See if the file has already been read into memory. If not, then
// read it.
try {
} catch (IOException ioe) {
}
} finally {
}
// If there is a second argument, then it should be either
// "sequential" or "random". If there isn't one, then we should
// assume "random".
isSequential = true;
nextIndex = 0;
isSequential = false;
} else {
getName(), lineNumber);
}
} else {
isSequential = false;
}
}
if (isSequential) {
nextIndex = 0;
}
} else {
}
}
}
/**
* Tag used to include a first name in the attribute value.
*/
static class FirstNameTag extends NameTag {
return "First";
}
}
}
/**
* Tag used to include a GUID in the attribute value.
*/
static class GUIDTag extends TemplateTag {
return "GUID";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
}
}
}
/**
*/
static abstract class IfTag extends TemplateTag {
/** The attribute type for which to make the determination. */
/** The value for which to make the determination. */
final boolean allowedInBranch() {
return true;
}
final void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
private void initialize(Schema schema, Branch branch, Template template, String[] arguments, int lineNumber)
throws DecodeException {
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber);
}
} else {
}
}
}
/**
* Tag used to base presence of one attribute on the absence of another
*/
static class IfAbsentTag extends IfTag {
return "IfAbsent";
}
}
if (assertionValue == null) {
}
for (TemplateValue v : values) {
}
}
}
}
/**
* Tag used to base presence of one attribute on the presence of another
*/
static class IfPresentTag extends IfTag {
return "IfPresent";
}
}
if (assertionValue == null) {
}
for (TemplateValue v : values) {
}
}
}
}
/**
* Tag used to include a last name in the attribute value.
*/
static class LastNameTag extends NameTag {
return "Last";
}
}
}
/**
* Tag used to select a value from a pre-defined list, optionally defining
* weights for each value that can impact the likelihood of a given item
* being selected.
* <p>
* The items to include in the list should be specified as arguments to the
* tag. If the argument ends with a semicolon followed by an integer, then
* that will be the weight for that particular item. If no weight is given,
* then the weight for that item will be assumed to be one.
*/
static class ListTag extends TemplateTag {
/** The ultimate cumulative weight. */
private int cumulativeWeight;
/** The set of cumulative weights for the list items. */
private int[] valueWeights;
/** The set of values in the list. */
private String[] valueStrings;
/** The random number generator for this tag. */
return "List";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
}
cumulativeWeight = 0;
int weight = 1;
if (semicolonPos >= 0) {
try {
} catch (NumberFormatException e) {
}
}
valueStrings[i] = value;
valueWeights[i] = cumulativeWeight;
}
}
if (selectedWeight <= valueWeights[i]) {
break;
}
}
}
}
/**
* Base tag to use to include a first name or last name in the attribute
* value.
*/
static abstract class NameTag extends TemplateTag {
/** The template file with which this tag is associated. */
final boolean allowedInBranch() {
return false;
}
this.templateFile = templateFile;
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
}
}
/**
* Base tag to use to include the DN of the parent entry in the attribute value.
*/
static abstract class ParentTag extends TemplateTag {
final boolean allowedInBranch() {
return false;
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
}
}
/**
* Tag used to include the DN of the parent entry in the attribute value.
*/
static class ParentDNTag extends ParentTag {
return "ParentDN";
}
}
}
}
/**
* Tag used to indicate that a value should only be included in a percentage
* of the entries.
*/
static class PresenceTag extends TemplateTag {
/**
* The percentage of the entries in which this attribute value should
* appear.
*/
private int percentage;
/** The random number generator for this tag. */
return "Presence";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
throws DecodeException {
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
try {
if (percentage < 0) {
}
if (percentage > 100) {
}
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
}
if (intValue < percentage) {
} else {
}
}
}
/**
* Tag 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>
*/
static class RandomTag extends TemplateTag {
static enum RandomType {
/**
* Generates values from a fixed number of characters from a given
* character set.
*/
/**
* Generates values from a variable number of characters from a given
* character set.
*/
/**
* Generates numbers.
*/
/**
* Generates months (as text).
*/
/**
* Generates telephone numbers.
*/
}
/**
* The character set that will be used for alphabetic characters.
*/
/**
* 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.
*/
public static final String[] MONTHS = { "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 = 1;
/** 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 RandomType 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 = 1L;
/** The random number generator for this tag. */
return "Random";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
// There must be at least one argument, to specify the type of
// random value to generate.
}
if (numArgs == 2) {
try {
if (minLength < 0) {
} else if (minLength == 0) {
}
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
if (numArgs == 4) {
try {
} catch (Exception e) {
}
} else {
}
try {
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
try {
}
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
} else {
}
}
if (numArgs == 1) {
maxLength = 0;
} else if (numArgs == 2) {
try {
if (maxLength <= 0) {
}
} catch (NumberFormatException nfe) {
getName(), lineNumber);
}
} else {
}
} else {
}
}
/**
* 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.
*/
private void decodeLength(String[] arguments, int startPos, int lineNumber, List<LocalizableMessage> warnings)
throws DecodeException {
if (numArgs == 2) {
// There is a fixed number of characters in the value.
try {
if (minLength < 0) {
} else if (minLength == 0) {
}
} catch (NumberFormatException nfe) {
}
} else if (numArgs == 3) {
// There are minimum and maximum lengths.
try {
if (minLength < 0) {
}
} catch (NumberFormatException nfe) {
}
try {
} else if (maxLength == 0) {
}
} catch (NumberFormatException nfe) {
}
} else {
}
}
switch (randomType) {
case CHARS_FIXED:
for (int i = 0; i < minLength; i++) {
}
break;
case CHARS_VARIABLE:
for (int i = 0; i < numChars; i++) {
}
break;
case NUMERIC:
if (decimalFormat == null) {
} else {
}
break;
case MONTH:
} else {
}
break;
case TELEPHONE:
for (int i = 0; i < 3; i++) {
}
for (int i = 0; i < 3; i++) {
}
for (int i = 0; i < 4; i++) {
}
break;
}
}
}
/**
* Tag used to include the RDN of the current entry in the attribute value.
*/
static class RDNTag extends TemplateTag {
return "RDN";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
void initializeForTemplate(Schema schema, TemplateFile templateFile, Template template, String[] arguments,
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
}
} else {
}
}
}
/**
* Tag that is used to include a sequentially-incrementing integer in the
* generated values.
*/
static class SequentialTag extends TemplateTag {
/** Indicates whether to reset for each parent. */
private boolean resetOnNewParents = true;
/** The initial value in the sequence. */
private int initialValue;
/** The next value in the sequence. */
private int nextValue;
return "Sequential";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
}
}
}
}
resetOnNewParents = true;
resetOnNewParents = false;
} else {
}
}
try {
} catch (NumberFormatException nfe) {
}
}
if (resetOnNewParents) {
}
}
}
}
/**
* Tag that is used to hold static text (i.e., text that appears outside of
* any tag).
*/
static class StaticTextTag extends TemplateTag {
/** The static text to include. */
return "StaticText";
}
boolean allowedInBranch() {
return true;
}
void initializeForBranch(Schema schema, TemplateFile templateFile, Branch branch, String[] arguments,
}
}
LocalizableMessage message = ERR_ENTRY_GENERATOR_TAG_INVALID_ARGUMENT_COUNT.get(getName(), lineNumber,
}
}
}
}
/**
* Tag used to include the DN of the current entry in the attribute value,
* with underscores in place of the commas.
*/
static class UnderscoreDNTag extends DNTag {
return "_DN";
}
}
}
/**
* Tag used to include the DN of the parent entry in the attribute value,
* with underscores in place of commas.
*/
static class UnderscoreParentDNTag extends ParentTag {
return "_ParentDN";
}
}
}
}
}
}