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.DN;
023import org.forgerock.util.Utils;
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, with underscores in place of commas.
031 */
032public class UnderscoreParentDNTag
033       extends Tag
034{
035  /** Creates a new instance of this underscore parent DN tag. */
036  public UnderscoreParentDNTag()
037  {
038    // No implementation required.
039  }
040
041  @Override
042  public String getName()
043  {
044    return "_ParentDN";
045  }
046
047  @Override
048  public boolean allowedInBranch()
049  {
050    return false;
051  }
052
053  @Override
054  public void initializeForTemplate(TemplateFile templateFile,
055                                    Template template, String[] arguments,
056                                    int lineNumber, List<LocalizableMessage> warnings)
057         throws InitializationException
058  {
059    if (arguments.length != 0)
060    {
061      LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_COUNT.get(
062          getName(), lineNumber, 0, arguments.length);
063      throw new InitializationException(message);
064    }
065  }
066
067  @Override
068  public TagResult generateValue(TemplateEntry templateEntry,
069                                 TemplateValue templateValue)
070  {
071    DN parentDN = templateEntry.getParentDN();
072    if (parentDN != null && !parentDN.isRootDN())
073    {
074      Utils.joinAsString(templateValue.getValue(), "_", parentDN);
075    }
076    return TagResult.SUCCESS_RESULT;
077  }
078}