Skip to content

Commit

Permalink
Store required namespaces found while parsing model
Browse files Browse the repository at this point in the history
  • Loading branch information
3dJan committed Mar 4, 2024
1 parent 05e675a commit 12f34f7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Include/Model/Classes/NMR_Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ namespace NMR {

CryptoRandGenDescriptor m_sRandDescriptor;

NameSpaces m_requiredNameSpaces;

// Add Resource to resource lookup tables
void addResourceToLookupTable(_In_ PModelResource pResource);

Expand Down Expand Up @@ -330,6 +332,8 @@ namespace NMR {
nfBool hasCryptoRandCallbak() const;
nfUint64 generateRandomBytes(nfByte *, nfUint64);


void registerRequiredNameSpace(std::string const& nameSpace);
/// @brief Determines the namespaces that are required by the model
/// @return The namespaces that are required by the model
NameSpaces getRequiredNameSpaces();
Expand Down
42 changes: 30 additions & 12 deletions Source/Model/Classes/NMR_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,22 +1315,40 @@ namespace NMR {
return size;
}

NameSpaces CModel::getRequiredNameSpaces()
void CModel::registerRequiredNameSpace(std::string const&nameSpace)
{
static const NameSpaces ExtensionNameSpaces {XML_3MF_NAMESPACE_MATERIALSPEC,
XML_3MF_NAMESPACE_PRODUCTIONSPEC,
XML_3MF_NAMESPACE_BEAMLATTICESPEC,
XML_3MF_NAMESPACE_SLICESPEC,
XML_3MF_NAMESPACE_SECURECONTENTSPEC,
XML_3MF_NAMESPACE_DIGITALSIGNATURESPEC,
XML_3MF_NAMESPACE_CIPHERVALUESPEC};

m_requiredNameSpaces.push_back(nameSpace);
}

NameSpaces CModel::getRequiredNameSpaces()
{
static const NameSpaces knownExtensionNameSpaces{XML_3MF_NAMESPACE_MATERIALSPEC,
XML_3MF_NAMESPACE_PRODUCTIONSPEC,
XML_3MF_NAMESPACE_BEAMLATTICESPEC,
XML_3MF_NAMESPACE_SLICESPEC,
XML_3MF_NAMESPACE_SECURECONTENTSPEC,
XML_3MF_NAMESPACE_DIGITALSIGNATURESPEC,
XML_3MF_NAMESPACE_CIPHERVALUESPEC};

NameSpaces requiredNameSpaces;
for (auto const &nameSpace : ExtensionNameSpaces) {
if (RequireExtension(nameSpace)) {

// Add all namespaces from m_requiredExtensions that are not in knownExtensionNameSpaces to requiredNameSpaces
for (auto const &nameSpace : m_requiredNameSpaces)
{
if (std::find(knownExtensionNameSpaces.begin(), knownExtensionNameSpaces.end(), nameSpace) == knownExtensionNameSpaces.end())
{
requiredNameSpaces.push_back(nameSpace);
}
}

// For the namespaces this version of lib3mf knows, we test if the namespace is required by the model
for (auto const &nameSpace : knownExtensionNameSpaces)
{
if (RequireExtension(nameSpace))
{
requiredNameSpaces.push_back(nameSpace);
}
}
return requiredNameSpaces;
}
}
}
2 changes: 2 additions & 0 deletions Source/Model/Reader/NMR_ModelReaderNode_ModelBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace NMR {
}
// is the extension supported?
std::string sExtensionURI = m_ListedExtensions[token];
m_pModel->registerRequiredNameSpace(sExtensionURI);

if (strcmp(sExtensionURI.c_str(), PACKAGE_XMLNS_100) != 0 &&
strcmp(sExtensionURI.c_str(), XML_3MF_NAMESPACE_MATERIALSPEC) != 0 &&
strcmp(sExtensionURI.c_str(), XML_3MF_NAMESPACE_PRODUCTIONSPEC) != 0 &&
Expand Down
17 changes: 17 additions & 0 deletions Tests/CPP_Bindings/Source/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,21 @@ namespace Lib3MF
EXPECT_EQ(requiredNamespaces->GetCurrent(), "http://schemas.microsoft.com/3dmanufacturing/beamlattice/2017/02");
EXPECT_EQ(requiredNamespaces->MoveNext(), false);
}

TEST_F(Model, GetRequiredNamespaces_3mfFileWithUnknownExtension_ContainsUnknownNameSpace)
{
auto model = wrapper->CreateModel(); // create a new model to avoid interference with other tests
auto reader3MF = model->QueryReader("3mf");


ASSERT_TRUE(reader3MF);
reader3MF->ReadFromFile(sTestFilesPath + "/RequiredExtensions/" + "Unsupported.3mf");
EXPECT_GE(reader3MF->GetWarningCount(), 1u);
auto requiredNameSpaces = model->GetRequiredNameSpaces();

EXPECT_TRUE(requiredNameSpaces->MoveNext());
std::string const fictionalNameSpace = "http://schemas.autodesk.com/dmg/ExtensionUnsupported/2999/12";
EXPECT_EQ(requiredNameSpaces->GetCurrent(), fictionalNameSpace);
EXPECT_FALSE(requiredNameSpaces->MoveNext());
}
}

0 comments on commit 12f34f7

Please sign in to comment.