blib/lib/GRNOC/Config.pm

filter: Statementstmt 1.6% 6 / 356 Branchbran 0.0% 0 / 168 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 11.1% 2 / 18 Podpod n/a totaltotal 1.3% 8 / 577 CC 95 SCAR 46.9
CC
95
Cov
1%
getOLD
306.0
get
210.0
validate
90.0
CRAP 108.7
↑ Summary
1 #--------------------------------------------------------------------
2 #----- Copyright(C) 2015 The Trustees of Indiana University
3 #--------------------------------------------------------------------
4 #----- a simple config module for use in all GRNOC programs with passwords
5 #-----
6 #---------------------------------------------------------------------
7  
8 package GRNOC::Config;
9  
10 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 require Data::Dumper;
14 require XML::Simple;
15 require XML::XPath;
16 require XML::LibXML;
17  
18 =head1 NAME
19  
20 GRNOC::Config - The GRNOC Default Config parser
21  
22 =head1 VERSION
23  
24 Version 1.0.9
25  
26 =cut
27  
28 our $VERSION = '1.0.9';
29  
30  
31 =head1 SYNOPSIS
32  
33 A module to allow everyone to access config files in a fairly standard way.
34 Uses XML::XPath and XML::Simple to parse our XML files, and stores all configs it has access to in this module
35  
36 Setting debug to true will give you extra debugging output (default is off)
37 Setting force_array to true will return everything in an array even if there is only 1 object returned (default is on)
38  
39 When asking for attributes denoted by '@' it will return only the attribute(s) without the hash
40  
41 use GRNOC::Config;
42  
43 my $foo = GRNOC::Config->new(config_file => '/etc/grnoc/db.xml', debug => 0, force_array => 0 schema => '/path/to/schema.csd')
44 my $db_username = $foo->get("/config/db/credentials");
45 print $db_username->{'user'};
46 print $db_username->{'password'};
47  
48 #just 1 attribute
49 my $user = $foo->get("/config/db/credentials[1]/@user");
50 my $password = $foo->get("/config/db/credentials[1]/@password");
51  
52 #if I have more than 1 result I get an array
53 my $hosts = $foo->get("/config/hosts") or die Dumper($foo->get_error());
54 foreach my $host ($hosts){
55 print $host->{'hostname'};
56 }
57  
58 ##turn debugging on
59 $foo->{'debug'} = 1;
60
61 ##get errors
62 print Dumper($foo->get_errors());
63  
64 # I always want an array... even if I only get 1 result
65 $foo->{'force_array'} = 1;
66 $db_username = $foo->get("/config/db/credentials") or die Dumper($foo->get_error());
67 print @{db_username}[0]->{'user'}
68 print @{db_username}[0]->{'password'}
69  
70 $user = $foo->get("/config/db/credentials[1]/@user") or die Dumper($foo->get_error());
71 $password = $foo->get("/config/db/credentials[1]/@password") or die Dumper($foo->get_error());
72 $user = @{$user}[0];
73 $password = @{$password}[0];
74
75 my $valid = $foo->validate();
76  
77 if(!$valid){
78 print STDERR Dumper($foo->get_error());
79 }
80  
81 my $valid2 = $foo->validate("/path/to/new/schema.xsd");
82  
83 ...
84  
85 =cut
86  
87 =head2 getOLD
88  
89 returns the requested data from the config file must pass in the path of the node/attribute you want
90 from the XML. Attributes are denoted by '@'
91  
92 to get an attribute the call would look like
93  
94 $foo->get("/path/to/@object");
95  
96 =cut
97  
98  
99 0 ▶sub getOLD{
Subroutine getOLD: not called CC=17 SCAR=57.2
100 0 my $self = shift;
101 0 my $path = shift;
102 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
103 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
104 0 return undef;
105 }
106 0 ▶ if(!defined($self->{'config'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}) 0 0 0
107 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
108 0 return undef;
109 }
110  
111 0 my $xp = $self->{'config'};
112
113 0 my $exists;
114 0 ▶ eval { $exists = $xp->exists($path); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
115
116 0 ▶ if($exists){
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) { } 0 0 0
117 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
118 0 print STDERR $path . " exists!";
119 }
120 0 $self->{'error'} = {};
121  
122 0 my $nodeset;
123 0 ▶ eval { $nodeset = $xp->find($path); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
124 0 my @results;
125 my @nodelist;
126 0 ▶ eval { @nodelist= $nodeset->get_nodelist; }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
127 0 ▶ if($#nodelist <= 1 && !($self->{'force_array'})){
Branch: 1 branch
BranchTrueFalseTotal
if ($#nodelist <= 1 and not $self->{'force_array'}) { } 0 0 0
Condition0%0/3
$#nodelist <= 1 and not $self->{"force_array"}
!ll&&!rl&&r
000
MC/DC0%0/2
$#nodelist <= 1 and not $self->{"force_array"}
$#nodelist <= 1 not $self->{"force_array"}
Truth table0%0/3
A and not(B)
A= $#nodelist <= 1B= not $self->{"force_array"}
ABresult
0X0
100
111
128 0 my $temp;
129 0 ▶ eval { $temp = XML::XPath::XMLParser::as_string($nodelist[0]); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
130 0 ▶ if($temp =~ /^<.*\/>$/){
Branch: 2 branches
BranchTrueFalseTotal
if ($temp =~ m[^<.*/>$]) { } 0 0 0
elsif ($temp =~ /.*=\"(.*)\"/) { } 0 0 0
131 0 return XML::Simple::XMLin($temp,ForceArray => $self->{'force_array'});
132 }elsif($temp =~ /.*=\"(.*)\"/){
133 0 return $1;
134 }else{
135 0 return XML::Simple::XMLin($temp, ForceArray => $self->{'force_array'});
136 }
137 }else{
138 0 foreach my $node (@nodelist){
139 0 my $temp;
140 0 ▶ eval { $temp = XML::XPath::XMLParser::as_string($node); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
141
142 0 ▶ if($temp =~ /^<.*\/>$/){
Branch: 2 branches
BranchTrueFalseTotal
if ($temp =~ m[^<.*/>$]) { } 0 0 0
elsif ($temp =~ /.*=\"(.*)\"/) { } 0 0 0
143 0 push(@results,XML::Simple::XMLin($temp, ForceArray => $self->{'force_array'}));
144 }elsif($temp =~ /.*=\"(.*)\"/){
145 0 push(@results,$1);
146 }else{
147 0 push(@results,XML::Simple::XMLin($temp, ForceArray => $self->{'force_array'}));
148 }
149 }
150 0 return \@results;
151 }
152 }else{
153 0 $self->{'error'}{'msg'} = $path . " does not exist in the config";
154 0 return undef;
155 }
156 }
157  
158 =head2 get2
159  
160 returns the requested data from the config file must pass in the path of the node/attribute you want
161 from the XML. Attributes are denoted by '@'
162  
163 to get an attribute the call would look like
164  
165 $foo->get2("/path/to/@object");
166  
167 =cut
168  
169  
170 0 ▶sub get2{
Subroutine get2: not called CC=5 SCAR=34.0
171 0 my $self = shift;
172 0 my $path = shift;
173 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
174 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
175 0 return undef;
176 }
177 #return "hello";
178 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
179 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
180 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
181 0 print STDERR "cannot find doc!\n";
182 }
183 0 return undef;
184 }
185  
186 ###ok ready to parse
187 0 my $root=$self->{'doc'}->getDocumentElement();
188 0 my $rvalue=$root->findvalue($path);
189 0 ▶ if(defined $rvalue){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rvalue) 0 0 0
190 0 $self->{'error'}{'msg'} = $path . " does not exist in the config";
191 0 return $rvalue;
192 }
193 0 return undef ;
194 }
195  
196  
197 =head2 get
198  
199 returns the requested data from the config file must pass in the path of the node/attribute you want
200 from the XML. Attributes are denoted by '@'
201  
202 to get an attribute the call would look like
203  
204 $foo->get3("/path/to/@object");
205  
206 =cut
207  
208  
209 0 ▶sub get{
Subroutine get: not called CC=14 SCAR=53.5
210 0 my $self = shift;
211 0 my $path = shift;
212 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
213 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
214 0 return undef;
215 }
216  
217 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
218 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
219 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
220 0 print STDERR "cannot find doc!\n";
221 }
222 0 return undef;
223 }
224  
225 ###ok ready to parse
226 0 my $root=$self->{'doc'}->getDocumentElement();
227
228 0 my @path_args = split('\/',$path);
229 #looking for attributes?
230 0 ▶ if($path_args[$#path_args] =~ /@/){
Branch: 1 branch
BranchTrueFalseTotal
if ($path_args[$#path_args] =~ /@/) { } 0 0 0
231 0 my @nodes = $root->findnodes($path);
232  
233 0 my @tmp;
234 0 ▶ if($#nodes >= 1 || $self->{'force_array'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($#nodes >= 1 or $self->{'force_array'}) { } 0 0 0
Condition0%0/3
$#nodes >= 1 or $self->{"force_array"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$#nodes >= 1 or $self->{"force_array"}
$#nodes >= 1 $self->{"force_array"}
Truth table0%0/3
A or B
A= $#nodes >= 1B= $self->{"force_array"}
ABresult
1X1
011
000
235 #print STDERR "returning an array of values\n";
236 0 foreach my $node (@nodes){
237 0 push(@tmp,$node->getValue());
238 }
239 0 return \@tmp;
240 }else{
241 #print STDERR "returning a single value\n";
242 0 ▶ if(!defined($nodes[0])){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes[0]) 0 0 0
243 0 return undef;
244 }
245 0 return $nodes[0]->getValue();
246 }
247  
248 0 $self->{'error'}{'msg'} = "$path does not exist in the config";
249 0 return undef;
250 }else{
251 #looking for nodes
252 0 my @nodes = $root->findnodes( $path );
253 0 my @tmp;
254
255 0 ▶ if($#nodes >= 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($#nodes >= 0) 0 0 0
256 0 ▶ if($#nodes >= 1 || $self->{'force_array'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($#nodes >= 1 or $self->{'force_array'}) { } 0 0 0
Condition0%0/3
$#nodes >= 1 or $self->{"force_array"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$#nodes >= 1 or $self->{"force_array"}
$#nodes >= 1 $self->{"force_array"}
Truth table0%0/3
A or B
A= $#nodes >= 1B= $self->{"force_array"}
ABresult
1X1
011
000
257 #print STDERR "Returning an array of nodes\n";
258 0 foreach my $node (@nodes){
259 0 push(@tmp,XML::Simple::XMLin($node->toString(), ForceArray => $self->{'force_array'}));
260 }
261 #print STDERR Data::Dumper::Dumper(@tmp);
262 0 ▶ if(defined($nodes[0])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nodes[0]) 0 0 0
263 0 return \@tmp;
264 }
265  
266 }else{
267 #print STDERR "Returning a single node\n";
268 0 return XML::Simple::XMLin($nodes[0]->toString(),ForceArray => $self->{'force_array'});
269
270 }
271 }
272 0 $self->{'error'}{'msg'} = "$path does not exist in the config";
273 0 return undef;
274 }
275  
276 0 return undef ;
277 }
278  
279  
280 =head2 update_node
281  
282 =cut
283  
284 0 ▶sub update_node{
Subroutine update_node: not called CC=6 SCAR=37.4
285 0 my $self = shift;
286 0 my $path = shift;
287 0 my $new_value = shift;
288  
289  
290 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
291 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
292 0 return undef;
293 }
294  
295 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
296 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
297 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
298 0 print STDERR "cannot find doc!\n";
299 }
300 0 return undef;
301 }
302  
303 0 $path .= "/text()";
304  
305 0 my $root = $self->{'doc'}->getDocumentElement();
306 0 my $nodes = $root->find($path);
307 0 my $node;
308  
309 0 ▶ if($nodes->size() == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->size == 1) { } 0 0 0
310 0 $node = $nodes->pop();
311 }else{
312 0 $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
313 0 return undef;
314 }
315  
316 0 ▶ if(defined($new_value)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 0 0 0
317 0 $node->setData($new_value);
318 }
319  
320 0 my $res = _save_xml($self);
321 0 return $res;
322 }
323  
324 =head2 add_node
325  
326  
327 =cut
328  
329 0 ▶sub add_node{
Subroutine add_node: not called CC=6 SCAR=37.4
330 0 my $self = shift;
331 0 my $path = shift;
332 0 my $node_name = shift;
333 0 my $node_value = shift;
334  
335 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
336 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
337 0 return undef;
338 }
339  
340 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
341 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
342 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
343 0 print STDERR "cannot find doc!\n";
344 }
345 0 return undef;
346 }
347  
348 0 my $root = $self->{'doc'}->getDocumentElement();
349 0 my $nodes = $root->find($path);
350 0 my $node;
351  
352 0 ▶ if($nodes->size() == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->size == 1) { } 0 0 0
353 0 $node = $nodes->pop();
354 }else{
355 0 $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
356 0 return undef;
357 }
358  
359 0 my $new_node = $self->{'doc'}->createElement($node_name);
360 0 ▶ if(defined($node_value)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node_value) 0 0 0
361 0 $new_node->appendText($node_value);
362 }
363  
364 0 $node->appendChild($new_node);
365  
366 0 my $res = _save_xml($self);
367 0 return $res;
368 }
369  
370 =head2 delete_nodes
371  
372 =cut
373  
374 0 ▶sub delete_nodes{
Subroutine delete_nodes: not called CC=5 SCAR=34.0
375 0 my $self = shift;
376 0 my $path = shift;
377  
378 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
379 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
380 0 return undef;
381 }
382  
383 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
384 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
385 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
386 0 print STDERR "cannot find doc!\n";
387 }
388 0 return undef;
389 }
390  
391 0 my $root = $self->{'doc'}->getDocumentElement();
392 0 my $nodes = $root->find($path);
393 0 my $node;
394
395 0 ▶ while(my $node = $nodes->pop()){
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $nodes->pop) 0 0 0
396 0 my $parent = $node->parentNode;
397 0 $parent->removeChild($node);
398 }
399  
400 0 return _save_xml($self);
401 }
402  
403  
404  
405  
406 =head2 delete_node
407  
408 =cut
409  
410 0 ▶sub delete_node{
Subroutine delete_node: not called CC=5 SCAR=34.0
411 0 my $self = shift;
412 0 my $path = shift;
413  
414 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
415 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
416 0 return undef;
417 }
418  
419 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
420 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
421 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
422 0 print STDERR "cannot find doc!\n";
423 }
424 0 return undef;
425 }
426  
427 0 my $root = $self->{'doc'}->getDocumentElement();
428 0 my $nodes = $root->find($path);
429 0 my $node;
430  
431 0 ▶ if($nodes->size() == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->size == 1) { } 0 0 0
432 0 $node = $nodes->pop();
433 }else{
434 0 print STDERR "Multiple Nodes at that path\n";
435 0 $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
436 0 return undef;
437 }
438  
439 0 my $parent = $node->parentNode;
440 0 $parent->removeChild($node);
441  
442 0 return _save_xml($self);
443  
444 }
445  
446 =head2 update_attribute
447  
448 =cut
449  
450 0 ▶sub update_attribute{
Subroutine update_attribute: not called CC=7 SCAR=40.3
451 0 my $self = shift;
452 0 my $path = shift;
453 0 my $attribute_name = shift;
454 0 my $attribute_value = shift;
455  
456 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
457 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
458 0 return undef;
459 }
460  
461 0 ▶ if($attribute_name eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute_name eq "") 0 0 0
462 0 $self->{'error'}{'msg'} = "Attribute not specified. Please specify an attribute\n";
463 0 return undef;
464 }
465  
466 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
467 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
468 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
469 0 print STDERR "cannot find doc!\n";
470 }
471 0 return undef;
472 }
473  
474 0 my $root = $self->{'doc'}->getDocumentElement();
475 0 my $nodes = $root->find($path);
476 0 my $node;
477  
478 0 ▶ if($nodes->size() == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->size == 1) { } 0 0 0
479 0 $node = $nodes->pop();
480 }else{
481 0 $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
482 0 return undef;
483 }
484  
485  
486 0 ▶ if(defined($node->getAttribute($attribute_name))){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->getAttribute($attribute_name)) { } 0 0 0
487 0 $node->setAttribute($attribute_name,$attribute_value);
488 }else{
489 0 $self->{'error'}{'msg'} = "That attribute is not defined, add the attribute before trying to update\n";
490 0 return undef;
491 }
492  
493 0 _save_xml($self);
494 }
495  
496 =head2 add_attribute
497  
498 =cut
499  
500 0 ▶sub add_attribute{
Subroutine add_attribute: not called CC=7 SCAR=40.3
501 0 my $self = shift;
502 0 my $path = shift;
503 0 my $attribute_name = shift;
504 0 my $attribute_value = shift;
505
506 0 ▶ if($path eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "") 0 0 0
507 0 $self->{'error'}{'msg'} = "Path not specified. Please specify a path\n";
508 0 return undef;
509 }
510  
511 0 ▶ if($attribute_name eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute_name eq "") 0 0 0
512 0 $self->{'error'}{'msg'} = "Attribute not specified. Please specify an attribute\n";
513 0 return undef;
514 }
515  
516 0 ▶ if(!defined($self->{'doc'})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
517 0 $self->{'error'}{'msg'} = "Config not loaded!!\n";
518 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
519 0 print STDERR "cannot find doc!\n";
520 }
521 0 return undef;
522 }
523  
524 0 my $root = $self->{'doc'}->getDocumentElement();
525 0 my $nodes = $root->find($path);
526 0 my $node;
527  
528 0 ▶ if($nodes->size() == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->size == 1) { } 0 0 0
529 0 $node = $nodes->pop();
530 }else{
531 0 $self->{'error'}{'msg'} = "Multiple Nodes at that Path please specify\n";
532 0 return undef;
533 }
534  
535 0 ▶ if(defined($node->getAttribute($attribute_name))){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node->getAttribute($attribute_name)) { } 0 0 0
536 0 $self->{'error'}{'msg'} = "That attribute is already defined, try updating instead of adding again\n";
537 0 return undef;
538 }else{
539 0 $node->setAttribute($attribute_name,$attribute_value);
540 }
541  
542 0 _save_xml($self);
543  
544 }
545  
546  
547 0 ▶sub _save_xml{
Subroutine _save_xml: not called CC=2 SCAR=17.9
548 0 my $self = shift;
549
550 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
551 0 print STDERR "Writing file " . $self->{'config_file'} . "\n";
552 }
553  
554 0 open my $CONFIG, "> " . $self->{'config_file'};
555 0 binmode $CONFIG;
556 0 print $CONFIG $self->{'doc'}->toString();
557 0 close $CONFIG;
558 0 return 1;
559 }
560  
561 =head2 validate
562  
563 my $valid = $config->validate();
564  
565 or
566
567 my $valid = $config->validate("/path/to/schema.xsd");
568  
569 returns 1 if xml validates
570 returns 0 if it fails to valiate
571 returns -1 if there is a problem with your schema
572  
573 =cut
574  
575 0 ▶sub validate{
Subroutine validate: not called CC=9 SCAR=45.0
576 0 my $self = shift;
577 0 my $new_schema = shift;
578  
579 #did the user pass in a new schema file?
580 0 ▶ if(defined($new_schema)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_schema) 0 0 0
581 0 $self->{'schema_file'} = $new_schema;
582 0 _load_schema($self);
583 }
584  
585 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
586 0 print STDERR "Validating document with schema\n";
587 }
588  
589 0 ▶ if(defined($new_schema)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_schema) 0 0 0
590
591 }
592  
593 0 ▶ if(defined($self->{'schema'}) && $self->{'schema'} ne ''){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'schema'} and $self->{'schema'} ne '') { } 0 0 0
Condition0%0/3
defined $self->{"schema"} and $self->{"schema"} ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"schema"} and $self->{"schema"} ne ""
defined $self->{"schema"} $self->{"schema"} ne ""
Truth table0%0/3
A and B
A= defined $self->{"schema"}B= $self->{"schema"} ne ""
ABresult
0X0
100
111
594 0 my $valid = -1;
595
596 0 eval{ $valid = $self->{'schema'}->validate($self->{'doc'}) };
597
598 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
599 0 $self->{'error'}{'backtrace'} = $@;
600 0 $self->{'error'}{'msg'} = "File failed to validate\n";
601 }
602
603 0 ▶ if($valid == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($valid == 0) { } 0 0 0
604 0 return 1;
605 }else{
606 0 return 0;
607 }
608 }else{
609 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
610 0 warn "No schema to validate against\n";
611 }
612 0 return -1;
613 }
614  
615 }
616  
617 0 ▶sub _load_schema{
Subroutine _load_schema: not called CC=5 SCAR=34.0
618 0 my $self = shift;
619
620 0 my $schema;
621
622  
623 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
624 0 warn "Loading schema file " . $self->{'schema_file'} . "\n";
625 }
626  
627 0 eval{ $schema = XML::LibXML::Schema->new( location => $self->{'schema_file'}) };
628  
629 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
630 0 ▶ if($self->{'debug'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
631 0 warn "Unable to load Schema!\n";
632 0 warn Data::Dumper::Dumper($@);
633 0 warn Data::Dumper::Dumper($schema);
634 }
635  
636 0 $self->{'error'}{'msg'} = "Unable to load Schema\n";
637 0 $self->{'error'}{'backtrace'} = $@;
638 }
639  
640 0 ▶ if(defined($schema)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $schema) { } 0 0 0
641 0 warn "Successfully Loaded Schema\n";
642 0 $self->{'schema'} = $schema;
643 0 return 1;
644 }else{
645 0 $self->{'schema'} = undef;
646 0 return 0;
647 }
648 }
649  
650 =head2 get_error
651  
652 returns any error that is generated from this module
653  
654 =cut
655  
656  
657 0 ▶sub get_error{
Subroutine get_error: not called CC=1 SCAR=6.9
658 0 my $self = shift;
659 0 return $self->{'error'};
660 }
661  
662 0 ▶sub _read_config{
Subroutine _read_config: not called CC=6 SCAR=37.4
663 0 my $self = shift;
664 0 my $ref;
665 0 my $config = $self->{'config_file'};
666 ##first check for config file exist
667 0 ▶ if($config ne '' && -e $config){
Branch: 1 branch
BranchTrueFalseTotal
if ($config ne '' and -e $config) { } 0 0 0
Condition0%0/3
$config ne "" and -e $config
!ll&&!rl&&r
000
MC/DC0%0/2
$config ne "" and -e $config
$config ne "" -e $config
Truth table0%0/3
A and B
A= $config ne ""B= -e $config
ABresult
0X0
100
111
668 0 ▶ eval { $ref = XML::XPath->new(filename => $config); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
669 0 ▶ if(!defined($ref)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 0 0
670 0 $self->{'error'}{'msg'} = "XPath was unable to parse config file";
671 0 $self->{'error'}{'backtrace'} = $self;
672 0 return undef;
673 }
674 }else{
675  
676 0 ▶ if($config eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($config eq '') { } 0 0 0
677 0 $self->{'error'}{'msg'} = "No File to parse!! ";
678 0 $self->{'error'}{'backtrace'} = $self;
679 }else{
680 0 $self->{'error'}{'msg'} = "File does not exist!! ";
681 0 $self->{'error'}{'backtrace'} = $self;
682 }
683  
684 0 return undef;
685 }
686 0 return $ref;
687 }
688  
689 0 ▶sub _read_config2{
Subroutine _read_config2: not called CC=9 SCAR=45.0
690 0 my $self = shift;
691 0 my $ref;
692 0 my $config_filename = $self->{'config_file'};
693 0 my $parser= XML::LibXML->new() ;
694 0 my $doc;
695 0 ▶ if(defined($self->{'xml_string'})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"xml_string"}) 0 0 0
696 0 ▶ eval{$doc = $parser->parse_string( $self->{'xml_string'});}; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
697 0 ▶ if(!defined($doc)){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $doc) { } 0 0 0
698 0 $self->{'error'}{'msg'} = "LibXML was unable to prase string";
699 0 $self->{'error'}{'backtrace'} = $self;
700 }else{
701 0 return $doc;
702 }
703  
704 }
705  
706 0 ▶ if (not (defined $config_filename && (-e $config_filename) )){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_filename and -e $config_filename) 0 0 0
Condition0%0/3
defined $config_filename and -e $config_filename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config_filename and -e $config_filename
defined $config_filename -e $config_filename
Truth table0%0/3
A and B
A= defined $config_filenameB= -e $config_filename
ABresult
0X0
100
111
707 0 ▶ if($config_filename eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($config_filename eq '') { } 0 0 0
708 0 $self->{'error'}{'msg'} = "No File to parse!! ";
709 0 $self->{'error'}{'backtrace'} = $self;
710 }else{
711 0 $self->{'error'}{'msg'} = "File does not exist!! ";
712 0 $self->{'error'}{'backtrace'} = $self;
713 }
714 0 return undef;
715 }
716
717 0 ▶ eval {$doc=$parser->parse_file($config_filename); }; $self->{'error'}{'error'} = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
718 0 ▶ if(!defined($doc)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $doc) 0 0 0
719 0 $self->{'error'}{'msg'} = "LibXML was unable to parse config file";
720 0 $self->{'error'}{'backtrace'} = $self;
721 0 return undef;
722 }
723
724 #this is the successfull exit;
725 0 return $doc;
726
727 }
728  
729  
730  
731 =head2 new
732 Creates a new GRNOC::Config object
733  
734 my $config = GRNOC::Config->new(config_file => $file, force_array => 0, debug => 0);
735  
736 or
737  
738 my $config = GRNOC::Config->new( xml_string => $string);
739  
740 =cut
741  
742  
743 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
744
745 0 my $that = shift;
746 0 ▶ my $class = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
747 0 my %args = (@_);
748 0 my $self = \%args;
749 0 ▶ if(!(defined($self->{'debug'}))){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"debug"}) 0 0 0
750 0 $self->{'debug'} = 0;
751 }
752
753 0 ▶ if(!(defined($self->{'force_array'}))){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"force_array"}) 0 0 0
754 0 $self->{'force_array'} = 1;
755 }
756  
757 0 $self->{'doc'} = _read_config2($self);
758 0 ▶ if(!defined ($self->{'doc'}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"doc"}) 0 0 0
759 0 $self->{'error'}{'msg'} .= "Unable to initialize config";
760 }
761
762
763 0 ▶ if(defined($self->{'schema'})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"schema"}) 0 0 0
764 0 $self->{'schema_file'} = $self->{'schema'};
765 0 _load_schema($self);
766 }
767  
768 0 bless ($self,$class);
769 0 return $self;
770 }
771  
772 1;
↑ Summary