001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.tools.makeldif;
028    import org.opends.messages.Message;
029    
030    
031    
032    import java.util.List;
033    import java.util.Random;
034    
035    import org.opends.server.types.InitializationException;
036    
037    import static org.opends.messages.ToolMessages.*;
038    
039    
040    
041    /**
042     * This class defines a tag that is used to indicate that a value should only be
043     * included in a percentage of the entries.
044     */
045    public class PresenceTag
046           extends Tag
047    {
048      // The percentage of the entries in which this attribute value should appear.
049      private int percentage;
050    
051      // The random number generator for this tag.
052      private Random random;
053    
054    
055    
056      /**
057       * Creates a new instance of this presence tag.
058       */
059      public PresenceTag()
060      {
061        percentage = 100;
062      }
063    
064    
065    
066      /**
067       * Retrieves the name for this tag.
068       *
069       * @return  The name for this tag.
070       */
071      public String getName()
072      {
073        return "Presence";
074      }
075    
076    
077    
078      /**
079       * Indicates whether this tag is allowed for use in the extra lines for
080       * branches.
081       *
082       * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
083       *          or <CODE>false</CODE> if not.
084       */
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      public void initializeForBranch(TemplateFile templateFile, Branch branch,
108                                      String[] arguments, int lineNumber,
109                                      List<Message> warnings)
110             throws InitializationException
111      {
112        initializeInternal(templateFile, arguments,  lineNumber);
113      }
114    
115    
116    
117      /**
118       * Performs any initialization for this tag that may be needed while parsing
119       * a template definition.
120       *
121       * @param  templateFile  The template file in which this tag is used.
122       * @param  template      The template in which this tag is used.
123       * @param  arguments     The set of arguments provided for this tag.
124       * @param  lineNumber    The line number on which this tag appears in the
125       *                       template file.
126       * @param  warnings      A list into which any appropriate warning messages
127       *                       may be placed.
128       *
129       * @throws  InitializationException  If a problem occurs while initializing
130       *                                   this tag.
131       */
132      public void initializeForTemplate(TemplateFile templateFile,
133                                        Template template, String[] arguments,
134                                        int lineNumber, List<Message> warnings)
135             throws InitializationException
136      {
137        initializeInternal(templateFile, arguments,  lineNumber);
138      }
139    
140    
141    
142      /**
143       * Performs any initialization for this tag that may be needed for this tag.
144       *
145       * @param  templateFile  The template file in which this tag is used.
146       * @param  arguments     The set of arguments provided for this tag.
147       * @param  lineNumber    The line number on which this tag appears in the
148       *                       template file.
149       *
150       * @throws  InitializationException  If a problem occurs while initializing
151       *                                   this tag.
152       */
153      private void initializeInternal(TemplateFile templateFile, String[] arguments,
154                                      int lineNumber)
155              throws InitializationException
156      {
157        random = templateFile.getRandom();
158    
159        if (arguments.length != 1)
160        {
161          Message message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
162              getName(), lineNumber, 1, arguments.length);
163          throw new InitializationException(message);
164        }
165    
166        try
167        {
168          percentage = Integer.parseInt(arguments[0]);
169    
170          if (percentage < 0)
171          {
172            Message message = ERR_MAKELDIF_TAG_INTEGER_BELOW_LOWER_BOUND.get(
173                percentage, 0, getName(), lineNumber);
174            throw new InitializationException(message);
175          }
176          else if (percentage > 100)
177          {
178            Message message = ERR_MAKELDIF_TAG_INTEGER_ABOVE_UPPER_BOUND.get(
179                percentage, 100, getName(), lineNumber);
180            throw new InitializationException(message);
181          }
182        }
183        catch (NumberFormatException nfe)
184        {
185          Message message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
186              arguments[0], getName(), lineNumber);
187          throw new InitializationException(message);
188        }
189      }
190    
191    
192    
193      /**
194       * Generates the content for this tag by appending it to the provided tag.
195       *
196       * @param  templateEntry  The entry for which this tag is being generated.
197       * @param  templateValue  The template value to which the generated content
198       *                        should be appended.
199       *
200       * @return  The result of generating content for this tag.
201       */
202      public TagResult generateValue(TemplateEntry templateEntry,
203                                     TemplateValue templateValue)
204      {
205        int intValue = random.nextInt(100);
206        if (intValue < percentage)
207        {
208          return TagResult.SUCCESS_RESULT;
209        }
210        else
211        {
212          return TagResult.OMIT_FROM_ENTRY;
213        }
214      }
215    }
216