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 2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017package org.opends.server.tools.makeldif; 018 019 020 021import java.util.List; 022import java.util.Random; 023 024import org.opends.server.types.InitializationException; 025 026import static org.opends.messages.ToolMessages.*; 027import org.forgerock.i18n.LocalizableMessage; 028 029 030/** 031 * This class defines a tag that may be used to select a value from a 032 * pre-defined list, optionally defining weights for each value that can impact 033 * the likelihood of a given item being selected. The itemts to include in the 034 * list should be specified as arguments to the tag. If the argument ends with 035 * a semicolon followed by an integer, then that will be the weight for that 036 * particular item. If no weight is given, then the weight for that item will 037 * be assumed to be one. 038 */ 039public class ListTag 040 extends Tag 041{ 042 /** The ultimate cumulative weight. */ 043 private int cumulativeWeight; 044 045 /** The set of cumulative weights for the list items. */ 046 private int[] valueWeights; 047 048 /** The set of values in the list. */ 049 private String[] valueStrings; 050 051 /** The random number generator for this tag. */ 052 private Random random; 053 054 055 056 /** 057 * Creates a new instance of this list tag. 058 */ 059 public ListTag() 060 { 061 // No implementation required. 062 } 063 064 065 066 /** 067 * Retrieves the name for this tag. 068 * 069 * @return The name for this tag. 070 */ 071 public String getName() 072 { 073 return "List"; 074 } 075 076 077 078 /** 079 * Indicates whether this tag is allowed for use in the extra lines for 080 * branches. 081 * 082 * @return <CODE>true</CODE> if this tag may be used in branch definitions, 083 * or <CODE>false</CODE> if not. 084 */ 085 public boolean allowedInBranch() 086 { 087 return true; 088 } 089 090 091 092 /** 093 * Performs any initialization for this tag that may be needed while parsing 094 * a branch definition. 095 * 096 * @param templateFile The template file in which this tag is used. 097 * @param branch The branch in which this tag is used. 098 * @param arguments The set of arguments provided for this tag. 099 * @param lineNumber The line number on which this tag appears in the 100 * template file. 101 * @param warnings A list into which any appropriate warning messages 102 * may be placed. 103 * 104 * @throws InitializationException If a problem occurs while initializing 105 * this tag. 106 */ 107 public void initializeForBranch(TemplateFile templateFile, Branch branch, 108 String[] arguments, int lineNumber, 109 List<LocalizableMessage> warnings) 110 throws InitializationException 111 { 112 initializeInternal(templateFile, arguments, lineNumber, warnings); 113 } 114 115 116 117 /** 118 * Performs any initialization for this tag that may be needed while parsing 119 * a template definition. 120 * 121 * @param templateFile The template file in which this tag is used. 122 * @param template The template in which this tag is used. 123 * @param arguments The set of arguments provided for this tag. 124 * @param lineNumber The line number on which this tag appears in the 125 * template file. 126 * @param warnings A list into which any appropriate warning messages 127 * may be placed. 128 * 129 * @throws InitializationException If a problem occurs while initializing 130 * this tag. 131 */ 132 public void initializeForTemplate(TemplateFile templateFile, 133 Template template, String[] arguments, 134 int lineNumber, List<LocalizableMessage> warnings) 135 throws InitializationException 136 { 137 initializeInternal(templateFile, arguments, lineNumber, warnings); 138 } 139 140 141 142 /** 143 * Performs any initialization for this tag that may be needed for this tag. 144 * 145 * @param templateFile The template file in which this tag is used. 146 * @param arguments The set of arguments provided for this tag. 147 * @param lineNumber The line number on which this tag appears in the 148 * template file. 149 * @param warnings A list into which any appropriate warning messages 150 * may be placed. 151 * 152 * @throws InitializationException If a problem occurs while initializing 153 * this tag. 154 */ 155 private void initializeInternal(TemplateFile templateFile, String[] arguments, 156 int lineNumber, List<LocalizableMessage> warnings) 157 throws InitializationException 158 { 159 if (arguments.length == 0) 160 { 161 throw new InitializationException( 162 ERR_MAKELDIF_TAG_LIST_NO_ARGUMENTS.get(lineNumber)); 163 } 164 165 166 valueStrings = new String[arguments.length]; 167 valueWeights = new int[arguments.length]; 168 cumulativeWeight = 0; 169 random = templateFile.getRandom(); 170 171 for (int i=0; i < arguments.length; i++) 172 { 173 String s = arguments[i]; 174 175 int weight = 1; 176 int semicolonPos = s.lastIndexOf(';'); 177 if (semicolonPos >= 0) 178 { 179 try 180 { 181 weight = Integer.parseInt(s.substring(semicolonPos+1)); 182 s = s.substring(0, semicolonPos); 183 } 184 catch (Exception e) 185 { 186 warnings.add(WARN_MAKELDIF_TAG_LIST_INVALID_WEIGHT.get( 187 lineNumber,s)); 188 } 189 } 190 191 cumulativeWeight += weight; 192 valueStrings[i] = s; 193 valueWeights[i] = cumulativeWeight; 194 } 195 } 196 197 198 199 /** 200 * Generates the content for this tag by appending it to the provided tag. 201 * 202 * @param templateEntry The entry for which this tag is being generated. 203 * @param templateValue The template value to which the generated content 204 * should be appended. 205 * 206 * @return The result of generating content for this tag. 207 */ 208 public TagResult generateValue(TemplateEntry templateEntry, 209 TemplateValue templateValue) 210 { 211 int selectedWeight = random.nextInt(cumulativeWeight) + 1; 212 for (int i=0; i < valueWeights.length; i++) 213 { 214 if (selectedWeight <= valueWeights[i]) 215 { 216 templateValue.getValue().append(valueStrings[i]); 217 break; 218 } 219 } 220 221 222 return TagResult.SUCCESS_RESULT; 223 } 224} 225