Loading...
Searching...
No Matches
UtilsXML.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25 */
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31/*
32 * UtilsXML.h
33 *
34 * Author: mpeura
35 */
36
37#ifndef DRAIN_UTILS_XML
38#define DRAIN_UTILS_XML
39
40#include "XML.h"
41
42namespace drain {
43
44
46class UtilsXML {
47
48public:
49
50
52
55 template <typename T>
56 static inline
57 T & assign(T & dst, const T & src){
58
59 if (&src != &dst){
60 dst.clear(); // clears children...
61 // ... but not copying src? (TreeUtils?)
62 // also dst->clear();
63 assignNode(dst.data, src); // removed - so is this polymorphic method ever used.
64 }
65
66 return dst;
67 }
68
70
73 template <typename TX>
74 static inline
75 TX & assign(TX & dst, const typename TX::xml_node_t & src){
76 assignNode(dst.data, src);
77 return dst;
78 }
79
80
82
85 template <typename T, typename V>
86 static inline
87 T & assign(T & tree, const V & arg){
88 tree->set(arg);
89 return tree;
90 }
91
93
96 template <typename T>
97 static
98 //T & assign(T & tree, std::initializer_list<std::pair<const char *,const char *> > l){
99 T & assign(T & tree, std::initializer_list<std::pair<const char *,const Variable> > l){
100
101 //switch (static_cast<intval_t>(tree->getType())){
102 switch (tree->getType()){
103 case XML::STYLE:
104 for (const auto & entry: l){
105 T & elem = tree[entry.first];
106 elem->setType(XML::STYLE_SELECT);
107 drain::MapTools::setValues(elem->getAttributes(), entry.second, ';', ':', std::string(" \t\n"));
108 }
109 break;
110 case XML::UNDEFINED:
111 tree->setType(XML::STYLE_SELECT);
112 // no break
113 case XML::STYLE_SELECT:
114 default:
115 tree->set(l);
116 break;
117 }
118
119 return tree;
120 };
121
122 // UNDER CONSTRUCTION!
124
129 template <typename TX>
130 static inline // NOT YET as template specification of assign(...)
131 TX & assignString(TX & tree, const std::string & s){
132 if (tree->isUndefined()){
133 tree->setType(XML::CTEXT);
134 }
135 tree->ctext = s;
136 return tree;
137 }
138
139 template <typename TX>
140 static inline // NOT YET as template specification of assign(...)
141 TX & appendString(TX & tree, const std::string & s){
142 if (tree->isCText()){
143 tree->ctext += s;
144 return tree;
145 }
146 else if (tree->isUndefined()){
147 tree->setType(XML::CTEXT);
148 // tree->setText(s);
149 tree->ctext += s;
150 return tree;
151 }
152 else {
153 // drain::Logger(__FILE__, __FUNCTION__).error("Assign string...");
154 TX & child = tree.addChild();
155 child->setType(XML::CTEXT);
156 child->setText(s);
157 return child;
158 }
159 }
160
161
163
167 template <typename TX>
168 static inline
169 TX & setType(TX & tree, const typename TX::node_data_t::xml_tag_t & type){
170 tree->setType(type);
171 return tree;
172 }
173
174
176
179 template <typename N>
180 static
181 typename N::xml_tag_t retrieveDefaultType(const N & parentNode){
182 typedef typename N::xml_default_elem_map_t map_t;
183 const typename map_t::const_iterator it = N::xml_default_elems.find(parentNode.getNativeType());
184 if (it != N::xml_default_elems.end()){
185 return (it->second);
186 }
187 else {
188 return static_cast<typename N::xml_tag_t>(0);
189 }
190 }
191
192 template <typename T>
193 static
194 bool initChildWithDefaultType(const T & tree, T & child){
195 typename T::node_data_t::xml_tag_t type = retrieveDefaultType(tree.data);
196 if (static_cast<int>(type) != 0){
197 child->setType(type);
198 return true;
199 }
200 else {
201 return false;
202 }
203 }
204
205
206
207
208};
209
210
211
212} // drain::
213
214#endif /* DRAIN_XML */
215
216/*
217template <typename T>
218static
219T & xmlGuessType(const typename T::node_data_t & parentNode, T & child){
220 typedef typename T::node_data_t::xml_default_elem_map_t map_t;
221 const typename map_t::const_iterator it = T::node_data_t::xml_default_elems.find(parentNode.getNativeType());
222 if (it != T::node_data_t::xml_default_elems.end()){
223 child->setType(it->second);
224 drain::Logger(__FILE__, __FUNCTION__).experimental<LOG_WARNING>("Default type set: ", child->getTag());
225 }
226 return child;
227}
228*/
229
231
263/*
264template <typename T>
265static
266T & addChild(T & tree){
267 typename T::node_data_t::xml_tag_t type = retrieveDefaultType(tree.data);
268 std::stringstream k; // ("elem");
269 k << "elem"; // number with 4 digits overwrites this?
270 k.width(3); // consider static member prefix
271 k.fill('0');
272 k << tree.getChildren().size();
273 return tree[k.str()](type);
274}
275
276template <typename T>
277static
278T & addChild(T & tree, const std::string & key){
279
280 if (!key.empty()){
281 typename T::node_data_t::xml_tag_t type = xmlRetrieveDefaultType(tree.data);
282 return tree[key](type);
283 }
284 else {
285 return addChild(tree);
286 }
287}
288*/
289
290
291// TX & xmlAppendString(TX & tree, const std::string & s){
292/*
293template <class V>
294static inline
295void attribToStream(std::ostream &ostr, const std::string & key, const V &value){
296 //StringTools::replace(XML::encodingMap, data.ctext, ostr);
297 //ostr << ' ' << key << '=' << '"' << value << '"'; // << ' ';
298
299 static const std::map<char,char> keyMap = {
300 {' ','_'},
301 {'"','_'},
302 {'=','_'},
303 };
304 ostr << ' ';
305 StringTools::replace(key, keyMap, ostr); // XML::encodingMap
306 //StringTools::replace(getEntityMap(), key, ostr); // XML::encodingMap
307
308 static const std::map<char,std::string> valueMap = {
309 {XML::entity_t::QUOTE, "'"},
310 {XML::entity_t::LESS_THAN,"(("},
311 {XML::entity_t::GREATER_THAN,"))"},
312 };
313 ostr << '=' << '"';
314 StringTools::replace(value, valueMap, ostr); // XML::encodingMap
315 //StringTools::replace(getEntityMap(), value, ostr); // XML::encodingMap
316 ostr << '"';
317 //<< key << '=' << '"' << value << '"'; // << ' ';
318}
319*/
320
static void setValues(M &dst, const std::map< std::string, S > &srcMap)
Definition MapTools.h:132
Base class for XML "nodes", to be data elements T for drain::Tree<T>
Definition UtilsXML.h:46
static T & assign(T &tree, std::initializer_list< std::pair< const char *, const Variable > > l)
Tree.
Definition UtilsXML.h:99
static T & assign(T &tree, const V &arg)
Assign property to a XML tree node.
Definition UtilsXML.h:87
static T & assign(T &dst, const T &src)
Assign another tree structure to another.
Definition UtilsXML.h:57
static N::xml_tag_t retrieveDefaultType(const N &parentNode)
Find node type (tag id) from a predefined list of default types for (some) parent types.
Definition UtilsXML.h:181
static TX & setType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition UtilsXML.h:169
static TX & assign(TX &dst, const typename TX::xml_node_t &src)
Copy node data to tree.
Definition UtilsXML.h:75
static TX & assignString(TX &tree, const std::string &s)
When assigning a string, create new element unless the element itself is of type CTEXT.
Definition UtilsXML.h:131
Definition DataSelector.cpp:1277