Fabric 0.2.0
Loading...
Searching...
No Matches
xml_helper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <vector>
5#include <algorithm>
6#include <string>
7#include <tinyxml2.h>
8
11
12namespace fabric
13{
22bool search(std::vector<std::string> list, std::string value)
23{
24 return (std::find(list.begin(), list.end(), value) != list.end());
25}
26
36tinyxml2::XMLElement* extract_plan(tinyxml2::XMLDocument& document)
37{
38 std::string plan_tag(document.FirstChildElement()->Name());
39
40 if (plan_tag == "Plan")
41 return document.FirstChildElement("Plan")->FirstChildElement();
42 else
43 return nullptr;
44}
45
54inline bool convert_to_string(tinyxml2::XMLElement* element, std::string& parameters)
55{
56 if (element)
57 {
58 tinyxml2::XMLPrinter printer;
59 element->Accept(&printer);
60 parameters = printer.CStr();
61 return true;
62 }
63 else
64 {
65 parameters = "";
66 return false;
67 }
68}
69
77void convert_to_string(tinyxml2::XMLDocument& document_xml, std::string& document_string)
78{
79 tinyxml2::XMLPrinter printer;
80 document_xml.Print(&printer);
81 document_string = printer.CStr();
82}
83
95bool check_syntax(tinyxml2::XMLElement* element, std::vector<std::string>& control_list, std::vector<std::string>& rejected, std::string& error)
96{
97 const char* type = nullptr;
98 const char* interface = nullptr;
99 const char* provider = nullptr;
100
101 std::string elementTag(element->Name());
102
103 std::string parameter_string;
104 convert_to_string(element, parameter_string);
105
106 bool returnValue = true;
107
108 std::string typetag = "";
109 std::string interfacetag = "";
110 std::string providertag = "";
111
112 bool hasChildren = !element->NoChildren();
113 bool hasSiblings = (element->NextSiblingElement() != nullptr);
114
115 if (elementTag == "Control")
116 {
117 element->QueryStringAttribute("type", &type);
118
119 if (type)
120 {
121 typetag = type;
122
123 if (!search(control_list, typetag))
124 {
125 error = "Control tag '" + typetag + "' not available in the valid list";
126 rejected.push_back(parameter_string);
127 return false;
128 }
129 }
130 else
131 {
132 error = "Control tag missing 'type' attribute: " + parameter_string;
133 rejected.push_back(parameter_string);
134 return false;
135 }
136
137 if (hasChildren)
138 returnValue &= check_syntax(element->FirstChildElement(), control_list, rejected, error);
139
140 if (hasSiblings)
141 returnValue &= check_syntax(element->NextSiblingElement(), control_list, rejected, error);
142 }
143 else if (elementTag == "Runner")
144 {
145 element->QueryStringAttribute("interface", &interface);
146 element->QueryStringAttribute("provider", &provider);
147
148 if (not interface)
149 {
150 error = "Runner tag missing 'interface' attribute: " + parameter_string;
151 rejected.push_back(parameter_string);
152 return false;
153 }
154
155 if (not provider)
156 {
157 error = "Runner tag missing 'provider' attribute: " + parameter_string;
158 rejected.push_back(parameter_string);
159 return false;
160 }
161
162 if (hasSiblings)
163 returnValue &= check_syntax(element->NextSiblingElement(), control_list, rejected, error);
164 }
165 else
166 {
167 error = "XML element is not valid :" + parameter_string;
168 rejected.push_back(parameter_string);
169 return false;
170 }
171
172 return returnValue;
173}
174
175} // namespace fabric
Definition parser_base.hpp:9
tinyxml2::XMLElement * extract_plan(tinyxml2::XMLDocument &document)
Extract the <Plan> element from the XML document.
Definition xml_helper.hpp:36
bool check_syntax(tinyxml2::XMLElement *element, std::vector< std::string > &control_list, std::vector< std::string > &rejected, std::string &error)
check the plan to make sure all control and runner XML elements are valid with minimal required attri...
Definition xml_helper.hpp:95
bool search(std::vector< std::string > list, std::string value)
search a string in a vector of strings
Definition xml_helper.hpp:22
bool convert_to_string(tinyxml2::XMLElement *element, std::string &parameters)
convert XMLElement to std::string
Definition xml_helper.hpp:54