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 * TreeXML.h
33 *
34 * Created on: Jun 24, 2012
35 * Author: mpeura
36 */
37
38
39
40#ifndef DRAIN_UTILS_XML
41#define DRAIN_UTILS_XML
42
43/*
44#include <ostream>
45#include <drain/Sprinter.h>
46#include <drain/FlexibleVariable.h>
47
48#include "ClassXML.h"
49// #include "UtilsXML.h"
50// #include "Flags.h"
51#include "ReferenceMap.h"
52#include "StyleXML.h"
53*/
54
55#include "XML.h"
56
57namespace drain {
58
59
61class UtilsXML {
62
63public:
64
65 // TX & xmlAppendString(TX & tree, const std::string & s){
66
67 template <class V>
68 static inline
69 void attribToStream(std::ostream &ostr, const std::string & key, const V &value){
70 //StringTools::replace(XML::encodingMap, data.ctext, ostr);
71 //ostr << ' ' << key << '=' << '"' << value << '"'; // << ' ';
72
73 static const std::map<char,char> keyMap = {
74 {' ','_'},
75 {'"','_'},
76 {'=','_'},
77 };
78 ostr << ' ';
79 StringTools::replace(key, keyMap, ostr); // XML::encodingMap
80 //StringTools::replace(getEntityMap(), key, ostr); // XML::encodingMap
81
82 static const std::map<char,std::string> valueMap = {
83 {XML::entity_t::QUOTE, "'"},
84 {XML::entity_t::LESS_THAN,"(("},
85 {XML::entity_t::GREATER_THAN,"))"},
86 };
87 ostr << '=' << '"';
88 StringTools::replace(value, valueMap, ostr); // XML::encodingMap
89 //StringTools::replace(getEntityMap(), value, ostr); // XML::encodingMap
90 ostr << '"';
91 //<< key << '=' << '"' << value << '"'; // << ' ';
92 }
93
95
98 template <typename T>
99 static inline
100 T & assign(T & dst, const T & src){
101
102 if (&src != &dst){
103 dst.clear(); // clears children...
104 // ... but not copying src? (TreeUtils?)
105 // also dst->clear();
106 assignNode(dst.data, src);
107 }
108
109 return dst;
110 }
111
113
116 template <typename TX>
117 static inline
118 TX & assign(TX & dst, const typename TX::xml_node_t & src){
119 assignNode(dst.data, src);
120 return dst;
121 }
122
124
151
154 template <typename T, typename V>
155 static inline
156 T & assign(T & tree, const V & arg){
157 tree->set(arg);
158 return tree;
159 }
160
162
165 template <typename T>
166 static
167 //T & assign(T & tree, std::initializer_list<std::pair<const char *,const char *> > l){
168 T & assign(T & tree, std::initializer_list<std::pair<const char *,const Variable> > l){
169
170 //switch (static_cast<intval_t>(tree->getType())){
171 switch (tree->getType()){
172 case XML::STYLE:
173 for (const auto & entry: l){
174 T & elem = tree[entry.first];
175 elem->setType(XML::STYLE_SELECT);
176 drain::MapTools::setValues(elem->getAttributes(), entry.second, ';', ':', std::string(" \t\n"));
177 }
178 break;
179 case XML::UNDEFINED:
180 tree->setType(XML::STYLE_SELECT);
181 // no break
182 case XML::STYLE_SELECT:
183 default:
184 tree->set(l);
185 break;
186 }
187
188 return tree;
189 };
190
191 // UNDER CONSTRUCTION!
193
198 template <typename TX>
199 static inline // NOT YET as template specification of assign(...)
200 TX & assignString(TX & tree, const std::string & s){
201 if (tree->isUndefined()){
202 tree->setType(XML::CTEXT);
203 }
204 tree->ctext = s;
205 return tree;
206 }
207
208 template <typename TX>
209 static inline // NOT YET as template specification of assign(...)
210 TX & appendString(TX & tree, const std::string & s){
211 if (tree->isCText()){
212 tree->ctext += s;
213 return tree;
214 }
215 else if (tree->isUndefined()){
216 tree->setType(XML::CTEXT);
217 // tree->setText(s);
218 tree->ctext += s;
219 return tree;
220 }
221 else {
222 // drain::Logger(__FILE__, __FUNCTION__).error("Assign string...");
223 TX & child = tree.addChild();
224 child->setType(XML::CTEXT);
225 child->setText(s);
226 return child;
227 }
228 }
229
230
232
236 template <typename TX>
237 static inline
238 TX & setType(TX & tree, const typename TX::node_data_t::xml_tag_t & type){
239 tree->setType(type);
240 return tree;
241 }
242
243
249 template <typename T>
250 static
251 T & addChild(T & tree){
252 typename T::node_data_t::xml_tag_t type = retrieveDefaultType(tree.data);
253 std::stringstream k; // ("elem");
254 k << "elem"; // number with 4 digits overwrites this?
255 k.width(3); // consider static member prefix
256 k.fill('0');
257 k << tree.getChildren().size();
258 return tree[k.str()](type);
259 }
260
261 template <typename T>
262 static
263 T & addChild(T & tree, const std::string & key){
264
265 if (!key.empty()){
266 typename T::node_data_t::xml_tag_t type = xmlRetrieveDefaultType(tree.data);
267 return tree[key](type);
268 }
269 else {
270 return addChild(tree);
271 /*
272 std::stringstream k; // ("elem");
273 k << "elem"; // number with 4 digits overwrites this?
274 k.width(3); // consider static member prefix
275 k.fill('0');
276 k << tree.getChildren().size();
277 return tree[k.str()](type);
278 */
279 }
280 }
281
282 template <typename N>
283 static
284 typename N::xml_tag_t retrieveDefaultType(const N & parentNode){
285 typedef typename N::xml_default_elem_map_t map_t;
286 const typename map_t::const_iterator it = N::xml_default_elems.find(parentNode.getNativeType());
287 if (it != N::xml_default_elems.end()){
288 return (it->second);
289 }
290 else {
291 return static_cast<typename N::xml_tag_t>(0);
292 }
293 }
294
295 /*
296 template <typename T>
297 static
298 T & xmlGuessType(const typename T::node_data_t & parentNode, T & child){
299 typedef typename T::node_data_t::xml_default_elem_map_t map_t;
300 const typename map_t::const_iterator it = T::node_data_t::xml_default_elems.find(parentNode.getNativeType());
301 if (it != T::node_data_t::xml_default_elems.end()){
302 child->setType(it->second);
303 drain::Logger(__FILE__, __FUNCTION__).experimental<LOG_WARNING>("Default type set: ", child->getTag());
304 }
305 return child;
306 }
307 */
308
309
310
311};
312
313
314
315} // drain::
316
317#endif /* DRAIN_XML */
318
static void setValues(M &dst, const std::map< std::string, S > &srcMap)
Definition MapTools.h:132
static void replace(const std::string &src, char search, char repl, std::ostream &ostr)
Replaces instances of 'search' to 'repl' in src.
Definition StringTools.cpp:94
Base class for XML "nodes", to be data elements T for drain::Tree<T>
Definition UtilsXML.h:61
static T & assign(T &tree, std::initializer_list< std::pair< const char *, const Variable > > l)
Tree.
Definition UtilsXML.h:168
static T & addChild(T &tree)
Definition UtilsXML.h:251
static T & assign(T &tree, const V &arg)
Assign tree node (data) to another.
Definition UtilsXML.h:156
static T & assign(T &dst, const T &src)
Assign another tree structure to another.
Definition UtilsXML.h:100
static TX & setType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition UtilsXML.h:238
static TX & assign(TX &dst, const typename TX::xml_node_t &src)
Copy node data to tree.
Definition UtilsXML.h:118
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:200
Definition DataSelector.cpp:1277