-
Notifications
You must be signed in to change notification settings - Fork 395
/
ValidationContext.java
485 lines (443 loc) · 13.6 KB
/
ValidationContext.java
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package com.adobe.epubcheck.opf;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import org.w3c.epubcheck.core.Checker;
import org.w3c.epubcheck.core.references.ReferenceRegistry;
import org.w3c.epubcheck.core.references.ResourceRegistry;
import org.w3c.epubcheck.util.url.URLUtils;
import com.adobe.epubcheck.api.EPUBProfile;
import com.adobe.epubcheck.api.FeatureReport;
import com.adobe.epubcheck.api.LocalizableReport;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.ocf.OCFContainer;
import com.adobe.epubcheck.overlay.OverlayTextChecker;
import com.adobe.epubcheck.util.EPUBVersion;
import com.adobe.epubcheck.util.GenericResourceProvider;
import com.adobe.epubcheck.util.URLResourceProvider;
import com.adobe.epubcheck.vocab.Property;
import com.google.common.base.MoreObjects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import io.mola.galimatias.GalimatiasParseException;
import io.mola.galimatias.URL;
/**
* Holds various contextual objects used during validation. This validation
* context is notalby used by {@link Checker} instances.
*
*/
public final class ValidationContext
{
/**
* The URL of the validated resource. Guaranteed non-null.
*/
public final URL url;
/**
* The path to the validated resource. Guaranteed non-null.
*/
public final String path;
/**
* The media type of the validated resource. Guaranteed non-null.
*/
public final String mimeType;
/**
* The EPUB version being used for validation. Guaranteed non-null.
*/
public final EPUBVersion version;
/**
* The validation profile in use (e.g. DEFAULT, EDUPUB, etc.). Guaranteed
* non-null.
*/
public final EPUBProfile profile;
/**
* The report object used to log validation messages. Guaranteed non-null.
*/
public final Report report;
/**
* The locale used to log validation messages. Guaranteed non-null.
*/
public final Locale locale;
/**
* Used to report some features of the validated resource, without logging.
* Guaranteed non-null.
*/
public final FeatureReport featureReport;
/**
* Used to open resource streams. Guaranteed non-null.
*/
public final GenericResourceProvider resourceProvider;
/**
* The Package Document item for the validated resource. Is absent if there is
* no item representing this resource in the Package Document.
*/
public final Optional<OPFItem> opfItem;
/**
* The OCF Package the resource being validated belongs to. Is absent for
* single-file validations.
*/
public final Optional<OCFContainer> container;
/**
* The publication resource registry, absent for single-file validation.
*/
public final Optional<ResourceRegistry> resourceRegistry;
/**
* The references registry, absent for single-file validation
*/
public final Optional<ReferenceRegistry> referenceRegistry;
/**
* The src checker for media overlay text elements, absent for single-file
* validation
*/
public final Optional<OverlayTextChecker> overlayTextChecker;
/**
* The set of 'dc:type' values declared at the OPF level. Guaranteed non-null,
* can be empty.
*/
public final Set<PublicationType> pubTypes;
/**
* The set of properties associated to the resource being validated.
*/
public final Set<Property> properties;
private ValidationContext(ValidationContextBuilder builder)
{
// FIXME 2022 add a default report if not provided
// FIXME 2022 add a better default resource provider
Preconditions.checkState(builder.url != null, "URL must be set");
Preconditions.checkState(builder.report != null, "report must be set");
this.url = builder.url;
this.container = Optional.fromNullable(builder.container);
this.resourceRegistry = Optional.fromNullable(builder.resourceRegistry);
this.referenceRegistry = Optional.fromNullable(builder.referenceRegistry);
this.mimeType = Strings.nullToEmpty(builder.mimeType);
this.version = Optional.fromNullable(builder.version).or(EPUBVersion.Unknown);
this.profile = Optional.fromNullable(builder.profile).or(EPUBProfile.DEFAULT);
this.report = builder.report;
this.locale = MoreObjects.firstNonNull(
(report instanceof LocalizableReport) ? ((LocalizableReport) report).getLocale() : null,
Locale.getDefault());
this.featureReport = Optional.fromNullable(builder.featureReport).or(new FeatureReport());
this.resourceProvider = Iterables.find(
Arrays.asList(builder.container, builder.resourceProvider, new URLResourceProvider()),
Predicates.notNull());
this.opfItem = Optional.fromNullable((builder.resourceRegistry != null)
? builder.resourceRegistry.getOPFItem(builder.url).orElse(null)
: null);
this.overlayTextChecker = Optional.fromNullable(builder.overlayTextChecker);
this.pubTypes = (builder.pubTypes != null) ? Sets.immutableEnumSet(builder.pubTypes)
: EnumSet.noneOf(PublicationType.class);
this.properties = builder.properties.build();
this.path = computePath();
}
// FIXME 2022 document and test
private String computePath()
{
if (container.isPresent() && !container.get().isRemote(url))
{
if (url.path() != null && !url.path().isEmpty())
{
return url.path().substring(1);
}
else
{
return "";
}
}
else if ("file".equals(url.scheme()))
{
try
{
return new File(url.toJavaURI()).getAbsolutePath();
} catch (URISyntaxException e)
{
return url.toHumanString();
}
}
else
{
return url.toHumanString();
}
}
public ValidationContextBuilder copy()
{
return new ValidationContextBuilder().copy(this);
}
// FIXME 2022 document
public boolean isRemote(URL url)
{
Preconditions.checkArgument(url != null, "URL is null");
if (container.isPresent())
{
return container.get().isRemote(url);
}
else
{
return URLUtils.isRemote(url, this.url);
}
}
public String relativize(URL url)
{
Preconditions.checkArgument(url != null, "URL is null");
if (container.isPresent())
{
return container.get().relativize(url);
}
else
{
return this.url.relativize(url);
}
}
public static final ValidationContextBuilder of(URL url)
{
return new ValidationContextBuilder().url(url);
}
public static final ValidationContextBuilder test()
{
try
{
return new ValidationContextBuilder().url(URL.parse("https://test.example.org"));
} catch (GalimatiasParseException e)
{
throw new AssertionError();
}
}
/**
* Builds a new {@link ValidationContext} from various member objects or by
* copying a parent context.
*
*/
public static final class ValidationContextBuilder
{
private URL url = null;
private String mimeType = null;
private EPUBVersion version = null;
private EPUBProfile profile = null;
private Report report = null;
private FeatureReport featureReport = null;
private GenericResourceProvider resourceProvider = null;
private OCFContainer container = null;
private ResourceRegistry resourceRegistry = null;
private ReferenceRegistry referenceRegistry = null;
private OverlayTextChecker overlayTextChecker = null;
private Set<PublicationType> pubTypes = null;
private ImmutableSet.Builder<Property> properties = ImmutableSet.<Property> builder();
public ValidationContextBuilder()
{
}
public ValidationContextBuilder(ValidationContext context)
{
copy(context);
}
public ValidationContextBuilder copy(ValidationContext context)
{
url = context.url;
mimeType = context.mimeType;
version = context.version;
profile = context.profile;
report = context.report;
featureReport = context.featureReport;
resourceProvider = context.resourceProvider;
container = context.container.orNull();
resourceRegistry = context.resourceRegistry.orNull();
referenceRegistry = context.referenceRegistry.orNull();
overlayTextChecker = context.overlayTextChecker.orNull();
pubTypes = context.pubTypes;
properties = ImmutableSet.<Property> builder().addAll(context.properties);
return this;
}
public ValidationContextBuilder url(URL url)
{
this.url = url;
return this;
}
public ValidationContextBuilder mimetype(String mimetype)
{
this.mimeType = mimetype;
return this;
}
public ValidationContextBuilder version(EPUBVersion version)
{
this.version = version;
return this;
}
public ValidationContextBuilder profile(EPUBProfile profile)
{
this.profile = profile;
return this;
}
public ValidationContextBuilder report(Report report)
{
this.report = report;
return this;
}
public ValidationContextBuilder featureReport(FeatureReport featureReport)
{
this.featureReport = featureReport;
return this;
}
public ValidationContextBuilder resourceProvider(GenericResourceProvider resourceProvider)
{
this.resourceProvider = resourceProvider;
return this;
}
public ValidationContextBuilder container(OCFContainer container)
{
this.container = container;
if (container != null)
{
this.resourceRegistry = new ResourceRegistry();
this.referenceRegistry = new ReferenceRegistry(container, resourceRegistry);
this.overlayTextChecker = new OverlayTextChecker();
}
return this;
}
public ValidationContextBuilder pubTypes(Set<PublicationType> pubTypes)
{
this.pubTypes = pubTypes;
return this;
}
public ValidationContextBuilder properties(Set<Property> properties)
{
this.properties = ImmutableSet.builder();
if (properties != null)
{
this.properties.addAll(properties);
}
return this;
}
public ValidationContextBuilder addProperty(Property property)
{
properties.add(Preconditions.checkNotNull(property));
return this;
}
public ValidationContext build()
{
return new ValidationContext(this);
}
}
/**
* Utility to create {@link Predicate}s applying to {@link ValidationContext}
* instances.
*
*/
public static final class ValidationContextPredicates
{
/**
* Returns a predicate that evaluates to <code>true</code> if the given
* property is declared in the context being tested.
*/
public static Predicate<ValidationContext> hasProp(final Property property)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.properties.contains(property);
}
};
}
/**
* Returns a predicate that evaluates to <code>true</code> if the given
* publication <code>dc:type</code> is declared in the context being tested.
*/
public static Predicate<ValidationContext> hasPubType(final PublicationType type)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.pubTypes.contains(type);
}
};
}
/**
* Returns a predicate that evaluates to <code>true</code> if the context
* being tested has the given media type.
*/
public static Predicate<ValidationContext> mimetype(final String mimetype)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.mimeType.equals(mimetype);
}
};
}
/**
* Returns a predicate that evaluates to <code>true</code> if the context
* being tested has the given path.
*/
public static Predicate<ValidationContext> path(final String path)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.path.equals(path);
}
};
}
/**
* Returns a predicate that evaluates to <code>true</code> if the context
* being tested declares the given validation profile.
*/
public static Predicate<ValidationContext> profile(final EPUBProfile profile)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.profile.equals(profile);
}
};
}
/**
* Returns a predicate that evaluates to <code>true</code> if the context
* being tested declares the given EPUB version.
*/
public static Predicate<ValidationContext> version(final EPUBVersion version)
{
return new Predicate<ValidationContext>()
{
@Override
public boolean apply(ValidationContext input)
{
return input.version.equals(version);
}
};
}
private ValidationContextPredicates()
{
}
}
public String getMimeType(URL url)
{
if (url == null) return null;
if ("data".equals(url.scheme()))
{
return URLUtils.getDataURLType(url);
}
else if (resourceRegistry.isPresent())
{
return resourceRegistry.get().getMimeType(url);
}
else
{
return null;
}
}
}