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