11
22package 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 ;
114import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
125import 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-
186import info .freelibrary .iiif .presentation .v3 .Annotation ;
197import info .freelibrary .iiif .presentation .v3 .CanvasResource ;
20- import info .freelibrary .iiif .presentation .v3 .ContextList ;
218import 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 ;
269import info .freelibrary .iiif .presentation .v3 .properties .selectors .MediaFragmentSelector ;
27- import info .freelibrary .iiif .presentation .v3 .utils .JSON ;
2810import 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