Skip to content

Commit

Permalink
remove rdf upon omex read, inject rdf upon omex write
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Apr 22, 2024
1 parent 4d12e5d commit 7143193
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
28 changes: 16 additions & 12 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 {
replaceXmlSchemaNamespaceInArchive(Paths.get(omexPath));
replaceMetadataRdfFile(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,20 +58,24 @@ public OmexHandler(String omexPath, String outDir) throws IOException {
}
}

private void replaceXmlSchemaNamespaceInArchive(Path zipFilePath) throws IOException {
private void replaceMetadataRdfFile(Path zipFilePath) throws IOException {
String pathInZip = "/metadata.rdf";
try( FileSystem fs = FileSystems.newFileSystem(zipFilePath) ) {
Path fileInsideZipPath = fs.getPath(pathInZip);
// if (!fileInsideZipPath.toFile().exists()) {
// return;
// }
// copy fileInsideZipPath to temp file
final Path fileInsideZipPath;
try {
fileInsideZipPath = fs.getPath(pathInZip);
} catch (InvalidPathException e) {
// there was no /metadata.rdf file in this archive
return;
}
// write empty RDF file to temp file and replace the file inside the zip
Path tempFile = Files.createTempFile("temp", ".rdf");
Files.copy(fileInsideZipPath, tempFile, StandardCopyOption.REPLACE_EXISTING);
// replace namespace in temp file
String content = new String(Files.readAllBytes(tempFile));
content = content.replace("xmlns:xmls=", "xmlns:schemaxml=");
Files.write(tempFile, content.getBytes());
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
44 changes: 23 additions & 21 deletions vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import cbit.vcell.solver.MathOverridesResolver.SymbolReplacement;
import cbit.vcell.xml.*;
import de.unirostock.sems.cbarchive.CombineArchive;
import de.unirostock.sems.cbarchive.meta.MetaDataObject;
import de.unirostock.sems.cbarchive.meta.OmexMetaDataObject;
import de.unirostock.sems.cbarchive.meta.omex.OmexDescription;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -1307,12 +1306,15 @@ public boolean createOmexArchive(String srcFolder, String sFileName) {
Path filePath = Paths.get(srcFolder, sd);
if (sd.endsWith(".rdf")) {
rdfFilePath = filePath;
// archive.addEntry(
// filePath.toFile(),
// "./" + sd,
// new URI("http://identifiers.org/combine.specifications/omex-metadata"),
// false
// );
// the CombineArchive library does not allow to directly write to the /metadata.rdf file
// instead, we copy the file to /metadata.rdf later after the archive is closed
//
// archive.addEntry(
// filePath.toFile(),
// "./metadata.rdf",
// new URI("http://identifiers.org/combine.specifications/omex-metadata"),
// false
// );
}
if (sd.endsWith(".png")) {
archive.addEntry(
Expand All @@ -1323,23 +1325,22 @@ public boolean createOmexArchive(String srcFolder, String sFileName) {
);
}
}
OmexDescription omexDescription = new OmexDescription();
omexDescription.setDescription("VCell Simulation Archive");
omexDescription.modified.add(new Date());
archive.addDescription(new OmexMetaDataObject(omexDescription));
if (rdfFilePath != null) {
// create temporary /metadata.rdf file so that an entry for /metadata.rdf is included in the Manifest
OmexDescription omexDescription = new OmexDescription();
omexDescription.setDescription("VCell Simulation Archive");
omexDescription.modified.add(new Date());
archive.addDescription(new OmexMetaDataObject(omexDescription));
}

archive.pack();
archive.close();

//
// wait until omex file is closed, and then replace the metadata.rdf file
// this is a workaround for the issue that CombineArchive library refuses to overwrite the /metadata.rdf file
//
// an alternative is the use the MetaDataObject classes from CombineArchive library - another day.
//
// if (rdfFilePath != null) {
// replaceFileInZip(omexFile.toPath(), rdfFilePath, "/metadata.rdf");
// }
if (rdfFilePath != null) {
// now that the OMEX archive is closed and written to disk, open it as a regular zip file
// and replace the generated metadata.rdf file with the one we created.
replaceMetadataRdfFileInArchive(omexFile.toPath(), rdfFilePath);
}

removeOtherFiles(srcFolder, files);

Expand All @@ -1349,7 +1350,8 @@ public boolean createOmexArchive(String srcFolder, String sFileName) {
return true;
}

private static void replaceFileInZip(Path zipFilePath, Path newFilePath, String pathInZip) throws IOException {
private static void replaceMetadataRdfFileInArchive(Path zipFilePath, Path newFilePath) throws IOException {
String pathInZip = "./metadata.rdf";
try( FileSystem fs = FileSystems.newFileSystem(zipFilePath) ) {
Path fileInsideZipPath = fs.getPath(pathInZip);
Files.delete(fileInsideZipPath);
Expand Down

0 comments on commit 7143193

Please sign in to comment.