blib/lib/Getopt/XML.pm

filter: Statementstmt 77.2% 78 / 101 Branchbran 55.8% 19 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 70.4% 119 / 169 CC 25 SCAR 25.8
CC
25
Cov
69%
XMLToGetoptArgsArray
24.1
GetXMLOptionsFromFile
20.0
GetXMLOptions
7.7
CRAP 13.2
↑ Summary
1 =pod
2  
3 =head1 NAME
4  
5 Getopt::XML - Provide the user input arguments to Getopt::Long as an XML document
6  
7 =head1 SYNOPSIS
8  
9 Read in a list of XML elements and process them as the input arguments to the
10 Getopt::Long module
11  
12 use XML::TreePP;
13 use Getopt::Long;
14 use Getopt::XML qw(GetXMLOptions);
15 use Data::Dump qw(dump);
16 #
17 # Set the XML Data
18 my $xmldata=<<"EOF_XMLDATA";
19 <apple>
20 <color>red</color>
21 <type>red delicious</type>
22 <isAvailable/>
23 <cityGrown>Macomb</cityGrown>
24 <cityGrown>Peoria</cityGrown>
25 <cityGrown>Galesburg</cityGrown>
26 </apple>
27 EOF_XMLDATA
28 #
29 # Parse the XML data using XML::TreePP module
30 my $tpp = XML::TreePP->new();
31 my $tree = $tpp->parse( $xmldata );
32 #
33 # Read the XML data in as arguments to Getopt::Long
34 my %options;
35 GetXMLOptions (
36 xmldoc => $tree,
37 xmlpath => '/apple',
38 Getopt_Long =>
39 [
40 \%options,
41 'isAvailable',
42 'color=s',
43 'type=s',
44 'cityGrown=s@'
45 ]
46 );
47 dump(\%options);
48  
49 This is the output:
50  
51 {
52 cityGrown => ["Macomb", "Peoria", "Galesburg"],
53 color => "red",
54 isAvailable => 1,
55 type => "red delicious",
56 }
57  
58 Alternately, the XML data can be in a file.
59 The above code would be rewritten as this:
60  
61 use Getopt::Long;
62 use Getopt::XML qw(GetXMLOptions);
63 use Data::Dump qw(dump);
64 #
65 my %options;
66 GetXMLOptions (
67 xmlfile => '/path/to/xml_file.xml',
68 xmlpath => '/apple',
69 Getopt_Long =>
70 [
71 \%options,
72 'isAvailable',
73 'color=s',
74 'type=s',
75 'cityGrown=s@'
76 ]
77 );
78 dump(\%options);
79  
80 =head1 DESCRIPTION
81  
82 This is simply another way to pass in user defined arguments to an application
83 using Getop::Long. The module provides a way to pass in user arguments from an
84 XML file into Getopt::Long.
85  
86 In this way, the user can provide input to an application via an XML file. And
87 this can be useful if the application is ran at a regular interval. Or it may
88 be useful if the input to the application can change between multiple
89 executions, but the provided content is consistant over time based on the
90 context of the execution.
91  
92 This input method may be desired for an application which has default values
93 for options the author wishes to exist, but the author wants those default
94 values to be changed by the user without having to edit the application code.
95  
96 Or perhaps the application will reside in a multi-user environment, and this
97 module would be used to store the input options as part of the user preferences.
98  
99 And finally, perhaps the options to be passed into the application resides
100 somewhere else in XML or another storage format that can be transformed into
101 XML as input to an application.
102  
103 =head1 REQUIREMENTS
104  
105 The following perl modules are depended on by this module:
106 ( I<Note: Dependency on Params::Validate was removed in version 0.52> )
107  
108 =over 4
109  
110 =item * XML::TreePP
111  
112 =item * XML::TreePP::XMLPath
113  
114 =item * Getopt::Long # Getopt::Long 2.37 or greater is required
115  
116 =back
117  
118 Both XML::TreePP and XML::TreePP::XMLPath required modules are Pure PERL
119 implementations.
120  
121 Getopt::Long version 2.37 introduces the GetOptionsFromArray() method. Versions
122 of Getopt::Long previous to 2.37 do not contain the GetOptionsFromArray() method.
123  
124 The process of transforming XML data into options that can be passed into
125 Getopt::Long is performed with the GetOptionsFromArray() method.
126  
127 =head1 IMPORTABLE METHODS
128  
129 When the calling application invokes this module in a use clause, the following
130 methods can be imported into its space.
131  
132 =over 4
133  
134 =item * C<GetXMLOptions>
135  
136 =item * C<GetXMLOptionsFromFile>
137  
138 =back
139  
140 Example:
141  
142 use Getopt::XML qw( GetXMLOptions GetXMLOptionsFromFile );
143  
144 =head1 METHODS
145  
146 =cut
147  
148 package Getopt::XML;
149  
150 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 ▶use Getopt::Long qw(GetOptionsFromArray); # requires Getopt::Long 2.37 or greater
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 ▶use XML::TreePP;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 1 ▶use XML::TreePP::XMLPath qw(filterXMLDoc);
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159  
160 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
161 1 ▶ use vars qw(@ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 @ISA = qw(Exporter);
163 1 @EXPORT = qw();
164 1 @EXPORT_OK = qw(&GetXMLOptions &GetXMLOptionsFromFile);
165  
166 1 ▶ use vars qw($REF_NAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 $REF_NAME = "Getopt::XML"; # package name
168  
169 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 $VERSION = '0.53';
171 }
172  
173  
174 =pod
175  
176 =head2 new
177  
178 Create a new object instances of this module.
179  
180 =over 4
181  
182 =item * I<returns>
183  
184 An object instance of this module.
185  
186 =back
187  
188 my $glx = new Getopt::XML();
189  
190 =cut
191  
192 # new
193 #
194 # It is not necessary to create an object of this module.
195 # However, if you choose to do so any way, here is how you do it.
196 #
197 # my $obj = new Getopt::XML;
198 #
199 # You will have problems with the methods if you call them in an object
200 # oriented mannor. So you are better off not creating an object instance of
201 # this module, unless you are sure you know what you are doing.
202 #
203 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
204 1 my $pkg = shift;
205 1 ▶ my $class = ref($pkg) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
206 1 my $self = bless {}, $class;
207 1 return $self;
208 }
209  
210  
211 =pod
212  
213 =head2 XMLToGetoptArgsArray
214  
215 This method formats a subtree of an XML document into an array that is
216 acceptable to be passed in to the Getopt::Long::GetOptionsFromArray() method.
217  
218 =over 4
219  
220 =item * B<XMLTree>
221  
222 The hash reference of a parsed XML file, which has been parsed
223 by XML::TreePP::parse()
224  
225 =item * I<returns>
226  
227 A reference to an array which an acceptable array input to the
228 Getopt::Long::GetOptionsFromArray() method.
229  
230 =back
231  
232 my $ha_list = XMLToGetoptArgsArray ( $XMLTree )
233  
234 =cut
235  
236 # XMLToGetoptArgsArray
237 # @param xmltree the XML::TreePP XML Tree
238 # @return [options] an array reference of options suitable for GetOpt::Long::GetoptionsFromArray()
239 sub XMLToGetoptArgsArray ($);
240 2 ▶sub XMLToGetoptArgsArray ($) {
Subroutine XMLToGetoptArgsArray: called CC=16 SCAR=31.8
Pod documented
241 2 ▶ my $self = shift if ref($_[0]) eq $REF_NAME || undef;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq $Getopt::XML::REF_NAME or undef 0 2 2
Condition50%1/2
ref $_[0] eq $Getopt::XML::REF_NAME or undef
l!l
02
MC/DC0%0/1
ref $_[0] eq $Getopt::XML::REF_NAME or undef
ref $_[0] eq $Getopt::XML::REF_NAME
Truth table50%1/2
A
A= ref $_[0] eq $Getopt::XML::REF_NAME
Aresult
11
00
242 0 ▶ if (@_ != 1) { carp 'method XMLToGetoptArgsArray($) requires one argument.'; return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ != 1) 0 2 2
243 2 my $xmltree = shift;
244 2 my @options;
245  
246 2 ▶ if (ref $xmltree eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $xmltree eq 'ARRAY') { } 1 1 2
elsif (ref $xmltree ne 'HASH') { } 0 1 1
247 # If the XMLPath filters to more than one node, we accept all results
248 1 foreach my $xml_e (@{$xmltree}) {
249 1 my $sopt = XMLToGetoptArgsArray($xml_e);
250 1 ▶ next if !defined $sopt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sopt 0 1 1
251 1 push (@options,@{$sopt});
252 }
253 1 return \@options;
254 } elsif (ref $xmltree ne "HASH") {
255 0 return undef;
256 }
257 1 ▶ while (my ($key,$val) = each(%{$xmltree})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$xmltree;}) 4 1 5
258 # We include attribute in the <tag>
259 4 ▶ if ($key =~ /^\-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^\-/) 0 4 4
260 0 $key =~ s/^\-//;
261 }
262 4 ▶ if (! defined $val) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $val) { } 1 3 4
elsif (ref $val eq 'ARRAY') { } 1 2 3
elsif (ref $val eq 'HASH') { } 0 2 2
elsif (ref $val eq 'SCALAR') { } 0 2 2
263 1 push(@options,('--'.$key));
264 1 next;
265 } elsif (ref $val eq "ARRAY") {
266 1 foreach my $a (@{$val}) {
267 3 push(@options,('--'.$key));
268 3 push(@options,$a);
269 }
270 } elsif (ref $val eq "HASH") {
271 0 push(@options,('--'.$key));
272 0 foreach (my ($skey,$sval) = each %{$val}) {
273 0 push(@options,($skey.'='.$sval));
274 }
275 } elsif (ref $val eq "SCALAR") {
276 0 push(@options,('--'.$key));
277 0 push(@options,${$val});
278 } else {
279 2 push(@options,('--'.$key));
280 2 push(@options,$val);
281 }
282 }
283 1 return \@options;
284 }
285  
286  
287 =pod
288  
289 =head2 GetXMLOptions
290  
291 Read a XML::TreePP parsed XML document, retrieve the XML data located at the
292 specified XML subtree, and transform it into an acceptable argument array that
293 can be passed into to the Getopt::Long::GetOptionsFromArray() method.
294  
295 =over 4
296  
297 =item * B<xmldoc>
298  
299 The hash reference of a parsed XML file, which has been parsed
300 by XML::TreePP::parse()
301  
302 =item * B<xmlpath> - I<optional>
303  
304 The XML Path as recognized by the XML::TreePP::XMLPath module.
305 Note that XMLPath is NOT an XPath, although it has similarities.
306  
307 =item * B<Getopt_Long>
308  
309 The hash array of a Getopt::Long configuration which is a definition
310 of the expected Getopt::Long::GetOptions() input options.
311  
312 =back
313  
314 GetXMLOptions ( xmldoc => $XMLTree, xmlpath => $XMLPath, Getopt_Long => \@options )
315  
316 =cut
317  
318 # GetXMLOptions
319 # @param xmldoc the XML::TreePP parsed XML file as a HASH
320 # @param xmlpath an XPath-like string used to retrieve a XML child element for the parsing, instead of the XML root
321 # @param Getopt_Long input for the Getopt::Long::GetOptions(@) method used in this method for input
322 # @return the result of Getopt::Long() using the 'xmldoc' as input (as opposed to @ARGV as input)
323 #
324 # This method does not look at nor touch @ARGV, but instead parses the XML
325 # document 'xmldoc', starting at 'path', and uses that in place of @ARGV.
326 # Thus, the returning data is from the result of
327 # Getopt::Long::GetOptionsFromArray(<parsed-xml-file>,@{'Getopt_Long'})
328 #
329 1 ▶sub GetXMLOptions (@) {
Subroutine GetXMLOptions: called CC=6 SCAR=20.4
Pod documented
330 1 ▶ my $self = shift if ref($_[0]) eq $REF_NAME || undef;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq $Getopt::XML::REF_NAME or undef 1 0 1
Condition50%1/2
ref $_[0] eq $Getopt::XML::REF_NAME or undef
l!l
10
MC/DC0%0/1
ref $_[0] eq $Getopt::XML::REF_NAME or undef
ref $_[0] eq $Getopt::XML::REF_NAME
Truth table50%1/2
A
A= ref $_[0] eq $Getopt::XML::REF_NAME
Aresult
11
00
331 0 ▶ if (! @_ >= 1) { carp 'method GetXMLOptions(@) requires at least one argument.'; return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if (!@_ >= 1) 0 1 1
332 1 my %args = @_; # xmldoc, dtddoc, xmlpath, Getopt_Long,
333 1 my $tree = $args{'xmldoc'};
334 # not yet supported # @param dtddoc (optional) the DTD doc to validate the XML doc
335 #my %args = validate ( @_, { xmldoc => { optional => 0 },
336 # dtddoc => { optional => 1 },
337 # xmlpath => { type => SCALAR, optional => 1 },
338 # Getopt_Long => { type => ARRAYREF, optional => 1 }
339 # }
340 # );
341 1 my $subtree;
342  
343 1 ▶ if (defined $args{'xmlpath'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'xmlpath'}) { } 1 0 1
344 1 $subtree = filterXMLDoc($tree,$args{'xmlpath'});
345 } else {
346 0 $subtree = $tree;
347 }
348 1 ▶ if (! defined $subtree) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subtree) 0 1 1
349 0 return undef;
350 }
351 1 my $xmlargs = XMLToGetoptArgsArray($subtree);
352 1 return GetOptionsFromArray($xmlargs,@{$args{'Getopt_Long'}});
353 }
354  
355  
356 =pod
357  
358 =head2 GetXMLOptionsFromFile
359  
360 This method is a wrapper around the Getopt::XML::GetXMLOptions() method.
361 Parse a XML file with the XML::TreePP::parsefile() method, then call the
362 GetXMLOptions() method with the resulting XML::TreePP parsed XML document and
363 other parameters passed in from the caller.
364  
365 =over 4
366  
367 =item * B<xmlfile>
368  
369 The XML file which will be parsed by XML::TreePP::parse() into a hash reference.
370  
371 =item * B<xmlpath> - I<optional>
372  
373 The XML Path as recognized by the XML::TreePP::XMLPath module.
374 Note that XMLPath is NOT an XPath, although it has similarities.
375  
376 =item * B<Getopt_Long>
377  
378 The hash array of a Getopt::Long configuration which is a definition
379 of the expected Getopt::Log::GetOptions() input options.
380  
381 =back
382  
383 GetXMLOptionsFromFile ( xmlfile => $XMLFile, xmlpath => $XMLPath, Getopt_Long => \@options )
384  
385 =cut
386  
387 # GetXMLOptionsFromFile
388 # @param xmlfile the XML file
389 # @param xmlpath an XPath-like string used to retrieve a XML child element for the parsing, instead of the XML root
390 # @param Getopt_Long input for the Getopt::Long::GetOptions(@) method used in this method for input
391 # @return the result of Getopt::Long() using the 'xmlfile' as input (as opposed to @ARGV as input)
392 #
393 # See GetXMLOptions method.
394 #
395 0 ▶sub GetXMLOptionsFromFile (@) {
Subroutine GetXMLOptionsFromFile: not called CC=4 SCAR=30.0
Pod documented
396 0 ▶ my $self = shift if ref($_[0]) eq $REF_NAME || undef;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq $Getopt::XML::REF_NAME or undef 0 0 0
Condition0%0/2
ref $_[0] eq $Getopt::XML::REF_NAME or undef
l!l
00
MC/DC0%0/1
ref $_[0] eq $Getopt::XML::REF_NAME or undef
ref $_[0] eq $Getopt::XML::REF_NAME
Truth table0%0/2
A
A= ref $_[0] eq $Getopt::XML::REF_NAME
Aresult
11
00
397 0 ▶ if (! @_ >= 1) { carp 'method GetXMLOptionsFromFile(@) requires at least one argument.'; return undef; }
Branch: 1 branch
BranchTrueFalseTotal
if (!@_ >= 1) 0 0 0
398 0 my %args = @_; # xmlfile, dtdfile, xmlpath, Getopt_Long,
399 # not yet supported # @param dtdfile (optional) the DTD file to validate the XML file
400 #my %args = validate ( @_, { xmlfile => { optional => 0 },
401 # dtdfile => { optional => 1 },
402 # xmlpath => { type => SCALAR, optional => 1 },
403 # Getopt_Long => { type => ARRAYREF, optional => 0 }
404 # }
405 # );
406  
407 0 my $tpp = XML::TreePP->new();
408 0 my $tree = $tpp->parsefile( $args{'xmlfile'} );
409  
410 return GetXMLOptions(
411 xmldoc => $tree,
412 xmlpath => $args{'xmlpath'},
413 0 Getopt_Long => $args{'Getopt_Long'}
414 );
415 }
416  
417 1 1;
418 __END__
↑ Summary