| 1 | package POE::Filter::XML::Handler; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 2 | $POE::Filter::XML::Handler::VERSION = '1.140700'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | |||||||||||||||
| 6 | #ABSTRACT: Default SAX Handler for POE::Filter::XML | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use MooseX::NonMoose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | extends 'XML::SAX::Base'; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 2 | ▶ | use POE::Filter::XML::Node; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | |||||||||||||||
| 16 | 2 | has current_node => | |||||||||||||
| 17 | ( | ||||||||||||||
| 18 | is => 'rw', | ||||||||||||||
| 19 | isa => 'POE::Filter::XML::Node', | ||||||||||||||
| 20 | predicate => '_has_current_node', | ||||||||||||||
| 21 | clearer => '_clear_current_node' | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | |||||||||||||||
| 25 | has finished_nodes => | ||||||||||||||
| 26 | ( | ||||||||||||||
| 27 | is => 'ro', | ||||||||||||||
| 28 | traits => ['Array'], | ||||||||||||||
| 29 | isa => 'ArrayRef', | ||||||||||||||
| 30 | 2 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 2 | handles => | |||||||||||||
| 32 | { | ||||||||||||||
| 33 | all_finished_nodes => 'elements', | ||||||||||||||
| 34 | has_finished_nodes => 'count', | ||||||||||||||
| 35 | add_finished_node => 'push', | ||||||||||||||
| 36 | get_finished_node => 'shift', | ||||||||||||||
| 37 | _clear_finished_nodes => 'clear', | ||||||||||||||
| 38 | } | ||||||||||||||
| 39 | ); | ||||||||||||||
| 40 | |||||||||||||||
| 41 | |||||||||||||||
| 42 | has depth_stack => | ||||||||||||||
| 43 | ( | ||||||||||||||
| 44 | is => 'ro', | ||||||||||||||
| 45 | traits => ['Array'], | ||||||||||||||
| 46 | isa => 'ArrayRef', | ||||||||||||||
| 47 | 2 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 48 | 2 | clearer => '_clear_depth_stack', | |||||||||||||
| 49 | handles => | ||||||||||||||
| 50 | { | ||||||||||||||
| 51 | push_depth_stack => 'push', | ||||||||||||||
| 52 | pop_depth_stack => 'pop', | ||||||||||||||
| 53 | depth => 'count', | ||||||||||||||
| 54 | } | ||||||||||||||
| 55 | ); | ||||||||||||||
| 56 | |||||||||||||||
| 57 | |||||||||||||||
| 58 | 2 | has not_streaming => ( is => 'ro', isa => 'Bool', default => 0 ); | |||||||||||||
| 59 | |||||||||||||||
| 60 | |||||||||||||||
| 61 | 1 | ▶ | sub reset { | ||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 62 | 1 | my ($self) = @_; | |||||||||||||
| 63 | 1 | $self->_clear_current_node(); | |||||||||||||
| 64 | 1 | $self->_clear_finished_nodes(); | |||||||||||||
| 65 | 1 | $self->_clear_depth_stack(); | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | |||||||||||||||
| 69 | 10 | ▶ | sub start_element { | ||||||||||||
|
Subroutine
start_element: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||
| 70 | 10 | my ($self, $data) = @_; | |||||||||||||
| 71 | 10 | my $node = POE::Filter::XML::Node->new($data->{'Name'}); | |||||||||||||
| 72 | |||||||||||||||
| 73 | 10 | foreach my $attrib (values %{$data->{'Attributes'}}) | |||||||||||||
| 74 | { | ||||||||||||||
| 75 | $node->setAttribute | ||||||||||||||
| 76 | ( | ||||||||||||||
| 77 | $attrib->{'Name'}, | ||||||||||||||
| 78 | 20 | $attrib->{'Value'} | |||||||||||||
| 79 | ); | ||||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 10 | ▶ | if($self->depth() == 0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | { | ||||||||||||||
| 84 | #start of a document | ||||||||||||||
| 85 | 2 | $self->push_depth_stack($node); | |||||||||||||
| 86 | |||||||||||||||
| 87 | 2 | ▶ | if($self->not_streaming) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | { | ||||||||||||||
| 89 | 1 | $self->current_node($node); | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | else | ||||||||||||||
| 92 | { | ||||||||||||||
| 93 | 1 | $node->_set_stream_start(1); | |||||||||||||
| 94 | 1 | $self->add_finished_node($node); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | else | ||||||||||||||
| 98 | { | ||||||||||||||
| 99 | # Top level fragment | ||||||||||||||
| 100 | 8 | $self->push_depth_stack($self->current_node); | |||||||||||||
| 101 | |||||||||||||||
| 102 | 8 | ▶ | if($self->depth() == 2) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | { | ||||||||||||||
| 104 | 6 | ▶ | if($self->not_streaming) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | { | ||||||||||||||
| 106 | 3 | $self->current_node->appendChild($node); | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | 6 | $self->current_node($node); | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | else | ||||||||||||||
| 111 | { | ||||||||||||||
| 112 | # Some node within a fragment | ||||||||||||||
| 113 | 2 | $self->current_node->appendChild($node); | |||||||||||||
| 114 | 2 | $self->current_node($node); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | } | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 10 | $self->SUPER::start_element($data); | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | |||||||||||||||
| 122 | 10 | ▶ | sub end_element { | ||||||||||||
|
Subroutine
end_element: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 123 | |||||||||||||||
| 124 | 10 | my ($self, $data) = @_; | |||||||||||||
| 125 | |||||||||||||||
| 126 | 10 | ▶ | if($self->depth() == 1) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 127 | { | ||||||||||||||
| 128 | 2 | ▶ | if($self->not_streaming) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | { | ||||||||||||||
| 130 | 1 | $self->add_finished_node($self->pop_depth_stack()); | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | else | ||||||||||||||
| 133 | { | ||||||||||||||
| 134 | 1 | my $end = POE::Filter::XML::Node->new($data->{'Name'}); | |||||||||||||
| 135 | 1 | $end->_set_stream_end(1); | |||||||||||||
| 136 | 1 | $self->add_finished_node($end); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | } | ||||||||||||||
| 139 | elsif($self->depth() == 2) | ||||||||||||||
| 140 | { | ||||||||||||||
| 141 | 6 | ▶ | if($self->not_streaming) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | { | ||||||||||||||
| 143 | 3 | $self->current_node($self->pop_depth_stack()); | |||||||||||||
| 144 | } | ||||||||||||||
| 145 | else | ||||||||||||||
| 146 | { | ||||||||||||||
| 147 | 3 | $self->add_finished_node($self->current_node); | |||||||||||||
| 148 | 3 | $self->_clear_current_node(); | |||||||||||||
| 149 | 3 | $self->pop_depth_stack(); | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | } | ||||||||||||||
| 152 | else | ||||||||||||||
| 153 | { | ||||||||||||||
| 154 | 2 | $self->current_node($self->pop_depth_stack()); | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 10 | $self->SUPER::end_element($data); | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | |||||||||||||||
| 161 | 2 | ▶ | sub characters { | ||||||||||||
|
Subroutine
characters: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||
| 162 | |||||||||||||||
| 163 | 2 | my ($self, $data) = @_; | |||||||||||||
| 164 | |||||||||||||||
| 165 | 2 | ▶ | if($self->depth() == 1) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 166 | { | ||||||||||||||
| 167 | 0 | return; | |||||||||||||
| 168 | } | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 2 | $self->current_node->appendText($data->{'Data'}); | |||||||||||||
| 171 | |||||||||||||||
| 172 | 2 | $self->SUPER::characters($data); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 2 | 1; | |||||||||||||
| 176 | |||||||||||||||
| 177 | |||||||||||||||
| 178 | |||||||||||||||
| 179 | =pod | ||||||||||||||
| 180 | |||||||||||||||
| 181 | =head1 NAME | ||||||||||||||
| 182 | |||||||||||||||
| 183 | POE::Filter::XML::Handler - Default SAX Handler for POE::Filter::XML | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =head1 VERSION | ||||||||||||||
| 186 | |||||||||||||||
| 187 | version 1.140700 | ||||||||||||||
| 188 | |||||||||||||||
| 189 | =head1 DESCRIPTION | ||||||||||||||
| 190 | |||||||||||||||
| 191 | POE::Filter::XML::Handler is the default SAX handler for POE::Filter::XML. It | ||||||||||||||
| 192 | extends XML::SAX::Base to provide different semantics for streaming vs. | ||||||||||||||
| 193 | non-streaming contexts. This handle by default builds POE::Filter::XML::Nodes. | ||||||||||||||
| 194 | |||||||||||||||
| 195 | =head1 PUBLIC_ATTRIBUTES | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =head2 not_streaming | ||||||||||||||
| 198 | |||||||||||||||
| 199 | is: ro, isa: Bool, default: false | ||||||||||||||
| 200 | |||||||||||||||
| 201 | not_streaming determines the behavior for the opening tag parsed. If what is | ||||||||||||||
| 202 | being parsed is not a stream, the document will be parsed in full then placed | ||||||||||||||
| 203 | into the finished_nodes attribute. Otherwise, the opening tag will be placed | ||||||||||||||
| 204 | immediately into the finished_nodes bucket. | ||||||||||||||
| 205 | |||||||||||||||
| 206 | =head1 PRIVATE_ATTRIBUTES | ||||||||||||||
| 207 | |||||||||||||||
| 208 | =head2 current_node | ||||||||||||||
| 209 | |||||||||||||||
| 210 | is: rw, isa: POE::Filter::XML::Node | ||||||||||||||
| 211 | |||||||||||||||
| 212 | current_node holds the node being immediately parsed. | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =head2 finished_nodes | ||||||||||||||
| 215 | |||||||||||||||
| 216 | is: ro, isa: ArrayRef, traits: Array | ||||||||||||||
| 217 | |||||||||||||||
| 218 | finished_nodes holds the nodes that have been completely parsed. Access to this | ||||||||||||||
| 219 | attribute is provided through the following methods: | ||||||||||||||
| 220 | |||||||||||||||
| 221 | handles => | ||||||||||||||
| 222 | { | ||||||||||||||
| 223 | all_finished_nodes => 'elements', | ||||||||||||||
| 224 | has_finished_nodes => 'count', | ||||||||||||||
| 225 | add_finished_node => 'push', | ||||||||||||||
| 226 | get_finished_node => 'shift', | ||||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | =head2 depth_stack | ||||||||||||||
| 230 | |||||||||||||||
| 231 | is: ro, isa: ArrayRef, traits: Array | ||||||||||||||
| 232 | |||||||||||||||
| 233 | depth_stack holds the operating stack for the parsed nodes. As nodes are | ||||||||||||||
| 234 | processed, ancendants of the current node are stored in the stack. When done | ||||||||||||||
| 235 | they are popped off the stack. Access to this attribute is provided through the | ||||||||||||||
| 236 | following methods: | ||||||||||||||
| 237 | |||||||||||||||
| 238 | handles => | ||||||||||||||
| 239 | { | ||||||||||||||
| 240 | push_depth_stack => 'push', | ||||||||||||||
| 241 | pop_depth_stack => 'pop', | ||||||||||||||
| 242 | depth => 'count', | ||||||||||||||
| 243 | } | ||||||||||||||
| 244 | |||||||||||||||
| 245 | =head1 PUBLIC_METHODS | ||||||||||||||
| 246 | |||||||||||||||
| 247 | =head2 reset | ||||||||||||||
| 248 | |||||||||||||||
| 249 | reset will clear the current node, the finished nodes, and the depth stack. | ||||||||||||||
| 250 | |||||||||||||||
| 251 | =head1 PROTECTED_METHODS | ||||||||||||||
| 252 | |||||||||||||||
| 253 | =head2 override start_element | ||||||||||||||
| 254 | |||||||||||||||
| 255 | (HashRef $data) | ||||||||||||||
| 256 | |||||||||||||||
| 257 | start_element is overriden from the XML::SAX::Base class to provide our custom | ||||||||||||||
| 258 | behavior for dealing with streaming vs. non-streaming data. It builds Nodes | ||||||||||||||
| 259 | then attaches them to either the root node (non-streaming) or as stand-alone | ||||||||||||||
| 260 | top level fragments (streaming) sets them to the current node. Children nodes | ||||||||||||||
| 261 | are appended to their parents before getting set as the current node. Then the | ||||||||||||||
| 262 | base class method is called via super() | ||||||||||||||
| 263 | |||||||||||||||
| 264 | =head2 override end_element | ||||||||||||||
| 265 | |||||||||||||||
| 266 | (HashRef $data) | ||||||||||||||
| 267 | |||||||||||||||
| 268 | end_element is overriden from the XML::SAX::Base class to provide our custom | ||||||||||||||
| 269 | behavior for dealing with streaming vs. non-streaming data. Mostly this method | ||||||||||||||
| 270 | is in charge of stack management when the depth of the stack reaches certain | ||||||||||||||
| 271 | points. In streaming documents, this means that top level fragments (not root) | ||||||||||||||
| 272 | are popped off the stack and added to the finished_nodes collection. Otherwise | ||||||||||||||
| 273 | a Node is created with stream_end set and added to the finished nodes. | ||||||||||||||
| 274 | |||||||||||||||
| 275 | Then the base class method is called via super() | ||||||||||||||
| 276 | |||||||||||||||
| 277 | =head2 override characters | ||||||||||||||
| 278 | |||||||||||||||
| 279 | (HashRef $data) | ||||||||||||||
| 280 | |||||||||||||||
| 281 | characters merely applies the character data as text to the current node being | ||||||||||||||
| 282 | processed. It then calls the base class method via super(). | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =head1 AUTHOR | ||||||||||||||
| 285 | |||||||||||||||
| 286 | Nicholas R. Perez <nperez@cpan.org> | ||||||||||||||
| 287 | |||||||||||||||
| 288 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 289 | |||||||||||||||
| 290 | This software is copyright (c) 2014 by Nicholas R. Perez <nperez@cpan.org>. | ||||||||||||||
| 291 | |||||||||||||||
| 292 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 293 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 294 | |||||||||||||||
| 295 | =cut | ||||||||||||||
| 296 | |||||||||||||||
| 297 | |||||||||||||||
| 298 | __END__ | ||||||||||||||