Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/file/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ File::Impl::Impl(const std::string &pFileNameOrUrl, bool pRetrieveContents)
} else {
mFilePath = stringToPath("/some/path/file");
}
} else if (!std::filesystem::exists(mFilePath)) {
} else if (!std::filesystem::exists(mFilePath) && pRetrieveContents) {
mType = Type::IRRETRIEVABLE_FILE;

addError("The file does not exist.");
Expand Down Expand Up @@ -87,30 +87,32 @@ void File::Impl::checkType(const FilePtr &pOwner, bool pResetType)
#endif
}

// Try to get a CellML file, a SED-ML file, or a COMBINE archive.
// Try to get a CellML file, a SED-ML file, or a COMBINE archive, but only if we have some contents.

mCellmlFile = CellmlFile::create(pOwner);
if (!contents().empty()) {
mCellmlFile = CellmlFile::create(pOwner);

if (mCellmlFile != nullptr) {
mType = Type::CELLML_FILE;
if (mCellmlFile != nullptr) {
mType = Type::CELLML_FILE;

addIssues(mCellmlFile);
} else {
mSedmlFile = SedmlFile::create(pOwner);

if (mSedmlFile != nullptr) {
mType = Type::SEDML_FILE;

addIssues(mSedmlFile);
addIssues(mCellmlFile);
} else {
mCombineArchive = CombineArchive::create(pOwner);
mSedmlFile = SedmlFile::create(pOwner);

if (mCombineArchive != nullptr) {
mType = Type::COMBINE_ARCHIVE;
if (mSedmlFile != nullptr) {
mType = Type::SEDML_FILE;

addIssues(mCombineArchive);
addIssues(mSedmlFile);
} else {
addError("The file is not a CellML file, a SED-ML file, or a COMBINE archive.");
mCombineArchive = CombineArchive::create(pOwner);

if (mCombineArchive != nullptr) {
mType = Type::COMBINE_ARCHIVE;

addIssues(mCombineArchive);
} else {
addError("The file is not a CellML file, a SED-ML file, or a COMBINE archive.");
}
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/support/cellml/cellmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,11 @@ CellmlFilePtr CellmlFile::create(const FilePtr &pFile)

// Try to parse the file contents as a CellML file, be it a CellML 1.x or a CellML 2.0 file.

auto fileContents = pFile->contents();
auto parser = libcellml::Parser::create(false);
auto model = parser->parseModel(toString(pFile->contents()));

if (!fileContents.empty() && (fileContents[0] != '\0')) {
auto contents = toString(fileContents);
auto parser = libcellml::Parser::create(false);
auto model = parser->parseModel(contents);

if (parser->errorCount() == 0) {
return CellmlFilePtr {new CellmlFile {pFile, model, false}};
}
if (parser->errorCount() == 0) {
return CellmlFilePtr {new CellmlFile {pFile, model, false}};
}

return {};
Expand Down
32 changes: 14 additions & 18 deletions src/support/sedml/sedmlfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,26 +289,22 @@ SedmlFilePtr SedmlFile::create(const FilePtr &pFile)

// Try to retrieve a SED-ML document.

auto fileContents = pFile->contents();

if (!fileContents.empty() && (fileContents[0] != '\0')) {
auto *document = libsedml::readSedMLFromString(toString(fileContents).c_str());

// A non-SED-ML file results in our SED-ML document having at least one error. That error may be the result of a
// malformed XML file (e.g., an HTML file is an XML-like file but not actually an XML file or a COMBINE archive
// which is just not an XML file) or a valid XML file but not a SED-ML file (e.g., a CellML file is an XML file
// but not a SED-ML file). So, we use these facts to determine whether our current SED-ML document is indeed a
// SED-ML file.

if ((document->getNumErrors() == 0)
|| ((document->getError(0)->getErrorId() > libsbml::XMLErrorCodesUpperBound)
&& (document->getError(0)->getErrorId() != libsedml::SedNotSchemaConformant))) {
return SedmlFilePtr {new SedmlFile {pFile, document}};
}

delete document;
auto *document = libsedml::readSedMLFromString(toString(pFile->contents()).c_str());

// A non-SED-ML file results in our SED-ML document having at least one error. That error may be the result of a
// malformed XML file (e.g., an HTML file is an XML-like file but not actually an XML file or a COMBINE archive
// which is just not an XML file) or a valid XML file but not a SED-ML file (e.g., a CellML file is an XML file but
// not a SED-ML file). So, we use these facts to determine whether our current SED-ML document is indeed a SED-ML
// file.

if ((document->getNumErrors() == 0)
|| ((document->getError(0)->getErrorId() > libsbml::XMLErrorCodesUpperBound)
&& (document->getError(0)->getErrorId() != libsedml::SedNotSchemaConformant))) {
return SedmlFilePtr {new SedmlFile {pFile, document}};
}

delete document;

return {};
}

Expand Down
4 changes: 2 additions & 2 deletions tests/api/file/basictests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ TEST(BasicFileTest, localVirtualFile)
EXPECT_EQ(file->url(), "");
EXPECT_EQ(file->path(), libOpenCOR::resourcePath(libOpenCOR::UNKNOWN_FILE));
EXPECT_TRUE(file->contents().empty());
EXPECT_EQ_ISSUES(file, EXPECTED_UNKNOWN_FILE_ISSUES);
EXPECT_EQ_ISSUES(file, EXPECTED_NO_ISSUES);

auto someUnknownContents = libOpenCOR::charArrayToUnsignedChars(libOpenCOR::UNKNOWN_CONTENTS);

Expand All @@ -137,7 +137,7 @@ TEST(BasicFileTest, remoteVirtualFile)
EXPECT_EQ(file->url(), libOpenCOR::UNKNOWN_REMOTE_FILE);
EXPECT_EQ(file->path(), libOpenCOR::UNKNOWN_REMOTE_FILE);
EXPECT_TRUE(file->contents().empty());
EXPECT_EQ_ISSUES(file, EXPECTED_UNKNOWN_FILE_ISSUES);
EXPECT_EQ_ISSUES(file, EXPECTED_NO_ISSUES);

auto someUnknownContents = libOpenCOR::charArrayToUnsignedChars(libOpenCOR::UNKNOWN_CONTENTS);

Expand Down
7 changes: 7 additions & 0 deletions tests/api/file/coveragetests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ TEST(CoverageFileTest, sedmlFileWithNoParent)
file->setContents(libOpenCOR::charArrayToUnsignedChars(libOpenCOR::SEDML_CONTENTS));
}

TEST(CoverageFileTest, irretrievableVirtualFile)
{
auto file = libOpenCOR::File::create(libOpenCOR::IRRETRIEVABLE_FILE, false);

EXPECT_FALSE(file->hasIssues());
}

TEST(CoverageFileTest, irretrievableRemoteFile)
{
libOpenCOR::File::create(libOpenCOR::IRRETRIEVABLE_REMOTE_FILE);
Expand Down
5 changes: 3 additions & 2 deletions tests/bindings/javascript/file.basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { assertIssues } from './utils.js';

const loc = await libOpenCOR();

const expectedNoIssues = [];
const expectedUnknownFileIssues = [
[loc.Issue.Type.ERROR, 'The file is not a CellML file, a SED-ML file, or a COMBINE archive.']
];
Expand Down Expand Up @@ -50,7 +51,7 @@ test.describe('File basic tests', () => {
assert.strictEqual(file.url, '');
assert.strictEqual(file.path, utils.LOCAL_FILE);
assert.deepStrictEqual(file.contents(), utils.NO_CONTENTS);
assertIssues(loc, file, expectedUnknownFileIssues);
assertIssues(loc, file, expectedNoIssues);

file.setContents(unknownContentsPtr, utils.UNKNOWN_CONTENTS.length);

Expand All @@ -67,7 +68,7 @@ test.describe('File basic tests', () => {
assert.strictEqual(file.url, utils.REMOTE_FILE);
assert.strictEqual(file.path, utils.REMOTE_FILE);
assert.deepStrictEqual(file.contents(), utils.NO_CONTENTS);
assertIssues(loc, file, expectedUnknownFileIssues);
assertIssues(loc, file, expectedNoIssues);

file.setContents(unknownContentsPtr, utils.UNKNOWN_CONTENTS.length);

Expand Down
4 changes: 2 additions & 2 deletions tests/bindings/python/test_file_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_local_virtual_file():
assert file.url == ""
assert file.path == utils.resource_path(utils.UnknownFile)
assert file.contents == []
assert_issues(file, expected_unknown_file_issues)
assert_issues(file, expected_no_issues)

some_unknown_contents_list = utils.string_to_list(utils.SomeUnknownContents)

Expand All @@ -136,7 +136,7 @@ def test_remote_virtual_file():
assert file.url == utils.UnknownRemoteFile
assert file.path == utils.UnknownRemoteFile
assert file.contents == []
assert_issues(file, expected_unknown_file_issues)
assert_issues(file, expected_no_issues)

some_unknown_contents_list = utils.string_to_list(utils.SomeUnknownContents)

Expand Down
6 changes: 6 additions & 0 deletions tests/bindings/python/test_file_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def test_sedml_file_with_no_parent():
file.contents = utils.string_to_list(utils.SomeSedmlContents)


def test_irretrievable_virtual_file():
file = loc.File(utils.IrretrievableFile, False)

assert not file.has_issues


def test_irretrievable_remote_file():
loc.File(utils.IrretrievableRemoteFile)

Expand Down
Loading