« blib/lib/Google/Ads/AdWords/Utilities/PageProcessor.pm
↑ Summary
lib/Google/Ads/AdWords/Utilities/ReportQueryBuilder.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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use version; | ||
| 20 | |||
| 21 | # The following needs to be on one line because CPAN uses a particularly hacky | ||
| 22 | # eval() to determine module versions. | ||
| 23 | use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION}; | ||
| 24 | |||
| 25 | use Class::Std::Fast; | ||
| 26 | use Carp; | ||
| 27 | use XML::LibXML; | ||
| 28 | |||
| 29 | # Keeps a mapping between SOAP objects and nodes. | ||
| 30 | my %OBJECTS_NODES_OF; | ||
| 31 | |||
| 32 | # SAX handlers used as callbacks during parsing. | ||
| 33 | my %handlers_of : ATTR(:name<handlers> :default<{}>); | ||
| 34 | |||
| 35 | # Initial XPath expression to filter results to parse. | ||
| 36 | # Defaults to the root element. | ||
| 37 | 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 | my %namespaces_of : ATTR(:name<namespaces> :default<1>); | ||
| 42 | |||
| 43 | # Stores the current element being parsed. | ||
| 44 | my %current_of : ATTR(:name<current> :default<>); | ||
| 45 | |||
| 46 | # Start the parsing. | ||
| 47 | # Takes the XML to parse. | ||
| 48 | sub parse { | ||
| 49 | my ($self, $xml) = @_; | ||
| 50 | my $doc = XML::LibXML->new->parse_string($xml); | ||
| 51 | my $root = $doc->documentElement(); | ||
| 52 | my $node = $root->findnodes($self->get_xpath_expression())->get_node(0); | ||
| 53 | if ($root) { | ||
| 54 | # Clear out the past cached objects to nodes mapping to avoid memory | ||
| 55 | # leaking. | ||
| 56 | %OBJECTS_NODES_OF = (); | ||
| 57 | $self->__parse_node($root); | ||
| 58 | $self->set_current(undef); | ||
| 59 | } else { | ||
| 60 | 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 | sub __parse_node { | ||
| 67 | my ($self, $node) = @_; | ||
| 68 | my $node_type = $node->nodeType(); | ||
| 69 | my %handlers = %{$self->get_handlers()}; | ||
| 70 | $self->set_current($node); | ||
| 71 | |||
| 72 | if ($node_type == XML::LibXML::XML_ELEMENT_NODE | ||
| 73 | && ($handlers{Start} || $handlers{End})) | ||
| 74 | { | ||
| 75 | # bundle up attributes | ||
| 76 | my %attribs = (); | ||
| 77 | foreach my $attr ($node->attributes()) { | ||
| 78 | my $att_name = | ||
| 79 | $self->get_namespaces() ? $attr->getLocalName() : $attr->name(); | ||
| 80 | if ($att_name) { | ||
| 81 | $attribs{$att_name} = $attr->value(); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | my $name = | ||
| 86 | $self->get_namespaces() ? $node->localName() : $node->nodeName(); | ||
| 87 | if ($handlers{Start}) { | ||
| 88 | $handlers{Start}($self, $name, \%attribs, $node); | ||
| 89 | } | ||
| 90 | foreach my $childNode (@{$node->childNodes()}) { | ||
| 91 | $self->__parse_node($childNode); | ||
| 92 | } | ||
| 93 | if ($handlers{End}) { | ||
| 94 | $handlers{End}($self, $name, \%attribs, $node); | ||
| 95 | } | ||
| 96 | } elsif ($node_type == XML::LibXML::XML_TEXT_NODE && $handlers{Char}) { | ||
| 97 | $handlers{Char}($self, $node->nodeValue(), $node); | ||
| 98 | } elsif ($node_type == XML::LibXML::XML_COMMENT_NODE && $handlers{Comment}) { | ||
| 99 | $handlers{Comment}($self, $node->nodeValue(), $node); | ||
| 100 | } elsif ($node_type == XML::LibXML::XML_PI_NODE | ||
| 101 | && $handlers{Proc}) | ||
| 102 | { | ||
| 103 | $handlers{Proc}($self, $node->getTarget(), $node->getData(), $node); | ||
| 104 | } else { | ||
| 105 | 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 | sub namespace { | ||
| 113 | my ($self, $node) = @_; | ||
| 114 | |||
| 115 | $node = $self->get_current() if not defined $node; | ||
| 116 | |||
| 117 | return $node->lookupNamespaceURI($node->prefix()); | ||
| 118 | } | ||
| 119 | |||
| 120 | # Retrieves a string representation of the current parsed node or any given | ||
| 121 | # node. | ||
| 122 | sub recognized_string { | ||
| 123 | my ($self, $node) = @_; | ||
| 124 | |||
| 125 | $node = $self->get_current() if not defined $node; | ||
| 126 | |||
| 127 | return $node->nodeName(); | ||
| 128 | } | ||
| 129 | |||
| 130 | # Associates a SOAP object with an object. | ||
| 131 | sub link_object_to_node { | ||
| 132 | my ($object, $node) = @_; | ||
| 133 | $OBJECTS_NODES_OF{${$object}} = $node; | ||
| 134 | $OBJECTS_NODES_OF{ident($node)} = $object; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Retrieves a node given its associated SOAP Object. | ||
| 138 | sub get_node_from_object { | ||
| 139 | my ($object) = @_; | ||
| 140 | return $OBJECTS_NODES_OF{${$object}}; | ||
| 141 | } | ||
| 142 | |||
| 143 | # Retrieves a SOAP Object given its associated node. | ||
| 144 | sub get_object_from_node { | ||
| 145 | my ($node) = @_; | ||
| 146 | return $OBJECTS_NODES_OF{ident($node)}; | ||
| 147 | } | ||
| 148 | |||
| 149 | 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 |