Skip to content

Commit

Permalink
Add fields compare in policy checker (WIP)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Gervais <[email protected]>
  • Loading branch information
g-maxime committed Apr 19, 2024
1 parent 4d44b94 commit 73d0eed
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 55 deletions.
226 changes: 188 additions & 38 deletions Source/Checker/Checker.cpp

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions Source/Checker/Checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//---------------------------------------------------------------------------
#ifndef TFSXML_NAMESPACE
#define TFSXML_NAMESPACE 1
#endif // TFSXML_NAMESPACEs
#endif // TFSXML_NAMESPACE
#include "ThirdParty/tfsxml/tfsxml.h"
#include "Path.h"

Expand Down Expand Up @@ -73,11 +73,21 @@ class PolicyChecker

class RuleElement : public Element {
public:
struct Source
{
std::vector<PathElement> path;
std::vector<std::string> values;
std::string scope;
std::string field;
std::string tracktype;
std::string occurrence;
};

RuleElement();
~RuleElement();

void reset();
bool compare(const std::string& value);
bool compare(const std::string& v1, const std::string& v2);

virtual void resolve();
virtual Result result();
Expand All @@ -95,6 +105,7 @@ class PolicyChecker
std::string requested;
std::vector<std::string> values;
std::vector<std::string> failing_values;
Source* source;
size_t tracks;

private:
Expand Down
5 changes: 2 additions & 3 deletions Source/Checker/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ std::vector<PathElement> parse_path(const std::string& xpath)
//---------------------------------------------------------------------------
bool path_is_matching(tfsxml_string& tfsxml_priv, tfsxml_string& node, PathElement path, size_t& occurrence)
{
tfsxml_string tfsxml_priv_local = tfsxml_priv;
tfsxml_string value;

// compare names
Expand All @@ -125,10 +126,8 @@ bool path_is_matching(tfsxml_string& tfsxml_priv, tfsxml_string& node, PathEleme

tfsxml_string attribute_name;
tfsxml_string attribute_value;
tfsxml_string tfsxml_priv_save=tfsxml_priv;
while (!tfsxml_attr(&tfsxml_priv, &attribute_name, &attribute_value))
while (!tfsxml_attr(&tfsxml_priv_local, &attribute_name, &attribute_value))
attributes[std::string(attribute_name.buf, attribute_name.len)]=std::string(attribute_value.buf, attribute_value.len);
tfsxml_priv=tfsxml_priv_save;

std::map<std::string, std::string>::iterator it=path.attributes.begin();
while (it!=path.attributes.end())
Expand Down
14 changes: 12 additions & 2 deletions Source/Common/JS_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,18 @@ int JsTree::rule_to_js_tree(MediaConchLib::XSLT_Policy_Rule* rule, std::string&
ss << "\",\"level\":\"" << unified_json_value(rule->level);
ss << "\",\"occurrence\":\"" << unified_json_value(rule->occurrence);
ss << "\",\"ope\":\"" << unified_json_value(rule->ope);
ss << "\",\"value\":\"" << unified_json_value(rule->value);
ss <<"\"}}";
if (rule->source)
{
ss << "\",\"source\":{";
ss << "\"tracktype\":\"" << unified_json_value(rule->source->tracktype);
ss << "\",\"field\":\"" << unified_json_value(rule->source->field);
ss << "\",\"scope\":\"" << unified_json_value(rule->source->scope);
ss << "\",\"occurrence\":\"" << unified_json_value(rule->source->occurrence);
ss <<"\"}";
}
else
ss << "\",\"value\":\"" << unified_json_value(rule->value) << "\"";
ss <<"}}";

json = ss.str();
return 0;
Expand Down
15 changes: 14 additions & 1 deletion Source/Common/MediaConchLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,20 @@ std::string MediaConchLib::XSLT_Policy_Rule::to_str() const
out << ",occurrence:\"" << occurrence << "\"";
if (ope.size())
out << ",\"ope\":\"" << ope << "\"";
if (value.size())
if (source)
{
out << ",source:{";
if (source->tracktype.size())
out << ",\"tracktype\":\"" << source->tracktype << "\"";
if (source->field.size())
out << ",\"field\":\"" << source->field << "\"";
if (source->scope.size())
out << ",\"scope\":\"" << source->scope << "\"";
if (source->occurrence.size())
out << ",occurrence:\"" << source->occurrence << "\"";
out << "}";
}
else if (value.size())
out << ",\"value\":\"" << value << "\"";
out << "}";
return out.str();
Expand Down
14 changes: 13 additions & 1 deletion Source/Common/MediaConchLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,18 @@ class MediaConchLib

struct XSLT_Policy_Rule
{
XSLT_Policy_Rule() : id(-1) {}
XSLT_Policy_Rule() : id(-1), source(NULL) {}
~XSLT_Policy_Rule() {
if (source)
delete source;
}

struct Source {
std::string tracktype;
std::string field;
std::string scope;
std::string occurrence;
};

int id;
std::string name;
Expand All @@ -186,6 +197,7 @@ class MediaConchLib
std::string occurrence;
std::string ope;
std::string value;
Source* source;
std::string to_str() const;
};

Expand Down
129 changes: 121 additions & 8 deletions Source/Common/XsltPolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ size_t XsltPolicyRule::rule_id = 0;
XsltPolicyRule::XsltPolicyRule() : XsltPolicyNode()
{
kind = XSLT_POLICY_RULE;
source = NULL;

}

//---------------------------------------------------------------------------
XsltPolicyRule::~XsltPolicyRule()
{
if (source)
delete source;
}

//---------------------------------------------------------------------------
Expand All @@ -89,6 +93,17 @@ XsltPolicyRule::XsltPolicyRule(const XsltPolicyRule* r) : XsltPolicyNode(r)
this->level = r->level;
this->occurrence = r->occurrence;
this->value = r->value;
if (r->source)
{
if (this->source)
delete this->source;

this->source = new Source;
this->source->track_type = r->source->track_type;
this->source->field = r->source->field;
this->source->scope = r->source->scope;
this->source->occurrence = r->source->occurrence;
}
}

//---------------------------------------------------------------------------
Expand All @@ -105,6 +120,17 @@ XsltPolicyRule::XsltPolicyRule(const XsltPolicyRule& r) : XsltPolicyNode(r)
this->level = r.level;
this->occurrence = r.occurrence;
this->value = r.value;
if (r.source)
{
if (this->source)
delete this->source;

this->source = new Source;
this->source->track_type = r.source->track_type;
this->source->field = r.source->field;
this->source->scope = r.source->scope;
this->source->occurrence = r.source->occurrence;
}
}

//---------------------------------------------------------------------------
Expand All @@ -118,6 +144,17 @@ int XsltPolicyRule::edit_policy_rule(const XsltPolicyRule* rule, std::string&)
this->level = rule->level;
this->occurrence = rule->occurrence;
this->value = rule->value;
if (rule->source)
{
if (this->source)
delete this->source;

this->source = new Source;
this->source->track_type = rule->source->track_type;
this->source->field = rule->source->field;
this->source->scope = rule->source->scope;
this->source->occurrence = rule->source->occurrence;
}

return 0;
}
Expand Down Expand Up @@ -274,7 +311,7 @@ int XsltPolicy::run_over_siblings_nodes(xmlNodePtr node, bool is_root, XsltPolic
{
switch (node->type)
{
case XML_TEXT_NODE:
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
continue;
default:;
Expand Down Expand Up @@ -482,14 +519,62 @@ int XsltPolicy::parse_policy_rule(xmlNodePtr node, bool is_root, XsltPolicy* cur
xmlFree(occurrence);
}

//Get value
xmlChar *value = xmlNodeGetContent(node);
if (value)
//Get source
for (xmlNodePtr child = node->children; child; child = child->next)
{
r->value = std::string((const char*)value);
xmlFree(value);
if (child->type == XML_ELEMENT_NODE && child->name && std::string((const char*)child->name) == "source")
{
if (r->source)
delete r->source;

r->source = new XsltPolicyRule::Source();

//Get source trackType
xmlChar *track_type = xmlGetNoNsProp(child, (const unsigned char*)"tracktype");
if (track_type)
{
r->source->track_type = std::string((const char*)track_type);
xmlFree(track_type);
}

//Get source field
xmlChar *field = xmlGetNoNsProp(child, (const unsigned char*)"value");
if (field)
{
r->source->field = std::string((const char*)field);
xmlFree(field);
}

//Get source scope
xmlChar *scope = xmlGetNoNsProp(child, (const unsigned char*)"scope");
if (scope)
{
r->source->scope = std::string((const char*)scope);
xmlFree(scope);
}

//Get source occurrence
xmlChar *occurrence = xmlGetNoNsProp(child, (const unsigned char*)"occurrence");
if (occurrence)
{
r->source->occurrence = std::string((const char*)occurrence);
xmlFree(occurrence);
}
break;
}
}

//Get value
if (!r->source)
{
xmlChar *value = xmlNodeGetContent(node);
if (value)
{
r->value = std::string((const char*)value);
xmlFree(value);
}
}

return 0;
}

Expand Down Expand Up @@ -615,8 +700,36 @@ int XsltPolicy::create_node_rule_child(xmlNodePtr& node, XsltPolicyRule *current
if (current->level.size())
xmlNewProp(node, (const xmlChar *)"level", (const xmlChar *)current->level.c_str());

//value
if (current->value.size())
//source or value
if (current->source)
{
xmlNodePtr child = xmlNewNode(NULL, (const xmlChar *)"source");
if (!child)
{
error = "Cannot create the source children";
return -1;
}

//field
if (current->source->field.size())
xmlNewProp(child, (const xmlChar *)"value", (const xmlChar *)current->source->field.c_str());

//track type
if (current->source->track_type.size())
xmlNewProp(child, (const xmlChar *)"tracktype", (const xmlChar *)current->source->track_type.c_str());


//scope
if (current->source->scope.size())
xmlNewProp(child, (const xmlChar *)"scope", (const xmlChar *)current->source->scope.c_str());

//occurrence
if (current->source->occurrence.size())
xmlNewProp(child, (const xmlChar *)"occurrence", (const xmlChar *)current->occurrence.c_str());

xmlAddChild(node, child);
}
else if (current->value.size())
xmlNodeSetContent(node, (const xmlChar*)current->value.c_str());

return 0;
Expand Down
8 changes: 8 additions & 0 deletions Source/Common/XsltPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class XsltPolicyNode
class XsltPolicyRule : public XsltPolicyNode
{
public:
struct Source {
std::string track_type;
std::string field;
std::string scope;
std::string occurrence;
};

XsltPolicyRule();
virtual ~XsltPolicyRule();
XsltPolicyRule(const XsltPolicyRule*);
Expand All @@ -88,6 +95,7 @@ class XsltPolicyRule : public XsltPolicyNode
std::string scope;
std::string level;
std::string occurrence;
Source* source;
std::string value;

static size_t rule_id;
Expand Down

0 comments on commit 73d0eed

Please sign in to comment.