diff --git a/vcell-cli/src/main/java/org/vcell/cli/run/OmexHandler.java b/vcell-cli/src/main/java/org/vcell/cli/run/OmexHandler.java index 0aa2f6f583..b404a0dd3b 100644 --- a/vcell-cli/src/main/java/org/vcell/cli/run/OmexHandler.java +++ b/vcell-cli/src/main/java/org/vcell/cli/run/OmexHandler.java @@ -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(); @@ -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 = """ + + + + """; + Files.write(tempFile, new_rdf_content.getBytes()); // replace fileInsideZipPath with temp file Files.delete(fileInsideZipPath); Files.copy(tempFile, fileInsideZipPath); diff --git a/vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java b/vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java index 01acbceeef..e6add74ab1 100644 --- a/vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java +++ b/vcell-core/src/main/java/org/vcell/sedml/SEDMLExporter.java @@ -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; @@ -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( @@ -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); @@ -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);