Skip to content

Commit

Permalink
RteKernel to provide YmlTree parser (#1286)
Browse files Browse the repository at this point in the history
* RteKernel to provide YmlTree parser 
* YmlTree parser
     find schema files
     allow override YmlTreeParserInterface
* RteKernel set/get  path to CMSIS-Toolbox Installation

---------

Co-authored-by: Evgueni Driouk <[email protected]>
  • Loading branch information
grasci-arm and edriouk authored Jan 19, 2024
1 parent 76d2052 commit 9f286aa
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 231 deletions.
18 changes: 18 additions & 0 deletions libs/rtefsutils/include/RteFsUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ class RteFsUtils
* @return string containing the absolute path
*/
static std::string GetAbsPathFromLocalUrl(const std::string& url);

/**
* @brief finds file in directories relative to the base path
* @param fileName file name to search for
* @param baseDir path to look in
* @param relSearchOrder vector of relative directories to look in
* @return absolute file name if found, empty string otherwise
*/
static std::string FindFile(const std::string& fileName, const std::string& baseDir,
const std::vector<std::string>& relSearchOrder);

/**
* @brief finds file in ./, ../etc/, and ../../etc directories relative to the base
* @param fileName file name to search for
* @param baseDir path to look in
* @return absolute file name if found, empty string otherwise
*/
static std::string FindFileInEtc(const std::string& fileName, const std::string& baseDir);
};

#endif // RteFsUtils_H
18 changes: 18 additions & 0 deletions libs/rtefsutils/src/RteFsUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,22 @@ string RteFsUtils::GetAbsPathFromLocalUrl(const string& url) {
return filepath;
}

std::string RteFsUtils::FindFile(const std::string& fileName, const std::string& baseDir,
const std::vector<std::string>& relSearchOrder)
{
for(auto& relPath : relSearchOrder) {
string file = baseDir + relPath + fileName;
if(RteFsUtils::Exists(file)) {
return MakePathCanonical(file);
}
}
return RteUtils::EMPTY_STRING;
}

std::string RteFsUtils::FindFileInEtc(const std::string& fileName, const std::string& baseDir)
{
static const std::vector<std::string>& relSearchOrder = { "./", "../etc/", "../../etc/" };
return FindFile(fileName, baseDir, relSearchOrder);
}

// End of RteFsUtils.cpp
2 changes: 1 addition & 1 deletion libs/rtemodel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ set_property(TARGET RteModel PROPERTY

target_include_directories(RteModel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_link_libraries(RteModel RteFsUtils RteUtils XmlTree CrossPlatform)
target_link_libraries(RteModel RteFsUtils RteUtils XmlTree YmlTree CrossPlatform)
42 changes: 38 additions & 4 deletions libs/rtemodel/include/RteKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "RteTarget.h"
#include "RteUtils.h"

#include "XMLTree.h"
#include "YmlTree.h"

#include <memory>

Expand Down Expand Up @@ -57,6 +57,19 @@ class RteKernel
*/
bool SetCmsisPackRoot(const std::string& cmsisPackRoot);

/**
* @brief getter for CMSIS-Toolbox installation directory
* @return CMSIS-Toolbox installation directory if available
*/
const std::string& GetCmsisToolboxDir() const { return m_cmsisToolboxDir; }

/**
* @brief setter for CMSIS-Toolbox installation directory
* @param cmsisToolboxDir CMSIS-Toolbox installation directory to set
*/
void SetCmsisToolboxDir(const std::string& cmsisToolboxDir) { m_cmsisToolboxDir = cmsisToolboxDir; }


/**
* @brief getter for RteCallback object
* @return pointer to RteCallback object
Expand Down Expand Up @@ -218,11 +231,19 @@ class RteKernel
std::string GetPdscFileFromPath(const XmlItem& attributes, const std::string& cprjPath, std::string& packId);

/**
* @brief create a smart pointer holding a XMLTree pointer
* @return a std::unique_ptr object holding a XMLTree pointer which is nullptr in the default implementation
* @brief create a smart pointer holding an XMLTree pointer to parse XML files
* @param itemBuilder pointer to IXmlItemBuilder item factory
* @return a std::unique_ptr object holding an XMLTree-derived pointer which is nullptr in the default implementation
*/
virtual std::unique_ptr<XMLTree> CreateUniqueXmlTree(IXmlItemBuilder* itemBuilder = nullptr) const;

/**
* @brief create a smart pointer holding a YmlTree pointer to parse YAML files
* @param itemBuilder pointer to IXmlItemBuilder item factory
* @return a std::unique_ptr object holding a YmlTree-derived pointer which is nullptr in the default implementation
*/
virtual std::unique_ptr<YmlTree> CreateUniqueYmlTree(IXmlItemBuilder* itemBuilder = nullptr) const;

/**
* @brief save active project into cprj file
* @param file cprj file name
Expand Down Expand Up @@ -274,7 +295,19 @@ class RteKernel
bool GetLocalPacksUrls(const std::string& rtePath, std::list<std::string>& urls) const;
XMLTreeElement* ParseLocalRepositoryIdx(const std::string& rtePath) const;

virtual XMLTree* CreateXmlTree(IXmlItemBuilder* itemBuilder) const { return nullptr; } // creates new XMLTree implementation
/**
* @brief create an XMLTree object to parse XML files
* @param itemBuilder pointer to IXmlItemBuilder item factory
* @return XMLTree-derived pointer which is nullptr in the default implementation
*/
virtual XMLTree* CreateXmlTree(IXmlItemBuilder* itemBuilder) const { return nullptr; }

/**
* @brief create a YmlTree object to parse YAML files
* @param itemBuilder pointer to IXmlItemBuilder item factory
* @return YmlTree-derived pointer
*/
virtual YmlTree* CreateYmlTree(IXmlItemBuilder* itemBuilder) const;

protected:
/**
Expand All @@ -288,5 +321,6 @@ class RteKernel
RteCallback* m_rteCallback;
XmlItem m_toolInfo;
std::string m_cmsisPackRoot;
std::string m_cmsisToolboxDir;
};
#endif // RteKernel_H
12 changes: 12 additions & 0 deletions libs/rtemodel/src/RteKernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "RteUtils.h"
#include "RteFsUtils.h"
#include "XmlFormatter.h"
#include "YmlFormatter.h"

using namespace std;

Expand Down Expand Up @@ -560,4 +561,15 @@ unique_ptr<XMLTree> RteKernel::CreateUniqueXmlTree(IXmlItemBuilder* itemBuilder)
return xmlTree;
}

unique_ptr<YmlTree> RteKernel::CreateUniqueYmlTree(IXmlItemBuilder* itemBuilder) const
{
unique_ptr<YmlTree> ymlTree(CreateYmlTree(itemBuilder));
return ymlTree;
}

YmlTree* RteKernel::CreateYmlTree(IXmlItemBuilder* itemBuilder) const
{
return new YmlTree(itemBuilder);
}

// End of RteKernel.cpp
15 changes: 15 additions & 0 deletions libs/rteutils/include/ISchemaChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define ISCHEMA_CHECKER_H

#include "RteError.h"
#include "RteUtils.h"
#include <list>

/**
Expand All @@ -29,6 +30,20 @@ class ISchemaChecker
*/
virtual bool ValidateFile(const std::string& file, const std::string& schemaFile) = 0;

/**
* @brief Validates a file against schema obtained by FindSchema() method
* @param fileName file to validate
* @return true if successful
*/
virtual bool Validate(const std::string& file) { return ValidateFile(file, FindSchema(file)); }

/**
* @brief Finds schema for given file to validate
* @param fileName file to validate
* @return schema file name if found, empty string otherwise
*/
virtual std::string FindSchema(const std::string& file) const { return RteUtils::EMPTY_STRING; }

/**
* @brief returns collection of error messages found during last run of validate
*/
Expand Down
9 changes: 7 additions & 2 deletions libs/xmltree/include/XMLTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
/******************************************************************************/
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -347,7 +347,7 @@ class XMLTree : public XMLTreeElement
* @param xmlString buffer containing XML formatted text
* @return true if buffer is successfully parsed
*/
bool ParseXmlString(const std::string& xmlString);
bool ParseString(const std::string& xmlString);

/**
* @brief parse either file or buffer containing XML formatted text
Expand Down Expand Up @@ -401,6 +401,11 @@ class XMLTree : public XMLTreeElement
protected:
// parses file or XML sting (fileName can be used to associate parsed document with the file)
bool DoParse(const std::string& fileName, const std::string& xmlString);
/**
* @brief creates underling parser interface
* @return XMLTreeParserInterface;
*/
virtual XMLTreeParserInterface* CreateParserInterface() = 0;

protected:
std::string m_schemaFile; // schema file with path
Expand Down
38 changes: 25 additions & 13 deletions libs/xmltree/src/XMLTree.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************/
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -120,7 +120,13 @@ void XMLTree::SetIgnoreTags(const set<string>& ignoreTags)

bool XMLTree::Init()
{
return m_p->Init();
if(!m_p) {
m_p = CreateParserInterface();
}
if(m_p) {
return m_p->Init();
}
return false;
}


Expand Down Expand Up @@ -173,30 +179,36 @@ bool XMLTree::ParseAll()

bool XMLTree::ParseFile(const string& fileName)
{
if(m_schemaChecker && !m_schemaFile.empty()) {
m_schemaChecker->ClearErrors();
if(!m_schemaChecker->ValidateFile(fileName, m_schemaFile)) {
for(auto& err : m_schemaChecker->GetErrors()) {
if(err.m_severity == RteError::SevERROR) {
m_nErrors++;
} else if(err.m_severity == RteError::SevWARNING) {
m_nWarnings++;
if(m_schemaChecker ) {
const string& schema = m_schemaFile.empty() ? m_schemaChecker->FindSchema(fileName) : m_schemaFile;
if(!schema.empty()) {
m_schemaChecker->ClearErrors();
if(!m_schemaChecker->ValidateFile(fileName, m_schemaFile)) {
for(auto& err : m_schemaChecker->GetErrors()) {
if(err.m_severity == RteError::SevERROR) {
m_nErrors++;
} else if(err.m_severity == RteError::SevWARNING) {
m_nWarnings++;
}
m_errorStrings.push_back(err.ToString());
}
m_errorStrings.push_back(err.ToString());
return false;
}
return false;
}
}
return Parse(fileName, EMPTY_STRING);
}


bool XMLTree::ParseXmlString(const std::string& xmlString) {
bool XMLTree::ParseString(const std::string& xmlString) {
return Parse(EMPTY_STRING, xmlString);
}

bool XMLTree::Parse(const std::string& fileName, const std::string& xmlString)
{
if(!Init()) {
return false;
}
m_p->Clear();
return DoParse(fileName, xmlString);
}
Expand Down
8 changes: 7 additions & 1 deletion libs/xmltree/test/src/XmlFormatterTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -14,6 +14,8 @@ class XMLTreeDummy : public XMLTree
{
public:
XMLTreeDummy(IXmlItemBuilder* itemBuilder = NULL) : XMLTree(itemBuilder) {}
protected:
XMLTreeParserInterface* CreateParserInterface() override { return nullptr; }
};

static const string xmlSchemaFile = "CPRJ.xsd";
Expand Down Expand Up @@ -84,6 +86,10 @@ static const string jsonResExpected = string("{\n") +
"}\n";


TEST(XMLTree, CannotParseWithoutInterface) {
auto tree = make_unique<XMLTreeDummy>();
EXPECT_FALSE(tree->ParseString(xmlResExpected));
}


TEST(XMLFormatterTest, HeaderAndContentTest)
Expand Down
14 changes: 10 additions & 4 deletions libs/xmltreeslim/include/XMLTreeSlim.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
/******************************************************************************/
/** @file XmlTreeSlim.h
* @brief A simple XML interface that reads data into a tree structure
* @uses XmlReader
*/
* @uses XmlReader
*/
/******************************************************************************/
/*
* The reader should be kept semantics-free:
* no special processing based on tag, attribute or value string
*/
/******************************************************************************/
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
*
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
/******************************************************************************/
Expand All @@ -35,6 +35,12 @@ class XMLTreeSlim : public XMLTree
* @param bIgnoreAttributePrefixes true to ignore attribute prefixes otherwise false
*/
XMLTreeSlim(IXmlItemBuilder* itemBuilder = NULL, bool bRedirectErrLog = false, bool bIgnoreAttributePrefixes = true);

protected:
XMLTreeParserInterface* CreateParserInterface() override;
bool m_bRedirectErrLog;
bool m_bIgnoreAttributePrefixes;

};

#endif // XMLTreeSlim_H
Expand Down
6 changes: 3 additions & 3 deletions libs/xmltreeslim/include/XMLTreeSlimString.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
*/
/******************************************************************************/
/*
* Copyright (c) 2020-2021 Arm Limited. All rights reserved.
* Copyright (c) 2020-2024 Arm Limited. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
/******************************************************************************/

#include "XMLTree.h"
#include "XMLTreeSlim.h"

class IXmlItemBuilder;

/**
* @brief XML interface that reads data into a tree structure
*/
class XMLTreeSlimString : public XMLTree
class XMLTreeSlimString : public XMLTreeSlim
{
public:
/**
Expand Down
Loading

0 comments on commit 9f286aa

Please sign in to comment.