Skip to content

Commit

Permalink
Add cMetadata classe to result format library
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <[email protected]>
  • Loading branch information
romanodanilo committed Jun 6, 2024
1 parent c2ab2bd commit a934870
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/checker_bundle_example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void RunChecks(const cParameterContainer &inputParams)
// Create a test checker with RuleUID and metadata
cChecker *pExampleRuleUIDChecker = pExampleCheckerBundle->CreateChecker("exampleRuleUIDChecker", "This is a description of ruleUID checker");
pExampleRuleUIDChecker->AddRule(new cRule("test.com::qwerty.qwerty"));
pExampleRuleUIDChecker->AddMetadata(new cMetadata("run date", "2024/06/06", "Date in which the checker was executed"));
pExampleRuleUIDChecker->AddMetadata(new cMetadata("reference project", "project01", "Name of the project that created the checker"));

// Lets add a summary for the checker bundle
unsigned int issueCount = pExampleCheckerBundle->GetIssueCount();
Expand Down
16 changes: 16 additions & 0 deletions include/common/result_format/c_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "../xml/util_xerces.h"
#include "c_issue.h"
#include "c_rule.h"
#include "c_metadata.h"
#include "c_parameter_container.h"

#include <list>
Expand All @@ -22,6 +23,7 @@
class cCheckerBundle;
class cIssue;
class cRule;
class cMetadata;

/*
* Definition of a basic checker
Expand All @@ -31,6 +33,7 @@ class cChecker
friend class cCheckerBundle;
friend class cIssue;
friend class cRule;
friend class cMetadata;

public:
static const XMLCh *TAG_CHECKER;
Expand Down Expand Up @@ -65,6 +68,12 @@ class cChecker
*/
cRule *AddRule(cRule *const ruleToAdd);

/*
* Adds an metadata info to the checker results
* \param instance if the result
*/
cMetadata *AddMetadata(cMetadata *const metadataToAdd);

// Clears all issues from the container
void Clear();

Expand All @@ -83,6 +92,9 @@ class cChecker
// Counts the Rules
unsigned int GetRuleCount();

// Counts the Rules
unsigned int GetMetadataCount();

// Updates the summary
void UpdateSummary();

Expand All @@ -92,6 +104,9 @@ class cChecker
// Returns the rules
std::list<cRule *> GetRules();

// Returns the rules
std::list<cMetadata *> GetMetadata();

// Processes every issue and does a defined processing
void DoProcessing(void (*funcIzteratorPtr)(cIssue *));

Expand Down Expand Up @@ -189,6 +204,7 @@ class cChecker

std::list<cIssue *> m_Issues;
std::list<cRule *> m_Rules;
std::list<cMetadata *> m_Metadata;
cParameterContainer m_Params;
};

Expand Down
66 changes: 66 additions & 0 deletions include/common/result_format/c_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2023 CARIAD SE.
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef cMetadata_h__
#define cMetadata_h__

#include "string"
#include "../xml/util_xerces.h"

class cChecker;


class cMetadata
{

public:
static const XMLCh *TAG_NAME;
static const XMLCh *ATTR_KEY;
static const XMLCh *ATTR_VALUE;
static const XMLCh *ATTR_DESCRIPTION;

/*
* Creates a new instance of cMetadata
* \param m_Key: metadata key
* \param m_Value: metadata value
* \param m_Description: metadata description
* \param description: Additional description
*/
cMetadata(const std::string& input_key, const std::string& input_value,const std::string& input_description):m_Key(input_key), m_Value(input_value), m_Description(input_description)
{
}

// Serialize this information
virtual XERCES_CPP_NAMESPACE::DOMElement *WriteXML(XERCES_CPP_NAMESPACE::DOMDocument *p_resultDocument);

// Unserialize this information
static cMetadata *ParseFromXML(XERCES_CPP_NAMESPACE::DOMNode *pXMLNode,
XERCES_CPP_NAMESPACE::DOMElement *pXMLElement, cChecker *checker);


// Assigns an issue to a checker
void AssignChecker(cChecker *checkerToAssign);

// Returns the Key
std::string GetKey() const;
// Returns the Value
std::string GetValue() const;
// Returns the Description
std::string GetDescription() const;


protected:
std::string m_Key, m_Value, m_Description;
cChecker *m_Checker;

private:
cMetadata();
cMetadata(const cMetadata &);
};

#endif
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ add_library(qc4openx-common STATIC
src/config_format/c_configuration_report_module.cpp
src/xml/c_x_path_evaluator.cpp
src/result_format/c_rule.cpp
src/result_format/c_metadata.cpp
)

target_include_directories(qc4openx-common PUBLIC ${PROJECT_SOURCE_DIR}/include
Expand Down
41 changes: 41 additions & 0 deletions src/common/src/result_format/c_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ DOMElement *cChecker::WriteXML(DOMDocument *pResultDocument)
pCheckerNode->appendChild(p_DataElement);
}

// Add Metadatas und cCheckerSummaries
for (std::list<cMetadata *>::const_iterator it = m_Metadata.begin(); it != m_Metadata.end(); ++it)
{
DOMElement *p_DataElement = (*it)->WriteXML(pResultDocument);

if (nullptr != p_DataElement)
pCheckerNode->appendChild(p_DataElement);
}


return pCheckerNode;
}

Expand Down Expand Up @@ -194,11 +204,32 @@ cRule *cChecker::AddRule(cRule *const ruleToAdd)
return nullptr;
}

cMetadata *cChecker::AddMetadata(cMetadata *const metadataToAdd)
{
if (nullptr == m_Bundle)
{
// use runtime_error instead of exception for linux
throw std::runtime_error("Create the checker by using CheckerBundle::CreateChecker()!");
}
else
{
metadataToAdd->AssignChecker(this);
m_Metadata.push_back(metadataToAdd);

return metadataToAdd;
}
return nullptr;
}

unsigned int cChecker::GetRuleCount()
{
return (unsigned int)m_Rules.size();
}

unsigned int cChecker::GetMetadataCount()
{
return (unsigned int)m_Metadata.size();
}
// Deletes all issues
void cChecker::Clear()
{
Expand All @@ -211,6 +242,11 @@ void cChecker::Clear()
delete *it;

m_Rules.clear();

for (std::list<cMetadata *>::iterator it = m_Metadata.begin(); it != m_Metadata.end(); it++)
delete *it;

m_Metadata.clear();
}

// Counts the Issues
Expand All @@ -230,6 +266,11 @@ std::list<cRule *> cChecker::GetRules()
return m_Rules;
}

std::list<cMetadata *> cChecker::GetMetadata()
{
return m_Metadata;
}

// Assigns a specific bundle to the checker
void cChecker::AssignCheckerBundle(cCheckerBundle *myBundle)
{
Expand Down
67 changes: 67 additions & 0 deletions src/common/src/result_format/c_metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2023 CARIAD SE.
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "common/result_format/c_metadata.h"

XERCES_CPP_NAMESPACE_USE

const XMLCh *cMetadata::TAG_NAME = CONST_XMLCH("Metadata");
const XMLCh *cMetadata::ATTR_KEY = CONST_XMLCH("key");
const XMLCh *cMetadata::ATTR_VALUE = CONST_XMLCH("value");
const XMLCh *cMetadata::ATTR_DESCRIPTION = CONST_XMLCH("description");

DOMElement *cMetadata::WriteXML(DOMDocument *p_resultDocument)
{

DOMElement *p_DataElement = p_resultDocument->createElement(TAG_NAME);
XMLCh *pKey = XMLString::transcode(m_Key.c_str());
XMLCh *pValue = XMLString::transcode(m_Value.c_str());
XMLCh *pDescription = XMLString::transcode(m_Description.c_str());
p_DataElement->setAttribute(ATTR_KEY, pKey);
p_DataElement->setAttribute(ATTR_VALUE, pValue);
p_DataElement->setAttribute(ATTR_DESCRIPTION, pDescription);

XMLString::release(&pKey);
XMLString::release(&pValue);
XMLString::release(&pDescription);

return p_DataElement;
}

cMetadata *cMetadata::ParseFromXML(DOMNode *, DOMElement *pXMLElement, cChecker *checker)
{
std::string strKey = XMLString::transcode(pXMLElement->getAttribute(ATTR_KEY));
std::string strValue = XMLString::transcode(pXMLElement->getAttribute(ATTR_VALUE));
std::string strDescription = XMLString::transcode(pXMLElement->getAttribute(ATTR_DESCRIPTION));

cMetadata *result = new cMetadata(strKey, strValue, strDescription);
result->AssignChecker(checker);

return result;
}

// Returns the key
std::string cMetadata::GetKey() const
{
return m_Key;
}

// Returns the value
std::string cMetadata::GetValue() const
{
return m_Value;
}

// Returns the description
std::string cMetadata::GetDescription() const
{
return m_Description;
}
void cMetadata::AssignChecker(cChecker *checkerToAssign)
{
m_Checker = checkerToAssign;
}

0 comments on commit a934870

Please sign in to comment.