1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.benerator.primitive; 28 29 import com.rapiddweller.benerator.Generator; 30 import com.rapiddweller.benerator.GeneratorContext; 31 import com.rapiddweller.benerator.InvalidGeneratorSetupException; 32 import com.rapiddweller.benerator.factory.GeneratorFactory; 33 import com.rapiddweller.benerator.factory.StochasticGeneratorFactory; 34 import com.rapiddweller.benerator.wrapper.NonNullGeneratorProxy; 35 import com.rapiddweller.model.data.Uniqueness; 36 37 import java.util.Locale; 38 39 /** 40 * Generates Strings that comply to a regular expression.<br/> 41 * <br/> 42 * Created: 18.07.2006 19:32:52 43 * 44 * @author Volker Bergmann 45 * @since 0.1 46 */ 47 public class RegexStringGenerator extends NonNullGeneratorProxy<String> { 48 49 /** 50 * Optional String representation of a regular expression 51 */ 52 private String pattern; 53 54 private boolean unique; 55 56 private boolean ordered; 57 58 /** 59 * The locale from which to choose letters 60 */ 61 private Locale locale; 62 63 private int minLength; 64 65 private final int maxLength; 66 67 // constructors ---------------------------------------------------------------------------------------------------- 68 69 /** 70 * Initializes the generator to an empty regular expression, a maxQuantity of 30 and the fallback locale 71 */ 72 public RegexStringGenerator() { 73 this(30); 74 } 75 76 /** 77 * Initializes the generator to an empty regular expression and the fallback locale 78 * 79 * @param maxLength the max length 80 */ 81 public RegexStringGenerator(int maxLength) { 82 this(null, maxLength); 83 } 84 85 /** 86 * Initializes the generator to a maxQuantity of 30 and the fallback locale 87 * 88 * @param pattern the pattern 89 */ 90 public RegexStringGenerator(String pattern) { 91 this(pattern, 30); 92 } 93 94 /** 95 * Initializes the generator to the fallback locale 96 * 97 * @param pattern the pattern 98 * @param maxLength the max length 99 */ 100 public RegexStringGenerator(String pattern, int maxLength) { 101 this(pattern, maxLength, false); 102 } 103 104 /** 105 * Initializes the generator with the String representation of a regular expression 106 * 107 * @param pattern the pattern 108 * @param maxLength the max length 109 * @param unique the unique 110 */ 111 public RegexStringGenerator(String pattern, Integer maxLength, boolean unique) { 112 super(String.class); 113 this.pattern = pattern; 114 this.maxLength = maxLength; 115 this.unique = unique; 116 this.ordered = false; 117 } 118 119 // config properties ----------------------------------------------------------------------------------------------- 120 121 /** 122 * Sets the String representation of the regular expression 123 * 124 * @return the pattern 125 */ 126 public String getPattern() { 127 return pattern; 128 } 129 130 /** 131 * Returns the String representation of the regular expression 132 * 133 * @param pattern the pattern 134 */ 135 public void setPattern(String pattern) { 136 this.pattern = pattern; 137 } 138 139 /** 140 * Is unique boolean. 141 * 142 * @return the boolean 143 */ 144 public boolean isUnique() { 145 return unique; 146 } 147 148 /** 149 * Sets unique. 150 * 151 * @param unique the unique 152 */ 153 public void setUnique(boolean unique) { 154 this.unique = unique; 155 } 156 157 /** 158 * Is ordered boolean. 159 * 160 * @return the boolean 161 */ 162 public boolean isOrdered() { 163 return ordered; 164 } 165 166 /** 167 * Sets ordered. 168 * 169 * @param ordered the ordered 170 */ 171 public void setOrdered(boolean ordered) { 172 this.ordered = ordered; 173 } 174 175 /** 176 * Gets locale. 177 * 178 * @return the locale 179 */ 180 public Locale getLocale() { 181 return locale; 182 } 183 184 /** 185 * Sets locale. 186 * 187 * @param locale the locale 188 */ 189 public void setLocale(Locale locale) { 190 this.locale = locale; 191 } 192 193 /** 194 * Gets min length. 195 * 196 * @return the min length 197 */ 198 public int getMinLength() { 199 return minLength; 200 } 201 202 /** 203 * Sets min length. 204 * 205 * @param minLength the min length 206 */ 207 public void setMinLength(int minLength) { 208 this.minLength = minLength; 209 } 210 211 /** 212 * Gets max length. 213 * 214 * @return the max length 215 */ 216 public int getMaxLength() { 217 return maxLength; 218 } 219 220 // Generator interface --------------------------------------------------------------------------------------------- 221 222 /** 223 * ensures consistency of the generators state 224 */ 225 @Override 226 public void init(GeneratorContext context) { 227 Generator<String> tmp = getGeneratorFactory(context).createRegexStringGenerator( 228 pattern, minLength, maxLength, Uniqueness.instance(unique, ordered)); 229 try { 230 setSource(tmp); 231 super.init(context); 232 } catch (Exception e) { 233 throw new InvalidGeneratorSetupException("Illegal regular expression: ", e); 234 } 235 } 236 237 /** 238 * Gets generator factory. 239 * 240 * @param context the context 241 * @return the generator factory 242 */ 243 protected GeneratorFactory getGeneratorFactory(GeneratorContext context) { 244 return (context != null ? context.getGeneratorFactory() : new StochasticGeneratorFactory()); 245 } 246 247 // java.lang.Object overrides -------------------------------------------------------------------------------------- 248 249 @Override 250 public String toString() { 251 return getClass().getSimpleName() + "[" + (unique ? "unique '" : "'") + pattern + "']"; 252 } 253 254 }