lib/Google/Ads/Common/LibXmlParser.pm

filter: Statementstmt 37.3% 28 / 75 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 50.0% 7 / 14 Podpod 100% 6 / 6 totaltotal 28.6% 41 / 143 CC 20 SCAR 50.7
CC
20
Cov
25%
__parse_node
306.0
parse
6.0
namespace
6.0
CRAP 159.1
« lib/Google/Ads/Common/ReportUtils.pm ↑ Summary lib/Google/Ads/AdWords/Utilities/PageProcessor.pm »
1 # Copyright 2017, Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14  
15 package Google::Ads::Common::LibXmlParser;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # The following needs to be on one line because CPAN uses a particularly hacky
22 # eval() to determine module versions.
23 1 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Keeps a mapping between SOAP objects and nodes.
30 1 my %OBJECTS_NODES_OF;
31  
32 # SAX handlers used as callbacks during parsing.
33 1 my %handlers_of : ATTR(:name<handlers> :default<{}>);
34  
35 # Initial XPath expression to filter results to parse.
36 # Defaults to the root element.
37 1 my %xpath_expression_of : ATTR(:name<xpath_expression> :default<"/*">);
38  
39 # Boolean to indicate when namespace prefixes have to be included 0 or
40 # excluded 1 from the reported names of elements parsed. Defaults to 1.
41 1 my %namespaces_of : ATTR(:name<namespaces> :default<1>);
42  
43 # Stores the current element being parsed.
44 1 my %current_of : ATTR(:name<current> :default<>);
45  
46 # Start the parsing.
47 # Takes the XML to parse.
48 0 ▶sub parse {
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
49 0 my ($self, $xml) = @_;
50 0 my $doc = XML::LibXML->new->parse_string($xml);
51 0 my $root = $doc->documentElement();
52 0 my $node = $root->findnodes($self->get_xpath_expression())->get_node(0);
53 0 ▶ if ($root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root) { } 0 0 0
54 # Clear out the past cached objects to nodes mapping to avoid memory
55 # leaking.
56 0 %OBJECTS_NODES_OF = ();
57 0 $self->__parse_node($root);
58 0 $self->set_current(undef);
59 } else {
60 0 die "Given expression " . $self->get_xpath_expresssion() . " didn't " .
61 "match any nodes to parse.";
62 }
63 }
64  
65 # The parsing is done recursively through this method.
66 0 ▶sub __parse_node {
Subroutine __parse_node: not called CC=17 SCAR=57.2
67 0 my ($self, $node) = @_;
68 0 my $node_type = $node->nodeType();
69 0 my %handlers = %{$self->get_handlers()};
70 0 $self->set_current($node);
71  
72 0 ▶ if ($node_type == XML::LibXML::XML_ELEMENT_NODE
Branch: 4 branches
BranchTrueFalseTotal
if ($node_type == 1 and $handlers{'Start'} || $handlers{'End'}) { } 0 0 0
elsif ($node_type == 3 and $handlers{'Char'}) { } 0 0 0
elsif ($node_type == 8 and $handlers{'Comment'}) { } 0 0 0
elsif ($node_type == 7 and $handlers{'Proc'}) { } 0 0 0
Condition0%0/6
$node_type == 1 and $handlers{"Start"} || $handlers{"End"}
!ll&&!rl&&r
000
$handlers{"Start"} or $handlers{"End"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$node_type == 1 and $handlers{"Start"} || $handlers{"End"}
$node_type == 1 $handlers{"Start"} $handlers{"End"}
Truth table0%0/4
A and B or C
A= $node_type == 1B= $handlers{"Start"}C= $handlers{"End"}
ABCresult
0XX0
1000
11X1
1011
73 && ($handlers{Start} || $handlers{End}))
74 {
75 # bundle up attributes
76 0 my %attribs = ();
77 0 foreach my $attr ($node->attributes()) {
78 0 ▶ my $att_name =
Branch: 1 branch
BranchTrueFalseTotal
$self->get_namespaces ? : 0 0 0
79 $self->get_namespaces() ? $attr->getLocalName() : $attr->name();
80 0 ▶ if ($att_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($att_name) 0 0 0
81 0 $attribs{$att_name} = $attr->value();
82 }
83 }
84  
85 0 ▶ my $name =
Branch: 1 branch
BranchTrueFalseTotal
$self->get_namespaces ? : 0 0 0
86 $self->get_namespaces() ? $node->localName() : $node->nodeName();
87 0 ▶ if ($handlers{Start}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handlers{"Start"}) 0 0 0
88 0 $handlers{Start}($self, $name, \%attribs, $node);
89 }
90 0 foreach my $childNode (@{$node->childNodes()}) {
91 0 $self->__parse_node($childNode);
92 }
93 0 ▶ if ($handlers{End}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handlers{"End"}) 0 0 0
94 0 $handlers{End}($self, $name, \%attribs, $node);
95 }
96 ▶ } elsif ($node_type == XML::LibXML::XML_TEXT_NODE && $handlers{Char}) {
Condition0%0/3
$node_type == 3 and $handlers{"Char"}
!ll&&!rl&&r
000
MC/DC0%0/2
$node_type == 3 and $handlers{"Char"}
$node_type == 3 $handlers{"Char"}
Truth table0%0/3
A and B
A= $node_type == 3B= $handlers{"Char"}
ABresult
0X0
100
111
97 0 $handlers{Char}($self, $node->nodeValue(), $node);
98 ▶ } elsif ($node_type == XML::LibXML::XML_COMMENT_NODE && $handlers{Comment}) {
Condition0%0/3
$node_type == 8 and $handlers{"Comment"}
!ll&&!rl&&r
000
MC/DC0%0/2
$node_type == 8 and $handlers{"Comment"}
$node_type == 8 $handlers{"Comment"}
Truth table0%0/3
A and B
A= $node_type == 8B= $handlers{"Comment"}
ABresult
0X0
100
111
99 0 $handlers{Comment}($self, $node->nodeValue(), $node);
100 ▶ } elsif ($node_type == XML::LibXML::XML_PI_NODE
Condition0%0/3
$node_type == 7 and $handlers{"Proc"}
!ll&&!rl&&r
000
MC/DC0%0/2
$node_type == 7 and $handlers{"Proc"}
$node_type == 7 $handlers{"Proc"}
Truth table0%0/3
A and B
A= $node_type == 7B= $handlers{"Proc"}
ABresult
0X0
100
111
101 && $handlers{Proc})
102 {
103 0 $handlers{Proc}($self, $node->getTarget(), $node->getData(), $node);
104 } else {
105 0 croak "Unknown node type: '", ref($node), "' type ", $node_type,
106 " or not handler found.\n";
107 }
108 }
109  
110 # Retrieves the expanded namespace name of the current parsed node or any
111 # given node.
112 0 ▶sub namespace {
Subroutine namespace: not called CC=2 SCAR=17.9
Pod documented
113 0 my ($self, $node) = @_;
114  
115 0 ▶ $node = $self->get_current() if not defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
116  
117 0 return $node->lookupNamespaceURI($node->prefix());
118 }
119  
120 # Retrieves a string representation of the current parsed node or any given
121 # node.
122 0 ▶sub recognized_string {
Subroutine recognized_string: not called CC=2 SCAR=17.9
Pod documented
123 0 my ($self, $node) = @_;
124  
125 0 ▶ $node = $self->get_current() if not defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
126  
127 0 return $node->nodeName();
128 }
129  
130 # Associates a SOAP object with an object.
131 0 ▶sub link_object_to_node {
Subroutine link_object_to_node: not called CC=1 SCAR=6.9
Pod documented
132 0 my ($object, $node) = @_;
133 0 $OBJECTS_NODES_OF{${$object}} = $node;
134 0 $OBJECTS_NODES_OF{ident($node)} = $object;
135 }
136  
137 # Retrieves a node given its associated SOAP Object.
138 0 ▶sub get_node_from_object {
Subroutine get_node_from_object: not called CC=1 SCAR=6.9
Pod documented
139 0 my ($object) = @_;
140 0 return $OBJECTS_NODES_OF{${$object}};
141 }
142  
143 # Retrieves a SOAP Object given its associated node.
144 0 ▶sub get_object_from_node {
Subroutine get_object_from_node: not called CC=1 SCAR=6.9
Pod documented
145 0 my ($node) = @_;
146 0 return $OBJECTS_NODES_OF{ident($node)};
147 }
148  
149 1 return 1;
150  
151 =pod
152  
153 =head1 NAME
154  
155 Google::Ads::Common::LibXmlParser
156  
157 =head1 DESCRIPTION
158  
159 Implements an XML parser based on the L<XML::LibXML>.
160  
161 =head1 METHODS
162  
163 =head2 parse
164  
165 Main entry point to start the parsing of the XML and report to the registered
166 handlers the parsed node in a depth-first traversal fashion as expected from
167 a parser.
168  
169 =head3 Parameters
170  
171 The XML to parse.
172  
173 =head2 __parse_node
174  
175 The parsing is done recursively through this method.
176  
177 =head3 Parameters
178  
179 The node being parsed.
180  
181 =head2 namespace
182  
183 Retrieves the expanded namespace name of the current parsed node or any
184 given node.
185  
186 =head3 Parameters
187  
188 Optional a node. If none given then the current parsed is used.
189  
190 =head3 Returns
191  
192 The registered namespace.
193  
194 =head2 recognized_string
195  
196 Retrieves a string representation of the current parsed node or any given
197 node.
198  
199 =head3 Parameters
200  
201 Optional a node. If none given then the current parsed is used.
202  
203 =head3 Returns
204  
205 The string representation.
206  
207 =head2 link_object_to_node
208  
209 Links a SOAP Object with its node.
210  
211 =head3 Parameters
212  
213 The SOAP object to associate.
214  
215 The node.
216  
217 =head2 get_node_from_object
218  
219 Retrieves a node given its associated SOAP Object.
220  
221 =head3 Parameters
222  
223 The SOAP object.
224  
225 =head3 Returns
226  
227 The associated node if found.
228  
229 =head2 get_object_from_node
230  
231 Retrieves a SOAP Object given its associated node.
232  
233 =head3 Parameters
234  
235 The node.
236  
237 =head3 Returns
238  
239 The associated SOAP Object.
240  
241 =cut
« lib/Google/Ads/Common/ReportUtils.pm ↑ Summary lib/Google/Ads/AdWords/Utilities/PageProcessor.pm »