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