Repository navigation
Expand file tree
/
Copy pathxml_tree.cpp
More file actions
447 lines (388 loc) · 11.4 KB
/
Copy pathxml_tree.cpp
File metadata and controls
447 lines (388 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
//mapnik
#include <mapnik/debug.hpp>
#include <mapnik/make_unique.hpp>
#include <mapnik/xml_tree.hpp>
#include <mapnik/xml_attribute_cast.hpp>
#include <mapnik/util/conversions.hpp>
#include <mapnik/enumeration.hpp>
#include <mapnik/color_factory.hpp>
#include <mapnik/rule.hpp>
#include <mapnik/feature_type_style.hpp>
#include <mapnik/text/text_properties.hpp>
#include <mapnik/config_error.hpp>
#include <mapnik/raster_colorizer.hpp>
#include <mapnik/expression.hpp>
#include <mapnik/text/font_feature_settings.hpp>
// stl
#include <type_traits>
namespace mapnik
{
class boolean_type;
template <typename T>
struct name_trait
{
static std::string name()
{
return "<unknown>";
}
// missing name_trait for type ...
// if you get here you are probably using a new type
// in the XML file. Just add a name trait for the new
// type below.
static_assert( sizeof(T) == 0, "missing name_trait for the type");
};
#define DEFINE_NAME_TRAIT( type, type_name ) \
template <> \
struct name_trait<type> \
{ \
static std::string name() { return std::string("type ") + type_name; } \
};
DEFINE_NAME_TRAIT( double, "double")
DEFINE_NAME_TRAIT( float, "float")
DEFINE_NAME_TRAIT( unsigned, "unsigned")
DEFINE_NAME_TRAIT( boolean_type, "boolean_type")
#ifdef BIGINT
DEFINE_NAME_TRAIT( mapnik::value_integer, "long long" )
#else
DEFINE_NAME_TRAIT( mapnik::value_integer, "int" )
#endif
DEFINE_NAME_TRAIT( std::string, "string" )
DEFINE_NAME_TRAIT( color, "color" )
DEFINE_NAME_TRAIT( expression_ptr, "expression_ptr" )
DEFINE_NAME_TRAIT( font_feature_settings, "font-feature-settings" )
template <typename ENUM, int MAX>
struct name_trait< mapnik::enumeration<ENUM, MAX> >
{
using Enum = enumeration<ENUM, MAX>;
static std::string name()
{
std::string value_list("one of [");
for (unsigned i = 0; i < Enum::MAX; ++i)
{
value_list += Enum::get_string( i );
if ( i + 1 < Enum::MAX ) value_list += ", ";
}
value_list += "]";
return value_list;
}
};
xml_tree::xml_tree(std::string const& encoding)
: node_(*this, "<root>"),
file_()
{
node_.set_processed(true); //root node is always processed
}
void xml_tree::set_filename(std::string const& fn)
{
file_ = fn;
}
std::string const& xml_tree::filename() const
{
return file_;
}
xml_node & xml_tree::root()
{
return node_;
}
const xml_node & xml_tree::root() const
{
return node_;
}
xml_attribute::xml_attribute(const char * value_)
: value(value_),
processed(false) {}
node_not_found::node_not_found(std::string const& node_name)
: node_name_(node_name),
msg_() {}
const char* node_not_found::what() const throw()
{
msg_ = std::string("Node "+node_name_+ "not found");
return msg_.c_str();
}
node_not_found::~node_not_found() throw() {}
attribute_not_found::attribute_not_found(std::string const& node_name,
std::string const& attribute_name)
: node_name_(node_name),
attribute_name_(attribute_name),
msg_() {}
const char* attribute_not_found::what() const throw()
{
msg_ = std::string("Attribute '" + attribute_name_ +"' not found in node '"+node_name_+ "'");
return msg_.c_str();
}
attribute_not_found::~attribute_not_found() throw() {}
more_than_one_child::more_than_one_child(std::string const& node_name)
: node_name_(node_name),
msg_() {}
const char* more_than_one_child::what() const throw()
{
msg_ = std::string("More than one child node in node '" + node_name_ +"'");
return msg_.c_str();
}
more_than_one_child::~more_than_one_child() throw() {}
xml_node::xml_node(xml_tree &tree, std::string && name, unsigned line, bool is_text)
: tree_(tree),
name_(std::move(name)),
is_text_(is_text),
line_(line),
processed_(false),
ignore_(false) {}
std::string xml_node::xml_text = "<xmltext>";
std::string const& xml_node::name() const
{
if (!is_text_)
{
return name_;
}
else
{
return xml_text;
}
}
std::string const& xml_node::text() const
{
if (is_text_)
{
processed_ = true;
return name_;
}
else
{
throw config_error("text() called on non-text node", *this);
}
}
std::string const& xml_node::filename() const
{
return tree_.filename();
}
bool xml_node::is_text() const
{
return is_text_;
}
bool xml_node::is(std::string const& name) const
{
if (name_ == name)
{
processed_ = true;
return true;
}
return false;
}
xml_node & xml_node::add_child(const char * name, unsigned line, bool is_text)
{
children_.emplace_back(tree_, name, line, is_text);
return children_.back();
}
void xml_node::add_attribute(const char * name, const char * value)
{
auto result = attributes_.emplace(name,xml_attribute(value));
if (!result.second)
{
MAPNIK_LOG_ERROR(xml_tree) << "ignoring duplicate attribute '" << name << "'";
}
}
xml_node::attribute_map const& xml_node::get_attributes() const
{
return attributes_;
}
void xml_node::set_processed(bool processed) const
{
processed_ = processed;
}
bool xml_node::processed() const
{
return processed_;
}
void xml_node::set_ignore(bool ignore) const
{
ignore_ = ignore;
}
bool xml_node::ignore() const
{
return ignore_;
}
std::size_t xml_node::size() const
{
return children_.size();
}
xml_node::const_iterator xml_node::begin() const
{
return children_.begin();
}
xml_node::const_iterator xml_node::end() const
{
return children_.end();
}
xml_node & xml_node::get_child(std::string const& name)
{
std::list<xml_node>::iterator itr = children_.begin();
std::list<xml_node>::iterator end = children_.end();
for (; itr != end; ++itr)
{
if (!(itr->is_text_) && itr->name_ == name)
{
itr->set_processed(true);
return *itr;
}
}
throw node_not_found(name);
}
xml_node const& xml_node::get_child(std::string const& name) const
{
xml_node const* node = get_opt_child(name);
if (!node) throw node_not_found(name);
return *node;
}
xml_node const* xml_node::get_opt_child(std::string const& name) const
{
const_iterator itr = children_.begin();
const_iterator end = children_.end();
for (; itr != end; itr++)
{
if (!(itr->is_text_) && itr->name_ == name)
{
itr->set_processed(true);
return &(*itr);
}
}
return 0;
}
bool xml_node::has_child(std::string const& name) const
{
return get_opt_child(name) != 0;
}
bool xml_node::has_attribute(std::string const& name) const
{
return attributes_.count(name) == 1 ? true : false;
}
template <typename T>
boost::optional<T> xml_node::get_opt_attr(std::string const& name) const
{
if (attributes_.empty()) return boost::optional<T>();
std::map<std::string, xml_attribute>::const_iterator itr = attributes_.find(name);
if (itr == attributes_.end()) return boost::optional<T>();
itr->second.processed = true;
boost::optional<T> result = xml_attribute_cast<T>(tree_, std::string(itr->second.value));
if (!result)
{
throw config_error(std::string("Failed to parse attribute '") +
name + "'. Expected " + name_trait<T>::name() +
" but got '" + itr->second.value + "'", *this);
}
return result;
}
template <typename T>
T xml_node::get_attr(std::string const& name, T const& default_opt_value) const
{
boost::optional<T> value = get_opt_attr<T>(name);
if (value) return *value;
return default_opt_value;
}
template <typename T>
T xml_node::get_attr(std::string const& name) const
{
boost::optional<T> value = get_opt_attr<T>(name);
if (value) return *value;
throw attribute_not_found(name_, name);
}
std::string const& xml_node::get_text() const
{
// FIXME : return boost::optional<std::string const&>
if (children_.empty())
{
if (is_text_)
{
return name_;
}
else
{
const static std::string empty;
return empty;
}
}
if (children_.size() == 1)
{
return children_.front().text();
}
throw more_than_one_child(name_);
}
template <typename T>
T xml_node::get_value() const
{
boost::optional<T> result = xml_attribute_cast<T>(tree_, get_text());
if (!result)
{
throw config_error(std::string("Failed to parse value. Expected ")
+ name_trait<T>::name() +
" but got '" + get_text() + "'", *this);
}
return *result;
}
unsigned xml_node::line() const
{
return line_;
}
std::string xml_node::line_to_string() const
{
std::string number;
util::to_string(number,line_);
return number;
}
#define compile_get_opt_attr(T) template boost::optional<T> xml_node::get_opt_attr<T>(std::string const&) const
#define compile_get_attr(T) template T xml_node::get_attr<T>(std::string const&) const; template T xml_node::get_attr<T>(std::string const&, T const&) const
#define compile_get_value(T) template T xml_node::get_value<T>() const
compile_get_opt_attr(boolean_type);
compile_get_opt_attr(std::string);
compile_get_opt_attr(unsigned);
compile_get_opt_attr(mapnik::value_integer);
compile_get_opt_attr(float);
compile_get_opt_attr(double);
compile_get_opt_attr(color);
compile_get_opt_attr(gamma_method_e);
compile_get_opt_attr(line_join_e);
compile_get_opt_attr(line_cap_e);
compile_get_opt_attr(text_transform_e);
compile_get_opt_attr(label_placement_e);
compile_get_opt_attr(vertical_alignment_e);
compile_get_opt_attr(horizontal_alignment_e);
compile_get_opt_attr(justify_alignment_e);
compile_get_opt_attr(text_upright_e);
compile_get_opt_attr(direction_e);
compile_get_opt_attr(halo_rasterizer_e);
compile_get_opt_attr(expression_ptr);
compile_get_opt_attr(font_feature_settings);
compile_get_attr(std::string);
compile_get_attr(filter_mode_e);
compile_get_attr(point_placement_e);
compile_get_attr(debug_symbolizer_mode_e);
compile_get_attr(marker_placement_e);
compile_get_attr(marker_multi_policy_e);
compile_get_attr(pattern_alignment_e);
compile_get_attr(line_rasterizer_e);
compile_get_attr(colorizer_mode);
compile_get_attr(double);
compile_get_value(value_integer);
compile_get_value(double);
compile_get_value(expression_ptr);
} //ns mapnik