blib/lib/Text/XmlMatch.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 88.8% 32 / 36 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 93.3% 154 / 165 CC 30 SCAR 22.9
CC
30
Cov
95%
findMatch
17.3
listGroups
8.0
_criteriaSort
6.0
CRAP 9.9
↑ Summary
1 package Text::XmlMatch;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 #XML package designed to provide easy, configurable groups via XML
6 #configuration file
7  
8 # 06/30/06 - Jason A. Lee - Original Code write
9 #
10 # 07/06/06 - JAL - Added new method listGroups();
11 #
12 # 07/13/06 - JAL - Removed unused variables, EXPORT, EXPORT_OK, etc.
13 #
14 # 02/06/07 - JAL - Hotfix 2 -> Correct POD section. No tag means hash
15 # value = '0'
16 #
17 # 02/07/07 - JAL - Hotfix 3 -> Corrected bug where config file with a
18 # single pattern caused the module to fail.
19 #
20 # 02/12/07 - JAL - Hotfix 4 -> Minor POD cleanup, added 'use warnings'
21 #
22 # 06/06/07 - JAL - Hotfix 5 -> Fixed bug in listGroups() failing when
23 # config file contains only a single pattern
24 #
25 # 06/06/07 - JAL - Hotfix 6 -> calling findMatch() prior to
26 # listGroups() was concealing another bug in
27 # listGroups() when only a single pattern name is
28 # specified in a config file.
29  
30  
31 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 ▶ use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 11 ▶ use Data::Dumper; #Here for debug/development
Subroutine BEGIN: called CC=1 SCAR=0.0
34 11 ▶ use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 ▶ $VERSION = 1.0006;
Subroutine BEGIN: not called CC=1 SCAR=0.0
36 }
37  
38  
39 12 ▶sub new { #Standard object constructor
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
40 12 my $obj = shift;
41 12 my $configFile = shift; #This is the filename passed as an argument
42 12 my $xmlConfig = XMLin($configFile);
43 12 bless $xmlConfig, $obj;
44 12 return $xmlConfig;
45 }
46  
47 53 ▶sub findMatch {
Subroutine findMatch: called CC=17 SCAR=28.5
Pod undocumented
48 53 my $config = shift; #this holds the XML object
49 53 our $name = shift; #this holds the string to be matched against, typically a FQDN
50  
51 53 my $r_matchResults = {}; #this is an intermediate hash, used prior to returning final hash, key->'pattern name'
52  
53 #Now crawl through the hash looking for matches
54 53 foreach my $group (keys %{$config->{pattern}}) { #walk through each pattern name
55 ############
56 # Hotfix 3 #
57 #################################################################
58 #The block below handles a special case where the user provides a
59 #configuration that contains a single pattern. In such cases,
60 #XML::Simple creates a hash structure that is completely different
61 #than the one that is created when two or more patterns are
62 #specified. This block creates a new hash structure that matches
63 #the expected format. Note that although we are currently in a
64 #foreach loop, the loop gets killed for this special case as there
65 #are no keys to step through.
66 #################################################################
67  
68 #print "DEBUG: Processing pattern $group\n";
69 1041 ▶ if (defined $config->{pattern}->{name}) { #if this key is defined then hash is collapsed
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"pattern"}{"name"}) 1 1040 1041
70 #print "DEBUG: Hash not found, must be single pattern!\n";
71 #print "Dumper Single: " . Dumper($config->{pattern}->{$group}) . "\n";
72 #rebuild hash structure so that it looks like what's expected
73 1 my $singlePatternName = $config->{pattern}->{name};
74 1 my $newHashStructure = {};
75 1 foreach my $single (keys %{$config->{pattern}}) {
76 3 ▶ next if ($single eq 'name'); # we're rebuilding hash to standard form, don't need this key
Branch: 1 branch
BranchTrueFalseTotal
if $single eq "name" 1 2 3
77 2 $newHashStructure->{pattern}->{$singlePatternName}->{$single} = $config->{pattern}->{$single};
78 }
79 #print "DEBUG: new hash structure: " . Dumper($newHashStructure) . "\n";
80 1 $config->{pattern} = $newHashStructure->{pattern}; #copy newly structured hash over the original
81 #print "DEBUG: new config: " . Dumper($config) . "\n";
82 1 $group = $singlePatternName;
83 }
84 #End of Hotfix 3 block#########################################
85  
86 #print "DEBUG Standard Dumper: " . Dumper($config->{pattern}->{$group}) . " \n";
87 1041 foreach my $criteria (keys %{$config->{pattern}->{$group}}) { #within each pattern, walk through qualifiers
88 #Now determine the criteria relative to each
89 #Sometimes it's a single element, other times it's an array ref or hash ref
90 #This can't be controlled, as XML::Simple will translate the XML to whatever makes more sense
91 #so must check for ARRAY, HASH, and SCALAR references
92 2297 my $deviceTag;
93 2297 ▶ if (ref ($config->{pattern}->{$group}->{$criteria}) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $config->{'pattern'}{$group}{$criteria} eq 'ARRAY') { } 92 2205 2297
elsif (ref $config->{'pattern'}{$group}{$criteria} eq 'HASH') { } 0 2205 2205
94 92 foreach my $matchPattern (@{$config->{pattern}->{$group}->{$criteria}}) { #walk through exclusion, inclusion, tag
95 #print "Array ref: $group $criteria $matchPattern\n";
96 232 _criteriaSort($r_matchResults,$criteria,$matchPattern,$group);
97 }
98 } elsif (ref ($config->{pattern}->{$group}->{$criteria}) eq "HASH") {
99 0 foreach my $matchPattern (keys %{$config->{pattern}->{$group}->{$criteria}}) {#walk through exclusion, inclusion, tag
100 #print "hash ref: $group $criteria $matchPattern\n";
101 0 _criteriaSort($r_matchResults,$criteria,$matchPattern,$group);
102 }
103 } else { #Not array reference, just print value
104 #print "scalar: $group $criteria $config->{pattern}->{$group}->{$criteria}\n";
105 2205 my $matchPattern = $config->{pattern}->{$group}->{$criteria};
106 2205 ▶ next if ($matchPattern eq ''); #Don't want to process null values in _criteriaSort
Branch: 1 branch
BranchTrueFalseTotal
if $matchPattern eq "" 0 2205 2205
107 2205 _criteriaSort($r_matchResults,$criteria,$matchPattern,$group);
108 }
109 }
110 ##########
111 #Hotfix 3#
112 #################################################################
113 #this line stops the looping through keys, as there are no keys to
114 #step through in the case of a single pattern XML config file.
115  
116 1041 ▶ last if (defined $config->{pattern}->{name}); #If case of single pattern, don't try to step through more
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"pattern"}{"name"} 1 1040 1041
117 }
118  
119 #After sifting through the XML mess and walking the trees, time to print final result
120 53 my $r_returnMatchList = {};
121  
122 53 foreach my $nameMap (keys %{$r_matchResults->{$name}}) {
123 1040 my $oldNameMap = $nameMap;
124 #my $newNameMap = ''; #we might change group name soon
125 #It's possible the group name became changed if user specified back-references, check for that here
126 1040 ▶ if ( $r_matchResults->{$name}->{$nameMap}->{newName} ) { #if group name has changed
Branch: 1 branch
BranchTrueFalseTotal
if ($r_matchResults->{$Text::XmlMatch::name}{$nameMap}{"newName"}) 114 926 1040
127 #print "$nameMap is changing name to $r_matchResults->{$name}->{$nameMap}->{newName}\n";
128 114 $nameMap = $r_matchResults->{$name}->{$nameMap}->{newName};
129 }
130 1040 ▶ if ( ($r_matchResults->{$name}->{$oldNameMap}->{inclusion}) && !($r_matchResults->{$name}->{$oldNameMap}->{exclusion})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"inclusion"} and not $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"exclusion"}) 80 960 1040
Condition100%3/3
$r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"inclusion"} and not $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"exclusion"}
!ll&&!rl&&r
951980
MC/DC100%2/2
$r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"inclusion"} and not $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"exclusion"}
$r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"inclusion"} not $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"exclusion"}
Truth table100%3/3
A and not(B)
A= $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"inclusion"}B= not $r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{"exclusion"}
ABresult
0X0
100
111
131 #print "name $name is clearly a member of group $nameMap\n";
132 #print "nameMap is currently $nameMap\n";
133 80 ▶ $r_returnMatchList->{$nameMap} = ($r_matchResults->{$name}->{$oldNameMap}->{tag}) ? $r_matchResults->{$name}->{$oldNameMap}->{tag} : 0; #If it has a tag, store it, if not, give it a default of 0
Branch: 1 branch
BranchTrueFalseTotal
$r_matchResults->{$Text::XmlMatch::name}{$oldNameMap}{'tag'} ? : 80 0 80
134 #if ($r_matchResults->{$name}->{$oldNameMap}->{tag}) {
135 #print "and this group is a $r_matchResults->{$name}->{$oldNameMap}->{tag}\n";
136 #}
137 }
138 }
139 #print Dumper($r_matchResults);
140 53 return $r_returnMatchList;
141  
142 2437 ▶ sub _criteriaSort {
Subroutine _criteriaSort: called CC=6 SCAR=17.9
143 #differentiate between tag, inclusion, and exclusion, as this sub will walk through all three possible
144 #values
145 2437 my ($r_matchResults,$criteria,$matchPattern,$group) = @_;
146 2437 my $newGroupName; #used in case the name gets updated as a result of a match
147 #because this module supports "back-references," it will allow the user to refer to regex matches
148 #made when defining group names.
149 #print "sub got criteria: $criteria\n";
150  
151 2437 ▶ if ($criteria eq 'tag') {
Branch: 3 branches
BranchTrueFalseTotal
if ($criteria eq 'tag') { } 1040 1397 2437
elsif ($criteria eq 'inclusion' and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1) { } 89 1308 1397
elsif ($criteria eq 'exclusion' and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1) { } 34 2365 2399
152 1040 my $deviceTag = $matchPattern; #in this case, $matchPattern isn't a pattern, but a group name
153 #print "device tag: $name $deviceTag\n";
154 1040 $$r_matchResults{$name}->{$group}->{tag} = $deviceTag;
155 #if the 'name' matches a regex defined in the inclusion list, then $name is a candidate for
156 #inclusion in this group. We won't know till the end of the XML as it could be excluded
157 ▶ } elsif (($criteria eq 'inclusion') && (_match($name,$matchPattern,\$group,\$newGroupName) == 1)) {
Condition100%3/3
$criteria eq "inclusion" and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
!ll&&!rl&&r
217109189
MC/DC100%2/2
$criteria eq "inclusion" and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
$criteria eq "inclusion" _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
Truth table100%3/3
A and B
A= $criteria eq "inclusion"B= _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
ABresult
0X0
100
111
158 #print "device inclusion: $name $matchPattern newGroupName: $newGroupName\n";
159 89 $$r_matchResults{$name}->{$group}->{inclusion} += 1;
160 89 $$r_matchResults{$name}->{$group}->{newName} = $newGroupName;
161 ▶ } elsif (($criteria eq 'exclusion') && (_match($name,$matchPattern,\$group,\$newGroupName) == 1)) {
Condition100%3/3
$criteria eq "exclusion" and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
!ll&&!rl&&r
109118334
MC/DC100%2/2
$criteria eq "exclusion" and _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
$criteria eq "exclusion" _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
Truth table100%3/3
A and B
A= $criteria eq "exclusion"B= _match($Text::XmlMatch::name, $matchPattern, \$group, \$newGroupName) == 1
ABresult
0X0
100
111
162 #print "device exclusion: $name $matchPattern newGroupName: $newGroupName\n";
163 34 $$r_matchResults{$name}->{$group}->{exclusion} += 1;
164 34 $$r_matchResults{$name}->{$group}->{newName} = $newGroupName;
165 }
166 #print "value of new group is: $newGroupName\n";
167 #foreach (keys %{$$r_matchResults{$name}->{$group}}) {
168 # $$r_matchResults{$name}->{$newGroupName}->{$_} = $$r_matchResults{$name}->{$group}->{$_};
169 #}
170 }
171 1397 ▶ sub _match {
Subroutine _match: called CC=2 SCAR=6.9
172 1397 my $name = shift;
173 1397 my $matchPattern = shift;
174 1397 my $r_group = shift; #like MARKET-MSO, MSO-$1, etc.
175 1397 my $r_newGroupName = shift; #in case the name gets updated
176 #print "comparing $name to $matchPattern\n";
177 1397 ▶ if ($name =~ qr /$matchPattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ qr/$matchPattern/) { } 123 1274 1397
178 #print "MATCH $name <-> $matchPattern - match variable $1,$2,$3,$4,$5\n";
179 123 my ($mv1,$mv2,$mv3,$mv4,$mv5) = ($1,$2,$3,$4,$5); #Store up to 5 memory variables from regex
180 123 my $newGroupName = "$$r_group";
181 123 $newGroupName =~ s/\$1/$mv1/;
182 123 $newGroupName =~ s/\$2/$mv2/;
183 123 $newGroupName =~ s/\$3/$mv3/;
184 123 $newGroupName =~ s/\$4/$mv4/;
185 #print "derived group name $newGroupName\n";
186 123 $$r_newGroupName = $newGroupName;
187 123 return 1
188 } else {
189 1274 return 0;
190 }
191 }
192 }
193  
194 4 ▶sub listGroups {
Subroutine listGroups: called CC=8 SCAR=20.8
Pod undocumented
195 4 my $config = shift; #this holds the XML object
196 #our $name = shift; #this holds the item to be matched against, typically a FQDN
197 #setup initialization variables before we start searching through the XML turned into a hash
198 4 my $r_groupList = []; #reference to anonymous array
199  
200 #Now crawl through the hash and iterate over each group name
201 #print "DEBUG: Original " . Dumper ($config) . "\n";
202 4 foreach my $group (keys %{$config->{pattern}}) { #walk through each pattern name
203 47 ▶ next if ($group =~ /^(inclusion|tag)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $group =~ /^(inclusion|tag)$/ 2 45 47
204 ############
205 # Hotfix 6 #
206 #################################################################
207 #The block below handles a special case where the user provides a
208 #configuration that contains a single pattern. In such cases,
209 #XML::Simple creates a hash structure that is completely different
210 #than the one that is created when two or more patterns are
211 #specified. This block creates a new hash structure that matches
212 #the expected format. Note that although we are currently in a
213 #foreach loop, the loop gets killed for this special case as there
214 #are no keys to step through.
215 #################################################################
216  
217 #print "DEBUG: Processing pattern $group\n";
218 45 ▶ if (defined $config->{pattern}->{name}) { #if this key is defined then hash is collapsed
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"pattern"}{"name"}) 2 43 45
219 #print "DEBUG: Hash not found, must be single pattern!\n";
220 #print "Dumper Single: " . Dumper($config->{pattern}->{$group}) . "\n";
221 #rebuild hash structure so that it looks like what's expected
222 2 my $singlePatternName = $config->{pattern}->{name};
223 2 my $newHashStructure = {};
224 2 foreach my $single (keys %{$config->{pattern}}) {
225 5 ▶ next if ($single eq 'name'); # we're rebuilding hash to standard form, don't need this key
Branch: 1 branch
BranchTrueFalseTotal
if $single eq "name" 2 3 5
226 3 $newHashStructure->{pattern}->{$singlePatternName}->{$single} = $config->{pattern}->{$single};
227 }
228 #print "DEBUG: new hash structure: " . Dumper($newHashStructure) . "\n";
229 2 $config->{pattern} = $newHashStructure->{pattern}; #copy newly structured hash over the original
230 #print "DEBUG: new config: " . Dumper($config) . "\n";
231 2 $group = $singlePatternName;
232 }
233 #End of Hotfix 6 block#########################################
234  
235 45 push @$r_groupList, $group; #store each group name in the array
236  
237 ##########
238 #Hotfix 6#
239 #################################################################
240 #this line stops the looping through keys, as there are no keys to
241 #step through in the case of a single pattern XML config file.
242  
243 45 ▶ last if (defined $config->{pattern}->{name}); #If case of single pattern, don't try to step through more
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{"pattern"}{"name"} 0 45 45
244  
245 }
246 4 ▶ return wantarray ? @$r_groupList : $r_groupList;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
247 #return $r_groupList; #pass the reference to the anonymous array back to the caller
248 }
249  
250 11 1;
251  
252 __END__
↑ Summary