Skip to content

Commit

Permalink
replace all .rdf files with placeholders in archive upon reading
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Apr 22, 2024
1 parent 7143193 commit ac8f633
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions vcell-cli/src/main/java/org/vcell/cli/run/OmexHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public OmexHandler(String omexPath, String outDir) throws IOException {
this.omexName = omexPath.substring(indexOfLastSlash + 1);
this.tempPath = RunUtils.getTempDir();
try {
replaceMetadataRdfFile(Paths.get(omexPath));
replaceMetadataRdfFiles(Paths.get(omexPath));
this.archive = new CombineArchive(new File(omexPath));
if (this.archive.hasErrors()){
String message = "Unable to initialise OMEX archive "+this.omexName+": "+this.archive.getErrors();
Expand All @@ -58,27 +58,33 @@ public OmexHandler(String omexPath, String outDir) throws IOException {
}
}

private void replaceMetadataRdfFile(Path zipFilePath) throws IOException {
String pathInZip = "/metadata.rdf";
private void replaceMetadataRdfFiles(Path zipFilePath) throws IOException {
try( FileSystem fs = FileSystems.newFileSystem(zipFilePath) ) {
final Path fileInsideZipPath;
try {
fileInsideZipPath = fs.getPath(pathInZip);
} catch (InvalidPathException e) {
// there was no /metadata.rdf file in this archive
return;
for (Path root : fs.getRootDirectories()) {
Files.walk(root)
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".rdf"))
.forEach(path -> {
try {
// write empty RDF file to temp file and replace the file inside the zip
Path tempFile = Files.createTempFile("temp", ".rdf");
String new_rdf_content =
"""
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
</rdf:RDF>
""";
Files.write(tempFile, new_rdf_content.getBytes());
// replace fileInsideZipPath with temp file
Files.delete(path);
Files.copy(tempFile, path);
Files.delete(tempFile);
} catch (IOException e) {
logger.error("Unable to delete metadata.rdf file from OMEX archive: " + e.getMessage(), e);
}
});
}
// write empty RDF file to temp file and replace the file inside the zip
Path tempFile = Files.createTempFile("temp", ".rdf");
String new_rdf_content = """
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
</rdf:RDF>
""";
Files.write(tempFile, new_rdf_content.getBytes());
// replace fileInsideZipPath with temp file
Files.delete(fileInsideZipPath);
Files.copy(tempFile, fileInsideZipPath);

}
}

Expand Down

0 comments on commit ac8f633

Please sign in to comment.