Skip to content

Commit

Permalink
Merge pull request #164 from craquet/fix/crate-reader
Browse files Browse the repository at this point in the history
Fix issues with crate reader
  • Loading branch information
sabrineChe authored Jul 10, 2024
2 parents 78cb1ee + da44684 commit 16f82e3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public T addContent(Path path, String id) {
public T addContent(URI uri) {
if (isUrl(uri.toString())) {
this.setId(uri.toString());
}
} else throw new IllegalArgumentException("This Data Entity remote ID does not resolve to a valid URL.");
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import edu.kit.datamanager.ro_crate.entities.data.RootDataEntity;
import edu.kit.datamanager.ro_crate.special.JsonUtilFunctions;
import static edu.kit.datamanager.ro_crate.special.UriUtil.decode;
import static edu.kit.datamanager.ro_crate.special.UriUtil.isUrl;

import edu.kit.datamanager.ro_crate.validation.JsonSchemaValidation;
import edu.kit.datamanager.ro_crate.validation.Validator;

Expand Down Expand Up @@ -92,17 +94,18 @@ public RoCrate readCrate(String location) {
// if the id is in the root hasPart list, we know this entity is a data entity
RootDataEntity root = crate.getRootDataEntity();
if (root != null && root.hasInHasPart(node.get(PROP_ID).asText())) {
// data entity
DataEntity.DataEntityBuilder dataEntity = new DataEntity.DataEntityBuilder()
.setAll(node.deepCopy());

// Handle data entities with corresponding file
File loc = checkFolderHasFile(node.get(PROP_ID).asText(), files);
if (loc != null) {
usedFiles.add(loc.getPath());

// data entity
DataEntity dataEntity = new DataEntity.DataEntityBuilder()
.setAll(node.deepCopy())
.addContent(loc.toPath(), loc.getName())
.build();
crate.addDataEntity(dataEntity, false);
dataEntity.addContent(loc.toPath(), loc.getName());
}

crate.addDataEntity(dataEntity.build(), false);
} else {
// contextual entity
crate.addContextualEntity(
Expand All @@ -124,6 +127,7 @@ public RoCrate readCrate(String location) {
}

protected File checkFolderHasFile(String id, File file) {
if (isUrl(id)) return null;
Path path = file.toPath().resolve(decode(id).get());
if (path.toFile().exists()) {
return path.toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public static boolean isValidUri(String uri) {
* @return true if it is a url, false otherwise.
*/
public static boolean isUrl(String uri) {
return asUrl(uri).isPresent();
try {
return asUrl(uri).isPresent();
} catch (Exception e) {
return false;
}
}

/**
Expand Down

0 comments on commit 16f82e3

Please sign in to comment.