uk.dioxic.faker.Faker Maven / Gradle / Ivy
package uk.dioxic.faker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import uk.dioxic.faker.exception.LocaleDoesNotExistException;
import uk.dioxic.faker.resolvable.Resolvable;
import uk.dioxic.faker.resolvable.ResolvableFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public class Faker {
private final static Logger logger = LoggerFactory.getLogger(Faker.class);
private final Map> globalMap = new HashMap<>();
private final ResolvableFactory factory = new ResolvableFactory();
protected Faker() {
put("IDNumber.ssn_valid", "###-##-####");
put("IDNumber.valid_sv_se_ssn", "###-##-####");
put("IDNumber.invalid_sv_se_ssn", "###-##-####");
}
public static Faker instance(Locale locale) {
InputStream langStream = getDefinitionStream(locale.getLanguage());
InputStream countryStream = getDefinitionStream(locale.getLanguage() + "-" + locale.getCountry().toUpperCase());
if (countryStream == null && langStream == null) {
throw new LocaleDoesNotExistException("locale [" + locale.toString() + "] not supported");
}
Yaml yaml = new Yaml();
Faker faker = new Faker();
logger.info("loading faker definitions for {}", locale);
if (langStream != null) {
faker.load(yaml.loadAll(langStream));
}
if (countryStream != null) {
faker.load(yaml.loadAll(countryStream));
}
else if (locale.getCountry() != null && !locale.getCountry().isEmpty()) {
logger.warn("Locale country [{}] not supported, using base language [{}]", locale.getCountry(), locale.getLanguage());
}
return faker;
}
public static Faker instance(InputStream... streams) {
Yaml yaml = new Yaml();
Faker faker = new Faker();
for (InputStream stream : streams) {
if (stream == null) {
throw new IllegalStateException("stream cannot be null!");
}
logger.info("loading faker definitions from stream");
faker.load(yaml.loadAll(stream));
}
return faker;
}
public static Faker instance(String... fakerFiles) throws IOException {
Yaml yaml = new Yaml();
Faker faker = new Faker();
for (String fakerFile : fakerFiles) {
logger.info("loading faker definitions from {}", fakerFile);
Files.newBufferedReader(Paths.get(fakerFile));
Reader reader = Files.newBufferedReader(Paths.get(fakerFile));
faker.load(yaml.loadAll(reader));
}
return faker;
}
private static InputStream getDefinitionStream(String filename) {
String filenameWithExtension = "locale/" + filename + ".yml";
InputStream streamOnClass = Faker.class.getResourceAsStream(filenameWithExtension);
if (streamOnClass == null) {
streamOnClass = Faker.class.getClassLoader().getResourceAsStream(filenameWithExtension);
}
return streamOnClass;
}
protected void load(Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy