line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Armadito::Agent::Patterns::Matcher; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
9256748
|
use strict; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
197
|
|
4
|
7
|
|
|
7
|
|
28
|
use warnings; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
207
|
|
5
|
7
|
|
|
7
|
|
29
|
use UNIVERSAL::require; |
|
7
|
|
|
|
|
61
|
|
|
7
|
|
|
|
|
74
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
0
|
|
|
0
|
1
|
|
my ( $class, %params ) = @_; |
10
|
|
|
|
|
|
|
|
11
|
0
|
|
|
|
|
|
my %patterns; |
12
|
|
|
|
|
|
|
my $self = { |
13
|
|
|
|
|
|
|
patterns => \%patterns, |
14
|
|
|
|
|
|
|
logger => $params{logger}, |
15
|
0
|
|
|
|
|
|
exclusion_patterns => [], |
16
|
|
|
|
|
|
|
hooks => [] |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
bless $self, $class; |
20
|
0
|
|
|
|
|
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub getResults { |
24
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
|
my $results = {}; |
27
|
0
|
|
|
|
|
|
my %patterns = %{ $self->{patterns} }; |
|
0
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
foreach my $name ( keys(%patterns) ) { |
30
|
0
|
|
|
|
|
|
$results->{$name} = $self->getResultsForPattern( $patterns{$name}, $name ); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return $results; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub getResultsForPattern { |
37
|
0
|
|
|
0
|
0
|
|
my ( $self, $pattern, $name ) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my $i = 0; |
40
|
0
|
|
|
|
|
|
my $j = 0; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $pattern_results = []; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
foreach my $match ( @{ $pattern->{matches} } ) { |
|
0
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $match_results = {}; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if ( $pattern->{labels} eq "" ) { |
49
|
0
|
|
|
|
|
|
$match_results = $pattern->{matches}[$i][0]; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
0
|
|
|
|
|
|
foreach my $label ( @{ $pattern->{labels} } ) { |
|
0
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
$match_results->{$label} = $pattern->{matches}[$i][$j]; |
54
|
0
|
|
|
|
|
|
$match_results->{$label} = $self->runHooksForLabel( $label, $match_results->{$label} ); |
55
|
0
|
|
|
|
|
|
$j++; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$i++; |
60
|
0
|
|
|
|
|
|
$j = 0; |
61
|
0
|
|
|
|
|
|
push( @{$pattern_results}, $match_results ); |
|
0
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return $pattern_results; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub addPattern { |
68
|
0
|
|
|
0
|
1
|
|
my ( $self, $name, $regex, $labels ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
if ( !defined($labels) ) { |
71
|
0
|
|
|
|
|
|
$labels = ""; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $pattern = { |
75
|
|
|
|
|
|
|
regex => $regex, |
76
|
|
|
|
|
|
|
matches => [], |
77
|
|
|
|
|
|
|
labels => $labels, |
78
|
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
${ $self->{patterns} }{$name} = $pattern; |
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub addExclusionPattern { |
84
|
0
|
|
|
0
|
0
|
|
my ( $self, $pattern ) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
push( @{ $self->{exclusion_patterns} }, $pattern ); |
|
0
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub addExclusionPatterns { |
90
|
0
|
|
|
0
|
0
|
|
my ( $self, $patterns ) = @_; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$self->{exclusion_patterns} = $patterns; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub addHookForLabel { |
96
|
0
|
|
|
0
|
0
|
|
my ( $self, $label, $funcref ) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $hook = { |
99
|
|
|
|
|
|
|
label => $label, |
100
|
|
|
|
|
|
|
function => $funcref |
101
|
|
|
|
|
|
|
}; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
push( @{ $self->{hooks} }, $hook ); |
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub runHooksForLabel { |
107
|
0
|
|
|
0
|
0
|
|
my ( $self, $label, $match ) = @_; |
108
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
foreach my $hook ( @{ $self->{hooks} } ) { |
|
0
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
if ( $hook->{label} eq $label ) { |
111
|
0
|
|
|
|
|
|
return $hook->{function}->($match); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
return $match; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub run { |
119
|
0
|
|
|
0
|
1
|
|
my ( $self, $input, $separator ) = @_; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
my @substrings = split( /$separator/, $input ); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
foreach my $substring (@substrings) { |
124
|
0
|
0
|
|
|
|
|
if ( !$self->_isExcluded($substring) ) { |
125
|
0
|
|
|
|
|
|
$self->_parseSubString($substring); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _isExcluded { |
131
|
0
|
|
|
0
|
|
|
my ( $self, $substring ) = @_; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
foreach my $pattern ( @{ $self->{exclusion_patterns} } ) { |
|
0
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
if ( $substring =~ m/$pattern/ms ) { |
135
|
0
|
|
|
|
|
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
return 0; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub _parseSubString { |
143
|
0
|
|
|
0
|
|
|
my ( $self, $substring ) = @_; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my %patterns = %{ $self->{patterns} }; |
|
0
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
my $matches = (); |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
foreach my $name ( keys(%patterns) ) { |
149
|
0
|
|
|
|
|
|
$matches = $self->_checkPattern( $substring, ${ $self->{patterns} }{$name} ); |
|
0
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
if ($matches) { |
151
|
0
|
|
|
|
|
|
push( @{ ${ $self->{patterns} }{$name}->{matches} }, $matches ); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub _checkPattern { |
157
|
0
|
|
|
0
|
|
|
my ( $self, $substring, $pattern ) = @_; |
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
|
if ( my @matches = ( $substring =~ m/$pattern->{regex}/ms ) ) { |
160
|
0
|
|
|
|
|
|
return \@matches; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__END__ |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 NAME |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Armadito::Agent::Patterns::Matcher - Parses an input string with multiple regular expressions. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 DESCRIPTION |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Given plain text content is parsed with multiple patterns. Each pattern should have capturing parentheses. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 METHODS |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 $parser->new(%params) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
New parser instanciation. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 $parser->addPattern() |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Add new pattern for parsing. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 $parser->run() |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Run parser on input given. |
189
|
|
|
|
|
|
|
|