Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support other annotations (W3C Web Annotations) #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
<systemPropertyVariables>
<jiiify.version>${project.version}</jiiify.version>
</systemPropertyVariables>
<argLine>${jacoco.agent.arg}</argLine>
<argLine>--add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<!-- Create an uber jar for use with JRuby, Jython, etc. -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

package info.freelibrary.iiif.presentation.v3; // NOPMD

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -28,6 +27,9 @@
import info.freelibrary.util.warnings.JDK;
import info.freelibrary.util.warnings.PMD;

import info.freelibrary.iiif.presentation.v3.annotations.PaintingAnnotation;
import info.freelibrary.iiif.presentation.v3.annotations.Relationship;
import info.freelibrary.iiif.presentation.v3.annotations.SupplementingAnnotation;
import info.freelibrary.iiif.presentation.v3.ids.Minter;
import info.freelibrary.iiif.presentation.v3.properties.Behavior;
import info.freelibrary.iiif.presentation.v3.properties.Label;
Expand Down Expand Up @@ -87,7 +89,7 @@ abstract class AbstractCanvas<T extends AbstractCanvas<T>> extends NavigableReso
/**
* The canvas' other canvases (other than painting or supplementing).
*/
private List<AnnotationPage<? extends Annotation<?>>> myOtherAnnotations;
private List<AnnotationPage<? extends AnnotationResource<?>>> myOtherAnnotations;

/**
* The canvas' duration.
Expand All @@ -108,29 +110,28 @@ abstract class AbstractCanvas<T extends AbstractCanvas<T>> extends NavigableReso
* Creates a new canvas.
*
* @param aID A canvas ID in string form
* @param aLabel A canvas label in string form
*/
protected AbstractCanvas(final String aID, final String aLabel) {
super(ResourceTypes.CANVAS, aID, aLabel);
protected AbstractCanvas(final String aID) {
super(ResourceTypes.CANVAS, aID);
}

/**
* Creates a new canvas.
*
* @param aID A canvas ID in string form
* @param aLabel A canvas label
* @param aLabel A canvas label in string form
*/
protected AbstractCanvas(final String aID, final Label aLabel) {
protected AbstractCanvas(final String aID, final String aLabel) {
super(ResourceTypes.CANVAS, aID, aLabel);
}

/**
* Creates a new canvas.
*
* @param aID A canvas ID
* @param aID A canvas ID in string form
* @param aLabel A canvas label
*/
protected AbstractCanvas(final URI aID, final Label aLabel) {
protected AbstractCanvas(final String aID, final Label aLabel) {
super(ResourceTypes.CANVAS, aID, aLabel);
}

Expand All @@ -144,24 +145,6 @@ protected AbstractCanvas(final Minter aMinter, final Label aLabel) {
super(ResourceTypes.CANVAS, aMinter.getCanvasID(), aLabel);
}

/**
* Creates a new canvas.
*
* @param aID A canvas ID
*/
protected AbstractCanvas(final URI aID) {
super(ResourceTypes.CANVAS, aID);
}

/**
* Creates a new canvas.
*
* @param aID A canvas ID in string form
*/
protected AbstractCanvas(final String aID) {
super(ResourceTypes.CANVAS, URI.create(aID));
}

/**
* Creates a new canvas, using a minter to create its ID.
*
Expand Down Expand Up @@ -271,7 +254,7 @@ public List<AnnotationPage<PaintingAnnotation>> getPaintingPages() {
* @return The canvas' non-painting annotation pages
*/
@JsonIgnore
public List<AnnotationPage<? extends Annotation<?>>> getOtherAnnotations() {
public List<AnnotationPage<? extends AnnotationResource<?>>> getOtherAnnotations() {
if (myOtherAnnotations == null) {
myOtherAnnotations = new ArrayList<>();
}
Expand All @@ -286,8 +269,9 @@ public List<AnnotationPage<? extends Annotation<?>>> getOtherAnnotations() {
* @return The canvas
*/
@JsonIgnore
public AbstractCanvas<T> setOtherAnnotations(final List<AnnotationPage<? extends Annotation<?>>> aAnnotationList) {
final List<AnnotationPage<? extends Annotation<?>>> annotations = getOtherAnnotations();
public AbstractCanvas<T>
setOtherAnnotations(final List<AnnotationPage<? extends AnnotationResource<?>>> aAnnotationList) {
final List<AnnotationPage<? extends AnnotationResource<?>>> annotations = getOtherAnnotations();

Objects.requireNonNull(aAnnotationList);
annotations.clear();
Expand All @@ -304,7 +288,8 @@ public AbstractCanvas<T> setOtherAnnotations(final List<AnnotationPage<? extends
*/
@SuppressWarnings(JDK.UNCHECKED)
@JsonIgnore
public AbstractCanvas<T> setOtherAnnotations(final AnnotationPage<? extends Annotation<?>>... aAnnotationArray) {
public AbstractCanvas<T>
setOtherAnnotations(final AnnotationPage<? extends AnnotationResource<?>>... aAnnotationArray) {
return setOtherAnnotations(Arrays.asList(aAnnotationArray));
}

Expand Down Expand Up @@ -696,7 +681,7 @@ private boolean canFrame(final ContentResource<?> aContent, final MediaFragmentS
*/
@SuppressWarnings({ PMD.N_PATH_COMPLEXITY, PMD.CYCLOMATIC_COMPLEXITY })
private Canvas getCanvasFragment(final MediaFragmentSelector aCanvasRegion) { // NOPMD
final URI canvasID = URI.create(getID().toString() + Constants.HASH + aCanvasRegion.toString());
final String canvasID = getID().toString() + Constants.HASH + aCanvasRegion.toString();
final Canvas canvasFragment = new Canvas(canvasID);
final double duration;
final int height;
Expand Down Expand Up @@ -772,8 +757,8 @@ private Canvas getCanvasFragment(final MediaFragmentSelector aCanvasRegion) { //
* @return A list of annotation pages
*/
@JsonGetter(JsonKeys.ANNOTATIONS)
private List<AnnotationPage<? extends Annotation<?>>> getAnnotations() {
final List<AnnotationPage<? extends Annotation<?>>> annotations = new ArrayList<>();
private List<AnnotationPage<? extends AnnotationResource<?>>> getAnnotations() {
final List<AnnotationPage<? extends AnnotationResource<?>>> annotations = new ArrayList<>();

getSupplementingPages().forEach(page -> {
annotations.add(page);
Expand All @@ -796,19 +781,19 @@ private List<AnnotationPage<? extends Annotation<?>>> getAnnotations() {
@JsonSetter(JsonKeys.ANNOTATIONS)
private AbstractCanvas<T> setAnnotations(final Object aObject) {
final List<AnnotationPage<SupplementingAnnotation>> supplementingPages = getSupplementingPages();
final List<AnnotationPage<? extends Annotation<?>>> otherAnnotations = getOtherAnnotations();
final List<AnnotationPage<? extends Annotation<?>>> annotationList = getDeserializedPageList(aObject);
final List<AnnotationPage<? extends AnnotationResource<?>>> otherAnnotations = getOtherAnnotations();
final List<AnnotationPage<? extends AnnotationResource<?>>> annotationList = getDeserializedPageList(aObject);

supplementingPages.clear();
otherAnnotations.clear();

annotationList.forEach(page -> {
// Check all the annotations on the page to make sure they are all supplementing annotations
final List<? extends Annotation<?>> annotations = page.getAnnotations();
final List<? extends AnnotationResource<?>> annotations = page.getAnnotations();

boolean supplementingAnnotations = true;

for (final Annotation<?> annotation : annotations) {
for (final AnnotationResource<?> annotation : annotations) {
if (!(annotation instanceof SupplementingAnnotation)) {
supplementingAnnotations = false;
}
Expand All @@ -833,8 +818,8 @@ private AbstractCanvas<T> setAnnotations(final Object aObject) {
* @return A list of AnnotationPage(s)
*/
@JsonIgnore
private List<AnnotationPage<? extends Annotation<?>>> getDeserializedPageList(final Object aObject) {
final List<AnnotationPage<? extends Annotation<?>>> pagesList = new ArrayList<>();
private List<AnnotationPage<? extends AnnotationResource<?>>> getDeserializedPageList(final Object aObject) {
final List<AnnotationPage<? extends AnnotationResource<?>>> pagesList = new ArrayList<>();

// Incoming object should be a list of AnnotationPage(s)
if (aObject instanceof List) {
Expand All @@ -858,23 +843,24 @@ private List<AnnotationPage<? extends Annotation<?>>> getDeserializedPageList(fi
* @return An AnnotationPage
*/
@JsonIgnore
private AnnotationPage<? extends Annotation<?>> getDeserializedPage(final Map<?, ?> aAnnotationPageMap) {
private AnnotationPage<? extends AnnotationResource<?>> getDeserializedPage(final Map<?, ?> aAnnotationPageMap) {
final List<?> items = (List<?>) aAnnotationPageMap.get(JsonKeys.ITEMS);
final TypeFactory typeFactory = JSON.getTypeFactory();
final JavaType javaType;

// If there are annotations, we can check whether they are supplementing or "other"
if (items == null) {
return JSON.convertValue(aAnnotationPageMap,
typeFactory.constructParametricType(AnnotationPage.class, Annotation.class));
typeFactory.constructParametricType(AnnotationPage.class, AnnotationResource.class));
}

boolean supplementingAnnotations = true;

for (final Object item : items) {
final Map<?, ?> annotation = (Map<?, ?>) item;

if (annotation != null && !SupplementingAnnotation.MOTIVATION.equals(annotation.get(JsonKeys.MOTIVATION))) {
if (annotation != null &&
!Relationship.SUPPLEMENTING.toString().equals(annotation.get(JsonKeys.MOTIVATION))) {
supplementingAnnotations = false;
}
}
Expand All @@ -883,7 +869,7 @@ private AnnotationPage<? extends Annotation<?>> getDeserializedPage(final Map<?,
if (supplementingAnnotations) {
javaType = typeFactory.constructParametricType(AnnotationPage.class, SupplementingAnnotation.class);
} else {
javaType = typeFactory.constructParametricType(AnnotationPage.class, Annotation.class);
javaType = typeFactory.constructParametricType(AnnotationPage.class, AnnotationResource.class);
}

return JSON.convertValue(aAnnotationPageMap, javaType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

package info.freelibrary.iiif.presentation.v3;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -49,17 +48,6 @@ protected AbstractContentResource(final String aType, final String aID) {
myFormat = MediaType.parse(aID).orElse(null);
}

/**
* Creates a content resource.
*
* @param aType The type of resource
* @param aID The resource ID
*/
protected AbstractContentResource(final String aType, final URI aID) {
super(aType, aID);
myFormat = MediaType.parse(aID).orElse(null);
}

/**
* Creates a content resource.
*
Expand Down Expand Up @@ -115,9 +103,8 @@ protected AbstractContentResource<T> setFormat(final MediaType aMediaType) {
private Optional<String> getFormatAsString() {
if (myFormat != null) {
return Optional.of(myFormat.toString());
} else {
return Optional.empty();
}
return Optional.empty();
}

/**
Expand Down Expand Up @@ -154,9 +141,8 @@ private Object getLanguage() {

if (languages.size() == SINGLE_LANGUAGE_COUNT) {
return languages.get(0);
} else {
return languages;
}
return languages;
}

/**
Expand All @@ -169,7 +155,8 @@ private Object getLanguage() {
private AbstractContentResource<T> setLanguage(final Object aObject) {
if (aObject instanceof String) {
return (AbstractContentResource<T>) setLanguages((String) aObject);
} else if (aObject instanceof String[]) {
}
if (aObject instanceof String[]) {
return (AbstractContentResource<T>) setLanguages((String[]) aObject);
}

Expand Down
Loading