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-2015 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  /**
047   * Creates a new instance of this presence tag.
048   */
049  public PresenceTag()
050  {
051    percentage = 100;
052  }
053
054
055
056  /**
057   * Retrieves the name for this tag.
058   *
059   * @return  The name for this tag.
060   */
061  public String getName()
062  {
063    return "Presence";
064  }
065
066
067
068  /**
069   * Indicates whether this tag is allowed for use in the extra lines for
070   * branches.
071   *
072   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
073   *          or <CODE>false</CODE> if not.
074   */
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  public void initializeForBranch(TemplateFile templateFile, Branch branch,
098                                  String[] arguments, int lineNumber,
099                                  List<LocalizableMessage> warnings)
100         throws InitializationException
101  {
102    initializeInternal(templateFile, arguments,  lineNumber);
103  }
104
105
106
107  /**
108   * Performs any initialization for this tag that may be needed while parsing
109   * a template definition.
110   *
111   * @param  templateFile  The template file in which this tag is used.
112   * @param  template      The template in which this tag is used.
113   * @param  arguments     The set of arguments provided for this tag.
114   * @param  lineNumber    The line number on which this tag appears in the
115   *                       template file.
116   * @param  warnings      A list into which any appropriate warning messages
117   *                       may be placed.
118   *
119   * @throws  InitializationException  If a problem occurs while initializing
120   *                                   this tag.
121   */
122  public void initializeForTemplate(TemplateFile templateFile,
123                                    Template template, String[] arguments,
124                                    int lineNumber, List<LocalizableMessage> warnings)
125         throws InitializationException
126  {
127    initializeInternal(templateFile, arguments,  lineNumber);
128  }
129
130
131
132  /**
133   * Performs any initialization for this tag that may be needed for this tag.
134   *
135   * @param  templateFile  The template file in which this tag is used.
136   * @param  arguments     The set of arguments provided for this tag.
137   * @param  lineNumber    The line number on which this tag appears in the
138   *                       template file.
139   *
140   * @throws  InitializationException  If a problem occurs while initializing
141   *                                   this tag.
142   */
143  private void initializeInternal(TemplateFile templateFile, String[] arguments,
144                                  int lineNumber)
145          throws InitializationException
146  {
147    random = templateFile.getRandom();
148
149    if (arguments.length != 1)
150    {
151      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
152          getName(), lineNumber, 1, arguments.length);
153      throw new InitializationException(message);
154    }
155
156    try
157    {
158      percentage = Integer.parseInt(arguments[0]);
159
160      if (percentage < 0)
161      {
162        LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_BELOW_LOWER_BOUND.get(
163            percentage, 0, getName(), lineNumber);
164        throw new InitializationException(message);
165      }
166      else if (percentage > 100)
167      {
168        LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_ABOVE_UPPER_BOUND.get(
169            percentage, 100, getName(), lineNumber);
170        throw new InitializationException(message);
171      }
172    }
173    catch (NumberFormatException nfe)
174    {
175      LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
176          arguments[0], getName(), lineNumber);
177      throw new InitializationException(message);
178    }
179  }
180
181
182
183  /**
184   * Generates the content for this tag by appending it to the provided tag.
185   *
186   * @param  templateEntry  The entry for which this tag is being generated.
187   * @param  templateValue  The template value to which the generated content
188   *                        should be appended.
189   *
190   * @return  The result of generating content for this tag.
191   */
192  public TagResult generateValue(TemplateEntry templateEntry,
193                                 TemplateValue templateValue)
194  {
195    int intValue = random.nextInt(100);
196    if (intValue < percentage)
197    {
198      return TagResult.SUCCESS_RESULT;
199    }
200    else
201    {
202      return TagResult.OMIT_FROM_ENTRY;
203    }
204  }
205}
206