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