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 static org.opends.messages.ToolMessages.*; 020import static org.opends.server.tools.makeldif.DNTagUtils.*; 021 022import java.util.List; 023 024import org.forgerock.i18n.LocalizableMessage; 025import org.forgerock.opendj.ldap.DN; 026import org.opends.server.types.InitializationException; 027 028/** 029 * This class defines a tag that is used to include the DN of the current entry 030 * in the attribute value. 031 */ 032public class DNTag 033 extends Tag 034{ 035 /** The number of DN components to include. */ 036 private int numComponents; 037 038 /** Creates a new instance of this DN tag. */ 039 public DNTag() 040 { 041 numComponents = 0; 042 } 043 044 @Override 045 public String getName() 046 { 047 return "DN"; 048 } 049 050 @Override 051 public boolean allowedInBranch() 052 { 053 return true; 054 } 055 056 @Override 057 public void initializeForBranch(TemplateFile templateFile, Branch branch, 058 String[] arguments, int lineNumber, 059 List<LocalizableMessage> warnings) 060 throws InitializationException 061 { 062 initializeInternal(arguments, lineNumber); 063 } 064 065 @Override 066 public void initializeForTemplate(TemplateFile templateFile, 067 Template template, String[] arguments, 068 int lineNumber, List<LocalizableMessage> warnings) 069 throws InitializationException 070 { 071 initializeInternal(arguments, lineNumber); 072 } 073 074 private void initializeInternal(String[] arguments, int lineNumber) throws InitializationException 075 { 076 if (arguments.length == 0) 077 { 078 numComponents = 0; 079 } 080 else if (arguments.length == 1) 081 { 082 try 083 { 084 numComponents = Integer.parseInt(arguments[0]); 085 } 086 catch (NumberFormatException nfe) 087 { 088 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get( 089 arguments[0], getName(), lineNumber); 090 throw new InitializationException(message); 091 } 092 } 093 else 094 { 095 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 096 getName(), lineNumber, 0, 1, arguments.length); 097 throw new InitializationException(message); 098 } 099 } 100 101 @Override 102 public TagResult generateValue(TemplateEntry templateEntry, TemplateValue templateValue) 103 { 104 DN dn = templateEntry.getDN(); 105 if (dn != null && !dn.isRootDN()) 106 { 107 templateValue.getValue().append(generateDNKeepingRDNs(dn, numComponents)); 108 } 109 return TagResult.SUCCESS_RESULT; 110 } 111}