-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoPilot.c
977 lines (909 loc) · 24.6 KB
/
autoPilot.c
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
/*
* Copyright (C) 2002-2015 XimpleWare, info@ximpleware.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*VTD-XML is protected by US patent 7133857, 7260652, an 7761459*/
#include "autoPilot.h"
#include "xpath.h"
/* This method insert a prefix/URL pair into the nsList, if there are prefix duplicates
the URL in the list is replaced with new URL*/
static _thread struct nsList *nl;
static _thread struct exprList *el;
static void insertItem(AutoPilot *ap, UCSChar *prefix, UCSChar *URL);
static void insertExpr(AutoPilot *ap, UCSChar *varName, expr *e);
static Boolean checkNsUniqueness(AutoPilot *ap, int index);
/* This method insert a prefix/URL pair into the nsList, if there are prefix duplicates
the URL is overwritten */
void insertItem(AutoPilot *ap, UCSChar *prefix, UCSChar *URL){
NsList *tmp = nl;
/* search first */
if (tmp == NULL){
tmp = (NsList *)malloc(sizeof(NsList));
if (tmp == NULL){
throwException2(out_of_mem, "out of memory in insertItem");
}
tmp->next = NULL;
tmp->prefix = prefix;
tmp->URL = URL;
nl = tmp;
return;
}else {
if (wcscmp(tmp->prefix,prefix) == 0){
tmp->URL = URL; /* overwritten */
return;
}
while(tmp->next!=NULL){
if (wcscmp(tmp->prefix,prefix) == 0){
tmp->URL = URL; /* overwritten */
return;
}
tmp = tmp->next;
}
tmp->next = (NsList *)malloc(sizeof(NsList));
if (tmp->next == NULL){
throwException2(out_of_mem, "out of memory in insertItem");
}
tmp->next->next = NULL;
tmp->next->prefix = prefix;
tmp->next->URL = URL;
return;
}
}
/* This method insert a name/expr pair into the exprList, if there are name duplicates
the expr is overwritten */
void insertExpr(AutoPilot *ap, UCSChar *varName, expr *e){
struct exprList *tmp = el;
/* search first */
if (tmp == NULL){
tmp = (struct exprList *)malloc(sizeof(struct exprList));
if (tmp == NULL){
throwException2(out_of_mem, "out of memory in insertExpr");
}
tmp->next = NULL;
tmp->variableName = varName;
tmp->ve = e;
el = tmp;
return;
}else {
if (wcscmp(tmp->variableName, varName) == 0){
tmp->ve->freeExpr(tmp->ve);
tmp->ve = e; /* overwritten */
return;
}
while(tmp->next!=NULL){
if (wcscmp(tmp->variableName,varName) == 0){
tmp->ve->freeExpr(tmp->ve);
tmp->ve = e; /* overwritten */
return;
}
tmp = tmp->next;
}
tmp->next = (struct exprList *)malloc(sizeof(struct exprList));
if (tmp->next == NULL){
throwException2(out_of_mem, "out of memory in insertExpr");
}
tmp->next->next = NULL;
tmp->next->variableName = varName;
tmp->next->ve = e;
return;
}
}
/* given a prefix, find the URL */
UCSChar *lookup(NsList *l, UCSChar *prefix){
NsList *tmp = l;
if (tmp==NULL)
return NULL;
while(tmp!= NULL){
if (wcscmp(tmp->prefix,prefix)==0){
return tmp->URL;
}
tmp = tmp->next;
}
return NULL;
}
/*given a variable Name, find the expression */
expr *getExprFromList(ExprList *el, UCSChar *varName){
ExprList *tmp = el;
if (tmp==NULL)
return NULL;
while(tmp!= NULL){
if (wcscmp(tmp->variableName,varName)==0){
return tmp->ve;
}
tmp = tmp->next;
}
return NULL;
}
/*create AutoPilot throw exception if allocation failed*/
AutoPilot *createAutoPilot(VTDNav *v){
AutoPilot *ap = NULL;
if (v == NULL){
throwException2(invalid_argument,
" createAutoPilot failed: can't take NULL VTDNav pointer");
return NULL;
}
ap = (AutoPilot *)malloc(sizeof(AutoPilot));
if (ap == NULL){
throwException2(out_of_mem,
"createAutoPilot failed");
return NULL;
}
ap->elementName = NULL;
ap->elementName2 = NULL;
ap->localName = NULL;
ap->URL = NULL;
ap->vn = v;
ap->it = UNDEFINED; /* initial state: not defined */
ap->ft = VTD_TRUE;
//ap->startIndex = -1;
ap->xpe = NULL;
nl = NULL;
ap->contextCopy = NULL;
ap->special = VTD_FALSE;
ap->fib = NULL;
ap->cachingOption = VTD_TRUE;
ap->stackSize=0;
return ap;
}
/* a argument-less constructor for autoPilot,
out_of_mem exception if allocation failed */
AutoPilot *createAutoPilot2(){
AutoPilot *ap = NULL;
ap = (AutoPilot *)malloc(sizeof(AutoPilot));
if (ap == NULL){
throwException2(out_of_mem,
"createAutoPilot failed");
return NULL;
}
ap->elementName = NULL;
ap->elementName2 = NULL;
ap->localName = NULL;
ap->URL = NULL;
ap->vn = NULL;
//depth = v.getCurrentDepth();
ap->it = UNDEFINED; // not defined
ap->ft = VTD_TRUE;
ap->xpe = NULL;
//ap->startIndex = -1;
nl = NULL;
ap->contextCopy = NULL;
ap->special = VTD_FALSE;
ap->fib = NULL;
ap->cachingOption = VTD_TRUE;
ap->stackSize=0;
return ap;
}
/* free AutoPilot */
void freeAutoPilot(AutoPilot *ap){
if (ap!=NULL){
/*nl has been made a package scoped static variable*/
/*NsList *tmp = nl;
NsList *tmp2 = NULL;
while(tmp!=NULL){
tmp2 = tmp->next;
free(tmp);
tmp = tmp2;
}*/
if (ap->contextCopy!=NULL)
free(ap->contextCopy);
if (ap->xpe!= NULL)
ap->xpe->freeExpr(ap->xpe);
if (ap->fib!=NULL)
freeFastIntBuffer(ap->fib);
if (ap->elementName2!=NULL)
free(ap->elementName2);
}
free(ap);
}
void printExprString(AutoPilot *ap){
if (ap->xpe!=NULL)
ap->xpe->toString(ap->xpe,NULL);
}
/* Select an attribute name for iteration, * choose all attributes of an element
this function is not called directly */
void selectAttr(AutoPilot *ap, UCSChar *an){
if (an == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement, elementName can't be NULL");
}
ap->it= ATTR;
ap->ft = VTD_TRUE;
ap->size = getTokenCount(ap->vn);
ap->elementName = an;
}
/* Select an attribute name, both local part and namespace URL part*/
void selectAttrNS(AutoPilot *ap, UCSChar *URL, UCSChar *ln){
if (ln == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement, localName can't be NULL");
}
ap->it = ATTR_NS;
ap->ft = VTD_TRUE;
ap->localName = ln;
ap->URL = URL;
}
/*Select the element name before iterating*/
void selectElement(AutoPilot *ap, UCSChar *en){
if (en == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement, elementName can't be NULL");
}
ap->it = SIMPLE;
ap->depth = getCurrentDepth(ap->vn);
ap->elementName = en;
ap->ft = VTD_TRUE;
}
/*Select the element name (name space version) before iterating.
// * URL, if set to *, matches every namespace
// * URL, if set to null, indicates the namespace is undefined.
// * localname, if set to *, matches any localname */
void selectElementNS(AutoPilot *ap, UCSChar *URL, UCSChar *ln){
if (ln == NULL){
throwException2( invalid_argument,
" invalid argument for selectElementNS, localName can't be NULL");
}
ap->it = SIMPLE_NS;
ap->depth = getCurrentDepth(ap->vn);
ap->localName = ln;
ap->URL = URL;
ap->ft = VTD_TRUE;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the descendent axis, without ns awareness
* @param en
*/
void selectElement_D(AutoPilot *ap, UCSChar *en){
if (en == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement_D, elementName can't be NULL");
}
ap->it = DESCENDANT;
ap->depth = getCurrentDepth(ap->vn);
ap->elementName = en;
ap->ft = VTD_TRUE;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the Descendent axis, with ns awareness
*/
void selectElementNS_D(AutoPilot *ap, UCSChar *URL, UCSChar *ln){
if (ln == NULL){
throwException2( invalid_argument,
" invalid argument for selectElementNS_D, localName can't be NULL");
}
ap->it = DESCENDANT_NS;
ap->depth = getCurrentDepth(ap->vn);
ap->localName = ln;
ap->URL = URL;
ap->ft = VTD_TRUE;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the following axis, without ns awareness
* @param en
*/
void selectElement_F(AutoPilot *ap, UCSChar *en){
if (en == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement_F, elementName can't be NULL");
}
ap->it = FOLLOWING;
ap->ft = VTD_TRUE;
ap->elementName = en;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the following axis, with ns awareness
*/
void selectElementNS_F(AutoPilot *ap, UCSChar *URL, UCSChar *ln){
if (ln == NULL){
throwException2( invalid_argument,
" invalid argument for selectElementNS_F, localName can't be NULL");
}
ap->it = FOLLOWING_NS;
ap->ft= VTD_TRUE;
ap->localName = ln;
ap->URL = URL;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the preceding axis, without ns awareness
* @param en
*/
void selectElement_P(AutoPilot *ap, UCSChar *en){
int i;
int a = sizeof(int)* ap->vn->nestingLevel;
if (en == NULL){
throwException2( invalid_argument,
" invalid argument for selectElement_P, elementName can't be NULL");
}
ap->depth = getCurrentDepth(ap->vn);
ap->it = PRECEDING;
ap->ft = VTD_TRUE;
ap->elementName = en;
ap->contextCopy = (int *)malloc(a); //(int[])vn.context.clone();
memcpy(ap->contextCopy,ap->vn->context,a);
ap->endIndex=getCurrentIndex2(ap->vn);
for(i = ap->vn->context[0]+1 ; i<ap->vn->nestingLevel ; i++){
ap->contextCopy[i]=-1;
}
ap->contextCopy[0]=ap->vn->rootIndex;
}
/**
* this function is intended to be called during XPath evaluation
* Select all descendent elements along the preceding axis, with ns awareness
*/
void selectElementNS_P(AutoPilot *ap, UCSChar *URL, UCSChar *ln){
int i;
int a = sizeof(int)* ap->vn->nestingLevel;
if (ln == NULL){
throwException2( invalid_argument,
" invalid argument for selectElementNS_P, localName can't be NULL");
}
ap->depth = getCurrentDepth(ap->vn);
ap->it = PRECEDING_NS;
ap->ft = VTD_TRUE;
ap->URL = URL;
ap->localName = ln;
ap->contextCopy = (int *)malloc(a); //(int[])vn.context.clone();
memcpy(ap->contextCopy,ap->vn->context,a);
ap->endIndex=getCurrentIndex2(ap->vn);
for(i = ap->vn->context[0]+1 ; i<ap->vn->nestingLevel ; i++){
ap->contextCopy[i]=-1;
}
ap->contextCopy[0]=ap->vn->rootIndex;
}
/**
* Setspecial is used by XPath evaluator to distinguish between
* node() and *
* node() corresponding to b= true;
*/
void setSpecial(AutoPilot *ap, Boolean b){
ap->special = b;
}
/*Iterate over all the selected element nodes in document order.*/
Boolean iterateAP(AutoPilot *ap){
switch (ap->it) {
case SIMPLE :
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft == VTD_FALSE)
return iterate(ap->vn, ap->depth, ap->elementName, ap->special);
else {
ap->ft = VTD_FALSE;
if (ap->special || matchElement(ap->vn, ap->elementName)) {
return VTD_TRUE;
} else
return iterate(ap->vn, ap->depth, ap->elementName,ap->special);
}
case SIMPLE_NS :
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft == VTD_FALSE)
return iterateNS(ap->vn, ap->depth, ap->URL, ap->localName);
else {
ap->ft = VTD_FALSE;
if (matchElementNS(ap->vn, ap->URL, ap->localName)) {
return VTD_TRUE;
} else
return iterateNS(ap->vn, ap->depth, ap->URL, ap->localName);
}
case DESCENDANT:
if (ap->vn->atTerminal)
return VTD_FALSE;
return iterate(ap->vn, ap->depth, ap->elementName, ap->special);
case DESCENDANT_NS:
if (ap->vn->atTerminal)
return VTD_FALSE;
return iterateNS(ap->vn, ap->depth, ap->URL, ap->localName);
case FOLLOWING:
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft == VTD_FALSE)
return iterate_following(ap->vn, ap->elementName, ap->special);
else {
ap->ft = VTD_FALSE;
while(VTD_TRUE){
while (toElement(ap->vn, NEXT_SIBLING)){
if (ap->special || matchElement(ap->vn,ap->elementName)) {
return VTD_TRUE;
}
return iterate_following(ap->vn, ap->elementName, ap->special);
}
if (toElement(ap->vn, PARENT)==VTD_FALSE){
return VTD_FALSE;
}
}
}
case FOLLOWING_NS:
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft == VTD_FALSE)
return iterate_followingNS(ap->vn,ap->URL,ap->localName);
else {
ap->ft = VTD_FALSE;
while(VTD_TRUE){
while (toElement(ap->vn, NEXT_SIBLING)){
if (matchElementNS(ap->vn, ap->URL,ap->localName)) {
return VTD_TRUE;
}
return iterate_followingNS(ap->vn, ap->URL,ap->localName);
}
if (toElement(ap->vn,PARENT)==VTD_FALSE){
return VTD_FALSE;
}
}
}
case PRECEDING:
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft){
ap->ft = VTD_FALSE;
toElement(ap->vn,ROOT);
}
return iterate_preceding(ap->vn, ap->elementName, ap->contextCopy, ap->endIndex);
case PRECEDING_NS:
if (ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft){
ap->ft = VTD_FALSE;
toElement(ap->vn,ROOT);
}
return iterate_precedingNS(ap->vn, ap->URL,ap->localName,ap->contextCopy,ap->endIndex);
default :
throwException2(pilot_exception,
"unknow iteration type for iterateAP");
return VTD_FALSE;
}
}
/**
* This method is meant to be called after calling
* selectAttr() or selectAttrNs(), it will return the
* vtd index attribute name or -1 if there is none left
* @return vtd index attribute name or -1 if there is none left
*/
int iterateAttr(AutoPilot *ap){
int i;
switch(ap->it){
case ATTR:
if (wcscmp(ap->elementName,L"*")==0){
if (ap->ft != VTD_FALSE){
ap->ft = VTD_FALSE;
ap->index = getCurrentIndex2(ap->vn)+1;
} else
ap->index +=2;
if (ap->vn->ns == VTD_FALSE){
while(ap->index<ap->size){
tokenType type = getTokenType(ap->vn,ap->index);
if (type == TOKEN_ATTR_NAME
|| type == TOKEN_ATTR_NS){
//ap->vn->LN = ap->index;
return ap->index;
}else{
return -1;
}
}
return -1;
}else {
while(ap->index<ap->size){
tokenType type = getTokenType(ap->vn,ap->index);
if (type == TOKEN_ATTR_NAME
|| type == TOKEN_ATTR_NS){
if (type == TOKEN_ATTR_NAME){
//ap->vn->LN = ap->index;
return ap->index;
}
else
ap->index += 2;
}else{
return -1;
}
}
return -1;
}
}else{
if (ap->ft == VTD_FALSE){
return -1;
} else {
ap->ft = VTD_FALSE;
i = getAttrVal(ap->vn,ap->elementName);
if(i!=-1){
//ap->vn->LN = i-1;
return i-1;
}
else
return -1;
}
}
case ATTR_NS:
if (ap->ft == VTD_FALSE){
return -1;
} else {
ap->ft = VTD_FALSE;
i = getAttrValNS(ap->vn,ap->URL,ap->localName);
if(i!=-1){
//ap->vn->LN = i-1;
return i-1;
}
else
return -1;
}
default:
throwException2(pilot_exception,
"unknow iteration type for iterateAP");
return -1;
}
}
/* This method implements the attribute axis for XPath*/
int iterateAttr2(AutoPilot *ap){
int i;
switch(ap->it){
case ATTR:
if (wcscmp(ap->elementName,L"*")==0){
if (ap->ft != VTD_FALSE){
ap->ft = VTD_FALSE;
ap->index = getCurrentIndex2(ap->vn)+1;
} else
ap->index +=2;
if (ap->vn->ns == VTD_FALSE){
while(ap->index<ap->size){
tokenType type = getTokenType(ap->vn,ap->index);
if (type == TOKEN_ATTR_NAME
|| type == TOKEN_ATTR_NS){
ap->vn->LN = ap->index;
return ap->index;
}else{
return -1;
}
}
return -1;
}else {
while(ap->index<ap->size){
tokenType type = getTokenType(ap->vn,ap->index);
if (type == TOKEN_ATTR_NAME
|| type == TOKEN_ATTR_NS){
if (type == TOKEN_ATTR_NAME){
ap->vn->LN = ap->index;
return ap->index;
}
else
ap->index += 2;
}else{
return -1;
}
}
return -1;
}
}else{
if (ap->ft == VTD_FALSE){
return -1;
} else {
ap->ft = VTD_FALSE;
i = getAttrVal(ap->vn,ap->elementName);
if(i!=-1){
ap->vn->LN = i-1;
return i-1;
}
else
return -1;
}
}
case ATTR_NS:
if (ap->ft == VTD_FALSE){
return -1;
} else {
ap->ft = VTD_FALSE;
i = getAttrValNS(ap->vn,ap->URL,ap->localName);
if(i!=-1){
ap->vn->LN = i-1;
return i-1;
}
else
return -1;
}
default:
throwException2(pilot_exception,
"unknow iteration type for iterateAP");
return -1;
}
}
/*
* This function selects the string representing XPath expression
* Usually evalXPath is called afterwards
*/
Boolean selectXPath(AutoPilot *ap, UCSChar *s){
if (s==NULL){
throwException2(xpath_parse_exception,
" xpath input string can't be NULL ");
return VTD_FALSE;
}
if(ap->xpe!=NULL)
ap->xpe->freeExpr(ap->xpe);
XMLChar_init();
ap->xpe = xpathParse(s, nl,el);
ap->ft = VTD_TRUE;
if (ap->xpe == NULL){
throwException2(xpath_parse_exception, "Invalid XPath expression");
return VTD_FALSE;
}
if (ap->cachingOption)
ap->xpe->markCacheable(ap->xpe);
return VTD_TRUE;
}
/*
* Evaluate XPath to a boolean
*/
Boolean evalXPathToBoolean(AutoPilot *ap){
return ap->xpe->evalBoolean(ap->xpe,ap->vn);
}
/*
* Evaluate XPath to a String
*/
UCSChar* evalXPathToString(AutoPilot *ap){
return ap->xpe->evalString(ap->xpe,ap->vn);
}
/*
* Evaluate XPath to a number
*/
double evalXPathToNumber(AutoPilot *ap){
return ap->xpe->evalNumber(ap->xpe,ap->vn);
}
/*
* Evaluate XPath
*/
int evalXPath(AutoPilot *ap){
if (ap->xpe != NULL){
if (ap->ft == VTD_TRUE){
if (ap->vn != NULL){
ap->stackSize = ap->vn->contextBuf2->size;
}
ap->ft = VTD_FALSE;
ap->xpe->adjust(ap->xpe, ap->vn->vtdSize);
}
return ap->xpe->evalNodeSet(ap->xpe,ap->vn);
}
throwException2(other_exception,"xpe is NULL in autoPilot");
return -1;
}
/*
* Reset XPath
*/
void resetXPath(AutoPilot *ap){
if (ap->xpe != NULL && ap->vn!=NULL){
ap->xpe->reset(ap->xpe,ap->vn);
ap->vn->contextBuf2->size = ap->stackSize;
ap->ft = VTD_TRUE;
if (ap->cachingOption)
ap->xpe->clearCache(ap->xpe);
}
}
/*
* This function creates URL ns prefix
* and is intended to be called prior to selectXPath
*/
void declareXPathNameSpace(AutoPilot *ap, UCSChar* prefix, UCSChar *URL){
if(ap!=NULL)
insertItem(ap,prefix,URL);
}
/**
*
*/
void apBind(AutoPilot *ap, VTDNav *new_vn){
ap->elementName = NULL;
ap->elementName2 = NULL;
ap->vn = new_vn;
ap->it = UNDEFINED;
ap->ft = VTD_TRUE;
ap->size = 0;
ap->special = VTD_FALSE;
}
/* Clear the namespace prefix URL bindings in the global list */
void clearXPathNameSpaces(){
/*nl has been made a package scoped static variable*/
NsList *tmp = nl;
NsList *tmp2 = NULL;
while(tmp!=NULL){
tmp2 = tmp->next;
free(tmp);
tmp = tmp2;
}
}
/* Clear the variable name and exprs in the global list */
void clearVariableExprs(){
ExprList *tmp = el;
ExprList *tmp2 = NULL;
while(tmp!=NULL){
tmp->ve->freeExpr(tmp->ve);
tmp2 = tmp->next;
free(tmp);
tmp = tmp2;
}
}
/* Declare the variable name and expression binding*/
void declareVariableExpr(AutoPilot *ap, UCSChar* varName, UCSChar* varExpr){
exception e;
expr* tmp = NULL;
if (varExpr==NULL || varName==NULL){
throwException2(xpath_parse_exception,
" xpath input string or variable name can't be NULL ");
//return VTD_FALSE;
}
tmp = getExprFromList(el,varName);
if(tmp!=NULL)
tmp->freeExpr(tmp);
Try{
tmp = xpathParse(varExpr, nl,el);
//ap->ft = VTD_TRUE;
if (tmp == NULL){
throwException2(xpath_parse_exception, "variable expr declaration failed ");
}
insertExpr(ap,varName,tmp);
}Catch(e){
freeAllObj();
Throw e;
}
}
void selectNameSpace(AutoPilot *ap, UCSChar *en){
if (en == NULL)
throwException2(invalid_argument,"namespace name can't be null");
ap->it = NAMESPACE;
ap->ft = VTD_TRUE;
ap->size = getTokenCount(ap->vn);
ap->elementName = en;
if (wcscmp(en,L"*")!=0){
UCSChar *tmp = malloc(sizeof(UCSChar)*(wcslen(en)+7));
if (tmp == NULL)
throwException2(out_of_mem,"allocating string failed in selectNameSpace()");
wcscpy(tmp,L"xmlns:");
wcscpy(tmp+6,en);
*(tmp+(wcslen(en)+6))= 0;
ap->elementName2=tmp;
}
if (ap->fib== NULL)
ap->fib = createFastIntBuffer2(4);
else
clearFastIntBuffer(ap->fib);
}
int iterateNameSpace(AutoPilot *ap){
if (ap->vn->ns == VTD_FALSE)
return -1;
if (ap->ft != VTD_FALSE) {
ap->ft = VTD_FALSE;
ap->index = getCurrentIndex2(ap->vn) + 1;
} else
ap->index += 2;
while (ap->index < ap->size) {
tokenType type = getTokenType(ap->vn,ap->index);
if (type == TOKEN_ATTR_NAME || type == TOKEN_ATTR_NS) {
if (type == TOKEN_ATTR_NS){
if (wcscmp(ap->elementName,L"*") == 0
|| matchRawTokenString(ap->vn,ap->index, ap->elementName2)
){
// check to see if the namespace has appeared before
if (checkNsUniqueness(ap,ap->index)){
ap->vn->LN = ap->index;
ap->vn->atTerminal = VTD_TRUE;
return ap->index;
}
}
}
ap->index += 2;
} else {
ap->vn->atTerminal = VTD_FALSE;
if (toElement(ap->vn, P) == VTD_FALSE) {
return -1;
} else {
ap->index = getCurrentIndex2(ap->vn) + 1;
}
}
}
return -1;
}
Boolean checkNsUniqueness(AutoPilot *ap, int index){
int j;
for (j=0;j<ap->fib->size;j++){
if (compareTokens(ap->vn,intAt(ap->fib,j), ap->vn, index)==0)
return VTD_FALSE;
}
appendInt(ap->fib,index);
return VTD_TRUE;
}
void selectNode(AutoPilot *ap){
ap->ft = VTD_TRUE;
ap->depth = getCurrentDepth(ap->vn);
ap->it = SIMPLE_NODE;
}
void selectPrecedingNode(AutoPilot *ap){
int i;
int a = sizeof(int)* ap->vn->nestingLevel;
ap->ft = VTD_TRUE;
ap->depth = getCurrentDepth(ap->vn);
ap->contextCopy = (int *)malloc(a); //(int[])vn.context.clone();
memcpy(ap->contextCopy,ap->vn->context,a);
if (ap->contextCopy[0]!=-1){
for (i=ap->contextCopy[0]+1;i<ap->vn->nestingLevel;i++){
ap->contextCopy[i]=0;
}
}//else{
// for (int i=1;i<contextCopy.length;i++){
// contextCopy[i]=0;
// }
//}
ap->it = PRECEDING_NODE;
ap->endIndex = getCurrentIndex(ap->vn);
}
void selectFollowingNode(AutoPilot *ap){
ap->ft = VTD_TRUE;
ap->depth = getCurrentDepth(ap->vn);
ap->it = FOLLOWING_NODE;
}
void selectDescendantNode(AutoPilot *ap){
ap->ft = VTD_TRUE;
ap->depth = getCurrentDepth(ap->vn);
ap->it = DESCENDANT_NODE;
}
Boolean iterateAP2(AutoPilot *ap){
switch (ap->it) {
case SIMPLE_NODE:
if (ap->ft && ap->vn->atTerminal)
return VTD_FALSE;
if (ap->ft){
ap->ft =VTD_FALSE;
return VTD_TRUE;
}
return iterateNode(ap->vn,ap->depth);
case DESCENDANT_NODE:
if (ap->ft&&ap->vn->atTerminal)
return VTD_FALSE;
else{
ap->ft=VTD_FALSE;
return iterateNode(ap->vn,ap->depth);
}
case FOLLOWING_NODE:
if (ap->ft){
Boolean b= VTD_FALSE;
do{
b = toNode(ap->vn,NEXT_SIBLING);
if (b){
ap->ft = VTD_FALSE;
return VTD_TRUE;
}else{
b = toNode(ap->vn,PARENT);
}
}while(b);
return VTD_FALSE;
}
return iterate_following_node(ap->vn);
case PRECEDING_NODE:
if(ap->ft){
ap->ft = VTD_FALSE;
toNode(ap->vn,ROOT);
toNode(ap->vn,PARENT);
}
return iterate_preceding_node(ap->vn,ap->contextCopy,ap->endIndex);
//case
default :
throwException2(pilot_exception," iteration action type undefined");
return VTD_FALSE;
}
}
void enableCaching(AutoPilot *ap, Boolean state){
ap->cachingOption = state;
}
VTDNav* ap_getNav(AutoPilot *ap) {
return ap->vn;
}