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 /** Creates a new instance of this attribute value tag. */ 044 public AttributeValueTag() 045 { 046 attributeType = null; 047 numCharacters = 0; 048 } 049 050 @Override 051 public String getName() 052 { 053 return "AttributeValue"; 054 } 055 056 @Override 057 public boolean allowedInBranch() 058 { 059 return true; 060 } 061 062 @Override 063 public void initializeForBranch(TemplateFile templateFile, Branch branch, 064 String[] arguments, int lineNumber, 065 List<LocalizableMessage> warnings) 066 throws InitializationException 067 { 068 if (arguments.length < 1 || arguments.length > 2) 069 { 070 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 071 getName(), lineNumber, 1, 2, arguments.length); 072 throw new InitializationException(message); 073 } 074 075 attributeType = DirectoryServer.getSchema().getAttributeType(arguments[0]); 076 if (! branch.hasAttribute(attributeType)) 077 { 078 LocalizableMessage message = 079 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 080 throw new InitializationException(message); 081 } 082 083 if (arguments.length == 2) 084 { 085 try 086 { 087 numCharacters = Integer.parseInt(arguments[1]); 088 if (numCharacters < 0) 089 { 090 LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_BELOW_LOWER_BOUND.get( 091 numCharacters, 0, getName(), lineNumber); 092 throw new InitializationException(message); 093 } 094 } 095 catch (NumberFormatException nfe) 096 { 097 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get( 098 arguments[1], getName(), lineNumber); 099 throw new InitializationException(message); 100 } 101 } 102 else 103 { 104 numCharacters = 0; 105 } 106 } 107 108 @Override 109 public void initializeForTemplate(TemplateFile templateFile, 110 Template template, String[] arguments, 111 int lineNumber, List<LocalizableMessage> warnings) 112 throws InitializationException 113 { 114 if (arguments.length < 1 || arguments.length > 2) 115 { 116 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 117 getName(), lineNumber, 1, 2, arguments.length); 118 throw new InitializationException(message); 119 } 120 121 attributeType = DirectoryServer.getSchema().getAttributeType(arguments[0]); 122 if (! template.hasAttribute(attributeType)) 123 { 124 LocalizableMessage message = 125 ERR_MAKELDIF_TAG_UNDEFINED_ATTRIBUTE.get(arguments[0], lineNumber); 126 throw new InitializationException(message); 127 } 128 129 if (arguments.length == 2) 130 { 131 try 132 { 133 numCharacters = Integer.parseInt(arguments[1]); 134 if (numCharacters < 0) 135 { 136 LocalizableMessage message = ERR_MAKELDIF_TAG_INTEGER_BELOW_LOWER_BOUND.get( 137 numCharacters, 0, getName(), lineNumber); 138 throw new InitializationException(message); 139 } 140 } 141 catch (NumberFormatException nfe) 142 { 143 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get( 144 arguments[1], getName(), lineNumber); 145 throw new InitializationException(message); 146 } 147 } 148 else 149 { 150 numCharacters = 0; 151 } 152 } 153 154 @Override 155 public TagResult generateValue(TemplateEntry templateEntry, 156 TemplateValue templateValue) 157 { 158 TemplateValue v = templateEntry.getValue(attributeType); 159 if (v == null) 160 { 161 // This is fine -- we just won't append anything. 162 return TagResult.SUCCESS_RESULT; 163 } 164 165 if (numCharacters > 0) 166 { 167 String valueString = v.getValue().toString(); 168 if (valueString.length() > numCharacters) 169 { 170 templateValue.append(valueString.substring(0, numCharacters)); 171 } 172 else 173 { 174 templateValue.append(valueString); 175 } 176 } 177 else 178 { 179 templateValue.getValue().append(v.getValue()); 180 } 181 182 return TagResult.SUCCESS_RESULT; 183 } 184}