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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.tools.makeldif;
028import org.forgerock.i18n.LocalizableMessage;
029
030
031
032import java.util.List;
033
034import org.opends.server.types.InitializationException;
035
036import static org.opends.messages.ToolMessages.*;
037
038
039
040/**
041 * This class defines a tag that is used to include a sequentially-incrementing
042 * integer in the generated values.
043 */
044public class SequentialTag
045       extends Tag
046{
047  /** Indicates whether to reset for each parent. */
048  private boolean resetOnNewParents;
049
050  /** The initial value in the sequence. */
051  private int initialValue;
052
053  /** The next value in the sequence. */
054  private int nextValue;
055
056
057
058  /**
059   * Creates a new instance of this sequential tag.
060   */
061  public SequentialTag()
062  {
063    // No implementation required.
064  }
065
066
067
068  /**
069   * Retrieves the name for this tag.
070   *
071   * @return  The name for this tag.
072   */
073  public String getName()
074  {
075    return "Sequential";
076  }
077
078
079
080  /**
081   * Indicates whether this tag is allowed for use in the extra lines for
082   * branches.
083   *
084   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
085   *          or <CODE>false</CODE> if not.
086   */
087  public boolean allowedInBranch()
088  {
089    return true;
090  }
091
092
093
094  /**
095   * Performs any initialization for this tag that may be needed while parsing
096   * a branch definition.
097   *
098   * @param  templateFile  The template file in which this tag is used.
099   * @param  branch        The branch in which this tag is used.
100   * @param  arguments     The set of arguments provided for this tag.
101   * @param  lineNumber    The line number on which this tag appears in the
102   *                       template file.
103   * @param  warnings      A list into which any appropriate warning messages
104   *                       may be placed.
105   *
106   * @throws  InitializationException  If a problem occurs while initializing
107   *                                   this tag.
108   */
109  public void initializeForBranch(TemplateFile templateFile, Branch branch,
110                                  String[] arguments, int lineNumber,
111                                  List<LocalizableMessage> warnings)
112         throws InitializationException
113  {
114    initializeInternal(templateFile, arguments, lineNumber);
115  }
116
117
118
119  /**
120   * Performs any initialization for this tag that may be needed while parsing
121   * a template definition.
122   *
123   * @param  templateFile  The template file in which this tag is used.
124   * @param  template      The template in which this tag is used.
125   * @param  arguments     The set of arguments provided for this tag.
126   * @param  lineNumber    The line number on which this tag appears in the
127   *                       template file.
128   * @param  warnings      A list into which any appropriate warning messages
129   *                       may be placed.
130   *
131   * @throws  InitializationException  If a problem occurs while initializing
132   *                                   this tag.
133   */
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);
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   *
152   * @throws  InitializationException  If a problem occurs while initializing
153   *                                   this tag.
154   */
155  private void initializeInternal(TemplateFile templateFile, String[] arguments,
156                                  int lineNumber)
157          throws InitializationException
158  {
159    switch (arguments.length)
160    {
161      case 0:
162        initialValue      = 0;
163        nextValue         = 0;
164        resetOnNewParents = true;
165        break;
166      case 1:
167        try
168        {
169          initialValue = Integer.parseInt(arguments[0]);
170        }
171        catch (NumberFormatException nfe)
172        {
173          LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
174              arguments[0], getName(), lineNumber);
175          throw new InitializationException(message);
176        }
177
178        nextValue         = initialValue;
179        resetOnNewParents = true;
180        break;
181      case 2:
182        try
183        {
184          initialValue = Integer.parseInt(arguments[0]);
185        }
186        catch (NumberFormatException nfe)
187        {
188          LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get(
189              arguments[0], getName(), lineNumber);
190          throw new InitializationException(message);
191        }
192
193        if (arguments[1].equalsIgnoreCase("true"))
194        {
195          resetOnNewParents = true;
196        }
197        else if (arguments[1].equalsIgnoreCase("false"))
198        {
199          resetOnNewParents = false;
200        }
201        else
202        {
203          LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_BOOLEAN.get(
204              arguments[1], getName(), lineNumber);
205          throw new InitializationException(message);
206        }
207
208        nextValue = initialValue;
209        break;
210      default:
211        LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get(
212            getName(), lineNumber, 0, 2, arguments.length);
213        throw new InitializationException(message);
214    }
215  }
216
217
218
219  /**
220   * Performs any initialization for this tag that may be needed when starting
221   * to generate entries below a new parent.
222   *
223   * @param  parentEntry  The entry below which the new entries will be
224   *                      generated.
225   */
226  public void initializeForParent(TemplateEntry parentEntry)
227  {
228    if (resetOnNewParents)
229    {
230      nextValue = initialValue;
231    }
232  }
233
234
235
236  /**
237   * Generates the content for this tag by appending it to the provided tag.
238   *
239   * @param  templateEntry  The entry for which this tag is being generated.
240   * @param  templateValue  The template value to which the generated content
241   *                        should be appended.
242   *
243   * @return  The result of generating content for this tag.
244   */
245  public TagResult generateValue(TemplateEntry templateEntry,
246                                 TemplateValue templateValue)
247  {
248    templateValue.getValue().append(nextValue++);
249    return TagResult.SUCCESS_RESULT;
250  }
251}
252