Debian Codegraph @

src/tinyxpath/xpath_processor.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 www.sourceforge.net/projects/tinyxpath
3 Copyright (c) 2002-2004 Yves Berquin ([email protected])
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24
25 #include <cmath>
26 #include "xpath_processor.h"
27 #include "xml_util.h"
28
29 using namespace TinyXPath;
30
31 #ifdef TINYXPATH_DEBUG
32 // only define DUMP_ACTION if TINYXPATH_DEBUG is defined
33 #define DUMP_ACTION
34 #endif
35
36 /// Utility class to allow us to delete a TiXmlElement without its children
37 class TiXmlElementNoDelete : public TiXmlElement
38 {
39 public :
40 TiXmlElementNoDelete () : TiXmlElement ("") {} // this constructor is never used
41 void v_clean_children ()
42 {
43 firstChild=0;
44 lastChild=0;
45 }
46 } ;
47
48 /// Utility class to reset the parent, prev and next pointers of a node
49 class TiXmlNodeManip : public TiXmlNode
50 {
51 public :
52 TiXmlNodeManip () : TiXmlNode (TiXmlNode::UNKNOWN) {}
53 void v_reset (const TiXmlNode * XNp_parent, const TiXmlNode * XNp_next, const TiXmlNode * XNp_prev)
54 {
55 parent = (TiXmlNode *) XNp_parent;
56 next = (TiXmlNode *) XNp_next;
57 prev = (TiXmlNode *) XNp_prev;
58 }
59 void v_isolate ()
60 {
61 parent = next = prev = NULL;
62 }
63 } ;
64
65 /// xpath_processor constructor
66 xpath_processor::xpath_processor (
67 const TiXmlNode * XNp_source_tree, ///< Source XML tree
68 const char * cp_xpath_expr) ///< XPath expression
69 : xpath_stream (cp_xpath_expr)
70 {
71 if (XNp_source_tree && cp_xpath_expr)
72 XNp_base = XNp_source_tree;
73 else
74 XNp_base = NULL;
75 XEp_context = NULL;
76 o_is_context_by_name = false;
77 }
78
79 /// Compute an XPath expression, and return the number of nodes in the resulting node set.
80 /// \n Returns 0 if the result is not a node set
81 unsigned xpath_processor::u_compute_xpath_node_set ()
82 {
83 er_compute_xpath ();
84 if (er_result . e_type != e_node_set)
85 return 0;
86 return er_result . nsp_get_node_set () -> u_get_nb_node_in_set ();
87 }
88
89 /// Get one of the XML nodes from the resulting node set. Can only be used after a call to u_compute_xpath_node_set
90 void xpath_processor::v_get_xpath_base (
91 unsigned u_order, ///< Order of the node. Must be between 0 and the number of nodes - 1
92 const TiXmlBase * & XBp_res, ///< Output node
93 bool & o_attrib) ///< True if the output node is an attribute, false if it's a TiXmlNode
94 {
95 XBp_res = NULL;
96 o_attrib = false;
97 if (er_result . e_type != e_node_set)
98 return;
99 if (u_order >= er_result . nsp_get_node_set () -> u_get_nb_node_in_set ())
100 return;
101 XBp_res = er_result . nsp_get_node_set () -> XBp_get_base_in_set (u_order);
102 o_attrib = er_result . nsp_get_node_set () -> o_is_attrib (u_order);
103 }
104
105 /// Retrieves an XPath node from the node set. This assumes you know it's not an attribute
106 TiXmlNode * xpath_processor::XNp_get_xpath_node (
107 unsigned u_order) ///< Order of the node. Must be between 0 and the number of nodes - 1
108 {
109 bool o_attrib;
110 const TiXmlBase * XBp_res;
111
112 o_attrib = false;
113 XBp_res = NULL;
114 v_get_xpath_base (u_order, XBp_res, o_attrib);
115 if (o_attrib)
116 return NULL;
117 return (TiXmlNode *) XBp_res;
118 }
119
120 /// Retrieves an XPath attribute from the node set. This assumes you know it's an attribute
121 TiXmlAttribute * xpath_processor::XAp_get_xpath_attribute (
122 unsigned u_order) ///< Order of the node. Must be between 0 and the number of nodes - 1
123 {
124 bool o_attrib;
125 const TiXmlBase * XBp_res;
126
127 o_attrib = false;
128 XBp_res = NULL;
129 v_get_xpath_base (u_order, XBp_res, o_attrib);
130 if (! o_attrib)
131 return NULL;
132 return (TiXmlAttribute *) XBp_res;
133 }
134
135 void xpath_processor::v_build_root ()
136 {
137 if (XNp_base)
138 {
139 // backup the parent, prev and next node pointers
140 // we will restore them in the destructor
141 XNp_caller_parent = XNp_base -> Parent ();
142 XNp_caller_prev = XNp_base -> PreviousSibling ();
143 XNp_caller_next = XNp_base -> NextSibling ();
144
145 // now, isolate this element from it's parent and siblings
146 TiXmlNodeManip * XNp_false_node = (TiXmlNodeManip *) XNp_base;
147 XNp_false_node -> v_isolate ();
148
149 // create a new root for it
150 XEp_root = new TiXmlElement ("root");
151 XEp_root -> LinkEndChild ((TiXmlNode *) XNp_base);
152
153 // set the main node as the context one, if it's an element
154 if (XNp_base -> ToElement ())
155 XEp_context = XNp_base -> ToElement ();
156 v_order_tree ();
157 }
158 else
159 XEp_root = NULL;
160 }
161
162 /// Remove our fake root, and restore the original relationships of the
163 /// node given to us as argument
164 void xpath_processor::v_remove_root ()
165 {
166 if (XEp_root)
167 {
168 assert (XNp_base);
169 // reset the original relationships
170 TiXmlNodeManip * XNp_false_node = (TiXmlNodeManip *) XNp_base;
171 XNp_false_node -> v_reset (XNp_caller_parent, XNp_caller_next, XNp_caller_prev);
172
173 TiXmlElementNoDelete * XEp_false_root = (TiXmlElementNoDelete *) XEp_root;
174 XEp_false_root -> v_clean_children ();
175 delete XEp_false_root;
176 }
177 }
178
179 /// Compute an XPath expression
180 expression_result xpath_processor::er_compute_xpath ()
181 {
182 try
183 {
184 v_build_root ();
185
186 if (! XEp_root)
187 // no correct initialization of the xpath_processor object
188 throw execution_error (1);
189
190 // Decode XPath expression
191 v_evaluate ();
192
193 // Compute result
194 v_execute_stack ();
195
196 /// The executions stack need to contain 1 and only 1 element, otherwize it's not valid
197 if (xs_stack . u_get_size () == 1)
198 {
199 er_result = * xs_stack . erp_top ();
200 xs_stack . v_pop ();
201 e_error = e_no_error;
202 }
203 else
204 {
205 expression_result er_null;
206 er_result = er_null;
207 e_error = e_error_stack;
208 }
209 }
210 catch (syntax_error)
211 {
212 expression_result er_null;
213 er_result = er_null;
214 e_error = e_error_syntax;
215 }
216 catch (syntax_overflow)
217 {
218 expression_result er_null;
219 er_result = er_null;
220 e_error = e_error_overflow;
221 }
222 catch (execution_error)
223 {
224 expression_result er_null;
225 er_result = er_null;
226 e_error = e_error_execution;
227 }
228
229 v_remove_root ();
230
231 return er_result;
232 }
233
234 /// Compute an XPath expression and return the result as a string
235 TIXML_STRING xpath_processor::S_compute_xpath ()
236 {
237 expression_result er_res;
238 TIXML_STRING S_res;
239
240 er_res = er_compute_xpath ();
241 S_res = er_res . S_get_string ();
242 return S_res;
243 }
244
245 /// Compute an XPath expression and return the result as an integer
246 int xpath_processor::i_compute_xpath ()
247 {
248 expression_result er_res;
249 int i_res;
250
251 er_res = er_compute_xpath ();
252 i_res = er_res . i_get_int ();
253 return i_res;
254 }
255
256 bool xpath_processor::o_compute_xpath ()
257 {
258 expression_result er_res;
259 bool o_res;
260
261 er_res = er_compute_xpath ();
262 o_res = er_res . o_get_bool ();
263 return o_res;
264 }
265
266 double xpath_processor::d_compute_xpath ()
267 {
268 expression_result er_res;
269 double d_res;
270
271 er_res = er_compute_xpath ();
272 d_res = er_res . d_get_double ();
273 return d_res;
274 }
275
276 /// Callback from the XPath decoder : a rule has to be applied
277 void xpath_processor::v_action (
278 xpath_construct xc_rule, ///< XPath Rule
279 unsigned u_sub, ///< Rule sub number
280 unsigned u_variable, ///< Parameter, depends on the rule
281 const char * cp_literal) ///< Input literal, depends on the rule
282 {
283 as_action_store . v_add (xc_rule, u_sub, u_variable, cp_literal);
284 #ifdef TINYXPATH_DEBUG
285 printf ("Action %2d : %s (%d,%d,%s)\n", as_action_store . i_get_size () - 1, cp_disp_construct (xc_rule), u_sub, u_variable, cp_literal);
286 #endif
287 }
288
289 /// Internal use. Retrieves the current action counter
290 int xpath_processor::i_get_action_counter ()
291 {
292 // callback for current stack position
293 return as_action_store . i_get_size ();
294 }
295
296 /// Internal use. Executes the XPath expression. The executions starts at the end of the as_action_store list
297 void xpath_processor::v_execute_stack ()
298 {
299 as_action_store . v_set_position (as_action_store . i_get_size () - 1);
300 v_execute_one (xpath_expr, false);
301 }
302
303 /// Retrieves one quadruplet from the action placeholder
304 void xpath_processor::v_pop_one_action (
305 xpath_construct & xc_action, ///< Next rule on placeholder
306 unsigned & u_sub, ///< Sub rule
307 unsigned & u_ref, ///< Rule optional parameter
308 TIXML_STRING & S_literal) ///< Rule optional string
309 {
310 int i_1, i_2, i_3;
311
312 as_action_store . v_get (as_action_store . i_get_position (), i_1, i_2, i_3, S_literal);
313 xc_action = (xpath_construct) i_1;
314 u_sub = i_2;
315 u_ref = i_3;
316 as_action_store . v_dec_position ();
317 }
318
319 /// Executes one XPath rule
320 void xpath_processor::v_execute_one (
321 xpath_construct xc_rule, ///< Rule number
322 bool o_skip_only) ///< True if we only need to skip rules and not act on the data stack
323 {
324 xpath_construct xc_action;
325 unsigned u_sub;
326 unsigned u_variable;
327 TIXML_STRING S_literal;
328 TIXML_STRING S_temp;
329 TIXML_STRING S_name;
330 expression_result ** erpp_arg;
331 unsigned u_arg;
332 bool o_error;
333
334 v_pop_one_action (xc_action, u_sub, u_variable, S_literal);
335 // verify it's the rule we were waiting for
336 if (xc_action != xc_rule)
337 throw execution_error (2);
338 switch (xc_action)
339 {
340 case xpath_expr :
341 // [14] Expr ::= OrExpr
342 v_execute_one (xpath_or_expr, o_skip_only);
343 break;
344
345 case xpath_or_expr :
346 switch (u_sub)
347 {
348 case xpath_or_expr_simple :
349 v_execute_one (xpath_and_expr, o_skip_only);
350 break;
351 case xpath_or_expr_or :
352 o_error = false;
353 erpp_arg = NULL;
354 try
355 {
356 v_execute_one (xpath_and_expr, o_skip_only);
357 if (! o_skip_only)
358 {
359 erpp_arg = new expression_result * [2];
360 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
361 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
362 xs_stack . v_pop ();
363 }
364 v_execute_one (xpath_and_expr, o_skip_only);
365 if (! o_skip_only)
366 {
367 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
368 xs_stack . v_pop ();
369 v_function_or (erpp_arg);
370 }
371 }
372 catch (execution_error)
373 {
374 o_error = true;
375 }
376 if (erpp_arg)
377 {
378 for (u_arg = 0; u_arg < 2; u_arg++)
379 {
380 if (erpp_arg [u_arg])
381 delete erpp_arg [u_arg];
382 }
383 delete [] erpp_arg;
384 }
385 if (o_error)
386 throw execution_error (3);
387 break;
388 }
389 break;
390
391 case xpath_and_expr :
392 switch (u_sub)
393 {
394 case xpath_and_expr_simple :
395 v_execute_one (xpath_equality_expr, o_skip_only);
396 break;
397 case xpath_and_expr_and :
398 o_error = false;
399 erpp_arg = NULL;
400 try
401 {
402 v_execute_one (xpath_equality_expr, o_skip_only);
403 if (! o_skip_only)
404 {
405 erpp_arg = new expression_result * [2];
406 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
407 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
408 xs_stack . v_pop ();
409 }
410 v_execute_one (xpath_equality_expr, o_skip_only);
411 if (! o_skip_only)
412 {
413 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
414 xs_stack . v_pop ();
415 v_function_and (erpp_arg);
416 }
417 }
418 catch (execution_error)
419 {
420 o_error = true;
421 }
422 if (erpp_arg)
423 {
424 for (u_arg = 0; u_arg < 2; u_arg++)
425 {
426 if (erpp_arg [u_arg])
427 delete erpp_arg [u_arg];
428 }
429 delete [] erpp_arg;
430 }
431 if (o_error)
432 throw execution_error (4);
433 break;
434 }
435 break;
436
437 case xpath_equality_expr :
438 switch (u_sub)
439 {
440 case xpath_equality_expr_simple :
441 v_execute_one (xpath_relational_expr, o_skip_only);
442 break;
443 case xpath_equality_expr_equal :
444 case xpath_equality_expr_not_equal :
445 o_error = false;
446 erpp_arg = NULL;
447 try
448 {
449 v_execute_one (xpath_relational_expr, o_skip_only);
450 if (! o_skip_only)
451 {
452 erpp_arg = new expression_result * [2];
453 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
454 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
455 xs_stack . v_pop ();
456 }
457 v_execute_one (xpath_relational_expr, o_skip_only); // this is buggy. should be xpath_equality_expr
458 if (! o_skip_only)
459 {
460 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
461 xs_stack . v_pop ();
462 if (u_sub == xpath_equality_expr_equal)
463 v_function_equal (erpp_arg);
464 else
465 v_function_not_equal (erpp_arg);
466 }
467 }
468 catch (execution_error)
469 {
470 o_error = true;
471 }
472 if (erpp_arg)
473 {
474 for (u_arg = 0; u_arg < 2; u_arg++)
475 {
476 if (erpp_arg [u_arg])
477 delete erpp_arg [u_arg];
478 }
479 delete [] erpp_arg;
480 }
481 if (o_error)
482 throw execution_error (5);
483 break;
484 }
485 break;
486
487 case xpath_relational_expr :
488 switch (u_sub)
489 {
490 case xpath_relational_expr_simple :
491 v_execute_one (xpath_additive_expr, o_skip_only);
492 break;
493 case xpath_relational_expr_lt :
494 case xpath_relational_expr_gt :
495 case xpath_relational_expr_lte :
496 case xpath_relational_expr_gte :
497 o_error = false;
498 erpp_arg = NULL;
499 try
500 {
501 v_execute_one (xpath_additive_expr, o_skip_only);
502 if (! o_skip_only)
503 {
504 erpp_arg = new expression_result * [2];
505 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
506 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
507 xs_stack . v_pop ();
508 }
509 v_execute_one (xpath_additive_expr, o_skip_only); // this is buggy. should be xpath_equality_expr
510 if (! o_skip_only)
511 {
512 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
513 xs_stack . v_pop ();
514 v_function_relational (erpp_arg, u_sub);
515 }
516 }
517 catch (execution_error)
518 {
519 o_error = true;
520 }
521 if (erpp_arg)
522 {
523 for (u_arg = 0; u_arg < 2; u_arg++)
524 {
525 if (erpp_arg [u_arg])
526 delete erpp_arg [u_arg];
527 }
528 delete [] erpp_arg;
529 }
530 if (o_error)
531 throw execution_error (6);
532 break;
533 default :
534 assert (false);
535 }
536 break;
537
538 case xpath_additive_expr :
539 switch (u_sub)
540 {
541 case xpath_additive_expr_simple :
542 v_execute_one (xpath_multiplicative_expr, o_skip_only);
543 break;
544 case xpath_additive_expr_plus :
545 case xpath_additive_expr_minus :
546 try
547 {
548 o_error = false;
549 erpp_arg = NULL;
550 v_execute_one (xpath_multiplicative_expr, o_skip_only);
551 if (! o_skip_only)
552 {
553 erpp_arg = new expression_result * [2];
554 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
555 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
556 xs_stack . v_pop ();
557 }
558 v_execute_one (xpath_multiplicative_expr, o_skip_only);
559 if (! o_skip_only)
560 {
561 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
562 xs_stack . v_pop ();
563 if (u_sub == xpath_additive_expr_plus)
564 v_function_plus (erpp_arg);
565 else
566 v_function_minus (erpp_arg);
567 }
568 }
569 catch (execution_error)
570 {
571 o_error = true;
572 }
573 if (erpp_arg)
574 {
575 for (u_arg = 0; u_arg < 2; u_arg++)
576 {
577 if (erpp_arg [u_arg])
578 delete erpp_arg [u_arg];
579 }
580 delete [] erpp_arg;
581 }
582 if (o_error)
583 throw execution_error (7);
584 break;
585 case xpath_additive_expr_more_plus :
586 case xpath_additive_expr_more_minus :
587 // These 2 cases are involved for expressions like a+b+c
588 // The second argument is an additive expression, not a multiplicative as it is the case
589 // when single a+b expressions are encountered
590 try
591 {
592 o_error = false;
593 erpp_arg = NULL;
594 v_execute_one (xpath_multiplicative_expr, o_skip_only);
595 if (! o_skip_only)
596 {
597 erpp_arg = new expression_result * [2];
598 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
599 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
600 xs_stack . v_pop ();
601 }
602 v_execute_one (xpath_additive_expr, o_skip_only);
603 if (! o_skip_only)
604 {
605 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
606 xs_stack . v_pop ();
607 if (u_sub == xpath_additive_expr_more_plus)
608 v_function_plus (erpp_arg);
609 else
610 v_function_minus (erpp_arg);
611 }
612 }
613 catch (execution_error)
614 {
615 o_error = true;
616 }
617 if (erpp_arg)
618 {
619 for (u_arg = 0; u_arg < 2; u_arg++)
620 {
621 if (erpp_arg [u_arg])
622 delete erpp_arg [u_arg];
623 }
624 delete [] erpp_arg;
625 }
626 if (o_error)
627 throw execution_error (7);
628 break;
629 }
630 break;
631
632 case xpath_multiplicative_expr :
633 switch (u_sub)
634 {
635 case xpath_multiplicative_expr_simple :
636 v_execute_one (xpath_unary_expr, o_skip_only);
637 break;
638 case xpath_multiplicative_expr_star :
639 case xpath_multiplicative_expr_div :
640 case xpath_multiplicative_expr_mod :
641 try
642 {
643 o_error = false;
644 erpp_arg = NULL;
645 v_execute_one (xpath_unary_expr, o_skip_only);
646 if (! o_skip_only)
647 {
648 erpp_arg = new expression_result * [2];
649 memset (erpp_arg, 0, 2 * sizeof (expression_result *));
650 erpp_arg [1] = new expression_result (* xs_stack . erp_top ());
651 xs_stack . v_pop ();
652 }
653 v_execute_one (xpath_unary_expr, o_skip_only);
654 if (! o_skip_only)
655 {
656 erpp_arg [0] = new expression_result (* xs_stack . erp_top ());
657 xs_stack . v_pop ();
658 v_function_mult (erpp_arg, u_sub);
659 }
660 }
661 catch (execution_error)
662 {
663 o_error = true;
664 }
665 if (erpp_arg)
666 {
667 for (u_arg = 0; u_arg < 2; u_arg++)
668 {
669 if (erpp_arg [u_arg])
670 delete erpp_arg [u_arg];
671 }
672 delete [] erpp_arg;
673 }
674 if (o_error)
675 throw execution_error (8);
676 break;
677 }
678 break;
679 case xpath_unary_expr :
680 switch (u_sub)
681 {
682 case xpath_unary_expr_simple :
683 // [27] UnaryExpr ::= UnionExpr
684 v_execute_one (xpath_union_expr, o_skip_only);
685 break;
686 case xpath_unary_expr_minus :
687 // [27] UnaryExpr ::= '-' UnaryExpr
688 v_execute_one (xpath_unary_expr, o_skip_only);
689 v_function_opposite ();
690 break;
691 }
692 break;
693
694 case xpath_union_expr :
695 switch (u_sub)
696 {
697 case xpath_union_expr_simple :
698 v_execute_one (xpath_path_expr, o_skip_only);
699 break;
700 case xpath_union_expr_union :
701 v_execute_one (xpath_union_expr, o_skip_only);
702 if (xs_stack . erp_top () -> e_type != e_node_set)
703 throw execution_error (9);
704 // here after is a block, so that the node_set are locals to it
705 {
706 node_set ns_1, ns_2;
707 ns_1 = ns_pop_node_set ();
708 v_execute_one (xpath_path_expr, o_skip_only);
709 ns_2 = ns_pop_node_set ();
710 v_function_union (ns_1, ns_2);
711 }
712 break;
713 }
714 break;
715
716 case xpath_path_expr :
717 switch (u_sub)
718 {
719 case xpath_path_expr_location_path :
720 v_execute_one (xpath_location_path, o_skip_only);
721 break;
722 case xpath_path_expr_filter :
723 v_execute_one (xpath_filter_expr, o_skip_only);
724 break;
725 case xpath_path_expr_slash :
726 v_execute_one (xpath_filter_expr, o_skip_only);
727 v_execute_one (xpath_relative_location_path, o_skip_only);
728 break;
729 case xpath_path_expr_2_slash :
730 v_execute_one (xpath_filter_expr, o_skip_only);
731 v_execute_one (xpath_relative_location_path, o_skip_only);
732 break;
733 }
734 break;
735
736 case xpath_filter_expr :
737 switch (u_sub)
738 {
739 case xpath_filter_expr_primary :
740 v_execute_one (xpath_primary_expr, o_skip_only);
741 break;
742 case xpath_filter_expr_predicate :
743 v_execute_one (xpath_filter_expr, o_skip_only);
744 v_execute_one (xpath_predicate, o_skip_only);
745 break;
746 }
747 break;
748
749 case xpath_primary_expr :
750 switch (u_sub)
751 {
752 case xpath_primary_expr_variable :
753 v_execute_one (xpath_variable_reference, o_skip_only);
754 break;
755 case xpath_primary_expr_paren_expr :
756 v_execute_one (xpath_expr, o_skip_only);
757 break;
758 case xpath_primary_expr_literal :
759 if (! o_skip_only)
760 v_push_string (S_literal);
761 break;
762 case xpath_primary_expr_number :
763 if (! o_skip_only)
764 {
765 if (strchr (S_literal . c_str (), '.'))
766 v_push_double (atof (S_literal . c_str ()));
767 else
768 v_push_int (atoi (S_literal . c_str ()), "primary number");
769 }
770 break;
771 case xpath_primary_expr_function_call :
772 v_execute_one (xpath_function_call, o_skip_only);
773 break;
774 }
775 break;
776
777 case xpath_function_call :
778 erpp_arg = NULL;
779 o_error = false;
780 try
781 {
782 if (u_sub)
783 {
784 // execute arguments
785 if (! o_skip_only)
786 {
787 erpp_arg = new expression_result * [u_variable];
788 memset (erpp_arg, 0, u_variable * sizeof (expression_result *));
789 }
790 for (u_arg = 0; u_arg < u_variable; u_arg++)
791 {
792 /// Compute each argument, and store them in a temporary list
793 v_execute_one (xpath_argument, o_skip_only);
794 if (! o_skip_only)
795 {
796 if (! xs_stack . u_get_size ())
797 throw execution_error (10);
798 erpp_arg [u_variable - u_arg - 1] = new expression_result (* xs_stack . erp_top ());
799 xs_stack . v_pop ();
800 }
801 }
802 }
803 v_execute_one (xpath_xml_q_name, o_skip_only);
804 if (! o_skip_only)
805 {
806 S_name = S_pop_string ();
807 v_execute_function (S_name, u_variable, erpp_arg);
808 }
809 }
810 catch (execution_error)
811 {
812 o_error = true;
813 }
814 if (erpp_arg)
815 {
816 for (u_arg = 0; u_arg < u_variable; u_arg++)
817 {
818 if (erpp_arg [u_arg])
819 delete erpp_arg [u_arg];
820 }
821 delete [] erpp_arg;
822 }
823 if (o_error)
824 throw execution_error (11);
825 break;
826
827 case xpath_xml_q_name :
828 switch (u_sub)
829 {
830 case xpath_xml_q_name_colon :
831 v_execute_one (xpath_xml_prefix, o_skip_only);
832 v_execute_one (xpath_xml_local_part, o_skip_only);
833 break;
834 case xpath_xml_q_name_simple :
835 v_execute_one (xpath_xml_local_part, o_skip_only);
836 break;
837 }
838 break;
839
840 case xpath_xml_local_part :
841 if (! o_skip_only)
842 v_push_string (S_literal);
843 break;
844
845 case xpath_xml_prefix :
846 if (! o_skip_only)
847 v_push_string (S_literal);
848 break;
849
850 case xpath_argument :
851 v_execute_one (xpath_expr, o_skip_only);
852 break;
853
854 case xpath_location_path :
855 switch (u_sub)
856 {
857 case xpath_location_path_rel :
858 v_execute_one (xpath_relative_location_path, o_skip_only);
859 break;
860 case xpath_location_path_abs :
861 v_execute_one (xpath_absolute_location_path, o_skip_only);
862 break;
863 }
864 break;
865
866 case xpath_relative_location_path :
867 switch (u_sub)
868 {
869 case xpath_relative_location_path_rel_step :
870 // RelativeLocationPath ::= RelativeLocationPath '/' Step
871 v_execute_one (xpath_relative_location_path, o_skip_only);
872 // v_execute_step (i_relative_action);
873 break;
874 case xpath_relative_location_path_rel_double_slash_step :
875 // RelativeLocationPath ::= RelativeLocationPath '//' Step
876 break;
877 case xpath_relative_location_path_step :
878 // RelativeLocationPath ::= Step
879 int i_dummy;
880 i_dummy = -2;
881 v_execute_step (i_dummy, o_skip_only);
882 break;
883 }
884 break;
885
886 case xpath_absolute_location_path :
887 switch (u_sub)
888 {
889 case xpath_absolute_location_path_slash :
890 // AbsoluteLocationPath ::= '/'
891 v_execute_absolute_path (u_variable, false, false);
892 break;
893 case xpath_absolute_location_path_slash_rel :
894 // AbsoluteLocationPath ::= '/' RelativeLocationPath
895 v_execute_absolute_path (u_variable, true, false);
896 break;
897 case xpath_absolute_location_path_abbrev :
898 // AbsoluteLocationPath ::= AbbreviatedAbsoluteLocationPath
899 v_execute_absolute_path (u_variable, true, true);
900 break;
901 }
902 break;
903
904 case xpath_axis_specifier :
905 switch (u_sub)
906 {
907 case xpath_axis_specifier_at :
908 // AxisSpecifier ::= '@'
909 if (! o_skip_only)
910 // will be used in the v_execute_step
911 v_push_int (1, "axis specifier is at");
912 break;
913 case xpath_axis_specifier_axis_name :
914 // AxisSpecifier ::= AxisName '::'
915 v_execute_one (xpath_axis_name, o_skip_only);
916 break;
917 case xpath_axis_specifier_empty :
918 if (! o_skip_only)
919 // will be used in the v_execute_step
920 v_push_int (0, "axis specifier is empty");
921 break;
922 }
923 break;
924
925 case xpath_axis_name :
926 if (! o_skip_only)
927 // will be used in the v_execute_step
928 v_push_int (u_variable, "axis is a name");
929 break;
930
931 case xpath_node_test :
932 // to do : processing instructions ???
933 switch (u_sub)
934 {
935 case xpath_node_test_reserved_keyword :
936 // will be used in the v_execute_step
937 if (! o_skip_only)
938 {
939 i_pop_int ();
940 v_push_int (u_variable, "axis is a keyword");
941 S_temp = "*";
942 v_push_string (S_temp);
943 }
944 break;
945 case xpath_node_test_pi :
946 break;
947 case xpath_node_test_pi_lit :
948 break;
949 case xpath_node_test_name_test :
950 v_execute_one (xpath_name_test, o_skip_only);
951 break;
952 }
953 break;
954
955 case xpath_predicate :
956 // [8] Predicate ::= '[' PredicateExpr ']'
957 v_execute_one (xpath_predicate_expr, o_skip_only);
958 break;
959
960 case xpath_predicate_expr :
961 // [9] PredicateExpr ::= Expr
962 v_execute_one (xpath_expr, o_skip_only);
963 break;
964
965 case xpath_name_test :
966 switch (u_sub)
967 {
968 case xpath_name_test_star :
969 if (! o_skip_only)
970 {
971 S_temp = "*";
972 v_push_string (S_temp);
973 }
974 break;
975 case xpath_name_test_ncname :
976 break;
977 case xpath_name_test_qname :
978 v_execute_one (xpath_xml_q_name, o_skip_only);
979 break;
980 }
981 break;
982
983 default :
984 throw execution_error (12);
985 }
986 }
987
988 /// Execute a full set of absolute/relative/relative/.. computation
989 void xpath_processor::v_execute_absolute_path (
990 unsigned u_action_position, ///< Position of the placeholder after the rule
991 bool o_with_rel, ///< true if there is some relative path
992 bool o_everywhere) ///< true if it's a '//' path
993 {
994 unsigned u_end_action;
995 int i_relative_action;
996 bool o_do_last;
997
998 u_end_action = u_action_position;
999
1000 if (o_with_rel)
1001 {
1002 int i_1, i_2, i_3;
1003 int i_bak_position, i_current, i_first, i_relative;
1004 TIXML_STRING S_lit;
1005
1006 // compute position of the first (absolute) step
1007 i_current = as_action_store . i_get_position ();
1008 if (o_everywhere)
1009 i_relative = i_current - 2;
1010 else
1011 i_relative = i_current - 1;
1012 as_action_store . v_get (i_relative, i_1, i_2, i_3, S_lit);
1013 if (i_1 == xpath_relative_location_path)
1014 {
1015 o_do_last = true;
1016 i_first = i_3 - 1;
1017 }
1018 else
1019 {
1020 o_do_last = false;
1021 i_first = i_relative;
1022 }
1023 // i_first = i_3 - 1;
1024 i_bak_position = as_action_store . i_get_position ();
1025 as_action_store . v_set_position (i_first);
1026 if (o_everywhere)
1027 i_relative_action = -1;
1028 else
1029 i_relative_action = 0;
1030 v_execute_step (i_relative_action, false);
1031
1032 bool o_end = false;
1033 do
1034 {
1035 i_relative--;
1036 as_action_store . v_get (i_relative, i_1, i_2, i_3, S_lit);
1037 if (i_1 != xpath_relative_location_path)
1038 o_end = true;
1039 else
1040 {
1041 as_action_store . v_set_position (i_3 - 1);
1042 v_execute_step (i_relative_action, false);
1043 }
1044 } while (! o_end);
1045
1046 if (o_do_last)
1047 {
1048 // apply last one
1049
1050 as_action_store . v_set_position (i_relative);
1051 v_execute_step (i_relative_action, false);
1052 }
1053 // resume the execution after the whole path construction
1054 as_action_store . v_set_position ((int) u_end_action - 1);
1055 }
1056 }
1057
1058 /// One step execution
1059 void xpath_processor ::v_execute_step (
1060 int & i_relative_action, ///< Path position : -1 if first in a '//' path, 0 if first in a '/' path, > 0 if followers
1061 bool o_skip_only)
1062 {
1063 bool o_by_name;
1064 int i_axis_type, i_end_store, i_node_store, i_pred_store;
1065 unsigned u_nb_node, u_node, u_pred, u_sub, u_variable;
1066 xpath_construct xc_action;
1067 TIXML_STRING S_literal, S_name;
1068 const TiXmlElement * XEp_child, * XEp_elem;
1069 const TiXmlElement * XEp_father;
1070 const TiXmlAttribute * XAp_attrib;
1071 const TiXmlNode * XNp_next, * XNp_parent;
1072 node_set ns_source, ns_target;
1073
1074 if (! o_skip_only)
1075 {
1076 /// Initialize the source node set if it's the first step of a path
1077 switch (i_relative_action)
1078 {
1079 case -2 :
1080 // relative to context
1081 ns_source . v_add_node_in_set (XEp_context);
1082 i_relative_action = 1;
1083 break;
1084 case -1 :
1085 // everywhere
1086 ns_source . v_copy_selected_node_recursive (XEp_root);
1087 i_relative_action = 1;
1088 break;
1089 case 0 :
1090 // first absolute
1091 ns_source . v_add_node_in_set (XEp_root);
1092 i_relative_action = 1;
1093 break;
1094 default :
1095 // second and following steps
1096 ns_source = * (xs_stack . erp_top () -> nsp_get_node_set ());
1097 xs_stack . v_pop ();
1098 break;
1099 }
1100 }
1101
1102 // Pop our step action from the action placeholder
1103 v_pop_one_action (xc_action, u_sub, u_variable, S_literal);
1104
1105 // Skip the predicates
1106 i_pred_store = as_action_store . i_get_position ();
1107 for (u_pred = 0; u_pred < u_variable; u_pred++)
1108 v_execute_one (xpath_predicate, true);
1109 i_node_store = as_action_store . i_get_position ();
1110
1111 // Skip the node test
1112 v_execute_one (xpath_node_test, true);
1113
1114 // Run the axis
1115 v_execute_one (xpath_axis_specifier, o_skip_only);
1116 i_end_store = as_action_store . i_get_position ();
1117
1118 // Run the node test
1119 as_action_store . v_set_position (i_node_store);
1120 v_execute_one (xpath_node_test, o_skip_only);
1121 as_action_store . v_set_position (i_pred_store);
1122
1123 if (! o_skip_only)
1124 {
1125 S_name = S_pop_string ();
1126 o_by_name = ! (S_name == "*");
1127
1128 // Retrieve the archive flag stored by the xpath_axis_specifier rule execution
1129 // o_attrib_flag = o_pop_bool ();
1130 i_axis_type = i_pop_int ();
1131
1132 u_nb_node = ns_source . u_get_nb_node_in_set ();
1133 for (u_node = 0; u_node < u_nb_node; u_node++)
1134 {
1135 if (! ns_source . o_is_attrib (u_node))
1136 {
1137 XEp_father = ns_source . XNp_get_node_in_set (u_node) -> ToElement ();
1138 if (XEp_father)
1139 {
1140 switch (i_axis_type)
1141 {
1142 case 0 :
1143 case lex_child :
1144 // none
1145 XEp_child = XEp_father -> FirstChildElement ();
1146 while (XEp_child)
1147 {
1148 ns_target . v_add_node_in_set_if_name_or_star (XEp_child, S_name);
1149 XEp_child = XEp_child -> NextSiblingElement ();
1150 }
1151 break;
1152 case 1 :
1153 case lex_attribute :
1154 // @
1155 XAp_attrib = XEp_father -> FirstAttribute ();
1156 while (XAp_attrib)
1157 {
1158 ns_target . v_add_attrib_in_set_if_name_or_star (XAp_attrib, S_name);
1159 XAp_attrib = XAp_attrib -> Next ();
1160 }
1161 break;
1162 case lex_parent :
1163 XNp_parent = XEp_father -> Parent ();
1164 if (XNp_parent)
1165 ns_target . v_add_node_in_set_if_name_or_star (XNp_parent, S_name);
1166 break;
1167 case lex_ancestor :
1168 XNp_parent = XEp_father -> Parent ();
1169 // we have to exclude our own dummy parent
1170 while (XNp_parent && XNp_parent != XEp_root)
1171 {
1172 ns_target . v_add_node_in_set_if_name_or_star (XNp_parent, S_name);
1173 XNp_parent = XNp_parent -> Parent ();
1174 }
1175 break;
1176 case lex_ancestor_or_self :
1177 if (XEp_father != XEp_root)
1178 ns_target . v_add_node_in_set_if_name_or_star (XEp_father, S_name);
1179 XNp_parent = XEp_father -> Parent ();
1180 // we have to exclude our own dummy parent
1181 while (XNp_parent && XNp_parent != XEp_root)
1182 {
1183 ns_target . v_add_node_in_set_if_name_or_star (XNp_parent, S_name);
1184 XNp_parent = XNp_parent -> Parent ();
1185 }
1186 break;
1187 case lex_following_sibling :
1188 XNp_next = XEp_father -> NextSiblingElement ();
1189 while (XNp_next)
1190 {
1191 ns_target . v_add_node_in_set_if_name_or_star (XNp_next, S_name);
1192 XNp_next = XNp_next -> NextSiblingElement ();
1193 }
1194 break;
1195 case lex_preceding_sibling :
1196 XNp_next = XEp_father -> PreviousSibling ();
1197 while (XNp_next)
1198 {
1199 if (XNp_next -> Type () == TiXmlNode::ELEMENT)
1200 ns_target . v_add_node_in_set_if_name_or_star (XNp_next, S_name);
1201 XNp_next = XNp_next -> PreviousSibling ();
1202 }
1203 break;
1204 case lex_descendant :
1205 if (S_name == "*")
1206 ns_target . v_copy_selected_node_recursive_no_attrib (XEp_father, NULL);
1207 else
1208 ns_target . v_copy_selected_node_recursive_no_attrib (XEp_father, S_name . c_str ());
1209 break;
1210 case lex_descendant_or_self :
1211 if (XEp_father != XEp_root)
1212 ns_target . v_add_node_in_set_if_name_or_star (XEp_father, S_name);
1213 if (S_name == "*")
1214 ns_target . v_copy_selected_node_recursive_no_attrib (XEp_father, NULL);
1215 else
1216 ns_target . v_copy_selected_node_recursive_no_attrib (XEp_father, S_name . c_str ());
1217 break;
1218 case lex_self :
1219 if (XEp_father != XEp_root)
1220 ns_target . v_add_node_in_set_if_name_or_star (XEp_father, S_name);
1221 break;
1222 case lex_following :
1223 ns_target . v_add_all_foll_node (XEp_father, S_name);
1224 break;
1225 case lex_preceding :
1226 ns_target . v_add_all_prec_node (XEp_father, S_name);
1227 break;
1228 case lex_comment :
1229 XNp_next = XEp_father -> FirstChild ();
1230 while (XNp_next)
1231 {
1232 if (XNp_next -> Type () == TiXmlNode::COMMENT)
1233 ns_target . v_add_node_in_set (XNp_next);
1234 XNp_next = XNp_next -> NextSibling ();
1235 }
1236 break;
1237 case lex_text :
1238 XNp_next = XEp_father -> FirstChild ();
1239 while (XNp_next)
1240 {
1241 if (XNp_next -> Type () == TiXmlNode::TEXT)
1242 ns_target . v_add_node_in_set (XNp_next);
1243 XNp_next = XNp_next -> NextSibling ();
1244 }
1245 break;
1246 case lex_node :
1247 XNp_next = XEp_father -> FirstChild ();
1248 while (XNp_next)
1249 {
1250 ns_target . v_add_node_in_set (XNp_next);
1251 XNp_next = XNp_next -> NextSibling ();
1252 }
1253 break;
1254 default :
1255 // an axis name followed by '::'
1256 throw error_not_yet ();
1257 break;
1258 }
1259 }
1260 }
1261 }
1262
1263 if (u_variable)
1264 {
1265 // we have predicates to apply
1266 node_set ns_after_predicate;
1267 for (u_node = 0; u_node < ns_target .u_get_nb_node_in_set (); u_node++)
1268 {
1269 if (! ns_target . o_is_attrib (u_node))
1270 {
1271 XEp_elem = ns_target . XNp_get_node_in_set (u_node) -> ToElement ();
1272 if (XEp_elem)
1273 {
1274 as_action_store . v_set_position (i_pred_store);
1275 if (o_check_predicate (XEp_elem, o_by_name))
1276 ns_after_predicate . v_add_node_in_set (XEp_elem);
1277 }
1278 }
1279 }
1280 v_push_node_set (& ns_after_predicate);
1281 }
1282 else
1283 v_push_node_set (& ns_target);
1284 }
1285 as_action_store . v_set_position (i_end_store);
1286 }
1287
1288 /// Spec extract :
1289 /// \n A PredicateExpr is evaluated by evaluating the Expr and converting the result to a
1290 /// boolean. If the result is a number, the result will be converted to true if the number
1291 /// is equal to the context position and will be converted to false otherwise; if the result
1292 /// is not a number, then the result will be converted as if by a call to the boolean function.
1293 /// Thus a location path para[3] is equivalent to para[position()=3].
1294 bool xpath_processor::o_check_predicate (const TiXmlElement * XEp_child, bool o_by_name)
1295 {
1296 expression_result * erp_top;
1297 bool o_keep;
1298
1299 v_set_context (XEp_child, o_by_name);
1300 v_execute_one (xpath_predicate, false);
1301 v_set_context (NULL, false);
1302 erp_top = xs_stack . erp_top ();
1303 switch (erp_top -> e_type)
1304 {
1305 case e_double :
1306 case e_int :
1307 o_keep = (erp_top -> i_get_int () == i_xml_cardinality (XEp_child, o_by_name));
1308 break;
1309 default :
1310 o_keep = erp_top -> o_get_bool ();
1311 break;
1312 }
1313 xs_stack . v_pop ();
1314 return o_keep;
1315 }
1316
1317 /**
1318 Execute an XPath function. The arguments are in normal order in erpp_arg\n
1319 Calls one of the following :
1320 - v_function_ceiling
1321 - v_function_concat
1322 - v_function_contains
1323 - v_function_count
1324 - v_function_false
1325 - v_function_floor
1326 - v_function_last
1327 - v_function_name
1328 - v_function_normalize_space
1329 - v_function_not
1330 - v_function_position
1331 - v_function_starts_with
1332 - v_function_string_length
1333 - v_function_substring
1334 - v_function_sum
1335 - v_function_true
1336 */
1337 void xpath_processor::v_execute_function (
1338 TIXML_STRING & S_name, ///< Function name
1339 unsigned u_nb_arg, ///< Nb of arguments
1340 expression_result ** erpp_arg) ///< Argument list
1341 {
1342 if (S_name == "ceiling")
1343 v_function_ceiling (u_nb_arg, erpp_arg);
1344 else
1345
1346 if (S_name == "concat")
1347 v_function_concat (u_nb_arg, erpp_arg);
1348 else
1349
1350 if (S_name == "contains")
1351 v_function_contains (u_nb_arg, erpp_arg);
1352 else
1353
1354 if (S_name == "count")
1355 v_function_count (u_nb_arg, erpp_arg);
1356 else
1357
1358 if (S_name == "false")
1359 v_function_false (u_nb_arg, erpp_arg);
1360 else
1361
1362 if (S_name == "floor")
1363 v_function_floor (u_nb_arg, erpp_arg);
1364 else
1365
1366 if (S_name == "last")
1367 v_function_last (u_nb_arg, erpp_arg);
1368 else
1369
1370 if (S_name == "name")
1371 v_function_name (u_nb_arg, erpp_arg);
1372 else
1373
1374 if (S_name == "normalize-space")
1375 v_function_normalize_space (u_nb_arg, erpp_arg);
1376 else
1377
1378 if (S_name == "not")
1379 v_function_not (u_nb_arg, erpp_arg);
1380 else
1381
1382 if (S_name == "position")
1383 v_function_position (u_nb_arg, erpp_arg);
1384 else
1385
1386 if (S_name == "starts-with")
1387 v_function_starts_with (u_nb_arg, erpp_arg);
1388 else
1389
1390 if (S_name == "string-length")
1391 v_function_string_length (u_nb_arg, erpp_arg);
1392 else
1393
1394 if (S_name == "substring")
1395 v_function_substring (u_nb_arg, erpp_arg);
1396 else
1397
1398 if (S_name == "sum")
1399 v_function_sum (u_nb_arg, erpp_arg);
1400 else
1401
1402 if (S_name == "text")
1403 v_function_text (u_nb_arg, erpp_arg);
1404 else
1405
1406 if (S_name == "true")
1407 v_function_true (u_nb_arg, erpp_arg);
1408 else
1409
1410 throw execution_error (13);
1411 }
1412
1413 /// XPath \b ceiling function
1414 void xpath_processor::v_function_ceiling (
1415 unsigned u_nb_arg, ///< Nb of arguments
1416 expression_result ** erpp_arg) ///< Argument list
1417 {
1418 int i_val;
1419
1420 if (u_nb_arg != 1)
1421 throw execution_error (14);
1422 switch (erpp_arg [0] -> e_type)
1423 {
1424 case e_int :
1425 case e_bool :
1426 i_val = erpp_arg [0] -> i_get_int ();
1427 break;
1428 case e_double :
1429 i_val = (int) ceil (erpp_arg [0] -> d_get_double ());
1430 break;
1431 default :
1432 i_val = 0;
1433 break;
1434 }
1435 v_push_int (i_val, "ceiling");
1436 }
1437
1438 /// XPath \b concat function
1439 void xpath_processor::v_function_concat (
1440 unsigned u_nb_arg, ///< Nb of arguments
1441 expression_result ** erpp_arg) ///< Argument list
1442 {
1443 TIXML_STRING S_res;
1444 unsigned u_arg;
1445
1446 if (! u_nb_arg)
1447 throw execution_error (15);
1448 S_res = erpp_arg [0] -> S_get_string ();
1449 for (u_arg = 1; u_arg < u_nb_arg; u_arg++)
1450 S_res += erpp_arg [u_arg] -> S_get_string () . c_str ();
1451 v_push_string (S_res);
1452 }
1453
1454 /// XPath \b contains function
1455 void xpath_processor::v_function_contains (
1456 unsigned u_nb_arg, ///< Nb of arguments
1457 expression_result ** erpp_arg) ///< Argument list
1458 {
1459 TIXML_STRING S_arg_1, S_arg_2;
1460
1461 if (u_nb_arg != 2)
1462 throw execution_error (16);
1463 S_arg_1 = erpp_arg [0] -> S_get_string ();
1464 S_arg_2 = erpp_arg [1] -> S_get_string ();
1465 v_push_bool (strstr (S_arg_1 . c_str (), S_arg_2 . c_str ()) ? true : false);
1466 }
1467
1468 /// XPath \b count function
1469 void xpath_processor::v_function_count (
1470 unsigned u_nb_arg, ///< Nb of arguments
1471 expression_result ** erpp_arg) ///< Argument list
1472 {
1473 int i_res;
1474
1475 if (! u_nb_arg)
1476 throw execution_error (17);
1477 if (erpp_arg [0] -> e_type != e_node_set)
1478 i_res = 0;
1479 else
1480 i_res = erpp_arg [0] -> nsp_get_node_set () -> u_get_nb_node_in_set ();
1481 v_push_int (i_res, "count result");
1482 }
1483
1484 /// XPath \b false function
1485 void xpath_processor::v_function_false (
1486 unsigned u_nb_arg, ///< Nb of arguments
1487 expression_result ** erpp_arg) ///< Argument list
1488 {
1489 if (u_nb_arg)
1490 throw execution_error (18);
1491 v_push_bool (false);
1492 }
1493
1494 /// XPath \b floor function
1495 void xpath_processor::v_function_floor (
1496 unsigned u_nb_arg, ///< Nb of arguments
1497 expression_result ** erpp_arg) ///< Argument list
1498 {
1499 int i_val;
1500
1501 if (u_nb_arg != 1)
1502 throw execution_error (19);
1503 switch (erpp_arg [0] -> e_type)
1504 {
1505 case e_int :
1506 case e_bool :
1507 i_val = erpp_arg [0] -> i_get_int ();
1508 break;
1509 case e_double :
1510 i_val = (int) floor (erpp_arg [0] -> d_get_double ());
1511 break;
1512 default :
1513 i_val = 0;
1514 break;
1515 }
1516 v_push_int (i_val, "floor");
1517 }
1518
1519 /// XPath \b last function
1520 void xpath_processor::v_function_last (
1521 unsigned u_nb_arg, ///< Nb of arguments
1522 expression_result ** erpp_arg) ///< Argument list
1523 {
1524 const TiXmlElement * XEp_context;
1525
1526 if (u_nb_arg)
1527 throw execution_error (20);
1528 XEp_context = XEp_get_context ();
1529 if (! XEp_context)
1530 throw execution_error (21);
1531 v_push_int (i_xml_family_size (XEp_context), "last()");
1532 }
1533
1534 /// XPath \b name function
1535 void xpath_processor::v_function_name (
1536 unsigned u_nb_arg, ///< Nb of arguments
1537 expression_result ** erpp_arg) ///< Argument list
1538 {
1539 TIXML_STRING S_res;
1540 node_set * nsp_set;
1541
1542 if (u_nb_arg != 1)
1543 throw execution_error (22);
1544 S_res = "";
1545 if (erpp_arg [0] -> e_type == e_node_set)
1546 {
1547 nsp_set = erpp_arg [0] -> nsp_get_node_set ();
1548 if (nsp_set -> u_get_nb_node_in_set ())
1549 {
1550 if (nsp_set -> o_is_attrib (0))
1551 S_res = nsp_set -> XAp_get_attribute_in_set (0) -> Name ();
1552 else
1553 S_res = nsp_set -> XNp_get_node_in_set (0) -> Value ();
1554 }
1555 }
1556 v_push_string (S_res);
1557 }
1558
1559 /// XPath \b normalize-space function
1560 void xpath_processor::v_function_normalize_space (
1561 unsigned u_nb_arg, ///< Nb of arguments
1562 expression_result ** erpp_arg) ///< Argument list
1563 {
1564 TIXML_STRING S_arg, S_res;
1565
1566 if (u_nb_arg != 1)
1567 throw execution_error (23);
1568 S_arg = erpp_arg [0] -> S_get_string ();
1569 S_res = S_remove_lead_trail (S_arg . c_str ());
1570 v_push_string (S_res);
1571 }
1572
1573 /// XPath \b not function
1574 void xpath_processor::v_function_not (
1575 unsigned u_nb_arg, ///< Nb of arguments
1576 expression_result ** erpp_arg) ///< Argument list
1577 {
1578 if (u_nb_arg != 1)
1579 throw execution_error (24);
1580 v_push_bool (! erpp_arg [0] -> o_get_bool ());
1581 }
1582
1583 /// XPath \b position function
1584 void xpath_processor::v_function_position (
1585 unsigned u_nb_arg, ///< Nb of arguments
1586 expression_result ** erpp_arg) ///< Argument list
1587 {
1588 const TiXmlElement * XEp_context;
1589
1590 if (u_nb_arg)
1591 throw execution_error (25);
1592 XEp_context = XEp_get_context ();
1593 if (! XEp_context)
1594 throw execution_error (26);
1595 v_push_int (i_xml_cardinality (XEp_context, o_is_context_by_name), "position()");
1596 }
1597
1598 /// XPath \b starts-with function
1599 void xpath_processor::v_function_starts_with (
1600 unsigned u_nb_arg, ///< Nb of arguments
1601 expression_result ** erpp_arg) ///< Argument list
1602 {
1603 TIXML_STRING S_arg_1, S_arg_2;
1604
1605 if (u_nb_arg != 2)
1606 throw execution_error (27);
1607 S_arg_1 = erpp_arg [0] -> S_get_string ();
1608 S_arg_2 = erpp_arg [1] -> S_get_string ();
1609 v_push_bool (! strncmp (S_arg_1 . c_str (), S_arg_2 . c_str (), S_arg_2 . length ()));
1610 }
1611
1612 /// XPath \b sttring-length function
1613 void xpath_processor::v_function_string_length (
1614 unsigned u_nb_arg, ///< Nb of arguments
1615 expression_result ** erpp_arg) ///< Argument list
1616 {
1617 TIXML_STRING S_arg;
1618
1619 if (u_nb_arg != 1)
1620 throw execution_error (28);
1621 S_arg = erpp_arg [0] -> S_get_string ();
1622 v_push_int (S_arg . length (), "string-length");
1623 }
1624
1625 /**
1626 XPath \b substring function\n
1627 Standard excerpt:\n
1628 The substring function returns the substring of the first argument starting at the position specified in the second argument
1629 with length specified in the third argument. For example, substring("12345",2,3) returns "234". If the third argument is not
1630 specified, it returns the substring starting at the position specified in the second argument and continuing to the end of
1631 the string. For example, substring("12345",2) returns "2345".
1632 */
1633 void xpath_processor::v_function_substring (
1634 unsigned u_nb_arg, ///< Nb of arguments
1635 expression_result ** erpp_arg) ///< Argument list
1636 {
1637 TIXML_STRING S_base, S_ret;
1638 int i_length, i_start;
1639 const char * cp_base;
1640 char * cp_work;
1641
1642 if (u_nb_arg != 2 && u_nb_arg != 3)
1643 throw execution_error (29);
1644 S_base = erpp_arg [0] -> S_get_string ();
1645 i_start = erpp_arg [1] -> i_get_int ();
1646 if (u_nb_arg == 3)
1647 i_length = erpp_arg [2] -> i_get_int ();
1648 else
1649 i_length = 0;
1650
1651 if (i_start < 1)
1652 S_ret = "";
1653 else
1654 {
1655 i_start--;
1656 if (i_start >= (int) S_base . length ())
1657 S_ret = "";
1658 else
1659 {
1660 cp_base = S_base . c_str () + i_start;
1661 if (u_nb_arg == 2 || (int) strlen (cp_base) <= i_length)
1662 S_ret = cp_base;
1663 else
1664 {
1665 cp_work = new char [i_length + 1];
1666 strncpy (cp_work, cp_base, i_length);
1667 cp_work [i_length] = 0;
1668 S_ret = cp_work;
1669 delete [] cp_work;
1670 }
1671 }
1672 }
1673 v_push_string (S_ret);
1674 }
1675
1676 /// XPath \b sum function\n
1677 /// Standard exerpt :\n
1678 /// The sum function returns the sum, for each node in the argument node-set,
1679 /// of the result of converting the string-values of the node to a number.
1680 void xpath_processor::v_function_sum (
1681 unsigned u_nb_arg, ///< Nb of arguments
1682 expression_result ** erpp_arg) ///< Argument list
1683 {
1684 int i_sum;
1685 double d_sum;
1686 unsigned u_node;
1687 node_set * nsp_set;
1688 bool o_out_double;
1689
1690 if (u_nb_arg != 1)
1691 throw execution_error (30);
1692 i_sum = 0;
1693 d_sum = 0.0;
1694 o_out_double = false;
1695 if (erpp_arg [0] -> e_type != e_node_set)
1696 throw execution_error (31);
1697 nsp_set = erpp_arg [0] -> nsp_get_node_set ();
1698 assert (nsp_set);
1699 if (nsp_set -> u_get_nb_node_in_set () > 1)
1700 nsp_set -> v_document_sort ();
1701 for (u_node = 0; u_node < nsp_set -> u_get_nb_node_in_set (); u_node++)
1702 {
1703 i_sum += nsp_set -> i_get_value (u_node);
1704 d_sum += nsp_set -> d_get_value (u_node);
1705 if (strchr (nsp_set -> S_get_value (u_node) . c_str (), '.'))
1706 o_out_double = true;
1707 }
1708 if (o_out_double)
1709 v_push_double (d_sum);
1710 else
1711 v_push_int (i_sum, "sum()");
1712 }
1713
1714 /// XPath \b text function
1715 void xpath_processor::v_function_text (
1716 unsigned u_nb_arg, ///< Nb of arguments
1717 expression_result ** erpp_arg) ///< Argument list
1718 {
1719 const TiXmlElement * XEp_context;
1720 const TiXmlNode * XNp_child;
1721 TIXML_STRING S_res;
1722
1723 if (u_nb_arg)
1724 throw execution_error (38);
1725 XEp_context = XEp_get_context ();
1726 if (! XEp_context)
1727 throw execution_error (39);
1728 XNp_child = XEp_context -> FirstChild ();
1729 while (XNp_child)
1730 {
1731 if (XNp_child -> Type () == TiXmlNode::TEXT)
1732 S_res += XNp_child -> Value ();
1733 XNp_child = XNp_child -> NextSibling ();
1734 }
1735 v_push_string (S_res);
1736 }
1737
1738
1739 /// XPath \b true function
1740 void xpath_processor::v_function_true (
1741 unsigned u_nb_arg, ///< Nb of arguments
1742 expression_result ** erpp_arg) ///< Argument list
1743 {
1744 if (u_nb_arg)
1745 throw execution_error (32);
1746 v_push_bool (true);
1747 }
1748
1749 /**
1750 This function is for internal use : evaluation of an equality expression \n
1751 Standard excerpt :\n
1752
1753 If both objects to be compared are node-sets, then the comparison will be true if and only if there is a node in the first
1754 node-set and a node in the second node-set such that the result of performing the comparison on the string-values of the
1755 two nodes is true. \n
1756
1757 If one object to be compared is a node-set and the other is a number, then the comparison will be true
1758 if and only if there is a node in the node-set such that the result of performing the comparison on the number to be compared
1759 and on the result of converting the string-value of that node to a number using the number function is true. \n
1760
1761 If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in
1762 the node-set such that the result of performing the comparison on the string-value of the node and the other string is true. \n
1763
1764 If one object to be compared is a node-set and the other is a boolean, then the comparison will be true if and only if the
1765 result of performing the comparison on the boolean and on the result of converting the node-set to a boolean using the boolean
1766 function is true.\n
1767
1768 When neither object to be compared is a node-set and the operator is = or !=, then the objects are compared by converting them
1769 to a common type as follows and then comparing them.
1770 - If at least one object to be compared is a boolean, then each object to be compared is converted to a boolean as if by
1771 applying the boolean function.
1772 - Otherwise, if at least one object to be compared is a number, then each object to be compared is converted to a number as
1773 if by applying the number function.
1774 - Otherwise, both objects to be compared are converted to strings as if by applying the string function. The = comparison will
1775 be true if and only if the objects are equal; the != comparison will be true if and only if the objects are not equal.
1776 - Numbers are compared for equality according to IEEE 754.
1777 - Two booleans are equal if either both are true or both are false.
1778 - Two strings are equal if and only if they consist of the same sequence of UCS characters.
1779 */
1780 void xpath_processor::v_function_equal (expression_result ** erpp_arg)
1781 {
1782 bool o_res;
1783
1784 assert (erpp_arg);
1785 assert (erpp_arg [0]);
1786 assert (erpp_arg [1]);
1787 if (erpp_arg [0] -> e_type == e_node_set)
1788 if (erpp_arg [1] -> e_type == e_node_set)
1789 v_function_equal_2_node (erpp_arg [0], erpp_arg [1]);
1790 else
1791 v_function_equal_node_and_other (erpp_arg [0], erpp_arg [1]);
1792 else
1793 if (erpp_arg [1] -> e_type == e_node_set)
1794 v_function_equal_node_and_other (erpp_arg [1], erpp_arg [0]);
1795 else
1796 {
1797 // none are node sets : alternate decision table on bools, numbers and strings
1798 if (erpp_arg [0] -> e_type == e_bool || erpp_arg [1] -> e_type == e_bool)
1799 o_res = erpp_arg [0] -> o_get_bool () == erpp_arg [1] -> o_get_bool ();
1800 else
1801 if (erpp_arg [0] -> e_type == e_int || erpp_arg [1] -> e_type == e_int ||
1802 erpp_arg [0] -> e_type == e_double || erpp_arg [1] -> e_type == e_double)
1803 o_res = erpp_arg [0] -> d_get_double () == erpp_arg [1] -> d_get_double ();
1804 else
1805 o_res = erpp_arg [0] -> S_get_string () == erpp_arg [1] -> S_get_string ();
1806 v_push_bool (o_res);
1807 }
1808 }
1809
1810 /// This function is for internal use : evaluation of a non-equality expression
1811 void xpath_processor::v_function_not_equal (expression_result ** erpp_arg)
1812 {
1813 v_function_equal (erpp_arg);
1814 v_push_bool (! o_pop_bool ());
1815 }
1816
1817 /**
1818 Utility function that evaluates the equality between a node set and a non-node set\n
1819
1820 Standard excerpt :\n
1821 If one object to be compared is a node-set and the other is a number, then the comparison will be true
1822 if and only if there is a node in the node-set such that the result of performing the comparison on the number to be compared
1823 and on the result of converting the string-value of that node to a number using the number function is true. \n
1824
1825 If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in
1826 the node-set such that the result of performing the comparison on the string-value of the node and the other string is true. \n
1827
1828 If one object to be compared is a node-set and the other is a boolean, then the comparison will be true if and only if the
1829 result of performing the comparison on the boolean and on the result of converting the node-set to a boolean using the boolean
1830 function is true.\n
1831 */
1832 void xpath_processor::v_function_equal_node_and_other (expression_result * erp_node_set, expression_result * erp_non)
1833 {
1834 bool o_res;
1835 node_set * nsp_ptr;
1836 unsigned u_node;
1837
1838 o_res = false;
1839 nsp_ptr = erp_node_set -> nsp_get_node_set ();
1840 switch (erp_non -> e_type)
1841 {
1842 case e_bool :
1843 o_res = erp_non -> o_get_bool () == erp_node_set -> o_get_bool ();
1844 break;
1845 case e_int :
1846 case e_double :
1847 for (u_node = 0; u_node < nsp_ptr -> u_get_nb_node_in_set (); u_node++)
1848 if (nsp_ptr -> i_get_value (u_node) == erp_non -> i_get_int ())
1849 o_res = true;
1850 break;
1851 case e_string :
1852 for (u_node = 0; u_node < nsp_ptr -> u_get_nb_node_in_set (); u_node++)
1853 if (nsp_ptr -> S_get_value (u_node) == erp_non -> S_get_string ())
1854 o_res = true;
1855 break;
1856 default :
1857 // this case should have been excluded before our call
1858 assert (false);
1859 break;
1860 }
1861 v_push_bool (o_res);
1862 }
1863
1864 /// \todo : Implement this function. We don't compare 2 node sets yet
1865 void xpath_processor::v_function_equal_2_node (expression_result * erp_node_set_, expression_result * erp_node_set_2)
1866 {
1867 throw error_not_yet ();
1868 }
1869
1870 /// Union function
1871 void xpath_processor::v_function_union (node_set & ns_1, node_set & ns_2)
1872 {
1873 node_set ns_target;
1874 unsigned u_node;
1875 const TiXmlBase * XBp_base;
1876
1877 ns_target = ns_1;
1878 for (u_node = 0; u_node < ns_2 . u_get_nb_node_in_set (); u_node++)
1879 {
1880 XBp_base = ns_2 . XBp_get_base_in_set (u_node);
1881 ns_target . v_add_base_in_set (XBp_base, ns_2 . o_is_attrib (u_node));
1882 }
1883 v_push_node_set (& ns_target);
1884 }
1885
1886 /// XPath <b> + </b> function
1887 void xpath_processor::v_function_plus (expression_result ** erpp_arg)
1888 {
1889 assert (erpp_arg);
1890 assert (erpp_arg [0]);
1891 assert (erpp_arg [1]);
1892 if (erpp_arg [0] -> e_type == e_double || erpp_arg [1] -> e_type == e_double)
1893 v_push_double (erpp_arg [0] -> d_get_double () + erpp_arg [1] -> d_get_double ());
1894 else
1895 v_push_int (erpp_arg [0] -> i_get_int () + erpp_arg [1] -> i_get_int (), "+");
1896 }
1897
1898 /// XPath <b> - </b> function
1899 void xpath_processor::v_function_minus (expression_result ** erpp_arg)
1900 {
1901 assert (erpp_arg);
1902 assert (erpp_arg [0]);
1903 assert (erpp_arg [1]);
1904 if (erpp_arg [0] -> e_type == e_double || erpp_arg [1] -> e_type == e_double)
1905 v_push_double (erpp_arg [0] -> d_get_double () - erpp_arg [1] -> d_get_double ());
1906 else
1907 v_push_int (erpp_arg [0] -> i_get_int () - erpp_arg [1] -> i_get_int (), "-");
1908 }
1909
1910 /// XPath \b or function
1911 void xpath_processor::v_function_or (expression_result ** erpp_arg)
1912 {
1913 assert (erpp_arg);
1914 assert (erpp_arg [0]);
1915 assert (erpp_arg [1]);
1916 v_push_bool (erpp_arg [0] -> o_get_bool () || erpp_arg [1] -> o_get_bool ());
1917 }
1918
1919 /// XPath \b and function
1920 void xpath_processor::v_function_and (expression_result ** erpp_arg)
1921 {
1922 assert (erpp_arg);
1923 assert (erpp_arg [0]);
1924 assert (erpp_arg [1]);
1925 v_push_bool (erpp_arg [0] -> o_get_bool () && erpp_arg [1] -> o_get_bool ());
1926 }
1927
1928 /// XPath relational comparison function
1929 void xpath_processor::v_function_relational (expression_result ** erpp_arg, unsigned u_sub)
1930 {
1931 bool o_res;
1932
1933 assert (erpp_arg);
1934 assert (erpp_arg [0]);
1935 assert (erpp_arg [1]);
1936
1937 if (erpp_arg [0] -> e_type == e_double || erpp_arg [1] -> e_type == e_double)
1938 {
1939 double d_arg_1, d_arg_2;
1940
1941 d_arg_1 = erpp_arg [0] -> d_get_double ();
1942 d_arg_2 = erpp_arg [1] -> d_get_double ();
1943 switch (u_sub)
1944 {
1945 case xpath_relational_expr_lt :
1946 o_res = d_arg_1 < d_arg_2;
1947 break;
1948 case xpath_relational_expr_gt :
1949 o_res = d_arg_1 > d_arg_2;
1950 break;
1951 case xpath_relational_expr_lte :
1952 o_res = d_arg_1 <= d_arg_2;
1953 break;
1954 case xpath_relational_expr_gte :
1955 o_res = d_arg_1 >= d_arg_2;
1956 break;
1957 default :
1958 assert (false);
1959 }
1960 }
1961 else
1962 {
1963 int i_arg_1, i_arg_2;
1964
1965 i_arg_1 = erpp_arg [0] -> i_get_int ();
1966 i_arg_2 = erpp_arg [1] -> i_get_int ();
1967 switch (u_sub)
1968 {
1969 case xpath_relational_expr_lt :
1970 o_res = i_arg_1 < i_arg_2;
1971 break;
1972 case xpath_relational_expr_gt :
1973 o_res = i_arg_1 > i_arg_2;
1974 break;
1975 case xpath_relational_expr_lte :
1976 o_res = i_arg_1 <= i_arg_2;
1977 break;
1978 case xpath_relational_expr_gte :
1979 o_res = i_arg_1 >= i_arg_2;
1980 break;
1981 default :
1982 assert (false);
1983 }
1984 }
1985 v_push_bool (o_res);
1986 }
1987
1988 /// XPath <b> * </b> function (arithmetic)
1989 void xpath_processor::v_function_mult (expression_result ** erpp_arg, unsigned u_sub)
1990 {
1991 assert (erpp_arg);
1992 assert (erpp_arg [0]);
1993 assert (erpp_arg [1]);
1994
1995 if (erpp_arg [0] -> e_type == e_double || erpp_arg [1] -> e_type == e_double || u_sub == xpath_multiplicative_expr_div)
1996 {
1997 double d_arg_1, d_arg_2, d_res;
1998
1999 d_arg_1 = erpp_arg [0] -> d_get_double ();
2000 d_arg_2 = erpp_arg [1] -> d_get_double ();
2001 switch (u_sub)
2002 {
2003 case xpath_multiplicative_expr_star :
2004 d_res = d_arg_1 * d_arg_2;
2005 break;
2006 case xpath_multiplicative_expr_div :
2007 if (fabs (d_arg_2) < 1.0e-6)
2008 throw execution_error (33);
2009 d_res = d_arg_1 / d_arg_2;
2010 break;
2011 case xpath_multiplicative_expr_mod :
2012 d_res = (int) d_arg_1 % (int) d_arg_2;
2013 break;
2014 default :
2015 assert (false);
2016 }
2017 v_push_double (d_res);
2018 }
2019 else
2020 {
2021 int i_arg_1, i_arg_2, i_res;
2022
2023 i_arg_1 = erpp_arg [0] -> i_get_int ();
2024 i_arg_2 = erpp_arg [1] -> i_get_int ();
2025 switch (u_sub)
2026 {
2027 case xpath_multiplicative_expr_star :
2028 i_res = i_arg_1 * i_arg_2;
2029 break;
2030 case xpath_multiplicative_expr_mod :
2031 i_res = i_arg_1 % i_arg_2;
2032 break;
2033 default :
2034 assert (false);
2035 }
2036 v_push_int (i_res, "*");
2037 }
2038 }
2039
2040
2041 /// This function, because it only operates on one argument retrieves it himself from the stack
2042 /// \n It computes the mathematical opposite
2043 void xpath_processor::v_function_opposite ()
2044 {
2045 expression_result er_arg;
2046
2047 er_arg = * xs_stack . erp_top ();
2048 xs_stack . v_pop ();
2049 switch (er_arg . e_type)
2050 {
2051 case e_double :
2052 v_push_double (-1.0 * er_arg . d_get_double ());
2053 break;
2054 default :
2055 v_push_int (-1 * er_arg . i_get_int (), "unary -");
2056 break;
2057 }
2058 }
2059
2060 /// Order the tree : assign an integer to all nodes based on the document order
2061 /// \n Right now, tinyxml doesn't have a user value associated to attributes ... arrrgh.
2062 /// We order everything else, and will have to compute attributes order on demand
2063 void xpath_processor::v_order_tree ()
2064 {
2065 int i_dummy;
2066
2067 i_dummy = 1;
2068 v_order_recurs (XEp_root, i_dummy);
2069 }
2070
2071 /// Recursive ordering of an XML tree according to XPath's document order.
2072 /// \n The result is stored as an integer in the void * pointer managed by TiXmlNode::SetUserData / TiXmlNode::GetUserData
2073 void xpath_processor::v_order_recurs (TiXmlNode * Np_base, int & i_current)
2074 {
2075 TiXmlNode * Np_child;
2076
2077 Np_base -> SetUserData ((void *) i_current++);
2078 Np_child = Np_base -> FirstChild ();
2079 while (Np_child)
2080 {
2081 v_order_recurs (Np_child, i_current);
2082 Np_child = Np_child -> NextSibling ();
2083 }
2084 }
2085
2086 /// Set the current context node for predicate evaluations
2087 void xpath_processor::v_set_context (
2088 const TiXmlElement * XEp_in, ///< Context node
2089 bool o_by_name) ///< true if the current node search is by name, false if it's a *
2090 {
2091 XEp_context = XEp_in;
2092 o_is_context_by_name = o_by_name;
2093 }