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    
034    import org.opends.server.types.InitializationException;
035    
036    import 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     */
044    public 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<Message> 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<Message> 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              Message 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              Message 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              Message 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            Message 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