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 ForgeRock AS.
016 */
017package org.opends.server.tools.makeldif;
018import org.forgerock.i18n.LocalizableMessage;
019
020
021
022import java.util.List;
023import java.util.UUID;
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 include a GUID in the attribute
033 * value.
034 */
035public class GUIDTag
036       extends Tag
037{
038  /**
039   * Creates a new instance of this GUID tag.
040   */
041  public GUIDTag()
042  {
043    // No implementation required.
044  }
045
046
047
048  /**
049   * Retrieves the name for this tag.
050   *
051   * @return  The name for this tag.
052   */
053  public String getName()
054  {
055    return "GUID";
056  }
057
058
059
060  /**
061   * Indicates whether this tag is allowed for use in the extra lines for
062   * branches.
063   *
064   * @return  <CODE>true</CODE> if this tag may be used in branch definitions,
065   *          or <CODE>false</CODE> if not.
066   */
067  public boolean allowedInBranch()
068  {
069    return true;
070  }
071
072
073
074  /**
075   * Performs any initialization for this tag that may be needed while parsing
076   * a branch definition.
077   *
078   * @param  templateFile  The template file in which this tag is used.
079   * @param  branch        The branch in which this tag is used.
080   * @param  arguments     The set of arguments provided for this tag.
081   * @param  lineNumber    The line number on which this tag appears in the
082   *                       template file.
083   * @param  warnings      A list into which any appropriate warning messages
084   *                       may be placed.
085   *
086   * @throws  InitializationException  If a problem occurs while initializing
087   *                                   this tag.
088   */
089  public void initializeForBranch(TemplateFile templateFile, Branch branch,
090                                  String[] arguments, int lineNumber,
091                                  List<LocalizableMessage> warnings)
092         throws InitializationException
093  {
094    if (arguments.length != 0)
095    {
096      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
097          getName(), lineNumber, 0, arguments.length);
098      throw new InitializationException(message);
099    }
100  }
101
102
103
104  /**
105   * Performs any initialization for this tag that may be needed while parsing
106   * a template definition.
107   *
108   * @param  templateFile  The template file in which this tag is used.
109   * @param  template      The template in which this tag is used.
110   * @param  arguments     The set of arguments provided for this tag.
111   * @param  lineNumber    The line number on which this tag appears in the
112   *                       template file.
113   * @param  warnings      A list into which any appropriate warning messages
114   *                       may be placed.
115   *
116   * @throws  InitializationException  If a problem occurs while initializing
117   *                                   this tag.
118   */
119  public void initializeForTemplate(TemplateFile templateFile,
120                                    Template template, String[] arguments,
121                                    int lineNumber, List<LocalizableMessage> warnings)
122         throws InitializationException
123  {
124    if (arguments.length != 0)
125    {
126      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
127          getName(), lineNumber, 0, arguments.length);
128      throw new InitializationException(message);
129    }
130  }
131
132
133
134  /**
135   * Generates the content for this tag by appending it to the provided tag.
136   *
137   * @param  templateEntry  The entry for which this tag is being generated.
138   * @param  templateValue  The template value to which the generated content
139   *                        should be appended.
140   *
141   * @return  The result of generating content for this tag.
142   */
143  public TagResult generateValue(TemplateEntry templateEntry,
144                                 TemplateValue templateValue)
145  {
146    templateValue.append(UUID.randomUUID().toString());
147    return TagResult.SUCCESS_RESULT;
148  }
149}
150