Skip to content

Commit 6c4c182

Browse files
authored
Merge pull request #258 from kit-data-manager/support-eln-files
Support .ELN-style crates in all zip readers and writers
2 parents 810d199 + 06b2146 commit 6c4c182

60 files changed

Lines changed: 1577 additions & 824 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ dependencies {
7171
implementation("org.freemarker:freemarker:2.3.34")
7272
}
7373

74+
// enable -Xlint:deprecation
75+
tasks.withType(JavaCompile).configureEach {
76+
options.compilerArgs << "-Xlint:deprecation"
77+
}
78+
7479
logging.captureStandardOutput LogLevel.INFO
7580

7681
def signingTasks = tasks.withType(Sign)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ public boolean checkEntity(AbstractEntity entity) {
113113
node.remove("@id");
114114
node.remove("@type");
115115

116-
Set<String> types = objectMapper.convertValue(entity.getProperties().get("@type"),
117-
new TypeReference<>() {
118-
});
116+
Set<String> types = objectMapper.convertValue(
117+
entity.getProperties().path("@type"),
118+
new TypeReference<>() {}
119+
);
119120
// check if the items in the array of types are present in the context
120121
for (String s : types) {
121122
// special cases:
@@ -174,15 +175,14 @@ public void addToContextFromUrl(String url) {
174175
}
175176
}
176177
if (jsonNode == null) {
177-
CloseableHttpClient httpclient = HttpClients.createDefault();
178178
HttpGet httpGet = new HttpGet(url);
179179
CloseableHttpResponse response;
180-
try {
180+
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
181181
response = httpclient.execute(httpGet);
182182
jsonNode = objectMapper.readValue(response.getEntity().getContent(),
183183
JsonNode.class);
184184
} catch (IOException e) {
185-
System.err.println(String.format("Cannot get context from url %s", url));
185+
System.err.printf("Cannot get context from url %s%n", url);
186186
return;
187187
}
188188
if (url.equals(DEFAULT_CONTEXT)) {

src/main/java/edu/kit/datamanager/ro_crate/entities/AbstractEntity.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,20 +558,62 @@ public T addIdFromCollectionOfEntities(String name, Collection<AbstractEntity> e
558558
}
559559

560560
/**
561-
* This sets everything from a json object to the property. Can be
562-
* useful when the entity is already available somewhere.
561+
* Deprecated. Equivalent to {@link #setAllIfValid(ObjectNode)}.
563562
*
564563
* @param properties the Json representing all the properties.
565-
* @return the generic builder.
564+
* @return the generic builder, either including all given properties
565+
* * or unchanged.
566+
*
567+
* @deprecated To enforce the user know what this method does,
568+
* we want the user to use one of the more explicitly named
569+
* methods {@link #setAllIfValid(ObjectNode)} or
570+
* {@link #setAllIfValid(ObjectNode)}.
571+
* @see #setAllIfValid(ObjectNode)
566572
*/
573+
@Deprecated(since = "2.1.0", forRemoval = true)
567574
public T setAll(ObjectNode properties) {
575+
return setAllIfValid(properties);
576+
}
577+
578+
/**
579+
* This sets everything from a json object to the property,
580+
* <b>if the result is valid</b>. Otherwise, it will do <b>nothing</b>.
581+
* <p>
582+
* Valid means here that the json object needs to be flat as specified
583+
* in the RO-Crate specification. In principle, this means that
584+
* primitives and objects referencing an ID are allowed,
585+
* as well as arrays of these.
586+
*
587+
* @param properties the Json representing all the properties.
588+
* @return the generic builder, either including all given properties
589+
* or unchanged.
590+
*/
591+
public T setAllIfValid(ObjectNode properties) {
568592
if (AbstractEntity.entityValidation.entityValidation(properties)) {
569593
this.properties = properties;
570594
this.relatedItems.addAll(JsonUtilFunctions.getIdPropertiesFromJsonNode(properties));
571595
}
572596
return self();
573597
}
574598

599+
/**
600+
* This sets everything from a json object to the property. Can be
601+
* useful when the entity is already available somewhere.
602+
* <p>
603+
* Errors on validation are printed, but everything will be added.
604+
* For more about validation, see {@link #setAllIfValid(ObjectNode)}.
605+
*
606+
* @param properties the Json representing all the properties.
607+
* @return the generic builder with all properties added.
608+
*/
609+
public T setAllUnsafe(ObjectNode properties) {
610+
// This will currently only print errors.
611+
AbstractEntity.entityValidation.entityValidation(properties);
612+
this.properties = properties;
613+
this.relatedItems.addAll(JsonUtilFunctions.getIdPropertiesFromJsonNode(properties));
614+
return self();
615+
}
616+
575617
public abstract T self();
576618

577619
public abstract AbstractEntity build();

src/main/java/edu/kit/datamanager/ro_crate/entities/contextual/JsonDescriptor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
public class JsonDescriptor extends ContextualEntity {
1515

16-
private static final String CONFORMS_TO = "conformsTo";
17-
protected static final String ID = "ro-crate-metadata.json";
16+
protected static final String CONFORMS_TO = "conformsTo";
17+
public static final String ID = "ro-crate-metadata.json";
1818

1919
/**
2020
* Returns a JsonDescriptor with the conformsTo value set to the latest stable
@@ -39,7 +39,7 @@ private JsonDescriptor(ContextualEntityBuilder builder) {
3939

4040
/**
4141
* Builder for the JsonDescriptor.
42-
*
42+
* <p>
4343
* Defaults to the latest stable crate version and no other conformsTo values.
4444
*/
4545
public static final class Builder {

src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataEntity.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
import edu.kit.datamanager.ro_crate.entities.AbstractEntity;
66
import edu.kit.datamanager.ro_crate.entities.contextual.ContextualEntity;
77
import static edu.kit.datamanager.ro_crate.special.IdentifierUtils.isUrl;
8-
import edu.kit.datamanager.ro_crate.util.ZipUtil;
98

10-
import java.io.File;
11-
import java.io.IOException;
129
import java.net.URI;
1310
import java.nio.file.Path;
1411
import java.util.ArrayList;
1512
import java.util.List;
16-
import net.lingala.zip4j.ZipFile;
17-
import net.lingala.zip4j.exception.ZipException;
18-
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
19-
import net.lingala.zip4j.model.ZipParameters;
20-
import org.apache.commons.io.FileUtils;
2113

2214
/**
2315
* The base class of every data entity.
@@ -56,54 +48,6 @@ public void addAuthorId(String id) {
5648
this.addIdProperty("author", id);
5749
}
5850

59-
/**
60-
* If the data entity contains a physical file. This method will write it
61-
* when the crate is being written to a zip archive.
62-
*
63-
* @param zipFile the zipFile where it should be written.
64-
* @throws ZipException when something goes wrong with the writing to the
65-
* zip file.
66-
*/
67-
public void saveToZip(ZipFile zipFile) throws ZipException {
68-
if (this.path != null) {
69-
ZipParameters zipParameters = new ZipParameters();
70-
zipParameters.setFileNameInZip(this.getId());
71-
zipFile.addFile(this.path.toFile(), zipParameters);
72-
}
73-
}
74-
75-
/**
76-
* If the data entity contains a physical file. This method will write it
77-
* when the crate is being written to a zip archive.
78-
*
79-
* @param zipStream The zip output stream where it should be written.
80-
* @throws ZipException when something goes wrong with the writing to the
81-
* zip file.
82-
* @throws IOException If opening the file input stream fails.
83-
*/
84-
public void saveToStream(ZipOutputStream zipStream) throws ZipException, IOException {
85-
if (this.path != null) {
86-
ZipUtil.addFileToZipStream(zipStream, this.path.toFile(), this.getId());
87-
}
88-
}
89-
90-
/**
91-
* If the data entity contains a physical file. This method will write it
92-
* when the crate is being written to a folder.
93-
*
94-
* @param file the folder location where the entity should be written.
95-
* @throws IOException if something goes wrong with the writing.
96-
*/
97-
public void savetoFile(File file) throws IOException {
98-
if (this.getPath() != null) {
99-
if (this.getPath().toFile().isDirectory()) {
100-
FileUtils.copyDirectory(this.getPath().toFile(), file.toPath().resolve(this.getId()).toFile());
101-
} else {
102-
FileUtils.copyFile(this.getPath().toFile(), file.toPath().resolve(this.getId()).toFile());
103-
}
104-
}
105-
}
106-
10751
@JsonIgnore
10852
public Path getPath() {
10953
return path;

src/main/java/edu/kit/datamanager/ro_crate/entities/data/DataSetEntity.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
55

66
import edu.kit.datamanager.ro_crate.entities.serializers.HasPartSerializer;
7-
import edu.kit.datamanager.ro_crate.util.ZipUtil;
8-
import java.io.IOException;
97

108
import java.util.HashSet;
119
import java.util.Set;
12-
import net.lingala.zip4j.ZipFile;
13-
import net.lingala.zip4j.exception.ZipException;
14-
import net.lingala.zip4j.io.outputstream.ZipOutputStream;
15-
import net.lingala.zip4j.model.ZipParameters;
1610

1711
/**
1812
* A helping class for the creating of Data entities of type Dataset.
@@ -43,26 +37,6 @@ public void removeFromHasPart(String str) {
4337
this.hasPart.remove(str);
4438
}
4539

46-
@Override
47-
public void saveToZip(ZipFile zipFile) throws ZipException {
48-
if (this.getPath() != null) {
49-
ZipParameters parameters = new ZipParameters();
50-
parameters.setRootFolderNameInZip(this.getId());
51-
parameters.setIncludeRootFolder(false);
52-
zipFile.addFolder(this.getPath().toFile(), parameters);
53-
}
54-
}
55-
56-
@Override
57-
public void saveToStream(ZipOutputStream zipOutputStream) throws IOException {
58-
if (this.getPath() != null) {
59-
ZipUtil.addFolderToZipStream(
60-
zipOutputStream,
61-
this.getPath().toAbsolutePath().toString(),
62-
this.getId());
63-
}
64-
}
65-
6640
public void addToHasPart(String id) {
6741
this.hasPart.add(id);
6842
}

src/main/java/edu/kit/datamanager/ro_crate/entities/validation/JsonSchemaValidation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public boolean validateEntity(JsonNode entity) {
6060
Set<ValidationMessage> errors = this.entitySchema.validate(entity);
6161
if (errors.size() != 0) {
6262
System.err.println("This entity does not comply to the basic RO-Crate entity structure.");
63+
errors.forEach(error -> System.err.println(error.getMessage()));
6364
return false;
6465
}
6566
return true;

src/main/java/edu/kit/datamanager/ro_crate/externalproviders/dataentities/ImportFromZenodo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ private static void addToCrateFromZotero(String url, Crate crate) {
112112
for (var entity : graph) {
113113
if (entity.get("@id").asText().equals(mainId)) {
114114
var dataEntity = new DataEntity.DataEntityBuilder()
115-
.setAll((ObjectNode) entity).build();
115+
.setAllUnsafe((ObjectNode) entity).build();
116116
crate.addDataEntity(dataEntity);
117117
} else {
118118
// here we have to think of a way to differentiate between data and contextual entities.
119119
var contextualEntity = new ContextualEntity.ContextualEntityBuilder()
120-
.setAll((ObjectNode) entity).build();
120+
.setAllUnsafe((ObjectNode) entity).build();
121121
crate.addContextualEntity(contextualEntity);
122122
}
123123
}

src/main/java/edu/kit/datamanager/ro_crate/externalproviders/personprovider/OrcidProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static PersonEntity getPerson(String url) {
7272
node.set(element.getKey(), element.getValue());
7373
}
7474
}
75-
return new PersonEntity.PersonEntityBuilder().setAll(node).build();
75+
return new PersonEntity.PersonEntityBuilder().setAllUnsafe(node).build();
7676
} catch (IOException e) {
7777
String errorMessage = String.format("IO error: %s", e.getMessage());
7878
logger.error(errorMessage);

src/main/java/edu/kit/datamanager/ro_crate/preview/AutomaticPreview.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package edu.kit.datamanager.ro_crate.preview;
22

3-
import edu.kit.datamanager.ro_crate.util.ZipUtil;
3+
import edu.kit.datamanager.ro_crate.util.ZipStreamUtil;
44
import java.io.File;
55
import java.io.FileWriter;
66
import java.io.IOException;
@@ -10,9 +10,9 @@
1010
import org.apache.commons.io.FileUtils;
1111

1212
/**
13-
* The default preview should use the rochtml tool
14-
* (https://www.npmjs.com/package/ro-crate-html-js) for creating a simple
15-
* preview file.
13+
* The default preview should use the
14+
* <a href="https://www.npmjs.com/package/ro-crate-html-js">rochtml tool</a>
15+
* for creating a simple preview file.
1616
*
1717
* @author Nikola Tzotchev on 6.2.2022 г.
1818
* @version 1
@@ -66,13 +66,13 @@ public void saveAllToStream(String metadata, ZipOutputStream stream) throws IOEx
6666
if (PreviewGenerator.isRochtmlAvailable()) {
6767
try {
6868
FileUtils.forceMkdir(new File("temp"));
69-
try (FileWriter writer = new FileWriter(new File("temp/ro-crate-metadata.json"))) {
69+
try (FileWriter writer = new FileWriter("temp/ro-crate-metadata.json")) {
7070
writer.write(metadata);
7171
writer.flush();
7272
}
7373
if (PreviewGenerator.isRochtmlAvailable()) {
7474
PreviewGenerator.generatePreview("temp");
75-
ZipUtil.addFileToZipStream(stream, new File("temp/ro-crate-preview.html"), "ro-crate-preview.html");
75+
ZipStreamUtil.addFileToZipStream(stream, new File("temp/ro-crate-preview.html"), "ro-crate-preview.html");
7676
}
7777
} finally {
7878
try {

0 commit comments

Comments
 (0)