/
OPFItem.java
executable file
·477 lines (428 loc) · 12.3 KB
/
OPFItem.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
/*
* Copyright (c) 2007 Adobe Systems Incorporated
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.adobe.epubcheck.opf;
import java.util.Set;
import org.w3c.epubcheck.url.URLUtils;
import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.ocf.OCFContainer;
import com.adobe.epubcheck.vocab.EpubCheckVocab;
import com.adobe.epubcheck.vocab.PackageVocabs;
import com.adobe.epubcheck.vocab.Property;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import io.mola.galimatias.GalimatiasParseException;
import io.mola.galimatias.URL;
/**
* Immutable representation of an item in a Package Document (OPF). Can
* represent a <code>item</code> element or <code>link</code> elements pointing
* to a container resource.
*/
public class OPFItem
{
private final String id;
private final URL url;
private final EPUBLocation location;
private final String path;
private final String mimetype;
private final Optional<String> fallback;
private final Optional<String> fallbackStyle;
private final Set<Property> properties;
private final boolean ncx;
private final boolean inSpine;
private final int spinePosition;
private final boolean nav;
private final boolean scripted;
private final boolean linear;
private final boolean fixedLayout;
private final boolean remote;
private final String mediaOverlay;
private OPFItem(Builder builder)
{
Preconditions.checkState(builder.id != null, "item ID is null");
Preconditions.checkState(builder.url != null, "item path is null");
Preconditions.checkState(builder.location != null, "item location is null");
if (builder.spinePosition < 0 || !builder.linear)
{
builder.propertiesBuilder.add(EpubCheckVocab.VOCAB.get(EpubCheckVocab.PROPERTIES.NON_LINEAR));
}
if (builder.fxl)
{
builder.propertiesBuilder
.add(EpubCheckVocab.VOCAB.get(EpubCheckVocab.PROPERTIES.FIXED_LAYOUT));
}
this.id = builder.id.trim();
this.url = builder.url;
this.mimetype = Optional.fromNullable(builder.mimeType).or("undefined").trim();
this.location = builder.location;
this.fallback = Optional
.fromNullable(Strings.emptyToNull(Strings.nullToEmpty(builder.fallback).trim()));
this.fallbackStyle = Optional
.fromNullable(Strings.emptyToNull(Strings.nullToEmpty(builder.fallbackStyle).trim()));
this.properties = builder.propertiesBuilder.build();
this.ncx = builder.ncx;
this.inSpine = builder.spinePosition > -1;
this.spinePosition = builder.spinePosition;
this.nav = properties.contains(PackageVocabs.ITEM_VOCAB.get(PackageVocabs.ITEM_PROPERTIES.NAV));
this.scripted = properties
.contains(PackageVocabs.ITEM_VOCAB.get(PackageVocabs.ITEM_PROPERTIES.SCRIPTED));
this.linear = builder.linear;
this.fixedLayout = builder.fxl;
this.mediaOverlay = builder.mediaOverlay;
this.remote = builder.remote;
// Compute a relative item path
try
{
// If the item is a remote resource, return the
// full URL string (decoded)
if (remote)
{
this.path = url.toHumanString();
}
// If the item is defined with a data URL, return
// the URL string truncated arbitrarily to 30 chars
else if ("data".equals(url.scheme()))
{
String urlString = url.toString();
this.path = url.toString().substring(0, Math.min(urlString.length(), 30)) + "…";
}
// If a container is present (full-publication check)
// the item path is relative to the root of the container
else if (builder.container.isPresent())
{
this.path = URLUtils.decode(builder.container.get().relativize(url));
}
// If a container is not present (single-file check)
// we try to relativize the path from the package document path
else
{
this.path = URLUtils.decode(location.url.resolve(".").relativize(URLUtils.docURL(url)));
}
} catch (GalimatiasParseException impossible)
{
throw new AssertionError(impossible);
}
}
/**
* Returns the ID of this item.
*
* @return the ID of this item, guaranteed non-null.
*/
public String getId()
{
return id;
}
/**
* The URL of this item (cannot be <code>null</code>).
*
* @return the container URL of this item
*/
public URL getURL()
{
return url;
}
/**
* The path of this item (cannot be <code>null</code>).
*
* @return the path of this item
*/
public String getPath()
{
return path;
}
/**
* Returns the media type of this item.
*
* @return the media type of this item, guaranteed non-null.
*/
public String getMimeType()
{
return mimetype;
}
/**
* @return the location in the package document where this item is declared
*/
public EPUBLocation getLocation()
{
return location;
}
/**
* Returns an {@link Optional} containing the ID of the fallback item for this
* item, if it has one.
*
* @return An optional containing the ID of the fallback item for this item if
* it has one, or {@link Optional#absent()} otherwise.
*/
public Optional<String> getFallback()
{
return fallback;
}
/**
* Returns An {@link Optional} containing the ID of the fallback stylesheet
* for this item, if it has one.
*
* @return An optional containing the ID of the fallback stylesheet for this
* item if it has one, or {@link Optional#absent()} otherwise.
*/
public Optional<String> getFallbackStyle()
{
return fallbackStyle;
}
/**
* Returns the set of {@link Property} declared on this item or any
* <code>itemref</code> pointing to this item.
*
* @return the properties of this item, or an empty set if none is declared.
*/
public Set<Property> getProperties()
{
return properties;
}
/**
* Returns the zero-based position of this item in the spine, or {@code -1} if
* this item is not in the spine.
*
* @return the position of this item in the spine, or {@code -1} if this item
* is not in the spine.
*/
public int getSpinePosition()
{
return spinePosition;
}
/**
* Returns <code>true</code> iff this item is an NCX document.
*
* @return <code>true</code> iff this item is an NCX document.
*/
public boolean isNcx()
{
return ncx;
}
/**
* Returns <code>true</code> iff this item is a scripted document.
*
* @return <code>true</code> iff this item is a scripted document.
*/
public boolean isScripted()
{
return scripted;
}
/**
* Returns <code>true</code> iff this item is a Navigation Document.
*
* @return <code>true</code> iff this item is an Navigation Document.
*/
public boolean isNav()
{
return nav;
}
/**
* Returns <code>true</code> iff this item is in the spine.
*
* @return <code>true</code> iff this item is in the spine.
*/
public boolean isInSpine()
{
return inSpine;
}
/**
* Returns <code>true</code> iff this item is a spine item part of the linear
* reading order, as declared by the <code>itemref/@linear</code> attribute.
*
* @return <code>true</code> iff this item is in the spine and is linear.
* @throws IllegalStateException
* if this item is not in the spine.
*/
public boolean isLinear()
{
if (!inSpine)
{
throw new IllegalStateException("linear");
}
return linear;
}
/**
* Returns <code>true</code> iff this item is a Fixed-Layout Document.
*
* @return <code>true</code> iff this item is a Fixed-Layout Document.
*/
public boolean isFixedLayout()
{
return fixedLayout;
}
/**
* Returns <code>true</code> iff this item is a remote resource.
*
* @return <code>true</code> iff this item is a remote resource.
*/
public boolean hasDataURL()
{
return "data".equals(url.scheme());
}
/**
* Returns <code>true</code> iff this item is a remote resource.
*
* @return <code>true</code> iff this item is a remote resource.
*/
public boolean isRemote()
{
return remote;
}
public String getMediaOverlay()
{
return mediaOverlay;
}
@Override
public String toString()
{
return url + "[" + id + "]";
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
OPFItem other = (OPFItem) obj;
if (id == null)
{
if (other.id != null) return false;
}
else if (!id.equals(other.id)) return false;
if (url == null)
{
if (other.url != null) return false;
}
else if (!url.equals(other.url)) return false;
return true;
}
/**
* A builder for {@link OPFItem}
*/
public static final class Builder
{
private String id;
private URL url;
private EPUBLocation location;
private Optional<OCFContainer> container;
private boolean remote = false;
private String mimeType;
private String fallback = null;
private String fallbackStyle = null;
private boolean ncx = false;
private boolean linear = true;
private int spinePosition = -1;
private boolean fxl = false;
private String mediaOverlay;
private ImmutableSet.Builder<Property> propertiesBuilder = new ImmutableSet.Builder<Property>();
public Builder id(String id)
{
this.id = id;
return this;
}
public Builder url(URL url)
{
this.url = url;
return this;
}
public Builder location(EPUBLocation location)
{
this.location = location;
return this;
}
public Builder container(Optional<OCFContainer> container)
{
this.container = container;
return this;
}
public Builder remote(boolean remote)
{
this.remote = remote;
return this;
}
public Builder mimetype(String mimetype)
{
this.mimeType = mimetype;
return this;
}
public Builder fallback(String fallback)
{
this.fallback = fallback;
return this;
}
public Builder fallbackStyle(String fallbackStyle)
{
this.fallbackStyle = fallbackStyle;
return this;
}
public Builder fixedLayout()
{
this.fxl = true;
return this;
}
public Builder mediaOverlay(String path)
{
this.mediaOverlay = path;
return this;
}
public Builder ncx()
{
this.ncx = true;
return this;
}
public Builder nonlinear()
{
this.linear = false;
return this;
}
public Builder inSpine(int position)
{
this.spinePosition = Preconditions.checkNotNull(position);
return this;
}
public Builder properties(Set<Property> properties)
{
if (properties != null)
{
this.propertiesBuilder.addAll(properties);
}
return this;
}
/**
* Builds a new immutable {@link OPFItem} from this builder.
*/
public OPFItem build()
{
return new OPFItem(this);
}
}
}