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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.server.tools.makeldif;
018import org.forgerock.i18n.LocalizableMessage;
019
020
021
022import java.util.List;
023import java.util.Random;
024
025import org.opends.server.types.InitializationException;
026
027import static org.opends.messages.ToolMessages.*;
028
029
030
031/**
032 * This class defines a tag that is used to indicate that a value should only be
033 * included in a percentage of the entries.
034 */
035public class PresenceTag
036       extends Tag
037{
038  /** The percentage of the entries in which this attribute value should appear. */
039  private int percentage;
040
041  /** The random number generator for this tag. */
042  private Random random;
043
044
045
046  /** Creates a new instance of this presence tag. */
047  public PresenceTag()
048  {
049    percentage = 100;
050  }
051
052
053
054  /**
055   * Retrieves the name for this tag.
056   *
057   * @return  The name for this tag.
058   */
059  @Override
060  public String getName()
061  {
062    return "Presence";
063  }
064
065
066
067  /**
068   * Indicates whether this tag is allowed for use in the extra lines for
069   * branches.
070   *
071   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
072   *          or <CODE>false</CODE> if not.
073   */
074  @Override
075  public boolean allowedInBranch()
076  {
077    return true;
078  }
079
080
081
082  /**
083   * Performs any initialization for this tag that may be needed while parsing
084   * a branch definition.
085   *
086   * @param  templateFile  The template file in which this tag is used.
087   * @param  branch        The branch in which this tag is used.
088   * @param  arguments     The set of arguments provided for this tag.
089   * @param  lineNumber    The line number on which this tag appears in the
090   *                       template file.
091   * @param  warnings      A list into which any appropriate warning messages
092   *                       may be placed.
093   *
094   * @throws  InitializationException  If a problem occurs while initializing
095   *                                   this tag.
096   */
097  @Override
098  public void initializeForBranch(TemplateFile templateFile, Branch branch,
099                                  String[] arguments, int lineNumber,
100                                  List<LocalizableMessage> warnings)
101         throws InitializationException
102  {
103    initializeInternal(templateFile, arguments,  lineNumber);
104  }
105
106
107
108  /**
109   * Performs any initialization for this tag that may be needed while parsing
110   * a template definition.
111   *
112   * @param  templateFile  The template file in which this tag is used.
113   * @param  template      The template in which this tag is used.
114   * @param  arguments     The set of arguments provided for this tag.
115   * @param  lineNumber    The line number on which this tag appears in the
116   *                       template file.
117   * @param  warnings      A list into which any appropriate warning messages
118   *                       may be placed.
119   *
120   * @throws  InitializationException  If a problem occurs while initializing
121   *                                   this tag.
122   */
123  @Override
124  public void initializeForTemplate(TemplateFile templateFile,
125                                    Template template, String[] arguments,
126                                    int lineNumber, List<LocalizableMessage> warnings)
127         throws InitializationException
128  {
129    initializeInternal(templateFile, arguments,  lineNumber);
130  }
131
132
133
134  /**
135   * Performs any initialization for this tag that may be needed for this tag.
136   *
137   * @param  templateFile  The template file in which this tag is used.
138   * @param  arguments     The set of arguments provided for this tag.
139   * @param  lineNumber    The line number on which this tag appears in the
140   *                       template file.
141   *
142   * @throws  InitializationException  If a problem occurs while initializing
143   *                                   this tag.
144   */
145  private void initializeInternal(TemplateFile templateFile, String[] arguments,
146                                  int lineNumber)
147          throws InitializationException
148  {
149    random = templateFile.getRandom();
150
151    if (arguments.length != 1)
152    {
153      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
154          getName(), lineNumber, 1, arguments.length);
155      throw new InitializationException(message);
156    }
157
158    try
159    {
160      percentage = Integer.parseInt(arguments[0]);
161
162      if (percentage < 0)
163      {
164        LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_BELOW_LOWER_BOUND.get(
165            percentage, 0, getName(), lineNumber);
166        throw new InitializationException(message);
167      }
168      else if (percentage > 100)
169      {
170        LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_ABOVE_UPPER_BOUND.get(
171            percentage, 100, getName(), lineNumber);
172        throw new InitializationException(message);
173      }
174    }
175    catch (NumberFormatException nfe)
176    {
177      LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
178          arguments[0], getName(), lineNumber);
179      throw new InitializationException(message);
180    }
181  }
182
183
184
185  /**
186   * Generates the content for this tag by appending it to the provided tag.
187   *
188   * @param  templateEntry  The entry for which this tag is being generated.
189   * @param  templateValue  The template value to which the generated content
190   *                        should be appended.
191   *
192   * @return  The result of generating content for this tag.
193   */
194  @Override
195  public TagResult generateValue(TemplateEntry templateEntry,
196                                 TemplateValue templateValue)
197  {
198    int intValue = random.nextInt(100);
199    if (intValue < percentage)
200    {
201      return TagResult.SUCCESS_RESULT;
202    }
203    else
204    {
205      return TagResult.OMIT_FROM_ENTRY;
206    }
207  }
208}
209