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-2015 ForgeRock AS. 016 */ 017package org.opends.server.tools.makeldif; 018import org.forgerock.i18n.LocalizableMessage; 019 020 021 022import java.util.List; 023 024import org.opends.server.types.InitializationException; 025 026import static org.opends.messages.ToolMessages.*; 027 028 029 030/** 031 * This class defines a tag that is used to include a sequentially-incrementing 032 * integer in the generated values. 033 */ 034public class SequentialTag 035 extends Tag 036{ 037 /** Indicates whether to reset for each parent. */ 038 private boolean resetOnNewParents; 039 040 /** The initial value in the sequence. */ 041 private int initialValue; 042 043 /** The next value in the sequence. */ 044 private int nextValue; 045 046 047 048 /** 049 * Creates a new instance of this sequential tag. 050 */ 051 public SequentialTag() 052 { 053 // No implementation required. 054 } 055 056 057 058 /** 059 * Retrieves the name for this tag. 060 * 061 * @return The name for this tag. 062 */ 063 public String getName() 064 { 065 return "Sequential"; 066 } 067 068 069 070 /** 071 * Indicates whether this tag is allowed for use in the extra lines for 072 * branches. 073 * 074 * @return <CODE>true</CODE> if this tag may be used in branch definitions, 075 * or <CODE>false</CODE> if not. 076 */ 077 public boolean allowedInBranch() 078 { 079 return true; 080 } 081 082 083 084 /** 085 * Performs any initialization for this tag that may be needed while parsing 086 * a branch definition. 087 * 088 * @param templateFile The template file in which this tag is used. 089 * @param branch The branch in which this tag is used. 090 * @param arguments The set of arguments provided for this tag. 091 * @param lineNumber The line number on which this tag appears in the 092 * template file. 093 * @param warnings A list into which any appropriate warning messages 094 * may be placed. 095 * 096 * @throws InitializationException If a problem occurs while initializing 097 * this tag. 098 */ 099 public void initializeForBranch(TemplateFile templateFile, Branch branch, 100 String[] arguments, int lineNumber, 101 List<LocalizableMessage> warnings) 102 throws InitializationException 103 { 104 initializeInternal(templateFile, arguments, lineNumber); 105 } 106 107 108 109 /** 110 * Performs any initialization for this tag that may be needed while parsing 111 * a template definition. 112 * 113 * @param templateFile The template file in which this tag is used. 114 * @param template The template in which this tag is used. 115 * @param arguments The set of arguments provided for this tag. 116 * @param lineNumber The line number on which this tag appears in the 117 * template file. 118 * @param warnings A list into which any appropriate warning messages 119 * may be placed. 120 * 121 * @throws InitializationException If a problem occurs while initializing 122 * this tag. 123 */ 124 public void initializeForTemplate(TemplateFile templateFile, 125 Template template, String[] arguments, 126 int lineNumber, List<LocalizableMessage> warnings) 127 throws InitializationException 128 { 129 initializeInternal(templateFile, arguments, lineNumber); 130 } 131 132 133 134 /** 135 * Performs any initialization for this tag that may be needed for this tag. 136 * 137 * @param templateFile The template file in which this tag is used. 138 * @param arguments The set of arguments provided for this tag. 139 * @param lineNumber The line number on which this tag appears in the 140 * template file. 141 * 142 * @throws InitializationException If a problem occurs while initializing 143 * this tag. 144 */ 145 private void initializeInternal(TemplateFile templateFile, String[] arguments, 146 int lineNumber) 147 throws InitializationException 148 { 149 switch (arguments.length) 150 { 151 case 0: 152 initialValue = 0; 153 nextValue = 0; 154 resetOnNewParents = true; 155 break; 156 case 1: 157 try 158 { 159 initialValue = Integer.parseInt(arguments[0]); 160 } 161 catch (NumberFormatException nfe) 162 { 163 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get( 164 arguments[0], getName(), lineNumber); 165 throw new InitializationException(message); 166 } 167 168 nextValue = initialValue; 169 resetOnNewParents = true; 170 break; 171 case 2: 172 try 173 { 174 initialValue = Integer.parseInt(arguments[0]); 175 } 176 catch (NumberFormatException nfe) 177 { 178 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_INTEGER.get( 179 arguments[0], getName(), lineNumber); 180 throw new InitializationException(message); 181 } 182 183 if (arguments[1].equalsIgnoreCase("true")) 184 { 185 resetOnNewParents = true; 186 } 187 else if (arguments[1].equalsIgnoreCase("false")) 188 { 189 resetOnNewParents = false; 190 } 191 else 192 { 193 LocalizableMessage message = ERR_MAKELDIF_TAG_CANNOT_PARSE_AS_BOOLEAN.get( 194 arguments[1], getName(), lineNumber); 195 throw new InitializationException(message); 196 } 197 198 nextValue = initialValue; 199 break; 200 default: 201 LocalizableMessage message = ERR_MAKELDIF_TAG_INVALID_ARGUMENT_RANGE_COUNT.get( 202 getName(), lineNumber, 0, 2, arguments.length); 203 throw new InitializationException(message); 204 } 205 } 206 207 208 209 /** 210 * Performs any initialization for this tag that may be needed when starting 211 * to generate entries below a new parent. 212 * 213 * @param parentEntry The entry below which the new entries will be 214 * generated. 215 */ 216 public void initializeForParent(TemplateEntry parentEntry) 217 { 218 if (resetOnNewParents) 219 { 220 nextValue = initialValue; 221 } 222 } 223 224 225 226 /** 227 * Generates the content for this tag by appending it to the provided tag. 228 * 229 * @param templateEntry The entry for which this tag is being generated. 230 * @param templateValue The template value to which the generated content 231 * should be appended. 232 * 233 * @return The result of generating content for this tag. 234 */ 235 public TagResult generateValue(TemplateEntry templateEntry, 236 TemplateValue templateValue) 237 { 238 templateValue.getValue().append(nextValue++); 239 return TagResult.SUCCESS_RESULT; 240 } 241} 242