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