/
style.cpp
666 lines (603 loc) · 18 KB
/
style.cpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#include "style.h"
#include "document.h"
#include "element.h"
#include "composite_element.h"
#include "helpers.h"
#include "composite_element.h"
#include "attribute.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <stdexcept>
#define XSL_NS "xsl"
#define XSL_TEMPLATES "template"
#define XSL_MATCH "match"
#define XSL_ROOT_MATCH "/"
#define XSL_STYLESHEET "stylesheet"
#define XSL_APPLY_TEMPLATES "apply-templates"
#define XSL_VALUE_OF "value-of"
#define XSL_FOR_EACH "for-each"
#define XSL_SELECT "select"
namespace Xsl
{
static bool is_element(Element const & element, std::string const & tag = "")
{
auto ns_split = element.ns_split();
// F**king lower() method
Helpers::lower(ns_split.first);
// Only xsl namespace
return ns_split.first == XSL_NS && (tag.empty() || ns_split.second == tag);
}
class Document
{
public:
Document(XMLDocument const &);
void apply_style_to(XMLDocument const &, std::ostream &) const;
void apply_templates(Element const *, Element const *, std::ostream &) const;
void value_of(Element const *, Element const *, std::ostream &) const;
void for_each(Element const *, Element const *, std::ostream &) const;
private:
void apply_all_templates(Content const *, std::ostream &) const;
Attribute const * get_directive_attribute(Element const *,
std::string const &, Element const *, std::ostream &) const;
CompositeElement const * _root_template;
std::map<std::string, Element const *> _templates;
};
class TemplateRenderer
{
public:
TemplateRenderer(Document const & doc, Element const * root_element, std::ostream & os):
_doc(doc), _root_element(root_element), _os(os) {}
~TemplateRenderer() = default;
void render_empty(Element const *);
void render_composite(CompositeElement const *);
void render_xsl(Element const *);
private:
Document const & _doc;
Element const * _root_element;
std::ostream & _os;
};
class Validator
{
public:
Validator(Element const &, std::ostream &);
~Validator() = default;
bool operator()();
private:
// Checkers
void check_tag_levels();
void check_apply_templates_count();
void check_apply_templates_select();
void check_tag_lower_levels(Element const &);
void check_that_there_at_least_one_root_template();
void check_that_directive_has_attribute(std::string const &, std::string const &);
void check_that_directive_has_attribute_r(Element const &,
std::string const &, std::string const &);
// Helpers
size_t count_apply_all_templates(Element const &);
void get_all_apply_template_select(Element const &, std::vector<std::string> &);
Element const & _root;
std::ostream & _os;
bool _good;
};
Document::Document(XMLDocument const & doc):
_root_template(), _templates()
{
for (auto child : doc.root()->children())
{
Element * element = dynamic_cast<Element *>(child);
if (element != nullptr && is_element(*element, XSL_TEMPLATES))
{
for (auto attr : element->attributes())
{
if (attr->name() == XSL_MATCH && !attr->value().empty())
{
if (attr->value() == "/")
{
_root_template = dynamic_cast<CompositeElement const *>(element);
}
else
{
_templates[attr->value()] = element;
}
break; // because f**k you that's why !
}
}
}
}
}
void Document::apply_style_to(XMLDocument const & xml, std::ostream & os) const
{
if (_root_template == nullptr)
{
apply_all_templates(xml.root(), os);
}
else
{
TemplateRenderer tr(*this, xml.root(), os);
tr.render_composite(_root_template);
}
}
void TemplateRenderer::render_composite(CompositeElement const * tmplt)
{
for (auto child : tmplt->children())
{
auto child_element = dynamic_cast<Element const *>(child);
if (child_element != nullptr)
{
render_empty(child_element);
}
else
{
_os << child->str() << std::endl;
}
}
}
void TemplateRenderer::render_empty(Element const * tmplt)
{
if (is_element(*tmplt))
{
render_xsl(tmplt);
}
else
{
auto ce = dynamic_cast<CompositeElement const *>(tmplt);
if (ce == nullptr)
{
_os << tmplt->str() << std::endl;
}
else
{
_os << ce->begin_str() << std::endl;
render_composite(ce);
_os << ce->end_str() << std::endl;
}
}
}
void TemplateRenderer::render_xsl(Element const * tmplt)
{
std::string xsl_operation = tmplt->ns_split().second;
// Very elegant code
if (xsl_operation == XSL_APPLY_TEMPLATES)
{
_doc.apply_templates(tmplt, _root_element, _os);
}
else if (xsl_operation == XSL_FOR_EACH)
{
_doc.for_each(tmplt, _root_element, _os);
}
else if (xsl_operation == XSL_VALUE_OF)
{
_doc.value_of(tmplt, _root_element, _os);
}
else
{
_os << tmplt->str() << std::endl;
}
}
void Document::apply_templates(Element const * tmplt, Element const * element, std::ostream & os) const
{
Attribute const * select_attr = tmplt->find_attribute(XSL_SELECT);
if (select_attr != nullptr)
{
auto it =_templates.find(select_attr->value());
if (it != _templates.end())
{
for (auto c : element->children())
{
auto cec = dynamic_cast<CompositeElement const *>(c);
if (cec != nullptr && cec->name() == select_attr->value())
{
TemplateRenderer tr(*this, cec, os);
tr.render_composite(static_cast<CompositeElement const *>(it->second));
break;
}
}
}
else
{
auto tmplt_ce = dynamic_cast<CompositeElement const *>(tmplt);
if (tmplt_ce != nullptr)
{
TemplateRenderer tr(*this, element, os);
os << tmplt_ce->begin_str() << std::endl;
tr.render_composite(tmplt_ce);
os << tmplt_ce->end_str() << std::endl;
}
else
{
os << tmplt->str() << std::endl;
}
}
}
else // Output ALL the templates!
{
for (auto c : element->children())
{
apply_all_templates(c, os);
}
}
}
Attribute const * Document::get_directive_attribute(Element const * tmplt,
std::string const & _attr, Element const * element, std::ostream & os) const
{
std::string attr(_attr);
Helpers::trim(attr);
Attribute const * select_attr_ptr = tmplt->find_attribute(attr);
if (select_attr_ptr == nullptr)
{
auto tmplt_ce = dynamic_cast<CompositeElement const *>(tmplt);
if (tmplt_ce != nullptr)
{
TemplateRenderer tr(*this, element, os);
os << tmplt_ce->begin_str() << std::endl;
tr.render_composite(tmplt_ce);
os << tmplt_ce->end_str() << std::endl;
}
else
{
os << tmplt->str() << std::endl;
}
}
return select_attr_ptr;
}
void Document::value_of(Element const * tmplt, Element const * element, std::ostream & os) const
{
Attribute const * select_attr_ptr = get_directive_attribute(tmplt, XSL_SELECT, element, os);
if (select_attr_ptr == nullptr)
{
return;
}
auto select_attr = *select_attr_ptr;
if (select_attr.value() == ".")
{
auto ce = dynamic_cast<CompositeElement const *>(element);
if (ce != nullptr)
{
for (auto child : ce->children())
{
os << child->str() << std::endl;
}
}
}
else
{
for (auto c : element->children())
{
auto ce = dynamic_cast<CompositeElement const *>(c);
if (ce != nullptr && ce->name() == select_attr.value())
{
for (auto child : ce->children())
{
os << child->str() << std::endl;
}
break;
}
}
}
}
void Document::for_each(Element const * tmplt, Element const * element, std::ostream & os) const
{
Attribute const * select_attr_ptr = get_directive_attribute(tmplt, XSL_SELECT, element, os);
if (select_attr_ptr == nullptr)
{
return;
}
// Normal case
auto select_attr = *select_attr_ptr;
std::string select_name = select_attr.value();
// FIXME find a better solution than ignore selection
auto slash_split = Helpers::split(select_name, '/');
if (slash_split.size() == 2)
{
select_name = slash_split.back();
}
for (auto c : element->children())
{
auto e = dynamic_cast<Element const *>(c);
if (e != nullptr && e->name() == select_name)
{
TemplateRenderer tr(*this, e, os);
tr.render_composite(static_cast<CompositeElement const *>(tmplt));
}
}
}
void Document::apply_all_templates(Content const * c, std::ostream & os) const
{
auto e = dynamic_cast<Element const *>(c);
if (e == nullptr)
{
return;
}
// Apply applicable templates
auto it = _templates.find(e->name());
if (it !=_templates.end())
{
TemplateRenderer tr(*this, e, os);
tr.render_composite(static_cast<CompositeElement const *>(it->second));
}
else // Recursion otherwise
{
for (auto c : e->children())
{
apply_all_templates(c, os);
}
}
}
Validator::Validator(Element const & root, std::ostream & os):
_root(root), _os(os), _good(true)
{
}
void Validator::check_tag_lower_levels(Element const & root)
{
for (auto it : root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(it);
if (ce != nullptr)
{
// Test presence of things different from a template on level 2
std::string xsl_operation = ce->ns_split().second;
if (xsl_operation == XSL_TEMPLATES || xsl_operation == XSL_STYLESHEET)
{
_os << "xsl lower level tag incorrect" << std::endl;
_good = false;
}
else
{
check_tag_lower_levels(*ce);
}
}
}
}
void Validator::check_that_there_at_least_one_root_template()
{
for (auto it : _root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(it);
if (ce != nullptr)
{
// Test presence of things different from a template on level 2
std::string xsl_operation = ce->ns_split().second;
if (xsl_operation == XSL_TEMPLATES)
{
for (auto attr : ce->attributes())
{
if (attr->name() == XSL_MATCH)
{
if (attr->value()==XSL_ROOT_MATCH)
{
return;
}
}
}
}
}
}
_os << "xsl no root template found" << std::endl;
_good=false;
}
void Validator::check_tag_levels()
{
if (!is_element(_root, XSL_STYLESHEET))
{
_os << "root directives should be " << XSL_STYLESHEET << std::endl;
_good = false;
}
for (auto it : _root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(it);
if (ce != nullptr)
{
// Test presence of things different from a template on level 2
std::string xsl_operation = ce->ns_split().second;
if (xsl_operation != XSL_TEMPLATES)
{
_os << "first child directives should all be " << XSL_TEMPLATES << std::endl;
_good = false;
}
else
{
check_tag_lower_levels(*ce);
}
}
else
{
// This would indicate that this differs from a composite
// element therefore it can't be a template
_os << "xsl level 2 tag incorrect" << std::endl;
_good = false;
}
}
}
size_t Validator::count_apply_all_templates(Element const & root)
{
size_t count = 0;
for (auto it : root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(it);
if (ce != nullptr)
{
count += count_apply_all_templates(*ce);
}
else
{
Element * e = dynamic_cast<Element *>(it);
if (e != nullptr)
{
std::string xsl_operation = e->ns_split().second;
if (xsl_operation == XSL_APPLY_TEMPLATES &&
!std::any_of(e->attributes().begin(), e->attributes().end(),
[](Attribute* attr) { return (attr->name() == XSL_SELECT); }))
{
count++;
}
}
}
}
return count;
}
void Validator::get_all_apply_template_select(Element const & root, std::vector<std::string> & vect_select)
{
for (auto c : root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(c);
if (ce != nullptr)
{
get_all_apply_template_select(*ce, vect_select);
}
else
{
Element * e = dynamic_cast<Element *>(c);
if (e != nullptr)
{
std::string xsl_operation = e->ns_split().second;
if (xsl_operation == XSL_APPLY_TEMPLATES)
{
for (auto attrs : e->attributes())
{
if (attrs->name() == XSL_SELECT)
{
vect_select.push_back(attrs->value());
}
}
if (e->attributes().size()<1){
// if there is no select we consider there si a select="."
vect_select.push_back(".");
}
}
}
}
}
}
void Validator::check_apply_templates_select()
{
std::vector<std::string> vect_select;
get_all_apply_template_select(_root, vect_select);
for (auto c : _root.children())
{
CompositeElement * ce = dynamic_cast<CompositeElement *>(c);
if (ce != nullptr)
{
for (auto attr : ce->attributes())
{
if (attr->name() == XSL_MATCH)
{
for (unsigned int i = 0; i < vect_select.size(); i++)
{
if (vect_select[i] == attr->value())
{
vect_select.erase(vect_select.begin() + i);
--i;
}else if (vect_select[i] == ".")
{
// we need to remove all the select that indicate the current folder
vect_select.erase(vect_select.begin() + i);
--i;
}
}
}
}
}
}
if (vect_select.size() > 0)
{
_os << XSL_APPLY_TEMPLATES << " given with no corresponding template" << std::endl;
_good = false;
}
}
void Validator::check_apply_templates_count()
{
size_t count_apply_templates_root=0;// = count_apply_all_templates(_root);
std::vector<std::string> vect_select;
for (auto it: _root.children()){
vect_select.clear();
CompositeElement * ce = dynamic_cast<CompositeElement *>(it);
if (ce != nullptr)
{
bool is_root_template= std::any_of(ce->attributes().begin(), ce->attributes().end(),
[](Attribute* attr) { return ((attr->name() == XSL_MATCH)&&(attr->value() == XSL_ROOT_MATCH)); });
get_all_apply_template_select(*ce, vect_select);
if (is_root_template)
{
if (std::find(vect_select.begin(),vect_select.end(),".")!=vect_select.end())
{
count_apply_templates_root++;
}
}
if (std::find(vect_select.begin(),vect_select.end(),XSL_ROOT_MATCH)!=vect_select.end())
{
count_apply_templates_root++;
}
}
else
{
//a good file should only have XSL_TEMPLATES under the root
//as such we can ignore this case
//another test verify it
}
}
if (count_apply_templates_root > 1)
{
_os << "only one " << XSL_APPLY_TEMPLATES << " directive can be given,"
<< " got " << count_apply_templates_root <<std::endl;
_good = false;
}
}
void Validator::check_that_directive_has_attribute(std::string const & directive,
std::string const & attr_name)
{
check_that_directive_has_attribute_r(_root, directive, attr_name);
}
void Validator::check_that_directive_has_attribute_r(Element const & root,
std::string const & directive, std::string const & attr_name)
{
for (auto child : root.children())
{
auto e = dynamic_cast<Element const *>(child);
if (e != nullptr)
{
if (is_element(*e, directive))
{
auto attr = e->find_attribute(attr_name);
if (attr == nullptr || attr->value().empty())
{
_os << "directive needs a " << attr_name << " attribute" << std::endl;
_good = false;
}
}
check_that_directive_has_attribute_r(*e, directive, attr_name);
}
}
}
bool Validator::operator()()
{
// Tag levels should be as specified (xsl:stylesheet)
check_tag_levels();
//there have to be a root template ("/")
// since root is no longer compulsory this test is desactivated
// check_that_there_at_least_one_root_template();
// The "apply-templates" directive which send u to root can only be given once (to prevent infinite loop)
check_apply_templates_count();
// The "apply-templates" directive should either be empty or refer to a given template
check_apply_templates_select();
// "for-each" and "value-of" directives should have a "select" attribute
check_that_directive_has_attribute(XSL_FOR_EACH, XSL_SELECT);
check_that_directive_has_attribute(XSL_VALUE_OF, XSL_SELECT);
// Why so serious?
return true;//_good;
}
// Only public function of the module
void apply_style(XMLDocument const & xml, XMLDocument const & xsl,
std::ostream & out, std::ostream & err)
{
if (Validator(*xsl.root(), err)())
{
Document xsl_doc(xsl);
xsl_doc.apply_style_to(xml, out);
}
}
}
// vim:ft=cpp et sw=2 sts=2: