forked from Klortho/edirect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtract
executable file
·2529 lines (1972 loc) · 59.4 KB
/
xtract
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
#!/usr/bin/perl
# ===========================================================================
#
# PUBLIC DOMAIN NOTICE
# National Center for Biotechnology Information (NCBI)
#
# This software/database is a "United States Government Work" under the
# terms of the United States Copyright Act. It was written as part of
# the author's official duties as a United States Government employee and
# thus cannot be copyrighted. This software/database is freely available
# to the public for use. The National Library of Medicine and the U.S.
# Government do not place any restriction on its use or reproduction.
# We would, however, appreciate having the NCBI and the author cited in
# any work or product based on this material.
#
# Although all reasonable efforts have been taken to ensure the accuracy
# and reliability of the software and data, the NLM and the U.S.
# Government do not and cannot warrant the performance or results that
# may be obtained by using this software or data. The NLM and the U.S.
# Government disclaim all warranties, express or implied, including
# warranties of performance, merchantability or fitness for any particular
# purpose.
#
# ===========================================================================
#
# File Name: xtract
#
# Author: Jonathan Kans
#
# Version Creation Date: 8/20/12
#
# ==========================================================================
# Entrez Direct - EDirect
# use strict;
use warnings;
# definitions
use constant false => 0;
use constant true => 1;
# xtract version number
$version = "2.60";
# initialize memory hash
my %memory = ();
# initialize synopsis variables
my %synopsis_acc = ();
my $synopsis_max = 0;
# initial level is global
my $initial_level = 7;
# utility subroutines
sub convert_http {
my $str = shift (@_);
$str =~ s/\&\;/\&/g;
$str =~ s/\&apos\;/\'/g;
$str =~ s/\>\;/\>/g;
$str =~ s/\<\;/\</g;
$str =~ s/\"\;/\"/g;
return $str;
}
sub convert_slash {
my $str = shift (@_);
$str =~ s/\\t/\t/g;
$str =~ s/\\n/\n/g;
$str =~ s/\\r/\n/g;
return $str;
}
# xtract parses and extracts data from XML, driven by command-line arguments
sub process_element {
my $line = shift (@_);
my $val = shift (@_);
my $prev = shift (@_);
my $pfx = shift (@_);
my $sfx = shift (@_);
my $sep = shift (@_);
my $cmmd = shift (@_);
my $variable = shift (@_);
my $indx = shift (@_);
my $sclr = shift (@_);
if ( $val eq "" ) {
return false;
}
# -element "*" writes current XML object
if ( $val eq "*" ) {
print "$prev";
print "$pfx";
print "$line";
print "$sfx";
return true;
}
my @accum = ();
# commas are used to select unrelated items to show between pfx and sfx
my @itms = split (',', $val);
foreach $itm (@itms) {
my @atts = ();
my @vals = ();
my @working = ();
my $do_count = false;
my $do_length = false;
# look for # or % modifiers before XML element specification
if ( $itm =~ /^#(.+)/ ) {
# items in quotations starting with # sign display count of matches
$do_count = true;
$itm = $1;
} elsif ( $itm =~ /^%(.+)/ ) {
# items in quotations starting with % sign display sum of matched string lengths
$do_length = true;
$itm = $1;
}
# INSDSeq special cases - &Feature and &Qualifier
if ( $itm =~ /^\&Feature$/ ) {
$itm = "INSDFeature_key";
} elsif ( $itm =~ /^\&Qualifier$/ ) {
$itm = "INSDQualifier_value";
}
# -element pattern recognition
if ( $itm =~ /^&([A-Z0-9]+)$/ ) {
# match stored &VARIABLE
my $idx = $1;
if ( exists ($memory{$idx}) and $memory{"$idx"} ne "" ) {
my $val = $memory{$idx};
$dec = convert_http($val);
push (@working, $dec);
}
} elsif ( $itm =~ /^\?([A-Z]+)\((.*)\)/ ) {
# match exploration flag (alias to print is in parentheses) (undocumented)
my $itm = $1;
my $rep = $2;
$dec = convert_http($rep);
# first and last elements are always present, and are the same for a one-element list
if ( $itm eq "FIRST" and $indx == 1 ) {
push (@working, $dec);
} elsif ( $itm eq "LAST" and $indx == $sclr ) {
push (@working, $dec);
# single, head, inner, tail are mutually exclusive situations
} elsif ( $itm eq "SINGLE" and $sclr == 1 ) {
push (@working, $dec);
} elsif ( $itm eq "HEAD" and $indx == 1 and $sclr > 1 ) {
push (@working, $dec);
} elsif ( $itm eq "INNER" and $indx > 1 and $indx < $sclr ) {
push (@working, $dec);
} elsif ( $itm eq "TAIL" and $indx == $sclr and $sclr > 1 ) {
push (@working, $dec);
# even and odd are based only on index value, not list length
} elsif ( $itm eq "EVEN" and ( $indx % 2) == 0 ) {
push (@working, $dec);
} elsif ( $itm eq "ODD" and ( $indx % 2) == 1 ) {
push (@working, $dec);
}
} elsif ( $itm =~ /^@(.+)/ ) {
# match unqualified @attribute
my $att = $1;
@atts = ($line =~ /<\S+ ([^>]+)>/g);
foreach $val (@atts) {
if ( $val =~ /$att=\"([^\"]+)\"/ ) {
$dec = convert_http($1);
push (@working, $dec);
}
}
} elsif ( $itm =~ /(.+)@(.+)/ ) {
# match qualified tag@attribute
my $tag = $1;
my $att = $2;
@atts = ($line =~ /<$tag ([^>]+)>/g);
foreach $val (@atts) {
if ( $val =~ /$att=\"([^\"]+)\"/ ) {
$dec = convert_http($1);
push (@working, $dec);
}
}
} elsif ( $itm =~ /(.+)\/(.+)/ ) {
# match parent/child by tracking depth level of tokens
my $tag = $1;
my $chd = $2;
@vals = ($line =~ /<$tag(?:\s+.+?)?>(.+?)<\/$tag>/g);
foreach $val (@vals) {
my $lvl = 0;
my @tokens = split (/(?<=>)(?=<)/, $val);
foreach $tkn (@tokens) {
if ( $lvl < 0 ) {
# ignore remainder if level drops below zero
} elsif ( $tkn =~ /^<.+?>.+?<\/.+?>$/ ) {
# content-containing tag does not change level
if ( $tkn =~ /<$chd(?:\s+.+?)?>(.+?)<\/$chd>/ ) {
# child tag matches, only accept if immediately below parent
if ( $lvl == 0 ) {
$dec = convert_http($1);
push (@working, $dec);
}
}
} elsif ( $tkn =~ /\/>$/ ) {
# self-closing token does not change level
} elsif ( $tkn =~ /^<[^\/]/ ) {
# open tag increments level
$lvl++;
} elsif ( $tkn =~ /^<\// ) {
# close tag decrements level
$lvl--;
}
}
}
} elsif ( $itm =~ /(.+)\((.*)\)/ ) {
# match self-closing tag (alias to print is in parentheses)
my $tag = $1;
my $rep = $2;
if ( $rep eq "" ) {
$rep = $tag;
}
# [ is it possible to look for both forms in one pass ? ]
@vals = ($line =~ /(<$tag.*?><\/$tag>)/g);
if ( scalar @vals != 0 ) {
# found alternative form ( <tag></tag> ) of self-closing tag
foreach $val (@vals) {
$dec = convert_http($rep);
push (@working, $dec);
}
} else {
# look for compact form ( <tag/> ) if no match to alternative form
if ( scalar @vals == 0 ) {
@vals = ($line =~ /(<$tag.*?\/>)/g);
foreach $val (@vals) {
$dec = convert_http($rep);
push (@working, $dec);
}
}
}
} else {
# match tag with contents
my $tag = $itm;
@vals = ($line =~ /<$tag(?:\s+.+?)?>(.+?)<\/$tag>/g);
foreach $val (@vals) {
$dec = convert_http($val);
push (@working, $dec);
}
}
my $num = scalar @working;
# if -first, -last, etc., remove all but matching subset
if ( $num < 1 ) {
# no matches collected, skip
} elsif ( $cmmd eq "-first" ) {
@working = splice (@working, 0, 1);
$num = scalar @working;
} elsif ( $cmmd eq "-last" ) {
@working = splice (@working, $num - 1, 1);
$num = scalar @working;
} elsif ( $cmmd eq "-single" ) {
if ( $num > 1 ) {
$num = 0;
}
} elsif ( $cmmd eq "-head" ) {
if ( $num < 2 ) {
$num = 0;
} else {
@working = splice (@working, 0, 1);
$num = scalar @working;
}
} elsif ( $cmmd eq "-inner" ) {
if ( $num < 3 ) {
$num = 0;
} else {
@working = splice (@working, 1, $num - 2);
$num = scalar @working;
}
} elsif ( $cmmd eq "-tail" ) {
if ( $num < 2 ) {
$num = 0;
} else {
@working = splice (@working, $num - 1, 1);
$num = scalar @working;
}
}
# store current results into accumulator
if ( $num == 0 ) {
# no printable results
} elsif ( $do_count ) {
push (@accum, $num);
} elsif ( $do_length ) {
my $len = 0;
foreach $dec (@working) {
$len += length ( $dec );
}
push (@accum, $len);
} elsif ( $cmmd eq "-even" ) {
my $take = false;
foreach $dec (@working) {
if ( $take ) {
push (@accum, $dec);
}
$take = (! $take);
}
} elsif ( $cmmd eq "-odd" ) {
my $take = true;
foreach $dec (@working) {
if ( $take ) {
push (@accum, $dec);
}
$take = (! $take);
}
} else {
foreach $dec (@working) {
push (@accum, $dec);
}
}
}
# process results
if ( scalar @accum == 0 ) {
return false;
} elsif ( $variable ne "") {
# store accumulated components into variable
my $str = $pfx;
my $between = "";
foreach $dec (@accum) {
$str .= "$between";
$between = $sep;
$str .= "$dec";
}
$str .= "$sfx";
$memory{"$variable"} = "$str";
} else {
# format accumulated elements
my $before = $prev . $pfx;
my $between = "";
my $after = "";
foreach $dec (@accum) {
print "$before";
$before = "";
print "$between";
$between = $sep;
$after = $sfx;
print "$dec";
}
print "$after";
return true;
}
return false;
}
sub process_flags {
my $line = shift (@_);
my @args = @{shift (@_)};
my $tab = shift (@_);
my $ret = shift (@_);
my $indx = shift (@_);
my $sclr = shift (@_);
my $pfx = "";
my $sfx = "";
my $sep = "\t";
my $col = "\t";
my $lin = "\n";
my $okay = false;
my $cmmd = "";
my $max = scalar @args;
for ( my $i = 0; $i < $max; $i++ ) {
# read command-line arguments
my $val = $args[$i];
# if new argument starts with hyphen
if ( $val =~ /^\-/ ) {
# not an -element value, so set $okay flag to false
$okay = false;
# all flags should be followed by at least one value
if ( $i + 1 >= $max and $val ne "-outline" and $val ne "-synopsis" ) {
# warn that the last flag has no value
print STDERR "No argument after '$val'\n";
# break out of for loop to return from function
last;
# otherwise safe to increment with $i++ and then dereference $args [$i]
}
}
# print outline of XML structure
if ( $val eq "-outline" ) {
my $lvl = 0;
my $dpth = 0;
my @tokens = split (/(?<=>)(?=<)/, $line);
foreach $tkn (@tokens) {
if ( $lvl < 0 ) {
# ignore remainder if level drops below zero
} elsif ( $tkn =~ /^<.+?>.+?<\/(.+?)>$/ ) {
# content-containing tag
if ( $tkn =~ /^<(\S+).*?>.*>$/ ) {
$tkn = $1;
$lvl++;
$dpth++;
for ( $i = 0; $i < $dpth; $i++ ) {
print " ";
}
print "$tkn\n";
$lvl--;
$dpth--;
}
} elsif ( $tkn =~ /^<.+?\/>$/ ) {
# self-closing token
if ( $tkn =~ /^<(\S+).*?\/>$/ ) {
$tkn = $1;
$lvl++;
$dpth++;
for ( $i = 0; $i < $dpth; $i++ ) {
print " ";
}
print "$tkn\n";
$lvl--;
$dpth--;
}
} elsif ( $tkn =~ /^<[^\/]/ ) {
# open tag increments level
$lvl++;
$dpth++;
if ( $tkn =~ /^<(\S+).*?>$/ ) {
$tkn = $1;
if ( $tkn ne "?xml" and
$tkn ne "!DOCTYPE" and
$tkn ne "eSummaryResult" and
$tkn ne "eLinkResult" and
$tkn ne "eInfoResult" and
$tkn ne "PubmedArticleSet" and
$tkn ne "DocumentSummarySet" and
$tkn ne "INSDSet" and
$tkn ne "Entrezgene-Set" and
$tkn ne "TaxaSet" ) {
for ( $i = 0; $i < $dpth; $i++ ) {
print " ";
}
print "$tkn\n";
} else {
$dpth--;
}
}
} elsif ( $tkn =~ /^<\// ) {
# close tag decrements level
$lvl--;
$dpth--;
}
}
# collect summary of XML paths, print at end
} elsif ( $val eq "-synopsis" ) {
my @arr = ();
my $lvl = 0;
my @tokens = split (/(?<=>)(?=<)/, $line);
foreach $tkn (@tokens) {
my $record_level = 0;
if ( $lvl < 0 ) {
# ignore remainder if level drops below zero
} elsif ( $tkn =~ /^<.+?>.+?<\/(.+?)>$/ ) {
# content-containing tag
if ( $tkn =~ /^<(\S+).*?>.*>$/ ) {
$tkn = $1;
$lvl++;
$arr[$lvl] = $tkn;
$record_level = $lvl;
$lvl--;
}
} elsif ( $tkn =~ /^<.+?\/>$/ ) {
# self-closing token
if ( $tkn =~ /^<(\S+).*?\/>$/ ) {
$tkn = $1;
$lvl++;
$arr[$lvl] = $tkn;
$record_level = $lvl;
$lvl--;
}
} elsif ( $tkn =~ /^<[^\/]/ ) {
# open tag increments level
$lvl++;
if ( $tkn =~ /^<(\S+).*?>$/ ) {
$tkn = $1;
$arr[$lvl] = $tkn;
$record_level = $lvl;
}
} elsif ( $tkn =~ /^<\// ) {
# close tag decrements level
$lvl--;
}
if ( $record_level > 0 ) {
my $str = "";
my $pfx = "";
for ( $i = 1; $i <= $record_level; $i++ ) {
$itm = $arr[$i];
if ( $itm ne "?xml" and
$itm ne "!DOCTYPE" and
$itm ne "eSummaryResult" and
$itm ne "eLinkResult" and
$itm ne "eInfoResult" and
$itm ne "PubmedArticleSet" and
$itm ne "DocumentSummarySet" and
$itm ne "INSDSet" and
$itm ne "Entrezgene-Set" and
$itm ne "TaxaSet" ) {
$str .= "$pfx";
$pfx = "/";
$str .= "$arr[$i]";
}
}
if ( $str ne "" ) {
my $val = 0;
if ( exists ($synopsis_acc{"$str"}) ) {
$val = $synopsis_acc{"$str"};
}
$val++;
$synopsis_acc{"$str"} = $val;
if ( $val > $synopsis_max ) {
$synopsis_max = $val;
}
}
}
}
# look for arguments that adjust output formatting
} elsif ( $val eq "-pfx" ) {
$i++;
$pfx = convert_slash ( $args[$i] );
} elsif ( $val eq "-sfx" ) {
$i++;
$sfx = convert_slash ( $args[$i] );
} elsif ( $val eq "-sep" ) {
$i++;
$sep = convert_slash ( $args[$i] );
} elsif ( $val eq "-tab" ) {
$i++;
$col = convert_slash ( $args[$i] );
} elsif ( $val eq "-ret" ) {
$i++;
$lin = convert_slash ( $args[$i] );
} elsif ( $val eq "-lbl" ) {
$i++;
print "$tab";
$lbl = convert_slash ( $args[$i] );
print "$lbl";
$tab = $col;
$ret = $lin;
# look for -element or limiting variants, non-hyphenated arguments to follow
} elsif ( $val eq "-element" or
$val eq "-first" or
$val eq "-last" or
$val eq "-even" or
$val eq "-odd" or
$val eq "-single" or
$val eq "-head" or
$val eq "-inner" or
$val eq "-tail" ) {
$okay = true;
$cmmd = $val;
# hyphen followed by capital letters or digits is a variable
} elsif ( $val =~ /^\-([A-Z0-9]+)([a-z]?)$/ ) {
my $variable = $1;
my $first_or_last = $2;
$cmmd = "-element";
# detect f, l, etc., variable suffix for -first, -last, etc. (undocumented)
if ( $first_or_last eq "f" ) {
$cmmd = "-first";
} elsif ( $first_or_last eq "l" ) {
$cmmd = "-last";
} elsif ( $first_or_last eq "e" ) {
$cmmd = "-even";
} elsif ( $first_or_last eq "o" ) {
$cmmd = "-odd";
} elsif ( $first_or_last eq "s" ) {
$cmmd = "-single";
} elsif ( $first_or_last eq "h" ) {
$cmmd = "-head";
} elsif ( $first_or_last eq "i" ) {
$cmmd = "-inner";
} elsif ( $first_or_last eq "t" ) {
$cmmd = "-tail";
}
# clear the variable to avoid old value persisting if no subsequent match
$memory{"$variable"} = "";
$i++;
$val = $args[$i];
# set variable to new value
if ( $val =~ /^\((.*)\)$/ ) {
# parentheses contain literal value for variable
$val = convert_slash ( $1 );
$memory{"$variable"} = "$val";
} else {
# if XML tag name, record what would otherwise be printed by -element
process_element ( $line, $val, $tab, $pfx, $sfx, $sep, $cmmd, $variable, $indx, $sclr );
}
# reality check on unsupported arguments
} elsif ( $val =~ /^\-/ ) {
print STDERR "Unrecognized argument '$val'\n";
} elsif ( $okay ) {
# process -element values
if ( process_element ( $line, $val, $tab, $pfx, $sfx, $sep, $cmmd, "", $indx, $sclr )) {
$tab = $col;
$ret = $lin;
}
} else {
print STDERR "No -element before '$val'\n";
}
}
return $tab, $ret;
}
sub has_content {
my $line = shift (@_);
my $str = shift (@_);
my $indx = shift (@_);
my $sclr = shift (@_);
if ( $str eq "" or $str eq "&" ) {
return false;
}
# match by non-empty variable value (undocumented)
if ( $str =~ /^&([A-Z0-9]+)$/ ) {
$str = $1;
if ( exists ($memory{"$str"}) and $memory{"$str"} ne "" ) {
return true;
}
return false;
}
# remove leading backslash used to prevent content from being mistaken for variable
$str =~ s/^\\&/&/;
# match by exploration flag (undocumented)
if ( $str =~ /^\?([A-Z]+)/ ) {
$str = $1;
# first and last elements are always present, and are the same for a one-element list
if ( $str eq "FIRST" and $indx == 1 ) {
return true;
} elsif ( $str eq "LAST" and $indx == $sclr ) {
return true;
# single, head, inner, tail are mutually exclusive situations
} elsif ( $str eq "SINGLE" and $sclr == 1 ) {
return true;
} elsif ( $str eq "HEAD" and $indx == 1 and $sclr > 1 ) {
return true;
} elsif ( $str eq "INNER" and $indx > 1 and $indx < $sclr ) {
return true;
} elsif ( $str eq "TAIL" and $indx == $sclr and $sclr > 1 ) {
return true;
# even and odd are based only on index value, not list length
} elsif ( $str eq "EVEN" and ( $indx % 2) == 0 ) {
return true;
} elsif ( $str eq "ODD" and ( $indx % 2) == 1 ) {
return true;
}
}
# remove leading backslash used to prevent content from being mistaken for flag
$str =~ s/^\\\?/?/;
# exact match by data contents, not using regular expression, case insensitive
if ( index ( uc($line), uc($str) ) >= 0 ) {
return true;
}
return false;
}
sub has_element {
my $line = shift (@_);
my $str = shift (@_);
if ( $str eq "" ) {
return false;
}
# track depth level of tokens if pattern was parent/child (undocumented)
if ( $str =~ /(.+)\/(.+)/ ) {
my $ptrn = $1;
my $chld = $2;
my @vals = ();
my $lvl = 0;
my $dpth = 0;
my $in_pat = false;
my @tokens = split (/(?<=>)(?=<)/, $line);
foreach $tkn (@tokens) {
if ( $lvl < 0 ) {
# ignore remainder if level drops below zero
} elsif ( $tkn =~ /^<.+?>.+?<\/.+?>$/ ) {
# content-containing tag does not change level
if ( $in_pat and $lvl == $dpth ) {
push (@vals, $tkn);
}
} elsif ( $tkn =~ /\/>$/ ) {
# self-closing token does not change level
if ( $in_pat and $lvl == $dpth ) {
push (@vals, $tkn);
}
} elsif ( $tkn =~ /^<[^\/]/ ) {
# open tag increments level
$lvl++;
if ( $tkn =~ /<$ptrn(?:\s+.+?)?>/ and $dpth == 0 ) {
$in_pat = true;
$dpth = $lvl;
push (@vals, $tkn);
}
} elsif ( $tkn =~ /^<\// ) {
# close tag decrements level
if ( $in_pat and $tkn =~ /<\/$ptrn>/ and $lvl == $dpth ) {
push (@vals, $tkn);
$in_pat = false;
$dpth = 0;
}
$lvl--;
}
}
if ( scalar @vals > 0 ) {
my $sub = join ("", @vals);
if ( has_element ( $sub, $chld )) {
return true;
}
}
return false;
}
# match by specific variable value (undocumented)
if ( $str =~ /^\&([A-Z0-9]+)\:(.+)$/ ) {
my $ptrn = $1;
my $chld = $2;
if ( exists ($memory{"$ptrn"}) and $memory{"$ptrn"} eq "$chld" ) {
return true;
}
return false;
}
# match by non-empty variable value (undocumented)
if ( $str =~ /^\&([A-Z0-9]+)$/ ) {
$str = $1;
if ( exists ($memory{"$str"}) and $memory{"$str"} ne "" ) {
return true;
}
return false;
}
# regular expression search for element@attribute:value
if ( $str =~ /(.+)@(.+)\:(.+)/ ) {
my $ptrn = $1;
my $attr = $2;
my $chld = $3;
if ( $line =~ /<$ptrn ([^>]+)>/i ) {
my $atts = $1;
if ( $atts =~ /$attr=\"$chld\"/ ) {
return true;
}
}
return false;
}
# regular expression search for @attribute:value
if ( $str =~ /@(.+)\:(.+)/ ) {
my $attr = $1;
my $chld = $2;
if ( $line =~ /<\S+ ([^>]+)>/i ) {
my $atts = $1;
if ( $atts =~ /$attr=\"$chld\"/ ) {
return true;
}
}
return false;
}
# regular expression search for element@attribute
if ( $str =~ /(.+)@(.+)/ ) {
my $ptrn = $1;
my $attr = $2;
if ( $line =~ /<$ptrn ([^>]+)>/i ) {
my $atts = $1;
if ( $atts =~ /$attr=\"([^\"]+)\"/ ) {
return true;
}
}
return false;
}
# regular expression search for @attribute
if ( $str =~ /@(.+)/ ) {
my $attr = $1;
if ( $line =~ /<\S+ ([^>]+)>/i ) {
my $atts = $1;
if ( $atts =~ /$attr=\"([^\"]+)\"/ ) {
return true;
}