001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.tools.makeldif;
018
019
020
021import java.util.List;
022import java.util.Random;
023
024import org.opends.server.types.InitializationException;
025
026import static org.opends.messages.ToolMessages.*;
027import org.forgerock.i18n.LocalizableMessage;
028
029
030/**
031 * This class defines a tag that may be used to select a value from a
032 * pre-defined list, optionally defining weights for each value that can impact
033 * the likelihood of a given item being selected.  The itemts to include in the
034 * list should be specified as arguments to the tag.  If the argument ends with
035 * a semicolon followed by an integer, then that will be the weight for that
036 * particular item.  If no weight is given, then the weight for that item will
037 * be assumed to be one.
038 */
039public class ListTag
040       extends Tag
041{
042  /** The ultimate cumulative weight. */
043  private int cumulativeWeight;
044
045  /** The set of cumulative weights for the list items. */
046  private int[] valueWeights;
047
048  /** The set of values in the list. */
049  private String[] valueStrings;
050
051  /** The random number generator for this tag. */
052  private Random random;
053
054
055
056  /** Creates a new instance of this list tag. */
057  public ListTag()
058  {
059    // No implementation required.
060  }
061
062
063
064  /**
065   * Retrieves the name for this tag.
066   *
067   * @return  The name for this tag.
068   */
069  @Override
070  public String getName()
071  {
072    return "List";
073  }
074
075
076
077  /**
078   * Indicates whether this tag is allowed for use in the extra lines for
079   * branches.
080   *
081   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
082   *          or <CODE>false</CODE> if not.
083   */
084  @Override
085  public boolean allowedInBranch()
086  {
087    return true;
088  }
089
090
091
092  /**
093   * Performs any initialization for this tag that may be needed while parsing
094   * a branch definition.
095   *
096   * @param  templateFile  The template file in which this tag is used.
097   * @param  branch        The branch in which this tag is used.
098   * @param  arguments     The set of arguments provided for this tag.
099   * @param  lineNumber    The line number on which this tag appears in the
100   *                       template file.
101   * @param  warnings      A list into which any appropriate warning messages
102   *                       may be placed.
103   *
104   * @throws  InitializationException  If a problem occurs while initializing
105   *                                   this tag.
106   */
107  @Override
108  public void initializeForBranch(TemplateFile templateFile, Branch branch,
109                                  String[] arguments, int lineNumber,
110                                  List<LocalizableMessage> warnings)
111         throws InitializationException
112  {
113    initializeInternal(templateFile, arguments, lineNumber, warnings);
114  }
115
116
117
118  /**
119   * Performs any initialization for this tag that may be needed while parsing
120   * a template definition.
121   *
122   * @param  templateFile  The template file in which this tag is used.
123   * @param  template      The template in which this tag is used.
124   * @param  arguments     The set of arguments provided for this tag.
125   * @param  lineNumber    The line number on which this tag appears in the
126   *                       template file.
127   * @param  warnings      A list into which any appropriate warning messages
128   *                       may be placed.
129   *
130   * @throws  InitializationException  If a problem occurs while initializing
131   *                                   this tag.
132   */
133  @Override
134  public void initializeForTemplate(TemplateFile templateFile,
135                                    Template template, String[] arguments,
136                                    int lineNumber, List<LocalizableMessage> warnings)
137         throws InitializationException
138  {
139    initializeInternal(templateFile, arguments, lineNumber, warnings);
140  }
141
142
143
144  /**
145   * Performs any initialization for this tag that may be needed for this tag.
146   *
147   * @param  templateFile  The template file in which this tag is used.
148   * @param  arguments     The set of arguments provided for this tag.
149   * @param  lineNumber    The line number on which this tag appears in the
150   *                       template file.
151   * @param  warnings      A list into which any appropriate warning messages
152   *                       may be placed.
153   *
154   * @throws  InitializationException  If a problem occurs while initializing
155   *                                   this tag.
156   */
157  private void initializeInternal(TemplateFile templateFile, String[] arguments,
158                                  int lineNumber, List<LocalizableMessage> warnings)
159          throws InitializationException
160  {
161    if (arguments.length == 0)
162    {
163      throw new InitializationException(
164              ERR_MAKELDIF_TAG_LIST_NO_ARGUMENTS.get(lineNumber));
165    }
166
167
168    valueStrings     = new String[arguments.length];
169    valueWeights     = new int[arguments.length];
170    cumulativeWeight = 0;
171    random           = templateFile.getRandom();
172
173    for (int i=0; i < arguments.length; i++)
174    {
175      String s = arguments[i];
176
177      int weight = 1;
178      int semicolonPos = s.lastIndexOf(';');
179      if (semicolonPos >= 0)
180      {
181        try
182        {
183          weight = Integer.parseInt(s.substring(semicolonPos+1));
184          s = s.substring(0, semicolonPos);
185        }
186        catch (Exception e)
187        {
188          warnings.add(WARN_MAKELDIF_TAG_LIST_INVALID_WEIGHT.get(
189                          lineNumber,s));
190        }
191      }
192
193      cumulativeWeight += weight;
194      valueStrings[i] = s;
195      valueWeights[i] = cumulativeWeight;
196    }
197  }
198
199
200
201  /**
202   * Generates the content for this tag by appending it to the provided tag.
203   *
204   * @param  templateEntry  The entry for which this tag is being generated.
205   * @param  templateValue  The template value to which the generated content
206   *                        should be appended.
207   *
208   * @return  The result of generating content for this tag.
209   */
210  @Override
211  public TagResult generateValue(TemplateEntry templateEntry,
212                                 TemplateValue templateValue)
213  {
214    int selectedWeight = random.nextInt(cumulativeWeight) + 1;
215    for (int i=0; i < valueWeights.length; i++)
216    {
217      if (selectedWeight <= valueWeights[i])
218      {
219        templateValue.getValue().append(valueStrings[i]);
220        break;
221      }
222    }
223
224
225    return TagResult.SUCCESS_RESULT;
226  }
227}
228