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