Skip to content

Commit

Permalink
Revert to single locale files. Release 1.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Jan 8, 2018
1 parent 1f4db90 commit b81cc9f
Show file tree
Hide file tree
Showing 101 changed files with 2,828 additions and 3,049 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The goal was to reuse their locale data files.
}

dependencies {
compile 'com.github.blocoio:faker:1.2.6'
compile 'com.github.blocoio:faker:1.2.7'
}

You can use ```testCompile``` or ```androidTestCompile```, if you only want to use Faker for testing.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'java'
apply plugin: 'maven'

version = '1.2.5'
version = '1.2.7'
group = 'com.github.blocoio'

sourceCompatibility = JavaVersion.VERSION_1_7
Expand Down
201 changes: 90 additions & 111 deletions src/main/java/io/bloco/faker/Faker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.bloco.faker;

import java.io.IOException;
import java.io.InputStream;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
Expand Down Expand Up @@ -37,118 +39,95 @@

public class Faker {

public static final String DEFAULT_LOCALE = "en";

public final Address address;
public final App app;
public final Avatar avatar;
public final Book book;
public final Bool bool;
public final Business business;
public final Color color;
public final Commerce commerce;
public final Company company;
public final Date date;
public final Food food;
public final Internet internet;
public final Lorem lorem;
public final Name name;
public final Number number;
public final Placeholdit placeholdit;
public final PhoneNumber phoneNumber;
public final SlackEmoji slackEmoji;
public final Team team;
public final Time time;
public final University university;

private final String locale;
private final FakerData data;

public Faker() {
this(DEFAULT_LOCALE);
public static final String DEFAULT_LOCALE = "en";

public final Address address;
public final App app;
public final Avatar avatar;
public final Book book;
public final Bool bool;
public final Business business;
public final Color color;
public final Commerce commerce;
public final Company company;
public final Date date;
public final Food food;
public final Internet internet;
public final Lorem lorem;
public final Name name;
public final Number number;
public final Placeholdit placeholdit;
public final PhoneNumber phoneNumber;
public final SlackEmoji slackEmoji;
public final Team team;
public final Time time;
public final University university;

private final String locale;
private final FakerData data;

public Faker() {
this(DEFAULT_LOCALE);
}

public Faker(String locale) {
this.locale = locale;

// Load data
Map<String, Object> data = loadData(DEFAULT_LOCALE); // Fallbacks first
if (!this.locale.equals(DEFAULT_LOCALE)) {
MapHelper.deepMerge(data, loadData(this.locale));
}

public Faker(String locale) {
this.locale = locale;

// Load data
Map<String, Object> data = loadData(DEFAULT_LOCALE); // Fallbacks first
if (!this.locale.equals(DEFAULT_LOCALE)) {
MapHelper.deepMerge(data, loadData(this.locale));
}
this.data = new FakerData(data);

// Load components
this.address = this.data.getComponent(Address.class);
this.app = this.data.getComponent(App.class);
this.avatar = this.data.getComponent(Avatar.class);
this.book = this.data.getComponent(Book.class);
this.bool = this.data.getComponent(Bool.class);
this.business = this.data.getComponent(Business.class);
this.color = this.data.getComponent(Color.class);
this.commerce = this.data.getComponent(Commerce.class);
this.company = this.data.getComponent(Company.class);
this.date = this.data.getComponent(Date.class);
this.food = this.data.getComponent(Food.class);
this.internet = this.data.getComponent(Internet.class);
this.lorem = this.data.getComponent(Lorem.class);
this.name = this.data.getComponent(Name.class);
this.number = this.data.getComponent(Number.class);
this.placeholdit = this.data.getComponent(Placeholdit.class);
this.phoneNumber = this.data.getComponent(PhoneNumber.class);
this.slackEmoji = this.data.getComponent(SlackEmoji.class);
this.team = this.data.getComponent(Team.class);
this.time = this.data.getComponent(Time.class);
this.university = this.data.getComponent(University.class);
}

public String getLocale() {
return locale;
}

private Map<String, Object> loadData(String locale) {
List<File> localeFiles = getLocaleFiles(locale);
Map<String, Object> fullData = new HashMap<>();
for (File localeFile : localeFiles) {
MapHelper.deepMerge(fullData, getDataFromLocaleFile(localeFile));
}
return fullData;
}

private List<File> getLocaleFiles(String locale) {
ClassLoader classLoader = getClass().getClassLoader();

URL baseResource = classLoader.getResource("locales/" + locale + ".yml");
if (baseResource == null) {
throw new IllegalArgumentException("Unavailable locale \'" + locale + "\'");
}

List<File> files = new ArrayList<>();
files.add(new File(baseResource.getPath()));

URL folderResource = classLoader.getResource("locales/" + locale);
if (folderResource != null) {
File[] folderFiles = new File(folderResource.getPath()).listFiles();
if (folderFiles != null) {
files.addAll(Arrays.asList(folderFiles));
}
}

return files;
}

private Map<String, Object> getDataFromLocaleFile(File file) {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage());
}

Yaml yaml = new Yaml();
Map<String, Object> root = (Map<String, Object>) yaml.load(fileInputStream);
Map<String, Object> fakerData = (Map<String, Object>) root.values().iterator().next();
return (Map<String, Object>) fakerData.get("faker");
this.data = new FakerData(data);

// Load components
this.address = this.data.getComponent(Address.class);
this.app = this.data.getComponent(App.class);
this.avatar = this.data.getComponent(Avatar.class);
this.book = this.data.getComponent(Book.class);
this.bool = this.data.getComponent(Bool.class);
this.business = this.data.getComponent(Business.class);
this.color = this.data.getComponent(Color.class);
this.commerce = this.data.getComponent(Commerce.class);
this.company = this.data.getComponent(Company.class);
this.date = this.data.getComponent(Date.class);
this.food = this.data.getComponent(Food.class);
this.internet = this.data.getComponent(Internet.class);
this.lorem = this.data.getComponent(Lorem.class);
this.name = this.data.getComponent(Name.class);
this.number = this.data.getComponent(Number.class);
this.placeholdit = this.data.getComponent(Placeholdit.class);
this.phoneNumber = this.data.getComponent(PhoneNumber.class);
this.slackEmoji = this.data.getComponent(SlackEmoji.class);
this.team = this.data.getComponent(Team.class);
this.time = this.data.getComponent(Time.class);
this.university = this.data.getComponent(University.class);
}

public String getLocale() {
return locale;
}

private Map<String, Object> loadData(String locale) {
Yaml yaml = new Yaml();
InputStream input = getDataInputStream(locale);

Map<String, Object> root = (Map<String, Object>) yaml.load(input);
Map<String, Object> fakerData = (Map<String, Object>) root.values().iterator().next();
return (Map<String, Object>) fakerData.get("faker");
}

private InputStream getDataInputStream(String locale) {
InputStream input = getClass().getClassLoader()
.getResourceAsStream("locales/" + locale + ".yml");

try {
if (input != null && input.available() != 0) {
return input;
}
} catch (IOException e) {
}

throw new IllegalArgumentException("Unavailable locale \'" + locale + "\'");
}
}
Loading

0 comments on commit b81cc9f

Please sign in to comment.