mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
14.50.2007 88b105177c72919b395055e2a040a30dc23369e0
Implement support for the "list" tag for use with MakeLDIF.  This was the last
major tag type that was supported by the SLAMD variant of MakeLDIF that was not
available with the OpenDS version. Unfortunately, the OpenDS version is not
able to use exactly the same syntax as the SLAMD version, but the OpenDS
version has all of the same functionality and its syntax is actually more
consistent with that used by other tags than the SLAMD version.

OpenDS Issue Number: 588
1 files added
2 files modified
270 ■■■■■ changed files
opends/src/server/org/opends/server/messages/ToolMessages.java 31 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/makeldif/ListTag.java 238 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/makeldif/TemplateFile.java 1 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/messages/ToolMessages.java
@@ -9751,6 +9751,27 @@
  /**
   * The message ID for the message that will be used if a list tag does not
   * contain any arguments.  This takes a single argument, which is the line
   * number on which it appears in the template file.
   */
  public static final int MSGID_MAKELDIF_TAG_LIST_NO_ARGUMENTS =
       CATEGORY_MASK_TOOLS | SEVERITY_MASK_MILD_ERROR | 1291;
  /**
   * The message ID for the message that will be used if a list tag includes an
   * argument with a semicolon that is not followed by an integer.  This takes
   * two arguments, which are the line number on which it appears in the
   * template file and the list item containing the semicolon.
   */
  public static final int MSGID_MAKELDIF_TAG_LIST_INVALID_WEIGHT =
       CATEGORY_MASK_TOOLS | SEVERITY_MASK_MILD_WARNING | 1292;
  /**
   * Associates a set of generic messages with the message IDs defined in this
   * class.
   */
@@ -11499,6 +11520,16 @@
                    "LDIF:  %s");
    registerMessage(MSGID_MAKELDIF_PROCESSING_COMPLETE,
                    "LDIF processing complete.  %d entries written");
    registerMessage(MSGID_MAKELDIF_TAG_LIST_NO_ARGUMENTS,
                    "The list tag on line %d of the template file does not " +
                    "contain any arguments to specify the list values.  At " +
                    "least one list value must be provided");
    registerMessage(MSGID_MAKELDIF_TAG_LIST_INVALID_WEIGHT,
                    "The list tag on line %d of the template file contains " +
                    "item '%s' that includes a semicolon but that semicolon " +
                    "is not followed by an integer.  The semicolon will be " +
                    "assumed to be part of the value and not a delimiter to " +
                    "separate the value from its relative weight");
    registerMessage(MSGID_LDIFMODIFY_CANNOT_ADD_ENTRY_TWICE,
opends/src/server/org/opends/server/tools/makeldif/ListTag.java
New file
@@ -0,0 +1,238 @@
/*
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  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
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
package org.opends.server.tools.makeldif;
import java.util.List;
import java.util.Random;
import org.opends.server.types.InitializationException;
import static org.opends.server.messages.MessageHandler.*;
import static org.opends.server.messages.ToolMessages.*;
/**
 * This class defines a tag that may be 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.  The itemts 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.
 */
public class ListTag
       extends Tag
{
  // 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.
  private Random random;
  /**
   * Creates a new instance of this list tag.
   */
  public ListTag()
  {
    // No implementation required.
  }
  /**
   * Retrieves the name for this tag.
   *
   * @return  The name for this tag.
   */
  public String getName()
  {
    return "List";
  }
  /**
   * 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.
   */
  public void initializeForBranch(TemplateFile templateFile, Branch branch,
                                  String[] arguments, int lineNumber,
                                  List<String> warnings)
         throws InitializationException
  {
    initializeInternal(templateFile, arguments, lineNumber, warnings);
  }
  /**
   * 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.
   */
  public void initializeForTemplate(TemplateFile templateFile,
                                    Template template, String[] arguments,
                                    int lineNumber, List<String> warnings)
         throws InitializationException
  {
    initializeInternal(templateFile, arguments, lineNumber, warnings);
  }
  /**
   * Performs any initialization for this tag that may be needed for this tag.
   *
   * @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.
   */
  private void initializeInternal(TemplateFile templateFile, String[] arguments,
                                  int lineNumber, List<String> warnings)
          throws InitializationException
  {
    if (arguments.length == 0)
    {
      int    msgID   = MSGID_MAKELDIF_TAG_LIST_NO_ARGUMENTS;
      String message = getMessage(msgID, lineNumber);
      throw new InitializationException(msgID, message);
    }
    valueStrings     = new String[arguments.length];
    valueWeights     = new int[arguments.length];
    cumulativeWeight = 0;
    random           = templateFile.getRandom();
    for (int i=0; i < arguments.length; i++)
    {
      String s = arguments[i];
      int weight = 1;
      int semicolonPos = s.lastIndexOf(';');
      if (semicolonPos >= 0)
      {
        try
        {
          weight = Integer.parseInt(s.substring(semicolonPos+1));
          s = s.substring(0, semicolonPos);
        }
        catch (Exception e)
        {
          int    msgID   = MSGID_MAKELDIF_TAG_LIST_INVALID_WEIGHT;
          String message = getMessage(msgID, lineNumber, s);
          warnings.add(message);
        }
      }
      cumulativeWeight += weight;
      valueStrings[i] = s;
      valueWeights[i] = cumulativeWeight;
    }
  }
  /**
   * 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.
   */
  public TagResult generateValue(TemplateEntry templateEntry,
                                 TemplateValue templateValue)
  {
    int selectedWeight = random.nextInt(cumulativeWeight) + 1;
    for (int i=0; i < valueWeights.length; i++)
    {
      if (selectedWeight <= valueWeights[i])
      {
        templateValue.getValue().append(valueStrings[i]);
        break;
      }
    }
    return TagResult.SUCCESS_RESULT;
  }
}
opends/src/server/org/opends/server/tools/makeldif/TemplateFile.java
@@ -284,6 +284,7 @@
      IfAbsentTag.class,
      IfPresentTag.class,
      LastNameTag.class,
      ListTag.class,
      ParentDNTag.class,
      PresenceTag.class,
      RandomTag.class,