Dev Essential  1.6.3
Loading...
Searching...
No Matches
datamodel_xml_configurationfromxml_base.h
Go to the documentation of this file.
1
20
21#ifndef MAPPING_CONFIGURATION_FROM_XML_BASE_INCLUDED
22#define MAPPING_CONFIGURATION_FROM_XML_BASE_INCLUDED
23
27
28#include <map>
29#include <sstream>
30
31namespace ddl {
32namespace mapping {
47template <typename DomNodeType>
66 template <typename T>
67 static utility::Optional<T> getAttribute(const DomNodeType& dom_node,
68 const std::string& attribute_name,
69 const utility::Optional<T>& default_value = {},
70 bool is_mandatory = false,
71 const std::string& parse_error_message = {},
72 const std::string& mapping_reading_version = {})
73 {
74 auto value = dom_node.getAttribute(attribute_name);
75 if (value.empty()) {
76 if (is_mandatory) {
77 throw utility::Error(std::string(mapping_reading_version) + "::getAttribute",
78 {"...", attribute_name},
79 parse_error_message);
80 }
81 return default_value;
82 }
83 else {
84 return utility::toType(value, default_value);
85 }
86 }
87
104 template <typename T>
105 static utility::Optional<T> getData(const DomNodeType& dom_node,
106 const utility::Optional<T>& default_value = {},
107 bool is_mandatory = false,
108 const std::string& parse_error_message = "",
109 const std::string& mapping_reading_version = {})
110 {
111 const std::string value = dom_node.getData();
112 if (value.empty()) {
113 if (is_mandatory) {
114 throw utility::Error(std::string(mapping_reading_version) + "::getData ",
115 {dom_node.getName(), "..."},
116 parse_error_message);
117 }
118 return default_value;
119 }
120 else {
121 return utility::toType(value, default_value);
122 }
123 }
124
142 template <typename T>
143 static utility::Optional<T> getDataSubNode(const DomNodeType& dom_node,
144 const std::string& sub_node_name,
145 const utility::Optional<T>& default_value = {},
146 bool is_mandatory = false,
147 const std::string& parse_error_message = {},
148 const std::string& mapping_reading_version = {})
149 {
150 DomNodeType sub_node;
151 std::string value = {};
152 if (dom_node.findNode(sub_node_name, sub_node)) {
153 return getData<T>(sub_node, default_value, is_mandatory, parse_error_message);
154 }
155 else {
156 if (is_mandatory) {
157 throw Error(std::string(mapping_reading_version) + "getDataSubNode",
158 {dom_node.getName(), sub_node_name},
159 parse_error_message);
160 }
161 return default_value;
162 }
163 }
164
170 static void attributesToString(const std::map<std::string, std::string>& attributes,
171 std::stringstream& property_stream)
172 {
173 if (!attributes.empty()) {
174 for (const auto& current: attributes) {
175 property_stream << " " << current.first << "=\"" << current.second << "\"";
176 }
177 }
178 }
179
186 static void nodeToString(const DomNodeType& dom_node,
187 std::stringstream& property_stream,
188 size_t level = 0)
189 {
190 const std::string tab(level, ' ');
191 const auto data = dom_node.getData();
192 if (!data.empty() && level != 0) {
193 property_stream << tab << "<" << dom_node.getName();
194 attributesToString(dom_node.getAttributes(), property_stream);
195 property_stream << ">";
196 property_stream << data;
197 property_stream << tab << "</" << dom_node.getName() << ">" << std::endl;
198 }
199 else {
200 const auto nodes = dom_node.getChildren();
201 if (nodes.empty() && level != 0) {
202 property_stream << tab << "<" << dom_node.getName();
203 attributesToString(dom_node.getAttributes(), property_stream);
204 property_stream << "/>" << std::endl;
205 }
206 else {
207 if (level != 0) {
208 property_stream << tab << "<" << dom_node.getName();
209 attributesToString(dom_node.getAttributes(), property_stream);
210 property_stream << ">" << std::endl;
211 }
212 for (const auto& current: nodes) {
213 nodeToString(current, property_stream, level + 1);
214 }
215 if (level != 0) {
216 property_stream << tab << "</" << dom_node.getName() << ">" << std::endl;
217 }
218 }
219 }
220 }
221
229 template <typename PropertiesType>
230 static void setDataOrSubNodesAsProperty(const DomNodeType& dom_node,
231 const std::string& property_name,
232 PropertiesType& properties_target)
233 {
234 auto data = dom_node.getData();
235 if (!data.empty()) {
236 utility::replaceInString(data, "&lt;", "<");
237 utility::replaceInString(data, "&gt;", ">");
238 properties_target.emplace({property_name, data});
239 }
240 else {
241 std::stringstream prop_stream;
242 nodeToString(dom_node, prop_stream);
243 properties_target.emplace({property_name, prop_stream.str()});
244 }
245 }
246};
247
248} // namespace mapping
249} // namespace ddl
250
251#endif // MAPPING_CONFIGURATION_FROM_XML_BASE_INCLUDED
definition of the mapping namespace
Definition ddl.h:50
ddl::utility::Error Error
mapping error definition
Definition mapping_configuration_types.h:35
void replaceInString(std::string &string_data, const std::string &to_replace, const std::string &replace_with)
In place replacement helper function.
::ddl::dd::Error Error
Exception helper class to collect information while parsing, adding DD Objects or other failed operat...
Definition ddl_error.h:110
Optional< int > toType(const std::string &from_string, const Optional< int > &default_value={})
specialized conversion to the Optional<int>
definition of the ddl namespace
Definition codec.h:21
An optional template as long as the std::optional is not available here.
Definition access_optional.h:31
Factory helper to create a MappingConfiguration out of a XML based description. This factory uses a t...
Definition datamodel_xml_configurationfromxml_base.h:48
static utility::Optional< T > getDataSubNode(const DomNodeType &dom_node, const std::string &sub_node_name, const utility::Optional< T > &default_value={}, bool is_mandatory=false, const std::string &parse_error_message={}, const std::string &mapping_reading_version={})
Get the data content of a subnode of dom_node. Return a valid Optional<T> if set, otherwise return de...
Definition datamodel_xml_configurationfromxml_base.h:143
static void nodeToString(const DomNodeType &dom_node, std::stringstream &property_stream, size_t level=0)
Helper function to serialize a xml node to a string.
Definition datamodel_xml_configurationfromxml_base.h:186
static void attributesToString(const std::map< std::string, std::string > &attributes, std::stringstream &property_stream)
Helper function to serialize xml attributes to a string.
Definition datamodel_xml_configurationfromxml_base.h:170
static utility::Optional< T > getAttribute(const DomNodeType &dom_node, const std::string &attribute_name, const utility::Optional< T > &default_value={}, bool is_mandatory=false, const std::string &parse_error_message={}, const std::string &mapping_reading_version={})
Get the Attribute from the dom_node. Return a valid Optional<T> if set, otherwise return default_valu...
Definition datamodel_xml_configurationfromxml_base.h:67
static utility::Optional< T > getData(const DomNodeType &dom_node, const utility::Optional< T > &default_value={}, bool is_mandatory=false, const std::string &parse_error_message="", const std::string &mapping_reading_version={})
Get the data content of a subnode of dom_node. Return a valid Optional<T> if set, otherwise return de...
Definition datamodel_xml_configurationfromxml_base.h:105
static void setDataOrSubNodesAsProperty(const DomNodeType &dom_node, const std::string &property_name, PropertiesType &properties_target)
Set the Data Or Sub Nodes As Property.
Definition datamodel_xml_configurationfromxml_base.h:230