File Coverage

blib/lib/WebSource/Filter/tests.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WebSource::Filter::tests;
2 1     1   6923 use strict;
  1         2  
  1         39  
3 1     1   5 use Carp;
  1         3  
  1         83  
4 1     1   4404 use String::Approx qw/amatch/;
  1         15091  
  1         116  
5              
6 1     1   146 use WebSource::Filter;
  0            
  0            
7             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 : Succeeds if a given XPATH expression returns a result
29              
30             =item B : Succeeds if a given regular expression matches the input
31              
32             =item B : Succeeds of a given string is approximately found in the
33             input
34             =back
35              
36             =head1 SYNOPSIS
37              
38             B
39              
40            
41             " action="keep" />
42            
43             match="" action="keep"/>
44            
45             match="" modifiers="" action="reject" />
46             ...
47            
48              
49              
50             =head1 METHODS
51              
52             =cut
53              
54             sub _init_ {
55             my $self = shift;
56             $self->SUPER::_init_;
57             $self->{wsdnode} or croak("No description node given");
58             }
59              
60             sub keep {
61             my $self = shift;
62             my $env = shift;
63             $self->log(3,"Testing");
64             $env->type eq "object/dom-node" or return 0; # only works for dom-nodes
65             my @nodes = $self->{wsdnode}->findnodes("test");
66             my $data = $env->data;
67             my $result = undef;
68             $self->log(3,"Found ",scalar(@nodes)," test nodes");
69             while(!$result && @nodes) {
70             my $match = 0;
71             my $n = shift @nodes;
72             my $type = $n->getAttribute("type");
73             $type or $type = "regexp";
74             if($type eq "exists") {
75             my $select = $n->getAttribute("select");
76             my @res = $data->findnodes($n->getAttribute("select"));
77             $match = (scalar(@res) > 0);
78             $self->log(5,"Existence with '$select' resulted in ",
79             scalar(@res)," nodes ($match)");
80             } elsif($type eq "xpath") {
81             my $select = $n->getAttribute("select");
82             $match = $data->find("boolean(".$n->getAttribute("select").")")->value();
83             $self->log(5,"Xpath test with '$select' resulted in $match");
84             } else {
85             my $str = $data->findvalue($n->getAttribute("select"));
86             my $pat = $n->getAttribute("match");
87             if($type eq "regexp") {
88             $self->log(3,"Trying to match '$str' with m/$pat/i");
89             $match = $str =~ m/$pat/i;
90             } elsif($type eq "approx") {
91             my $mod = $n->getAttribute("modifiers");
92             $self->log(3,"Matching '$str' approximately against",
93             " pattern '$pat' with modifier string '$mod'");
94             $match = amatch($pat,[ $mod ],$str);
95             }
96             }
97             if($match) {
98             my $action = $n->getAttribute("action");
99             $action or $action = "keep";
100             $result = $action;
101             $self->log(5,"Found a match, taking action : ",$result);
102             }
103             }
104             return ($result eq "keep");
105             }
106              
107             =head1 SEE ALSO
108              
109             B, B
110              
111             =cut
112              
113             1;