-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxpath_processor.cpp
2061 lines (1895 loc) · 79.1 KB
/
xpath_processor.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
www.sourceforge.net/projects/tinyxpath
Copyright (c) 2002-2004 Yves Berquin ([email protected])
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
/*
@history:
Modified on 16 December 2006 by Aman Aggarwal
::Added support for Expressions like ( Expr or Expr or Expr)
Modified on 18 December 2006 by Aman Aggarwal
::Added support for translate()
*/
#include "xpath_processor.h"
#include <math.h>
#include <stdexcept>
#include "xml_util.h"
using namespace std;
using namespace tinyxml2;
using namespace TinyXPath;
#ifdef TINYXPATH_DEBUG
// only define DUMP_ACTION if TINYXPATH_DEBUG is defined
#define DUMP_ACTION
#endif
/// xpath_processor constructor
xpath_processor::xpath_processor(const XMLNode* XNp_source_tree, ///< Source XML tree
const char* cp_xpath_expr) ///< XPath expression
: xpath_stream(cp_xpath_expr), _e_error(e_no_error) {
if (XNp_source_tree && cp_xpath_expr)
_XNp_base = XNp_source_tree;
else
_XNp_base = nullptr;
_er_result.v_set_root(_XNp_base);
_xs_stack.v_set_root(_XNp_base);
_XEp_context = nullptr;
_o_is_context_by_name = false;
_XNp_base_parent = nullptr;
}
/// Compute an XPath expression, and return the number of nodes in the resulting node set.
/// \n Returns 0 if the result is not a node set
unsigned xpath_processor::u_compute_xpath_node_set() {
er_compute_xpath();
if (_er_result._e_type != e_node_set)
return 0;
return _er_result.nsp_get_node_set()->u_get_nb_node_in_set();
}
/// Retrieves an XPath node from the node set. This assumes you know it's not an attribute
const XMLNode* xpath_processor::XNp_get_xpath_node(
unsigned u_order) ///< Order of the node. Must be between 0 and the number of nodes - 1
{
if (_er_result._e_type != e_node_set)
return nullptr;
if (u_order >= _er_result.nsp_get_node_set()->u_get_nb_node_in_set())
return nullptr;
if (_er_result.nsp_get_node_set()->o_is_attrib(u_order))
return nullptr;
return _er_result.nsp_get_node_set()->XNp_get_node_in_set(u_order);
}
/// Retrieves an XPath attribute from the node set. This assumes you know it's an attribute
const XMLAttribute* xpath_processor::XAp_get_xpath_attribute(
unsigned u_order) ///< Order of the node. Must be between 0 and the number of nodes - 1
{
if (_er_result._e_type != e_node_set)
return nullptr;
if (u_order >= _er_result.nsp_get_node_set()->u_get_nb_node_in_set())
return nullptr;
if (!_er_result.nsp_get_node_set()->o_is_attrib(u_order))
return nullptr;
return _er_result.nsp_get_node_set()->XAp_get_attribute_in_set(u_order);
}
void xpath_processor::v_build_root() {
if (_XNp_base) {
_XNp_base_parent = _XNp_base->Parent();
if (!_XNp_base_parent)
// no correct initialization of the xpath_processor object
throw execution_error(1);
// set the main node as the context one, if it's an element
if (_XNp_base->ToElement())
_XEp_context = _XNp_base->ToElement();
} else
_XNp_base_parent = nullptr;
}
/// Compute an XPath expression
expression_result xpath_processor::er_compute_xpath() {
try {
_XNp_base_parent = _XNp_base->Parent();
if (!_XNp_base_parent)
// no correct initialization of the xpath_processor object
throw execution_error(1);
// set the main node as the context one, if it's an element
if (_XNp_base->ToElement())
_XEp_context = _XNp_base->ToElement();
// Decode XPath expression
v_evaluate();
// Compute result
v_execute_stack();
/// The executions stack need to contain 1 and only 1 element, otherwize it's not valid
if (_xs_stack.u_get_size() == 1) {
_er_result = *_xs_stack.erp_top();
_xs_stack.v_pop();
_e_error = e_no_error;
} else {
expression_result er_null(nullptr);
_er_result = er_null;
_e_error = e_error_stack;
}
} catch (syntax_error) {
expression_result er_null(nullptr);
_er_result = er_null;
_e_error = e_error_syntax;
} catch (syntax_overflow) {
expression_result er_null(nullptr);
_er_result = er_null;
_e_error = e_error_overflow;
} catch (execution_error) {
expression_result er_null(nullptr);
_er_result = er_null;
_e_error = e_error_execution;
}
return _er_result;
}
/// Compute an XPath expression and return the result as a string
string xpath_processor::S_compute_xpath() {
expression_result er_res(_XNp_base);
string S_res;
er_res = er_compute_xpath();
S_res = er_res.S_get_string();
return S_res;
}
/// Compute an XPath expression and return the result as an integer
int xpath_processor::i_compute_xpath() {
expression_result er_res(_XNp_base);
int i_res;
er_res = er_compute_xpath();
i_res = er_res.i_get_int();
return i_res;
}
bool xpath_processor::o_compute_xpath() {
expression_result er_res(_XNp_base);
bool o_res;
er_res = er_compute_xpath();
o_res = er_res.o_get_bool();
return o_res;
}
double xpath_processor::d_compute_xpath() {
expression_result er_res(_XNp_base);
double d_res;
er_res = er_compute_xpath();
d_res = er_res.d_get_double();
return d_res;
}
/// Callback from the XPath decoder : a rule has to be applied
void xpath_processor::v_action(xpath_construct xc_rule, ///< XPath Rule
xpath_sub xp_sub, ///< Rule sub number
lex u_variable, ///< Parameter, depends on the rule
const char* cp_literal) ///< Input literal, depends on the rule
{
_as_action_store.v_add(
static_cast<int>(xc_rule), static_cast<int>(xp_sub), static_cast<unsigned>(u_variable), cp_literal);
#ifdef TINYXPATH_DEBUG
printf("Action %2d : %s (%d,%d,%s)\n", _as_action_store.i_get_size() - 1, cp_disp_construct(xc_rule),
static_cast<int>(xp_sub), static_cast<unsigned>(u_variable), cp_literal);
#endif
}
/// Internal use. Retrieves the current action counter
int xpath_processor::i_get_action_counter() {
// callback for current stack position
return _as_action_store.i_get_size();
}
/// Internal use. Executes the XPath expression. The executions starts at the end of the _as_action_store list
void xpath_processor::v_execute_stack() {
_as_action_store.v_set_position(_as_action_store.i_get_size() - 1);
v_execute_one(xpath_construct::expr, false);
}
/// Retrieves one quadruplet from the action placeholder
void xpath_processor::v_pop_one_action(xpath_construct& xc_action, ///< Next rule on placeholder
xpath_sub& xp_sub, ///< Sub rule
unsigned& u_ref, ///< Rule optional parameter
string& S_literal) ///< Rule optional string
{
int i_1, i_2, i_3;
_as_action_store.v_get(_as_action_store.i_get_position(), i_1, i_2, i_3, S_literal);
xc_action = static_cast<xpath_construct>(i_1);
xp_sub = static_cast<xpath_sub>(i_2);
u_ref = i_3;
_as_action_store.v_dec_position();
}
/// Executes one XPath rule
void xpath_processor::v_execute_one(xpath_construct xc_rule, ///< Rule number
bool o_skip_only) ///< True if we only need to skip rules and not act on the data stack
{
xpath_construct xc_action;
xpath_sub xp_sub;
unsigned u_variable;
string S_literal;
string S_temp;
string S_name;
expression_result** erpp_arg;
unsigned u_arg;
bool o_error;
v_pop_one_action(xc_action, xp_sub, u_variable, S_literal);
// verify it's the rule we were waiting for
if (xc_action != xc_rule) {
throw execution_error(2);
}
switch (xc_action) {
case xpath_construct::expr:
// [14] Expr ::= OrExpr
v_execute_one(xpath_construct::or_expr, o_skip_only);
break;
case xpath_construct::or_expr:
switch (xp_sub) {
case xpath_sub::or_expr_simple:
v_execute_one(xpath_construct::and_expr, o_skip_only);
break;
case xpath_sub::or_expr_or:
o_error = false;
erpp_arg = nullptr;
try {
v_execute_one(xpath_construct::and_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::and_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
v_function_or(erpp_arg);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(3);
}
break;
case xpath_sub::or_expr_more: {
// These case is involved for expressions like a or b or c
try {
o_error = false;
erpp_arg = nullptr;
v_execute_one(xpath_construct::and_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::or_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
v_function_or(erpp_arg);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(7);
}
} break;
}
break;
case xpath_construct::and_expr:
switch (xp_sub) {
case xpath_sub::and_expr_simple:
v_execute_one(xpath_construct::equality_expr, o_skip_only);
break;
case xpath_sub::and_expr_and:
o_error = false;
erpp_arg = nullptr;
try {
v_execute_one(xpath_construct::equality_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::equality_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
v_function_and(erpp_arg);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(4);
}
break;
}
break;
case xpath_construct::equality_expr:
switch (xp_sub) {
case xpath_sub::equality_expr_simple:
v_execute_one(xpath_construct::relational_expr, o_skip_only);
break;
case xpath_sub::equality_expr_equal:
case xpath_sub::equality_expr_not_equal:
o_error = false;
erpp_arg = nullptr;
try {
v_execute_one(xpath_construct::relational_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::relational_expr,
o_skip_only); // this is buggy. should be xpath_construct::equality_expr
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
if (xp_sub == xpath_sub::equality_expr_equal) {
v_function_equal(erpp_arg);
} else {
v_function_not_equal(erpp_arg);
}
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(5);
}
break;
}
break;
case xpath_construct::relational_expr:
switch (xp_sub) {
case xpath_sub::relational_expr_simple:
v_execute_one(xpath_construct::additive_expr, o_skip_only);
break;
case xpath_sub::relational_expr_lt:
case xpath_sub::relational_expr_gt:
case xpath_sub::relational_expr_lte:
case xpath_sub::relational_expr_gte:
o_error = false;
erpp_arg = nullptr;
try {
v_execute_one(xpath_construct::additive_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::additive_expr,
o_skip_only); // this is buggy. should be xpath_construct::equality_expr
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
v_function_relational(erpp_arg, xp_sub);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(6);
}
break;
default:
assert(false);
}
break;
case xpath_construct::additive_expr:
switch (xp_sub) {
case xpath_sub::additive_expr_simple:
v_execute_one(xpath_construct::multiplicative_expr, o_skip_only);
break;
case xpath_sub::additive_expr_plus:
case xpath_sub::additive_expr_minus:
try {
o_error = false;
erpp_arg = nullptr;
v_execute_one(xpath_construct::multiplicative_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::multiplicative_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
if (xp_sub == xpath_sub::additive_expr_plus) {
v_function_plus(erpp_arg);
} else {
v_function_minus(erpp_arg);
}
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(7);
}
break;
case xpath_sub::additive_expr_more_plus:
case xpath_sub::additive_expr_more_minus:
// These 2 cases are involved for expressions like a+b+c
// The second argument is an additive expression, not a multiplicative as it is the case
// when single a+b expressions are encountered
try {
o_error = false;
erpp_arg = nullptr;
v_execute_one(xpath_construct::multiplicative_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::additive_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
if (xp_sub == xpath_sub::additive_expr_more_plus) {
v_function_plus(erpp_arg);
} else {
v_function_minus(erpp_arg);
}
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(7);
}
break;
}
break;
case xpath_construct::multiplicative_expr:
switch (xp_sub) {
case xpath_sub::multiplicative_expr_simple:
v_execute_one(xpath_construct::unary_expr, o_skip_only);
break;
case xpath_sub::multiplicative_expr_star:
case xpath_sub::multiplicative_expr_div:
case xpath_sub::multiplicative_expr_mod:
try {
o_error = false;
erpp_arg = nullptr;
v_execute_one(xpath_construct::unary_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg = new expression_result*[2];
memset(erpp_arg, 0, 2 * sizeof(expression_result*));
erpp_arg[1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
v_execute_one(xpath_construct::unary_expr, o_skip_only);
if (!o_skip_only) {
erpp_arg[0] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
v_function_mult(erpp_arg, xp_sub);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < 2; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(8);
}
break;
}
break;
case xpath_construct::unary_expr:
switch (xp_sub) {
case xpath_sub::unary_expr_simple:
// [27] UnaryExpr ::= UnionExpr
v_execute_one(xpath_construct::union_expr, o_skip_only);
break;
case xpath_sub::unary_expr_minus:
// [27] UnaryExpr ::= '-' UnaryExpr
v_execute_one(xpath_construct::unary_expr, o_skip_only);
v_function_opposite();
break;
}
break;
case xpath_construct::union_expr:
switch (xp_sub) {
case xpath_sub::union_expr_simple:
v_execute_one(xpath_construct::path_expr, o_skip_only);
break;
case xpath_sub::union_expr_union:
v_execute_one(xpath_construct::union_expr, o_skip_only);
if (_xs_stack.erp_top()->_e_type != e_node_set)
throw execution_error(9);
// here after is a block, so that the node_set are locals to it
{
node_set ns_1, ns_2;
ns_1 = ns_pop_node_set();
v_execute_one(xpath_construct::path_expr, o_skip_only);
ns_2 = ns_pop_node_set();
v_function_union(ns_1, ns_2);
}
break;
}
break;
case xpath_construct::path_expr:
switch (xp_sub) {
case xpath_sub::path_expr_location_path:
v_execute_one(xpath_construct::location_path, o_skip_only);
break;
case xpath_sub::path_expr_filter:
v_execute_one(xpath_construct::filter_expr, o_skip_only);
break;
case xpath_sub::path_expr_slash:
v_execute_one(xpath_construct::filter_expr, o_skip_only);
v_execute_one(xpath_construct::relative_location_path, o_skip_only);
break;
case xpath_sub::path_expr_2_slash:
v_execute_one(xpath_construct::filter_expr, o_skip_only);
v_execute_one(xpath_construct::relative_location_path, o_skip_only);
break;
}
break;
case xpath_construct::filter_expr:
switch (xp_sub) {
case xpath_sub::filter_expr_primary:
v_execute_one(xpath_construct::primary_expr, o_skip_only);
break;
case xpath_sub::filter_expr_predicate:
v_execute_one(xpath_construct::filter_expr, o_skip_only);
v_execute_one(xpath_construct::predicate, o_skip_only);
break;
}
break;
case xpath_construct::primary_expr:
switch (xp_sub) {
case xpath_sub::primary_expr_variable:
v_execute_one(xpath_construct::variable_reference, o_skip_only);
break;
case xpath_sub::primary_expr_paren_expr:
v_execute_one(xpath_construct::expr, o_skip_only);
break;
case xpath_sub::primary_expr_literal:
if (!o_skip_only)
v_push_string(S_literal);
break;
case xpath_sub::primary_expr_number:
if (!o_skip_only) {
if (strchr(S_literal.c_str(), '.'))
v_push_double(atof(S_literal.c_str()));
else
v_push_int(atoi(S_literal.c_str()), "primary number");
}
break;
case xpath_sub::primary_expr_function_call:
v_execute_one(xpath_construct::function_call, o_skip_only);
break;
}
break;
case xpath_construct::function_call:
erpp_arg = nullptr;
o_error = false;
try {
if (xp_sub != xpath_sub::absolute_location_path_slash_rel) { // TODO not sure if this is a valid check
// execute arguments
if (!o_skip_only) {
erpp_arg = new expression_result*[u_variable];
memset(erpp_arg, 0, u_variable * sizeof(expression_result*));
}
for (u_arg = 0; u_arg < u_variable; u_arg++) {
/// Compute each argument, and store them in a temporary list
v_execute_one(xpath_construct::argument, o_skip_only);
if (!o_skip_only) {
if (!_xs_stack.u_get_size()) {
throw execution_error(10);
}
erpp_arg[u_variable - u_arg - 1] = new expression_result(*_xs_stack.erp_top());
_xs_stack.v_pop();
}
}
}
v_execute_one(xpath_construct::xml_q_name, o_skip_only);
if (!o_skip_only) {
S_name = S_pop_string();
v_execute_function(S_name, u_variable, erpp_arg);
}
} catch (execution_error) {
o_error = true;
}
if (erpp_arg) {
for (u_arg = 0; u_arg < u_variable; u_arg++) {
if (erpp_arg[u_arg]) {
delete erpp_arg[u_arg];
}
}
delete[] erpp_arg;
}
if (o_error) {
throw execution_error(11);
}
break;
case xpath_construct::xml_q_name:
switch (xp_sub) {
case xpath_sub::xml_q_name_colon:
v_execute_one(xpath_construct::xml_local_part, o_skip_only);
v_execute_one(xpath_construct::xml_prefix, o_skip_only);
break;
case xpath_sub::xml_q_name_simple:
v_execute_one(xpath_construct::xml_local_part, o_skip_only);
break;
}
break;
case xpath_construct::xml_local_part:
if (!o_skip_only) {
v_push_string(S_literal);
}
break;
case xpath_construct::xml_prefix:
if (!o_skip_only) {
// we replace the current stack content (local_part) by the fully
// qualified XML name (prefix:local_part)
S_name = S_pop_string();
S_literal += ":";
S_literal += S_name;
v_push_string(S_literal);
}
break;
case xpath_construct::argument:
v_execute_one(xpath_construct::expr, o_skip_only);
break;
case xpath_construct::location_path:
switch (xp_sub) {
case xpath_sub::location_path_rel:
v_execute_one(xpath_construct::relative_location_path, o_skip_only);
break;
case xpath_sub::location_path_abs:
v_execute_one(xpath_construct::absolute_location_path, o_skip_only);
break;
}
break;
case xpath_construct::relative_location_path:
switch (xp_sub) {
case xpath_sub::relative_location_path_rel_step:
// RelativeLocationPath ::= RelativeLocationPath '/' Step
v_execute_one(xpath_construct::relative_location_path, o_skip_only);
// v_execute_step (i_relative_action);
break;
case xpath_sub::relative_location_path_rel_double_slash_step:
// RelativeLocationPath ::= RelativeLocationPath '//' Step
break;
case xpath_sub::relative_location_path_step:
// RelativeLocationPath ::= Step
int i_dummy;
i_dummy = -2;
v_execute_step(i_dummy, o_skip_only);
break;
}
break;
case xpath_construct::absolute_location_path:
switch (xp_sub) {
case xpath_sub::absolute_location_path_slash:
// AbsoluteLocationPath ::= '/'
v_execute_absolute_path(u_variable, false, false);
break;
case xpath_sub::absolute_location_path_slash_rel:
// AbsoluteLocationPath ::= '/' RelativeLocationPath
v_execute_absolute_path(u_variable, true, false);
break;
case xpath_sub::absolute_location_path_abbrev:
// AbsoluteLocationPath ::= AbbreviatedAbsoluteLocationPath
v_execute_absolute_path(u_variable, true, true);
break;
}
break;
case xpath_construct::axis_specifier:
switch (xp_sub) {
case xpath_sub::axis_specifier_at:
// AxisSpecifier ::= '@'
if (!o_skip_only)
// will be used in the v_execute_step
v_push_int(1, "axis specifier is at");
break;
case xpath_sub::axis_specifier_axis_name:
// AxisSpecifier ::= AxisName '::'
v_execute_one(xpath_construct::axis_name, o_skip_only);
break;
case xpath_sub::axis_specifier_empty:
if (!o_skip_only)
// will be used in the v_execute_step
v_push_int(0, "axis specifier is empty");
break;
}
break;
case xpath_construct::axis_name:
if (!o_skip_only)
// will be used in the v_execute_step
v_push_int(u_variable, "axis is a name");
break;
case xpath_construct::node_test:
// to do : processing instructions ???
switch (xp_sub) {
case xpath_sub::node_test_reserved_keyword:
// will be used in the v_execute_step
if (!o_skip_only) {
i_pop_int();
v_push_int(u_variable, "axis is a keyword");
S_temp = "*";
v_push_string(S_temp);
}
break;
case xpath_sub::node_test_pi:
break;
case xpath_sub::node_test_pi_lit:
break;
case xpath_sub::node_test_name_test:
v_execute_one(xpath_construct::name_test, o_skip_only);
break;
}
break;
case xpath_construct::predicate:
// [8] Predicate ::= '[' PredicateExpr ']'
v_execute_one(xpath_construct::predicate_expr, o_skip_only);
break;
case xpath_construct::predicate_expr:
// [9] PredicateExpr ::= Expr
v_execute_one(xpath_construct::expr, o_skip_only);
break;
case xpath_construct::name_test:
switch (xp_sub) {
case xpath_sub::name_test_star:
if (!o_skip_only) {
S_temp = "*";
v_push_string(S_temp);
}
break;
case xpath_sub::name_test_ncname:
break;
case xpath_sub::name_test_qname:
v_execute_one(xpath_construct::xml_q_name, o_skip_only);
break;
}
break;
default:
throw execution_error(12);
}
}
/// Execute a full set of absolute/relative/relative/.. computation
void xpath_processor::v_execute_absolute_path(
unsigned u_action_position, ///< Position of the placeholder after the rule
bool o_with_rel, ///< true if there is some relative path
bool o_everywhere) ///< true if it's a '//' path
{
unsigned u_end_action;
int i_relative_action;
bool o_do_last;
u_end_action = u_action_position;
if (o_with_rel) {
int i_1, i_2, i_3;
int i_current, i_first, i_relative;
string S_lit;
// compute position of the first (absolute) step
i_current = _as_action_store.i_get_position();
if (o_everywhere)
i_relative = i_current - 2;
else
i_relative = i_current - 1;
_as_action_store.v_get(i_relative, i_1, i_2, i_3, S_lit);
if (static_cast<xpath_construct>(i_1) == xpath_construct::relative_location_path) {
o_do_last = true;
i_first = i_3 - 1;
} else {
o_do_last = false;
i_first = i_relative;
}
// i_first = i_3 - 1;
_as_action_store.v_set_position(i_first);
if (o_everywhere)
i_relative_action = -1;
else
i_relative_action = 0;
v_execute_step(i_relative_action, false);
bool o_end = false;
do {
i_relative--;
_as_action_store.v_get(i_relative, i_1, i_2, i_3, S_lit);
if (static_cast<xpath_construct>(i_1) != xpath_construct::relative_location_path)
o_end = true;
else {
_as_action_store.v_set_position(i_3 - 1);
v_execute_step(i_relative_action, false);
}
} while (!o_end);
if (o_do_last) {
// apply last one
_as_action_store.v_set_position(i_relative);
v_execute_step(i_relative_action, false);
}
// resume the execution after the whole path construction
_as_action_store.v_set_position((int)u_end_action - 1);
}
}
/// One step execution
void xpath_processor ::v_execute_step(
int& i_relative_action, ///< Path position : -1 if first in a '//' path, 0 if first in a '/' path, > 0 if followers
bool o_skip_only) {
bool o_by_name;
int i_end_store, i_node_store, i_pred_store;
unsigned u_nb_node, u_node, u_pred, u_variable;
xpath_construct xc_action;
xpath_sub xp_sub;
string S_literal, S_name;
const XMLElement *XEp_child, *XEp_elem;
const XMLNode* XNp_father;
const XMLAttribute* XAp_attrib;
const XMLNode *XNp_next, *XNp_parent;
node_set ns_source, ns_target;
if (!o_skip_only) {
/// Initialize the source node set if it's the first step of a path
switch (i_relative_action) {
case -2:
// relative to context
ns_source.v_add_node_in_set(_XEp_context);
i_relative_action = 1;
break;
case -1:
// everywhere
ns_source.v_copy_selected_node_recursive_root_only(_XNp_base_parent, _XNp_base);
i_relative_action = 1;
break;
case 0:
// first absolute
ns_source.v_add_node_in_set(_XNp_base_parent);
i_relative_action = 1;
break;
default:
// second and following steps
ns_source = *(_xs_stack.erp_top()->nsp_get_node_set());
_xs_stack.v_pop();
break;
}
}
// Pop our step action from the action placeholder
v_pop_one_action(xc_action, xp_sub, u_variable, S_literal);
// Skip the predicates
i_pred_store = _as_action_store.i_get_position();
for (u_pred = 0; u_pred < u_variable; u_pred++) {
v_execute_one(xpath_construct::predicate, true);
}
i_node_store = _as_action_store.i_get_position();