Skip to content

Commit 8803c60

Browse files
committed
feat: add support for context prefixes in entity checks
1 parent 9d639bd commit 8803c60

File tree

3 files changed

+26
-256
lines changed

3 files changed

+26
-256
lines changed

src/main/java/edu/kit/datamanager/ro_crate/context/RoCrateMetadataContext.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.util.*;
1414
import java.util.function.Consumer;
15+
import java.util.function.Function;
1516

1617
import edu.kit.datamanager.ro_crate.special.IdentifierUtils;
1718
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -117,6 +118,11 @@ public boolean checkEntity(AbstractEntity entity) {
117118
entity.getProperties().path("@type"),
118119
new TypeReference<>() {}
119120
);
121+
122+
final Function<String, Boolean> isFail = checkMeStr -> this.contextMap.get(checkMeStr) == null
123+
&& this.contextMap.keySet().stream()
124+
.noneMatch(key -> checkMeStr.startsWith(key + ":"));
125+
120126
// check if the items in the array of types are present in the context
121127
for (String s : types) {
122128
// special cases:
@@ -134,7 +140,7 @@ public boolean checkEntity(AbstractEntity entity) {
134140
continue;
135141
}
136142

137-
if (this.contextMap.get(s) == null) {
143+
if (isFail.apply(s)) {
138144
System.err.println("type " + s + " is missing from the context!");
139145
return false;
140146
}
@@ -147,7 +153,7 @@ public boolean checkEntity(AbstractEntity entity) {
147153
// full URLs are considered fine
148154
continue;
149155
}
150-
if (this.contextMap.get(s) == null) {
156+
if (isFail.apply(s)) {
151157
System.err.println("attribute name " + s + " is missing from context;");
152158
return false;
153159
}

src/test/java/edu/kit/datamanager/ro_crate/context/ContextTest.java

Lines changed: 0 additions & 254 deletions
This file was deleted.

src/test/java/edu/kit/datamanager/ro_crate/context/RoCrateMetadataContextTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.node.ObjectNode;
77

88
import edu.kit.datamanager.ro_crate.HelpFunctions;
9+
import edu.kit.datamanager.ro_crate.RoCrate;
910
import edu.kit.datamanager.ro_crate.entities.AbstractEntity;
1011
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
1112
import edu.kit.datamanager.ro_crate.entities.data.DataEntity;
@@ -278,4 +279,21 @@ void testReadPairs() {
278279
// prove immutability
279280
assertThrows(UnsupportedOperationException.class, () -> given.put("newKey", "newValue"));
280281
}
282+
283+
@Test
284+
void checkEntity_withDefinedPrefixedType_succeeds() throws IOException {
285+
// assume we read a crate just for demonstration
286+
RoCrate crate = new RoCrate();
287+
// and we extend the context
288+
RoCrateMetadataContext context = new RoCrateMetadataContext();
289+
context.addToContext("rdfs", "https://www.w3.org/2000/01/rdf-schema#");
290+
crate.setMetadataContext(context);
291+
// then we use the new context
292+
DataEntity entity = new DataEntity.DataEntityBuilder()
293+
.addType("rdfs:Property")
294+
.build();
295+
crate.addDataEntity(entity);
296+
// Then we expect this to work
297+
assertTrue(context.checkEntity(entity));
298+
}
281299
}

0 commit comments

Comments
 (0)