forked from 4akloon/epub_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_data_generator.dart
258 lines (223 loc) · 8.05 KB
/
random_data_generator.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import 'dart:math' show Random;
import 'package:epub_plus/epub_plus.dart';
import 'package:epub_plus/src/schema/navigation/epub_navigation_target.dart';
import 'package:epub_plus/src/schema/opf/epub_metadata_contributor.dart';
import 'package:epub_plus/src/schema/opf/epub_metadata_date.dart';
import 'package:epub_plus/src/schema/opf/epub_metadata_identifier.dart';
import 'package:epub_plus/src/schema/opf/epub_metadata_meta.dart';
class RandomString {
final Random rng;
RandomString(this.rng);
static const asciiStart = 33;
static const asciiEnd = 126;
static const numericStart = 48;
static const numericEnd = 57;
static const lowerAlphaStart = 97;
static const lowerAlphaEnd = 122;
static const upperAlphaStart = 65;
static const upperAlphaEnd = 90;
/// Generates a random integer where [from] <= [to].
int randomBetween(int from, int to) {
if (from > to) throw Exception('$from is not > $to');
return ((to - from) * rng.nextDouble()).toInt() + from;
}
/// Generates a random string of [length] with characters
/// between ascii [from] to [to].
/// Defaults to characters of ascii '!' to '~'.
String randomString(
int length, {
int from = asciiStart,
int to = asciiEnd,
}) {
return String.fromCharCodes(
List.generate(length, (index) => randomBetween(from, to)));
}
/// Generates a random string of [length] with only numeric characters.
String randomNumeric(int length) =>
randomString(length, from: numericStart, to: numericEnd);
/// Generates a random string of [length] with only alpha characters.
String randomAlpha(int length) {
var lowerAlphaLength = randomBetween(0, length);
var upperAlphaLength = length - lowerAlphaLength;
var lowerAlpha = randomString(lowerAlphaLength,
from: lowerAlphaStart, to: lowerAlphaEnd);
var upperAlpha = randomString(upperAlphaLength,
from: upperAlphaStart, to: upperAlphaEnd);
return randomMerge(lowerAlpha, upperAlpha);
}
/// Generates a random string of [length] with alpha-numeric characters.
String randomAlphaNumeric(int length) {
var alphaLength = randomBetween(0, length);
var numericLength = length - alphaLength;
var alpha = randomAlpha(alphaLength);
var numeric = randomNumeric(numericLength);
return randomMerge(alpha, numeric);
}
/// Merge [a] with [b] and scramble characters.
String randomMerge(String a, String b) {
List<int> mergedCodeUnits = List.from("$a$b".codeUnits);
mergedCodeUnits.shuffle(rng);
return String.fromCharCodes(mergedCodeUnits);
}
}
class RandomDataGenerator {
final Random rng;
final RandomString _randomString;
final int _length;
RandomDataGenerator(this.rng, this._length)
: _randomString = RandomString(rng);
String randomString() {
return _randomString.randomAlphaNumeric(_length);
}
EpubNavigationPoint randomEpubNavigationPoint([int depth = 0]) {
return EpubNavigationPoint(
id: randomString(),
classs: randomString(),
playOrder: randomString(),
navigationLabels: [randomEpubNavigationLabel()],
content: randomEpubNavigationContent(),
childNavigationPoints:
depth > 0 ? [randomEpubNavigationPoint(depth - 1)] : [],
);
}
EpubNavigationContent randomEpubNavigationContent() {
return EpubNavigationContent(
id: randomString(),
source: randomString(),
);
}
EpubNavigationTarget randomEpubNavigationTarget() {
return EpubNavigationTarget(
id: randomString(),
classs: randomString(),
value: randomString(),
playOrder: randomString(),
navigationLabels: [randomEpubNavigationLabel()],
content: randomEpubNavigationContent(),
);
}
EpubNavigationLabel randomEpubNavigationLabel() {
return EpubNavigationLabel(text: randomString());
}
EpubNavigationHead randomEpubNavigationHead() {
return EpubNavigationHead(metadata: [randomNavigationHeadMeta()]);
}
EpubNavigationHeadMeta randomNavigationHeadMeta() {
return EpubNavigationHeadMeta(
content: randomString(),
name: randomString(),
scheme: randomString(),
);
}
EpubNavigationDocTitle randomNavigationDocTitle() {
return EpubNavigationDocTitle(titles: [randomString()]);
}
EpubNavigationDocAuthor randomNavigationDocAuthor() {
return EpubNavigationDocAuthor(authors: [randomString()]);
}
EpubPackage randomEpubPackage() {
return EpubPackage(
guide: randomEpubGuide(),
manifest: randomEpubManifest(),
metadata: randomEpubMetadata(),
spine: randomEpubSpine(),
version: rng.nextBool() ? EpubVersion.epub2 : EpubVersion.epub3,
);
}
EpubSpine randomEpubSpine() {
final reference = EpubSpine(
tableOfContents: _randomString.randomAlpha(_length),
items: [randomEpubSpineItemRef()],
ltr: true,
);
return reference;
}
EpubSpineItemRef randomEpubSpineItemRef() {
return EpubSpineItemRef(
idRef: _randomString.randomAlpha(_length),
isLinear: true,
);
}
EpubManifest randomEpubManifest() {
return EpubManifest(items: [randomEpubManifestItem()]);
}
EpubManifestItem randomEpubManifestItem() {
return EpubManifestItem(
fallback: _randomString.randomAlpha(_length),
fallbackStyle: _randomString.randomAlpha(_length),
href: _randomString.randomAlpha(_length),
id: _randomString.randomAlpha(_length),
mediaType: _randomString.randomAlpha(_length),
requiredModules: _randomString.randomAlpha(_length),
requiredNamespace: _randomString.randomAlpha(_length),
);
}
EpubGuide randomEpubGuide() {
return EpubGuide(items: [randomEpubGuideReference()]);
}
EpubGuideReference randomEpubGuideReference() {
return EpubGuideReference(
href: _randomString.randomAlpha(_length),
title: _randomString.randomAlpha(_length),
type: _randomString.randomAlpha(_length),
);
}
EpubMetadata randomEpubMetadata() {
var reference = EpubMetadata(
contributors: [randomEpubMetadataContributor()],
coverages: [_randomString.randomAlpha(_length)],
creators: [randomEpubMetadataCreator()],
dates: [randomEpubMetadataDate()],
description: _randomString.randomAlpha(_length),
formats: [_randomString.randomAlpha(_length)],
identifiers: [randomEpubMetadataIdentifier()],
languages: [_randomString.randomAlpha(_length)],
metaItems: [randomEpubMetadataMeta()],
publishers: [_randomString.randomAlpha(_length)],
relations: [_randomString.randomAlpha(_length)],
rights: [_randomString.randomAlpha(_length)],
sources: [_randomString.randomAlpha(_length)],
subjects: [_randomString.randomAlpha(_length)],
titles: [_randomString.randomAlpha(_length)],
types: [_randomString.randomAlpha(_length)],
);
return reference;
}
EpubMetadataMeta randomEpubMetadataMeta() {
return EpubMetadataMeta(
content: _randomString.randomAlpha(_length),
id: _randomString.randomAlpha(_length),
name: _randomString.randomAlpha(_length),
property: _randomString.randomAlpha(_length),
refines: _randomString.randomAlpha(_length),
scheme: _randomString.randomAlpha(_length),
);
}
EpubMetadataIdentifier randomEpubMetadataIdentifier() {
return EpubMetadataIdentifier(
id: _randomString.randomAlpha(_length),
scheme: _randomString.randomAlpha(_length),
identifier: _randomString.randomAlpha(_length),
);
}
EpubMetadataDate randomEpubMetadataDate() {
return EpubMetadataDate(
event: _randomString.randomAlpha(_length),
date: _randomString.randomAlpha(_length),
);
}
EpubMetadataContributor randomEpubMetadataContributor() {
return EpubMetadataContributor(
fileAs: _randomString.randomAlpha(_length),
role: _randomString.randomAlpha(_length),
contributor: _randomString.randomAlpha(_length),
);
}
EpubMetadataCreator randomEpubMetadataCreator() {
return EpubMetadataCreator(
fileAs: _randomString.randomAlpha(_length),
role: _randomString.randomAlpha(_length),
creator: _randomString.randomAlpha(_length),
);
}
}