blib/lib/WWW/Mechanize/XML.pm

filter: Statementstmt 96.2% 51 / 53 Branchbran 78.5% 11 / 14 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 92.3% 85 / 92 CC 13 SCAR 16.9
CC
13
Cov
92%
new
8.2
xml
6.0
BEGIN
1.0
CRAP 5.4
↑ Summary
1 package WWW::Mechanize::XML;
2  
3 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $VERSION = '0.02';
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw( WWW::Mechanize );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp qw( tempfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 WWW::Mechanize::XML - adds an XML DOM accessor to L<WWW::Mechanize>.
17  
18 =head1 VERSION
19  
20 This document describes WWW::Mechanize::XML version 0.02
21  
22 =head1 SYNOPSIS
23  
24 use WWW::Mechanize::XML;
25 use Test::More;
26 my $mech = WWW::Mechanize::XML->new();
27
28 ok($mech->get('http://flickr.com/service/?method=getPhotos'), 'got photo list');
29 lives_ok {
30 $dom = $mech->xml();
31 } 'got xml dom object';
32 $root = $dom->domumentElement();
33 my @photos = $root->findnodes('/rsp/photos/photo');
34 is(scalar @photos, 23, 'got 23 photos');
35  
36 =head1 DESCRIPTION
37  
38 This is a subclass of L<WWW::Mechanize> that provides an XML DOM accessor which
39 parses the contents of the response and returns it as a
40 L<XML::LibXML::Document>. The motivation for developing this module was to
41 facilitate testing of XML APIs and XHTML web pages.
42  
43 =head1 METHODS
44  
45 =head2 new( %options )
46  
47 Creates a new C<WWW::Mechanize::XML> object with the specified options. This
48 constructor method accepts all of the arguments accepted by L<WWW::Mechanize> -
49 see L<WWW::Mechanize> for further details. Other optional arguments accepted by
50 this method are C<xml_parser_options> and C<xml_error_options>:
51  
52 =head3 xml_parser_options
53  
54 This argument, if specified, must be a hashref of valid L<XML::LibXML::Parser>
55 options which will be used to instantiate the XML parser. If no parser options
56 are specified defaults are used. Please see the documentation for
57 L<XML::LibXML::Parser> for option descriptions and default values. Valid parser
58 options accepted are:
59  
60 =over
61  
62 =item validation
63  
64 =item recover
65  
66 =item recover_silently
67  
68 =item expand_entities
69  
70 =item keep_blanks
71  
72 =item pedantic_parser
73  
74 =item line_numbers
75  
76 =item load_ext_dtd
77  
78 =item complete_attributes
79  
80 =item expand_xinclude
81  
82 =item clean_namespaces
83  
84 =back
85  
86 =head3 xml_error_options
87  
88 This argument, if specified, must be a hashref containing at least the
89 C<trigger_xpath> key. If there is a value for the given xpath expression it will
90 cause the call to C<xml()> to die with that value. If a C<trigger_value> key is
91 specified the call will only die if the value at C<trigger_xpath> equals
92 C<trigger_value>. If a C<message_xpath> key is specified the call to C<xml()>
93 will die with the value at that path.
94  
95 =cut
96  
97 1 my @valid_parser_options = qw(
98 validation
99 recover
100 recover_silently
101 expand_entities
102 keep_blanks
103 pedantic_parser
104 line_numbers
105 load_ext_dtd
106 complete_attributes
107 expand_xinclude
108 clean_namespaces
109 );
110  
111 31 ▶sub new {
Subroutine new: called CC=8 SCAR=21.1
Pod documented
112 31 my ( $class, %args ) = @_;
113
114 # check for 'parser_options' for backwards compatability
115 my $parser_options =
116 31 ▶ delete $args{xml_parser_options} || delete $args{parser_options} || {};
Condition80%4/5
delete $args{'xml_parser_options'} || delete $args{'parser_options'} || {}
l!l
229
delete $args{"xml_parser_options"} or delete $args{"parser_options"}
l!l&&r!l&&!r
2209
MC/DC50%1/2
delete $args{'xml_parser_options'} || delete $args{'parser_options'} || {}
delete $args{"xml_parser_options"} delete $args{"parser_options"}
Truth table66%2/3
A or B
A= delete $args{"xml_parser_options"}B= delete $args{"parser_options"}
ABresult
1X1
011
000
117 31 ▶ unless (ref $parser_options eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $parser_options eq "HASH") 0 31 31
118 0 die "'xml_parser_options' must be a hash-ref" ;
119 }
120
121 31 ▶ my $error_options = delete $args{xml_error_options} || {};
Condition100%2/2
delete $args{'xml_error_options'} || {}
l!l
427
MC/DC100%1/1
delete $args{'xml_error_options'} || {}
delete $args{'xml_error_options'}
Truth table100%2/2
A
A= delete $args{'xml_error_options'}
Aresult
11
00
122 31 ▶ unless (ref $error_options eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $error_options eq "HASH") 0 31 31
123 0 die "'xml_error_options' must be a hash-ref" ;
124 }
125
126 # use catalog to speed up parsing if DTD is loaded
127 31 my ( $catalog_fh, $catalog_file ) = tempfile();
128 31 my $parser = XML::LibXML->new();
129 31 $parser->load_catalog( $catalog_file );
130
131 # set each parser option is valid
132 31 foreach my $option (keys %$parser_options) {
133 22 ▶ if (grep { $_ =~ $option } @valid_parser_options) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ =~ /$option/;} @valid_parser_options) { } 20 2 22
134 20 $parser->$option( $parser_options->{$option} );
135 } else {
136 2 die "Invalid parser option: $option";
137 }
138 }
139
140 29 my $self = bless WWW::Mechanize->SUPER::new( %args ), $class;
141 29 $self->{xml_parser} = $parser;
142 29 $self->{xml_error_options} = $error_options;
143 29 return $self;
144 }
145  
146 =head2 xml( )
147  
148 Returns a L<XML::LibXML::Document> object created from the response content by
149 calling the L<XML::LibXML::Parser> parse_string() method. Any parsing errors
150 will propogate up. If C<xml_error_options> were specified for this instance
151 the response document is check for errors accordingly - this method will die
152 if errors are found in the document as specified by the options.
153  
154 =cut
155  
156 7 ▶sub xml {
Subroutine xml: called CC=6 SCAR=17.9
Pod documented
157 7 my $self = shift;
158
159 7 my $dom = $self->{xml_parser}->parse_string( $self->content() );
160 6 $dom->indexElements(); # speed up XPath queries for static documents
161
162 # if a trigger_xpath is set check for error
163 6 ▶ if ($self->{xml_error_options}->{trigger_xpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml_error_options"}{"trigger_xpath"}) 4 2 6
164 4 my $root = $dom->documentElement();
165 4 my $error = $root->findvalue($self->{xml_error_options}->{trigger_xpath});
166
167 # if error found at trigger_xpath...
168 4 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 4 0 4
169
170 # if trigger_value is specified only die if the error has that value
171 4 my $tv = $self->{xml_error_options}->{trigger_value};
172 4 ▶ return $dom if ($tv && $tv ne $error);
Branch: 1 branch
BranchTrueFalseTotal
if $tv and $tv ne $error 1 3 4
Condition100%3/3
$tv and $tv ne $error
!ll&&!rl&&r
121
MC/DC100%2/2
$tv and $tv ne $error
$tv $tv ne $error
Truth table100%3/3
A and B
A= $tvB= $tv ne $error
ABresult
0X0
100
111
173
174 # if message_xpath is specified die with the value at that location
175 3 ▶ if ($self->{xml_error_options}->{message_xpath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"xml_error_options"}{"message_xpath"}) 1 2 3
176 1 die $root->findvalue($self->{xml_error_options}->{message_xpath});
177 }
178
179 2 die $error;
180 }
181 }
182
183 2 return $dom;
184 }
185  
186 =head1 DEPENDENCIES
187  
188 L<WWW::Mechanize>
189 L<XML::LibXML>
190 L<File::Temp>
191  
192 =head1 BUGS
193  
194 Please report any bugs you find via the CPAN RT system.
195  
196 =head1 COPYRIGHT
197  
198 Copyright Fotango 2006. All rights reserved.
199  
200 This module is free software; you can redistribute it and/or modify it under
201 the same terms as Perl itself.
202  
203 =head1 AUTHOR
204  
205 Barry White <bwhite@fotango.com>
206  
207 =cut
208  
209 1 1;
210  
211 __END__
↑ Summary