|
1 | 1 | package io.bloco.faker;
|
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
3 | 5 | import org.yaml.snakeyaml.Yaml;
|
4 | 6 |
|
5 | 7 | import java.io.File;
|
|
37 | 39 |
|
38 | 40 | public class Faker {
|
39 | 41 |
|
40 |
| - public static final String DEFAULT_LOCALE = "en"; |
41 |
| - |
42 |
| - public final Address address; |
43 |
| - public final App app; |
44 |
| - public final Avatar avatar; |
45 |
| - public final Book book; |
46 |
| - public final Bool bool; |
47 |
| - public final Business business; |
48 |
| - public final Color color; |
49 |
| - public final Commerce commerce; |
50 |
| - public final Company company; |
51 |
| - public final Date date; |
52 |
| - public final Food food; |
53 |
| - public final Internet internet; |
54 |
| - public final Lorem lorem; |
55 |
| - public final Name name; |
56 |
| - public final Number number; |
57 |
| - public final Placeholdit placeholdit; |
58 |
| - public final PhoneNumber phoneNumber; |
59 |
| - public final SlackEmoji slackEmoji; |
60 |
| - public final Team team; |
61 |
| - public final Time time; |
62 |
| - public final University university; |
63 |
| - |
64 |
| - private final String locale; |
65 |
| - private final FakerData data; |
66 |
| - |
67 |
| - public Faker() { |
68 |
| - this(DEFAULT_LOCALE); |
| 42 | + public static final String DEFAULT_LOCALE = "en"; |
| 43 | + |
| 44 | + public final Address address; |
| 45 | + public final App app; |
| 46 | + public final Avatar avatar; |
| 47 | + public final Book book; |
| 48 | + public final Bool bool; |
| 49 | + public final Business business; |
| 50 | + public final Color color; |
| 51 | + public final Commerce commerce; |
| 52 | + public final Company company; |
| 53 | + public final Date date; |
| 54 | + public final Food food; |
| 55 | + public final Internet internet; |
| 56 | + public final Lorem lorem; |
| 57 | + public final Name name; |
| 58 | + public final Number number; |
| 59 | + public final Placeholdit placeholdit; |
| 60 | + public final PhoneNumber phoneNumber; |
| 61 | + public final SlackEmoji slackEmoji; |
| 62 | + public final Team team; |
| 63 | + public final Time time; |
| 64 | + public final University university; |
| 65 | + |
| 66 | + private final String locale; |
| 67 | + private final FakerData data; |
| 68 | + |
| 69 | + public Faker() { |
| 70 | + this(DEFAULT_LOCALE); |
| 71 | + } |
| 72 | + |
| 73 | + public Faker(String locale) { |
| 74 | + this.locale = locale; |
| 75 | + |
| 76 | + // Load data |
| 77 | + Map<String, Object> data = loadData(DEFAULT_LOCALE); // Fallbacks first |
| 78 | + if (!this.locale.equals(DEFAULT_LOCALE)) { |
| 79 | + MapHelper.deepMerge(data, loadData(this.locale)); |
69 | 80 | }
|
70 |
| - |
71 |
| - public Faker(String locale) { |
72 |
| - this.locale = locale; |
73 |
| - |
74 |
| - // Load data |
75 |
| - Map<String, Object> data = loadData(DEFAULT_LOCALE); // Fallbacks first |
76 |
| - if (!this.locale.equals(DEFAULT_LOCALE)) { |
77 |
| - MapHelper.deepMerge(data, loadData(this.locale)); |
78 |
| - } |
79 |
| - this.data = new FakerData(data); |
80 |
| - |
81 |
| - // Load components |
82 |
| - this.address = this.data.getComponent(Address.class); |
83 |
| - this.app = this.data.getComponent(App.class); |
84 |
| - this.avatar = this.data.getComponent(Avatar.class); |
85 |
| - this.book = this.data.getComponent(Book.class); |
86 |
| - this.bool = this.data.getComponent(Bool.class); |
87 |
| - this.business = this.data.getComponent(Business.class); |
88 |
| - this.color = this.data.getComponent(Color.class); |
89 |
| - this.commerce = this.data.getComponent(Commerce.class); |
90 |
| - this.company = this.data.getComponent(Company.class); |
91 |
| - this.date = this.data.getComponent(Date.class); |
92 |
| - this.food = this.data.getComponent(Food.class); |
93 |
| - this.internet = this.data.getComponent(Internet.class); |
94 |
| - this.lorem = this.data.getComponent(Lorem.class); |
95 |
| - this.name = this.data.getComponent(Name.class); |
96 |
| - this.number = this.data.getComponent(Number.class); |
97 |
| - this.placeholdit = this.data.getComponent(Placeholdit.class); |
98 |
| - this.phoneNumber = this.data.getComponent(PhoneNumber.class); |
99 |
| - this.slackEmoji = this.data.getComponent(SlackEmoji.class); |
100 |
| - this.team = this.data.getComponent(Team.class); |
101 |
| - this.time = this.data.getComponent(Time.class); |
102 |
| - this.university = this.data.getComponent(University.class); |
103 |
| - } |
104 |
| - |
105 |
| - public String getLocale() { |
106 |
| - return locale; |
107 |
| - } |
108 |
| - |
109 |
| - private Map<String, Object> loadData(String locale) { |
110 |
| - List<File> localeFiles = getLocaleFiles(locale); |
111 |
| - Map<String, Object> fullData = new HashMap<>(); |
112 |
| - for (File localeFile : localeFiles) { |
113 |
| - MapHelper.deepMerge(fullData, getDataFromLocaleFile(localeFile)); |
114 |
| - } |
115 |
| - return fullData; |
116 |
| - } |
117 |
| - |
118 |
| - private List<File> getLocaleFiles(String locale) { |
119 |
| - ClassLoader classLoader = getClass().getClassLoader(); |
120 |
| - |
121 |
| - URL baseResource = classLoader.getResource("locales/" + locale + ".yml"); |
122 |
| - if (baseResource == null) { |
123 |
| - throw new IllegalArgumentException("Unavailable locale \'" + locale + "\'"); |
124 |
| - } |
125 |
| - |
126 |
| - List<File> files = new ArrayList<>(); |
127 |
| - files.add(new File(baseResource.getPath())); |
128 |
| - |
129 |
| - URL folderResource = classLoader.getResource("locales/" + locale); |
130 |
| - if (folderResource != null) { |
131 |
| - File[] folderFiles = new File(folderResource.getPath()).listFiles(); |
132 |
| - if (folderFiles != null) { |
133 |
| - files.addAll(Arrays.asList(folderFiles)); |
134 |
| - } |
135 |
| - } |
136 |
| - |
137 |
| - return files; |
138 |
| - } |
139 |
| - |
140 |
| - private Map<String, Object> getDataFromLocaleFile(File file) { |
141 |
| - FileInputStream fileInputStream; |
142 |
| - try { |
143 |
| - fileInputStream = new FileInputStream(file); |
144 |
| - } catch (FileNotFoundException e) { |
145 |
| - throw new RuntimeException(e.getMessage()); |
146 |
| - } |
147 |
| - |
148 |
| - Yaml yaml = new Yaml(); |
149 |
| - Map<String, Object> root = (Map<String, Object>) yaml.load(fileInputStream); |
150 |
| - Map<String, Object> fakerData = (Map<String, Object>) root.values().iterator().next(); |
151 |
| - return (Map<String, Object>) fakerData.get("faker"); |
| 81 | + this.data = new FakerData(data); |
| 82 | + |
| 83 | + // Load components |
| 84 | + this.address = this.data.getComponent(Address.class); |
| 85 | + this.app = this.data.getComponent(App.class); |
| 86 | + this.avatar = this.data.getComponent(Avatar.class); |
| 87 | + this.book = this.data.getComponent(Book.class); |
| 88 | + this.bool = this.data.getComponent(Bool.class); |
| 89 | + this.business = this.data.getComponent(Business.class); |
| 90 | + this.color = this.data.getComponent(Color.class); |
| 91 | + this.commerce = this.data.getComponent(Commerce.class); |
| 92 | + this.company = this.data.getComponent(Company.class); |
| 93 | + this.date = this.data.getComponent(Date.class); |
| 94 | + this.food = this.data.getComponent(Food.class); |
| 95 | + this.internet = this.data.getComponent(Internet.class); |
| 96 | + this.lorem = this.data.getComponent(Lorem.class); |
| 97 | + this.name = this.data.getComponent(Name.class); |
| 98 | + this.number = this.data.getComponent(Number.class); |
| 99 | + this.placeholdit = this.data.getComponent(Placeholdit.class); |
| 100 | + this.phoneNumber = this.data.getComponent(PhoneNumber.class); |
| 101 | + this.slackEmoji = this.data.getComponent(SlackEmoji.class); |
| 102 | + this.team = this.data.getComponent(Team.class); |
| 103 | + this.time = this.data.getComponent(Time.class); |
| 104 | + this.university = this.data.getComponent(University.class); |
| 105 | + } |
| 106 | + |
| 107 | + public String getLocale() { |
| 108 | + return locale; |
| 109 | + } |
| 110 | + |
| 111 | + private Map<String, Object> loadData(String locale) { |
| 112 | + Yaml yaml = new Yaml(); |
| 113 | + InputStream input = getDataInputStream(locale); |
| 114 | + |
| 115 | + Map<String, Object> root = (Map<String, Object>) yaml.load(input); |
| 116 | + Map<String, Object> fakerData = (Map<String, Object>) root.values().iterator().next(); |
| 117 | + return (Map<String, Object>) fakerData.get("faker"); |
| 118 | + } |
| 119 | + |
| 120 | + private InputStream getDataInputStream(String locale) { |
| 121 | + InputStream input = getClass().getClassLoader() |
| 122 | + .getResourceAsStream("locales/" + locale + ".yml"); |
| 123 | + |
| 124 | + try { |
| 125 | + if (input != null && input.available() != 0) { |
| 126 | + return input; |
| 127 | + } |
| 128 | + } catch (IOException e) { |
152 | 129 | }
|
153 | 130 |
|
| 131 | + throw new IllegalArgumentException("Unavailable locale \'" + locale + "\'"); |
| 132 | + } |
154 | 133 | }
|
0 commit comments