Skip to content

Commit 40ce1f7

Browse files
authored
Unify WebAnnotations (#272)
* Unify WebAnnotations * Fix native/default profile PMD misconfig
1 parent 04f04a8 commit 40ce1f7

21 files changed

+558
-1117
lines changed

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@
226226

227227
<plugin>
228228
<artifactId>maven-pmd-plugin</artifactId>
229+
<configuration>
230+
<excludes>
231+
<exclude>**/info/freelibrary/iiif/presentation/v3/utils/csv/*.java</exclude>
232+
<exclude>**/info/freelibrary/iiif/presentation/v3/utils/cmdline/*.java</exclude>
233+
</excludes>
234+
</configuration>
229235
</plugin>
230236

231237
<plugin>
@@ -401,13 +407,14 @@
401407
<configuration>
402408
<rules combine.self="override">
403409
<requireJavaVersion>
404-
<version>[24,)</version>
410+
<version>[25,)</version>
405411
</requireJavaVersion>
406412
</rules>
407413
</configuration>
408414
</execution>
409415
</executions>
410416
</plugin>
417+
411418
<plugin>
412419
<groupId>org.apache.maven.plugins</groupId>
413420
<artifactId>maven-compiler-plugin</artifactId>
@@ -489,6 +496,13 @@
489496
]]></argLine>
490497
</configuration>
491498
</plugin>
499+
500+
<plugin>
501+
<artifactId>maven-pmd-plugin</artifactId>
502+
<configuration>
503+
<excludes combine.self="override"/>
504+
</configuration>
505+
</plugin>
492506
</plugins>
493507
</build>
494508
</profile>
Lines changed: 23 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,33 @@
11

22
package info.freelibrary.iiif.presentation.v3.annotation;
33

4-
import java.net.URI;
5-
import java.util.ArrayList;
6-
import java.util.Arrays;
7-
import java.util.List;
8-
import java.util.Optional;
9-
10-
import com.fasterxml.jackson.core.JsonProcessingException;
114
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
125
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
13-
14-
import info.freelibrary.util.I18nRuntimeException;
15-
import info.freelibrary.util.Logger;
16-
import info.freelibrary.util.LoggerFactory;
17-
186
import info.freelibrary.iiif.presentation.v3.Annotation;
197
import info.freelibrary.iiif.presentation.v3.CanvasResource;
20-
import info.freelibrary.iiif.presentation.v3.ContextList;
218
import info.freelibrary.iiif.presentation.v3.Manifest;
22-
import info.freelibrary.iiif.presentation.v3.content.ContentResource;
23-
import info.freelibrary.iiif.presentation.v3.id.UriUtils;
24-
import info.freelibrary.iiif.presentation.v3.properties.Label;
25-
import info.freelibrary.iiif.presentation.v3.properties.TimeMode;
269
import info.freelibrary.iiif.presentation.v3.properties.selectors.MediaFragmentSelector;
27-
import info.freelibrary.iiif.presentation.v3.utils.JSON;
2810
import info.freelibrary.iiif.presentation.v3.utils.MessageCodes;
29-
import info.freelibrary.iiif.presentation.v3.utils.json.ContentStateDeserializer;
30-
import info.freelibrary.iiif.presentation.v3.utils.json.ContentStateSerializer;
11+
import info.freelibrary.iiif.presentation.v3.utils.json.WebAnnotationDeserializer;
12+
import info.freelibrary.iiif.presentation.v3.utils.json.WebAnnotationSerializer;
13+
import info.freelibrary.util.Logger;
14+
import info.freelibrary.util.LoggerFactory;
15+
import info.freelibrary.util.warnings.Eclipse;
16+
17+
import java.util.List;
3118

3219
/**
3320
* A content state annotation provides a way to refer to a IIIF Presentation API resource, or part of a resource, in a
3421
* compact format that can be used to initialize the view of that resource in any client. For more information, see the
3522
* <a href="https://iiif.io/api/content-state/1.0/">IIIF Content State API</a> specification.
3623
*/
37-
@JsonSerialize(using = ContentStateSerializer.class)
38-
@JsonDeserialize(using = ContentStateDeserializer.class)
39-
public class ContentStateAnnotation implements Annotation<ContentStateAnnotation> {
24+
@JsonSerialize(using = WebAnnotationSerializer.class)
25+
@JsonDeserialize(using = WebAnnotationDeserializer.class)
26+
public class ContentStateAnnotation extends WebAnnotation implements Annotation<WebAnnotation> {
4027

4128
/** The Content State annotation's logger. */
4229
private static final Logger LOGGER = LoggerFactory.getLogger(ContentStateAnnotation.class, MessageCodes.BUNDLE);
4330

44-
/** A boolean flag indicating whether the annotation body contains a choice. */
45-
private boolean myBodyHasChoice;
46-
47-
/** The context list for free-standing annotations. */
48-
private ContextList myContexts;
49-
50-
/** The annotation's ID. */
51-
private String myID;
52-
53-
/** The annotation's label. */
54-
private Label myLabel;
55-
56-
/** The annotation's motivation. */
57-
private final Motivation myMotivation;
58-
59-
/** The annotation's resources. */
60-
private List<ContentResource> myResources;
61-
62-
/** The annotation's targets. */
63-
private List<Target> myTargets;
64-
65-
/** The annotation's time mode. */
66-
private TimeMode myTimeMode;
67-
6831
/**
6932
* Creates a new content state annotation.
7033
*
@@ -86,10 +49,8 @@ public <C extends CanvasResource<C>> ContentStateAnnotation(final String aID, fi
8649
*/
8750
public <C extends CanvasResource<C>> ContentStateAnnotation(final String aID, final CanvasResource<C> aCanvas,
8851
final MediaFragmentSelector aCanvasRegion) {
89-
myTargets = new ArrayList<>();
90-
myTargets.add(new SpecificResource(aCanvas.getID(), aCanvasRegion));
91-
myID = UriUtils.checkID(aID, false);
92-
myMotivation = Motivation.fromLabel(Purpose.CONTENT_STATE);
52+
super(aID, aCanvas, aCanvasRegion);
53+
setMotivation(Motivation.fromLabel(Purpose.CONTENT_STATE));
9354
}
9455

9556
/**
@@ -112,10 +73,8 @@ public <C extends CanvasResource<C>> ContentStateAnnotation(final String aID, fi
11273
* @param aTargetList An annotation target list
11374
*/
11475
public ContentStateAnnotation(final String aID, final List<Target> aTargetList) {
115-
myID = UriUtils.checkID(aID, false);
116-
myTargets = new ArrayList<>();
117-
myTargets.addAll(aTargetList);
118-
myMotivation = Motivation.fromLabel(Purpose.CONTENT_STATE);
76+
super(aID, aTargetList);
77+
setMotivation(Motivation.fromLabel(Purpose.CONTENT_STATE));
11978
}
12079

12180
/**
@@ -135,169 +94,26 @@ public ContentStateAnnotation(final String aID, final Manifest aManifest) {
13594
* @param aTargetArray An annotation target array
13695
*/
13796
public ContentStateAnnotation(final String aID, final Target... aTargetArray) {
138-
myID = UriUtils.checkID(aID, false);
139-
myTargets = new ArrayList<>();
140-
myTargets.addAll(Arrays.asList(aTargetArray));
141-
myMotivation = Motivation.fromLabel(Purpose.CONTENT_STATE);
142-
}
143-
144-
@Override
145-
public boolean bodyHasChoice() {
146-
return myBodyHasChoice;
147-
}
148-
149-
@Override
150-
public List<ContentResource> getBody() {
151-
if (myResources == null) {
152-
myResources = new ArrayList<>();
153-
}
154-
155-
return myResources;
156-
}
157-
158-
/**
159-
* Gets the resource's contexts.
160-
*
161-
* @return The contexts
162-
*/
163-
public List<URI> getContexts() {
164-
if (myContexts == null) {
165-
myContexts = new ContextList();
166-
}
167-
168-
return myContexts;
169-
}
170-
171-
@Override
172-
public String getID() {
173-
return myID;
174-
}
175-
176-
@Override
177-
public Optional<Label> getLabel() {
178-
return Optional.ofNullable(myLabel);
179-
}
180-
181-
@Override
182-
public Optional<Motivation> getMotivation() {
183-
return Optional.of(myMotivation);
184-
}
185-
186-
@Override
187-
public List<Target> getTargets() {
188-
if (myTargets == null) {
189-
myTargets = new ArrayList<>();
190-
}
191-
192-
return myTargets;
193-
}
194-
195-
@Override
196-
public Optional<TimeMode> getTimeMode() {
197-
return Optional.ofNullable(myTimeMode);
198-
}
199-
200-
@Override
201-
public ContentStateAnnotation setBody(final ContentResource... aResourceArray) {
202-
final List<ContentResource> body = getBody();
203-
204-
body.clear();
205-
body.addAll(Arrays.asList(aResourceArray));
206-
207-
return this;
208-
}
209-
210-
@Override
211-
public ContentStateAnnotation setBody(final List<ContentResource> aResourceList) {
212-
final List<ContentResource> body = getBody();
213-
214-
body.clear();
215-
body.addAll(aResourceList);
216-
217-
return this;
218-
}
219-
220-
@Override
221-
public ContentStateAnnotation setChoice(final boolean aChoice) {
222-
myBodyHasChoice = aChoice;
223-
return this;
97+
super(aID, aTargetArray);
98+
setMotivation(Motivation.fromLabel(Purpose.CONTENT_STATE));
22499
}
225100

226101
/**
227-
* Sets the contexts for ContentState annotations used independently.
228-
*
229-
* @param aContextList A list of contexts
230-
* @return This ContentState annotation
102+
* Creates a content state annotation. This is used by Jackson's deserialization processes.
231103
*/
232-
public ContentStateAnnotation setContexts(final List<URI> aContextList) {
233-
if (aContextList instanceof final ContextList contextList) {
234-
myContexts = contextList;
235-
} else {
236-
myContexts = new ContextList(aContextList);
237-
}
238-
239-
return this;
240-
}
241-
242-
@Override
243-
public ContentStateAnnotation setID(final String aID) {
244-
myID = aID;
245-
return this;
104+
@SuppressWarnings(Eclipse.UNUSED)
105+
private ContentStateAnnotation() {
106+
super();
107+
setMotivation(Motivation.fromLabel(Purpose.CONTENT_STATE));
246108
}
247109

248110
@Override
249-
public ContentStateAnnotation setLabel(final Label aLabel) {
250-
myLabel = aLabel;
251-
return this;
252-
}
253-
254-
@Override
255-
public ContentStateAnnotation setMotivation(final Motivation aMotivation) {
111+
public final ContentStateAnnotation setMotivation(final Motivation aMotivation) {
256112
if (!Purpose.CONTENT_STATE.toString().equalsIgnoreCase(aMotivation.toString())) {
257113
throw new IllegalArgumentException(LOGGER.getMessage(MessageCodes.JPA_038,
258114
ContentStateAnnotation.class.getSimpleName(), Purpose.CONTENT_STATE, aMotivation));
259115
}
260116

261-
return this;
262-
}
263-
264-
@Override
265-
public ContentStateAnnotation setTargets(final List<Target> aTargetList) {
266-
final List<Target> targets = getTargets();
267-
268-
targets.clear();
269-
targets.addAll(aTargetList);
270-
271-
return this;
272-
}
273-
274-
@Override
275-
public ContentStateAnnotation setTargets(final Target... aTargetArray) {
276-
final List<Target> targets = getTargets();
277-
278-
targets.clear();
279-
targets.addAll(Arrays.asList(aTargetArray));
280-
281-
return this;
282-
}
283-
284-
@Override
285-
public ContentStateAnnotation setTimeMode(final TimeMode aTimeMode) {
286-
myTimeMode = aTimeMode;
287-
return this;
288-
}
289-
290-
/**
291-
* Gets a JSON string representation of this object.
292-
*
293-
* @return A JSON string representation
294-
*/
295-
@Override
296-
public String toString() {
297-
try {
298-
return JSON.getWriter(getClass()).writeValueAsString(this);
299-
} catch (final JsonProcessingException details) {
300-
throw new I18nRuntimeException(details);
301-
}
117+
return (ContentStateAnnotation) super.setMotivation(aMotivation);
302118
}
303119
}

0 commit comments

Comments
 (0)