| 1 | package WebSource::Filter::tests; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use String::Approx qw/amatch/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use WebSource::Filter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | our @ISA = ('WebSource::Filter'); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | WebSource::Filter::tests - apply tests to filter xmlnodes | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | The tests type of filter allows to declare a series of tests | ||||||||||||||||||||||||||||
| 16 | and apply them to the input data in order to determine whether | ||||||||||||||||||||||||||||
| 17 | or not to send it further on. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | The tests are executed in the order of their declaration until one of them | ||||||||||||||||||||||||||||
| 20 | matches. They associated action (keep or reject) is taken. By default the | ||||||||||||||||||||||||||||
| 21 | action is to keep elements matching the test. If the element does not match | ||||||||||||||||||||||||||||
| 22 | any tests it is rejected. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Current existing tests include : | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =over 2 | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =item B<exists> : Succeeds if a given XPATH expression returns a result | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item B<regexp> : Succeeds if a given regular expression matches the input | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item B<approx> : Succeeds of a given string is approximately found in the | ||||||||||||||||||||||||||||
| 33 | input | ||||||||||||||||||||||||||||
| 34 | =back | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | B<In wsd file...> | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | <ws:filter name="somename" type="tests"> | ||||||||||||||||||||||||||||
| 41 | <test type="exists" select"<xpath-expr>" action="keep" /> | ||||||||||||||||||||||||||||
| 42 | <test type="regexp" select="<xpath-expr>" | ||||||||||||||||||||||||||||
| 43 | match="<regexp>" action="keep"/> | ||||||||||||||||||||||||||||
| 44 | <test type="approx" select="<xpath-expr>" | ||||||||||||||||||||||||||||
| 45 | match="<pattern>" modifiers="" action="reject" /> | ||||||||||||||||||||||||||||
| 46 | ... | ||||||||||||||||||||||||||||
| 47 | </ws:filter> | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 METHODS | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub _init_ { | ||||||||||||||||||||||||||
|
Subroutine
_init_: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 55 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 0 | $self->SUPER::_init_; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->{wsdnode} or croak("No description node given"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub keep { | ||||||||||||||||||||||||||
|
Subroutine
keep: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | my $env = shift; | |||||||||||||||||||||||||||
| 63 | 0 | $self->log(3,"Testing"); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $env->type eq "object/dom-node" or return 0; # only works for dom-nodes | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | my @nodes = $self->{wsdnode}->findnodes("test"); | |||||||||||||||||||||||||||
| 66 | 0 | my $data = $env->data; | |||||||||||||||||||||||||||
| 67 | 0 | my $result = undef; | |||||||||||||||||||||||||||
| 68 | 0 | $self->log(3,"Found ",scalar(@nodes)," test nodes"); | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | while(!$result && @nodes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $result and @nodes
MC/DC0%0/2
not $result and @nodes
not $result
@nodes
Truth table0%0/3
not(A) and B
A= not $resultB= @nodes
| |||||||||||||||||||||||||||||
| 70 | 0 | my $match = 0; | |||||||||||||||||||||||||||
| 71 | 0 | my $n = shift @nodes; | |||||||||||||||||||||||||||
| 72 | 0 | my $type = $n->getAttribute("type"); | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | $type or $type = "regexp"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($type eq "exists") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 0 | my $select = $n->getAttribute("select"); | |||||||||||||||||||||||||||
| 76 | 0 | my @res = $data->findnodes($n->getAttribute("select")); | |||||||||||||||||||||||||||
| 77 | 0 | $match = (scalar(@res) > 0); | |||||||||||||||||||||||||||
| 78 | 0 | $self->log(5,"Existence with '$select' resulted in ", | |||||||||||||||||||||||||||
| 79 | scalar(@res)," nodes ($match)"); | ||||||||||||||||||||||||||||
| 80 | } elsif($type eq "xpath") { | ||||||||||||||||||||||||||||
| 81 | 0 | my $select = $n->getAttribute("select"); | |||||||||||||||||||||||||||
| 82 | 0 | $match = $data->find("boolean(".$n->getAttribute("select").")")->value(); | |||||||||||||||||||||||||||
| 83 | 0 | $self->log(5,"Xpath test with '$select' resulted in $match"); | |||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 0 | my $str = $data->findvalue($n->getAttribute("select")); | |||||||||||||||||||||||||||
| 86 | 0 | my $pat = $n->getAttribute("match"); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | if($type eq "regexp") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->log(3,"Trying to match '$str' with m/$pat/i"); | |||||||||||||||||||||||||||
| 89 | 0 | $match = $str =~ m/$pat/i; | |||||||||||||||||||||||||||
| 90 | } elsif($type eq "approx") { | ||||||||||||||||||||||||||||
| 91 | 0 | my $mod = $n->getAttribute("modifiers"); | |||||||||||||||||||||||||||
| 92 | 0 | $self->log(3,"Matching '$str' approximately against", | |||||||||||||||||||||||||||
| 93 | " pattern '$pat' with modifier string '$mod'"); | ||||||||||||||||||||||||||||
| 94 | 0 | $match = amatch($pat,[ $mod ],$str); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if($match) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | my $action = $n->getAttribute("action"); | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | $action or $action = "keep"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $result = $action; | |||||||||||||||||||||||||||
| 101 | 0 | $self->log(5,"Found a match, taking action : ",$result); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | return ($result eq "keep"); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | B<WebSource>, B<WebSource::Filter> | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | 1; | |||||||||||||||||||||||||||