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