blib/lib/Google/Ads/Common/XPathSAXParser.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Google/Ads/Common/CaptchaRequiredError.pm ↑ Summary lib/Google/Ads/AdWords/Client.pm »
1 # Copyright 2011, 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::XPathSAXParser;
16  
17 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The following needs to be on one line because CPAN uses a particularly hacky
20 # eval() to determine module versions.
21 1 ▶use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
26  
27 # Keeps a mapping between SOAP objects and XPath nodes.
28 my %OBJECTS_NODES_OF;
29  
30 # SAX handlers used as callbacks during parsing.
31 my %handlers_of : ATTR(:name<handlers> :default<{}>);
32  
33 # Initial XPath expression to filter results to parse.
34 # Defaults to the root element.
35 my %xpath_expression_of : ATTR(:name<xpath_expression> :default<"/*">);
36  
37 # Boolean to indicate when namespace prefixes have to be included 0 or
38 # excluded 1 from the reported names of elements parsed. Defaults to 1.
39 my %namespaces_of : ATTR(:name<namespaces> :default<1>);
40  
41 # Stores the current element being parsed.
42 my %current_of : ATTR(:name<current> :default<>);
43  
44 # Start the parsing.
45 # Takes the XML to parse.
46 sub parse {
47 my $self = shift;
48 my $xml = shift;
49 my $xp = XML::XPath->new(xml => $xml);
50 my $node = $xp->find($self->get_xpath_expression())->get_node(0);
51  
52 if ($node) {
53 # Clear out the past cached objects to nodes mapping to avoid memory
54 # leaking.
55 %OBJECTS_NODES_OF = ();
56 $self->__parse_node($node);
57 $self->set_current(undef);
58 } else {
59 die "Given expression " . $self->get_xpath_expresssion() . " didn't " .
60 "match any nodes to parse.";
61 }
62 }
63  
64 # The parsing is done recursively through this method.
65 sub __parse_node {
66 my $self = shift;
67 my $node = shift;
68 my $node_type = $node->getNodeType();
69 my $parent = $node->getParentNode();
70 my %handlers = %{$self->get_handlers()};
71  
72 $self->set_current($node);
73  
74 if ($node_type == XML::XPath::Node::ELEMENT_NODE &&
75 ($handlers{Start} || $handlers{End})) {
76 # bundle up attributes
77 my %attribs = ();
78 foreach my $attr (@{$node->getAttributes()}) {
79 my $att_name = $self->get_namespaces() ?
80 $attr->getLocalName() : $attr->getName();
81 $attribs{$att_name} = $attr->getNodeValue();
82 }
83  
84 my $name = $self->get_namespaces() ?
85 $node->getLocalName() : $node->getName();
86 if ($handlers{Start}) {
87 $handlers{Start}($self, $name, \%attribs, $node);
88 }
89 foreach my $childNode (@{$node->getChildNodes()}) {
90 $self->__parse_node($childNode);
91 }
92 if ($handlers{End}) {
93 $handlers{End}($self, $name, \%attribs, $node);
94 }
95 } elsif ($node_type == XML::XPath::Node::TEXT_NODE && $handlers{Char}) {
96 $handlers{Char}($self, $node->getValue(), $node);
97 } elsif ($node_type == XML::XPath::Node::COMMENT_NODE && $handlers{Comment}) {
98 $handlers{Comment}($self, $node->getValue(), $node);
99 } elsif ($node_type == XML::XPath::Node::PROCESSING_INSTRUCTION_NODE &&
100 $handlers{Proc}) {
101 $handlers{Proc}($self, $node->getTarget(), $node->getData(), $node);
102 } else {
103 croak "Unknown node type: '", ref($node), "' type ", $node_type,
104 " or not handler found.\n";
105 }
106 }
107  
108 # Retrieves the expanded namespace name of the current parsed node or any
109 # given node.
110 sub namespace {
111 my ($self, $node) = @_;
112  
113 $node = $self->get_current() if not defined $node;
114  
115 return $node->getNamespace($node->getPrefix())->getExpanded();
116 }
117  
118 # Retrieves a string representation of the current parsed node or any given
119 # node.
120 sub recognized_string {
121 my ($self, $node) = @_;
122  
123 $node = $self->get_current() if not defined $node;
124  
125 return $node->get_name();
126 }
127  
128 # Associates a SOAP object with an XPath object.
129 sub link_object_to_node {
130 my ($object, $node) = @_;
131 $OBJECTS_NODES_OF{${$object}} = $node;
132 $OBJECTS_NODES_OF{ident($node)} = $object;
133 }
134  
135 # Retrieves an XPath node given its associated SOAP Object.
136 sub get_node_from_object {
137 my ($object) = @_;
138 return $OBJECTS_NODES_OF{${$object}};
139 }
140  
141 # Retrieves a SOAP Object given its associated XPath node.
142 sub get_object_from_node {
143 my ($node) = @_;
144 return $OBJECTS_NODES_OF{ident($node)};
145 }
146  
147 return 1;
148  
149 =pod
150  
151 =head1 NAME
152  
153 Google::Ads::Common::XPathSAXParser
154  
155 =head1 DESCRIPTION
156  
157 Implements a SAX type of XML parser based on the L<XML::XPath:XML::XPath>.
158 And it is basically used during deserialization to tie XPath module nodes
159 with SOAP objects so the XPath module search functionalities can be used
160 to find SOAP objects.
161  
162 =head1 METHODS
163  
164 =head2 parse
165  
166 Main entry point to start the parsing of the XML and report to the registered
167 handlers the parsed node in a depth-first traversal fashion as expected from
168 a SAX parser.
169  
170 =head3 Parameters
171  
172 The XML to parse.
173  
174 =head2 __parse_node
175  
176 The parsing is done recursively through this method.
177  
178 =head3 Parameters
179  
180 The node being parsed.
181  
182 =head2 namespace
183  
184 Retrieves the expanded namespace name of the current parsed node or any
185 given node.
186  
187 =head3 Parameters
188  
189 Optional an XPath node. If none given then the current parsed is used.
190  
191 =head3 Returns
192  
193 The registered namespace.
194  
195 =head2 recognized_string
196  
197 Retrieves a string representation of the current parsed node or any given
198 node.
199  
200 =head3 Parameters
201  
202 Optional an XPath node. If none given then the current parsed is used.
203  
204 =head3 Returns
205  
206 The string representation.
207  
208 =head2 link_object_to_node
209  
210 Links a SOAP Object with its XPath node.
211  
212 =head3 Parameters
213  
214 The SOAP object to associate.
215  
216 The XPath node.
217  
218 =head2 get_node_from_object
219  
220 Retrieves an XPath node given its associated SOAP Object.
221  
222 =head3 Parameters
223  
224 The SOAP object.
225  
226 =head3 Returns
227  
228 The associated XPath node if found.
229  
230 =head2 get_object_from_node
231  
232 Retrieves a SOAP Object given its associated XPath node..
233  
234 =head3 Parameters
235  
236 The XPath node.
237  
238 =head3 Returns
239  
240 The associated SOAP Object.
241  
242 =head1 LICENSE AND COPYRIGHT
243  
244 Copyright 2011 Google Inc.
245  
246 Licensed under the Apache License, Version 2.0 (the "License");
247 you may not use this file except in compliance with the License.
248 You may obtain a copy of the License at
249  
250 http://www.apache.org/licenses/LICENSE-2.0
251  
252 Unless required by applicable law or agreed to in writing, software
253 distributed under the License is distributed on an "AS IS" BASIS,
254 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
255 See the License for the specific language governing permissions and
256 limitations under the License.
257  
258 =head1 AUTHOR
259  
260 David Torres E<lt>api.davidtorres at gmail.comE<gt>
261  
262 =head1 REPOSITORY INFORMATION
263  
264 $Rev: $
265 $LastChangedBy: $
266 $Id: $
267  
268 =cut
« lib/Google/Ads/Common/CaptchaRequiredError.pm ↑ Summary lib/Google/Ads/AdWords/Client.pm »