| 1 | package MS::Reader::XML; | ||
| 2 | |||
| 3 | 8 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 8 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 8 | ▶ | use parent qw/MS::Reader/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 8 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 8 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 8 | ▶ | use Data::Lock qw/dlock dunlock/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 8 | ▶ | use Storable qw/dclone/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use XML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | |||
| 15 | # this will be called at the end of the new() constructor (prior to parsing) | ||
| 16 | sub _post_load { | ||
| 17 | |||
| 18 | my ($self) = @_; | ||
| 19 | |||
| 20 | # clean toplevel | ||
| 21 | dunlock($self) if ($self->{__lock}); | ||
| 22 | my $toplevel = $self->{_toplevel}; | ||
| 23 | if (defined $toplevel) { | ||
| 24 | $self->{$_} = $self->{$toplevel}->{$_} | ||
| 25 | for (keys %{ $self->{$toplevel} }); | ||
| 26 | delete $self->{$toplevel}; | ||
| 27 | } | ||
| 28 | |||
| 29 | my @iterators = exists $self->{__iterators} | ||
| 30 | ? @{ $self->{__iterators} } | ||
| 31 | : (); | ||
| 32 | |||
| 33 | # reset all record iterators | ||
| 34 | $_->{__pos} = 0 for (@iterators); | ||
| 35 | |||
| 36 | # delete temporary entries (start with "_") | ||
| 37 | for (keys %{$self}) { | ||
| 38 | delete $self->{$_} if ($_ =~ /^_[^_]/); | ||
| 39 | } | ||
| 40 | |||
| 41 | return; | ||
| 42 | |||
| 43 | } | ||
| 44 | |||
| 45 | sub _lock { | ||
| 46 | |||
| 47 | my ($self) = @_; | ||
| 48 | dlock($self); | ||
| 49 | _iter_unlock($self); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | sub _unlock { | ||
| 54 | |||
| 55 | my ($self) = @_; | ||
| 56 | dunlock($self); | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | sub _iter_unlock { | ||
| 61 | |||
| 62 | my ($ref) = @_; | ||
| 63 | return if (! ref $ref); | ||
| 64 | if (ref($ref) ne 'ARRAY') { | ||
| 65 | for ( grep {$_ =~ /^__/} keys %{$ref} ) { | ||
| 66 | dunlock($ref->{$_}); | ||
| 67 | } | ||
| 68 | _iter_unlock($ref->{$_}) for ( grep {$_ !~ /^__/} keys %{$ref} ); | ||
| 69 | } | ||
| 70 | else { | ||
| 71 | _iter_unlock($_) for @{$ref}; | ||
| 72 | } | ||
| 73 | |||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub _load_new { | ||
| 78 | |||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | my $fh = $self->{__fh}; | ||
| 82 | |||
| 83 | $self->{_curr_ref} = $self; | ||
| 84 | |||
| 85 | my $p = XML::Parser->new(); | ||
| 86 | $p->setHandlers( | ||
| 87 | Start => sub{ $self->_handle_start( @_) }, | ||
| 88 | End => sub{ $self->_handle_end( @_) }, | ||
| 89 | Char => sub{ $self->_handle_char( @_) }, | ||
| 90 | ); | ||
| 91 | |||
| 92 | $p->parse($fh); | ||
| 93 | |||
| 94 | seek $fh, 0, 0; | ||
| 95 | |||
| 96 | return; | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | # XML stream handlers | ||
| 102 | |||
| 103 | sub _handle_start { | ||
| 104 | |||
| 105 | my ($self, $p, $el, %attrs) = @_; | ||
| 106 | |||
| 107 | # track offsets of requested items | ||
| 108 | if (defined $self->{_make_index}->{ $el }) { | ||
| 109 | |||
| 110 | my $id = $attrs{ $self->{_make_index}->{$el} } | ||
| 111 | or croak "ID attribute missing on indexed element"; | ||
| 112 | |||
| 113 | my $iter = defined $self->{_curr_ref}->{__offsets} | ||
| 114 | ? scalar @{ $self->{_curr_ref}->{__offsets} } | ||
| 115 | : 0; | ||
| 116 | $self->{_curr_ref}->{__offsets}->[$iter] = $p->current_byte; | ||
| 117 | $self->{_curr_ref}->{__index}->{$id} = $iter; | ||
| 118 | $self->{_curr_ref}->{__count} = $iter + 1; | ||
| 119 | if (! defined $self->{_curr_ref}->{__pos}) { | ||
| 120 | $self->{_curr_ref}->{__pos} = 0; | ||
| 121 | push @{ $self->{__iterators} }, $self->{_curr_ref}; # clean up after! | ||
| 122 | $self->{_curr_ref}->{__record_type} = $el; | ||
| 123 | } | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | # skip parsing inside certain elements | ||
| 128 | if (defined $self->{_skip_inside}->{ $el }) { | ||
| 129 | $p->setHandlers( | ||
| 130 | Start => undef, | ||
| 131 | End => sub{ $self->_handle_end( @_) }, | ||
| 132 | Char => undef, | ||
| 133 | ); | ||
| 134 | $self->{_skip_parse} = 1; | ||
| 135 | return; | ||
| 136 | } | ||
| 137 | |||
| 138 | my $new_ref = {%attrs}; | ||
| 139 | $new_ref->{_back} = $self->{_curr_ref}; | ||
| 140 | |||
| 141 | # Elements that should be grouped by name/id | ||
| 142 | if (defined $self->{_make_named_array}->{ $el }) { | ||
| 143 | |||
| 144 | my $id_name = $self->{_make_named_array}->{ $el }; | ||
| 145 | my $id = $attrs{$id_name}; | ||
| 146 | delete $new_ref->{$id_name}; | ||
| 147 | push @{ $self->{_curr_ref}->{$el}->{$id} }, $new_ref; | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | # Elements that should be grouped by name/id | ||
| 152 | elsif (defined $self->{_make_named_hash}->{ $el }) { | ||
| 153 | my $id_name = $self->{_make_named_hash}->{ $el }; | ||
| 154 | my $id = $attrs{$id_name}; | ||
| 155 | die "Colliding ID $id" | ||
| 156 | if (defined $self->{_curr_ref}->{$el}->{$id}); | ||
| 157 | delete $new_ref->{$id_name}; | ||
| 158 | $self->{_curr_ref}->{$el}->{$id} = $new_ref; | ||
| 159 | } | ||
| 160 | |||
| 161 | # Elements that should be grouped with no name | ||
| 162 | elsif (defined $self->{_make_anon_array}->{ $el } ) { | ||
| 163 | push @{ $self->{_curr_ref}->{$el} }, $new_ref; | ||
| 164 | } | ||
| 165 | |||
| 166 | # Everything else | ||
| 167 | else { | ||
| 168 | $self->{_curr_ref}->{$el} = $new_ref; | ||
| 169 | } | ||
| 170 | |||
| 171 | # Step up linked list | ||
| 172 | $self->{_curr_ref} = $new_ref; | ||
| 173 | |||
| 174 | return; | ||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | sub _handle_end { | ||
| 179 | |||
| 180 | my ($self, $p, $el) = @_; | ||
| 181 | |||
| 182 | # Track length of indexed elements | ||
| 183 | if (defined $self->{_make_index}->{$el}) { | ||
| 184 | my $iter = scalar @{ $self->{_curr_ref}->{__offsets} } - 1; | ||
| 185 | my $offset = $self->{_curr_ref}->{__offsets}->[$iter]; | ||
| 186 | |||
| 187 | my $len = $p->current_byte - $offset; | ||
| 188 | # Don't forget to add length of tag and "</>" chars | ||
| 189 | # if an element is not empty (i.e. if it has a closing tag). | ||
| 190 | # There may be a better way to deduce this based on the parser itself, | ||
| 191 | # but current empty elements must be defined in the subclass itself. | ||
| 192 | if (! defined $self->{_empty_el}->{$el}) { | ||
| 193 | $len += length($el) + 3; | ||
| 194 | } | ||
| 195 | $self->{_curr_ref}->{__lengths}->[$iter] = $len; | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | # Reset handlers for skipped elements | ||
| 200 | if (defined $self->{_skip_inside}->{$el}) { | ||
| 201 | $p->setHandlers( | ||
| 202 | Start => sub{ $self->_handle_start( @_) }, | ||
| 203 | End => sub{ $self->_handle_end( @_) }, | ||
| 204 | Char => sub{ $self->_handle_char( @_) }, | ||
| 205 | ); | ||
| 206 | delete $self->{_skip_parse}; | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | # Don't do anything if inside skipped element | ||
| 211 | return if ($self->{_skip_parse}); | ||
| 212 | |||
| 213 | # Step back down linked list | ||
| 214 | my $last_ref = $self->{_curr_ref}->{_back}; | ||
| 215 | delete $self->{_curr_ref}->{_back}; | ||
| 216 | $self->{_curr_ref} = $last_ref; | ||
| 217 | |||
| 218 | return; | ||
| 219 | |||
| 220 | } | ||
| 221 | |||
| 222 | sub _handle_char { | ||
| 223 | |||
| 224 | my ($self, $p, $data) = @_; | ||
| 225 | $self->{_curr_ref}->{pcdata} .= $data | ||
| 226 | if ($data =~ /\S/); | ||
| 227 | return; | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | sub goto { | ||
| 232 | |||
| 233 | my ($self, $ref, $idx) = @_; | ||
| 234 | croak "Bad list ref" if (! exists $ref->{__pos}); | ||
| 235 | croak "$idx not an integer" if ($idx =~ /\D/); | ||
| 236 | # $idx allowed to be equal to count because this indicates end-of-records | ||
| 237 | croak "$idx out of range" if ($idx < 0 || $idx > $ref->{__count}); | ||
| 238 | $ref->{__pos} = $idx; | ||
| 239 | return; | ||
| 240 | |||
| 241 | } | ||
| 242 | |||
| 243 | sub fetch_record { | ||
| 244 | |||
| 245 | my ($self, $ref, $idx, %args) = @_; | ||
| 246 | |||
| 247 | croak "Bad list ref" if (! exists $ref->{__pos}); | ||
| 248 | |||
| 249 | # check record cache if used | ||
| 250 | return $ref->{__memoized}->{$idx} | ||
| 251 | if ($self->{__use_cache} && exists $ref->{__memoized}->{$idx}); | ||
| 252 | |||
| 253 | my $offset = $ref->{__offsets}->[ $idx ]; | ||
| 254 | croak "Record not found for $idx" if (! defined $offset); | ||
| 255 | |||
| 256 | my $to_read = $ref->{__lengths}->[ $idx ]; | ||
| 257 | my $el = $self->_read_element($offset,$to_read); | ||
| 258 | |||
| 259 | my $type = $ref->{__record_type}; | ||
| 260 | my $class = $self->{__record_classes}->{$type}; | ||
| 261 | croak "No class defined for record type $type\n" if (! defined $class); | ||
| 262 | my $record = $class->new( xml => $el, | ||
| 263 | use_cache => $self->{__use_cache}, %args ); | ||
| 264 | |||
| 265 | # cache record if necessary | ||
| 266 | if ($self->{__use_cache}) { | ||
| 267 | #dunlock($ref); | ||
| 268 | $ref->{__memoized}->{$idx} = $record; | ||
| 269 | #dlock($ref); | ||
| 270 | } | ||
| 271 | |||
| 272 | return $record; | ||
| 273 | |||
| 274 | } | ||
| 275 | |||
| 276 | sub next_record { | ||
| 277 | |||
| 278 | my ($self, $ref, %args) = @_; | ||
| 279 | |||
| 280 | my $pos = $ref->{__pos}; | ||
| 281 | return if ($pos == $ref->{__count}); #EOF | ||
| 282 | |||
| 283 | my $record; | ||
| 284 | |||
| 285 | # There is a while loop here because a return value of -1 from | ||
| 286 | # fetch_record() means the record was filtered out, in which case we | ||
| 287 | # keep trying to find a valid record to return | ||
| 288 | my $c = 0; | ||
| 289 | while ($record = $self->fetch_record( $ref => $pos, %args)) { | ||
| 290 | ++$pos; | ||
| 291 | $ref->{__pos} = $pos; | ||
| 292 | |||
| 293 | return $record if (! defined $record || ! $record->{filtered}); | ||
| 294 | return if ($pos == $ref->{__count}); #EOF | ||
| 295 | } | ||
| 296 | |||
| 297 | return $record; | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | sub record_count { | ||
| 302 | |||
| 303 | my ($self, $ref) = @_; | ||
| 304 | die "Bad list ref" if (! exists $ref->{__count}); | ||
| 305 | return $ref->{__count}; | ||
| 306 | |||
| 307 | } | ||
| 308 | |||
| 309 | sub get_index_by_id { | ||
| 310 | |||
| 311 | my ($self, $ref, $id) = @_; | ||
| 312 | die "Bad list ref" if (! exists $ref->{__index}); | ||
| 313 | return $ref->{__index}->{$id}; | ||
| 314 | |||
| 315 | } | ||
| 316 | |||
| 317 | sub curr_index { | ||
| 318 | |||
| 319 | my ($self, $ref) = @_; | ||
| 320 | die "Bad list ref" if (! exists $ref->{__pos}); | ||
| 321 | return $ref->{__pos}; | ||
| 322 | |||
| 323 | } | ||
| 324 | |||
| 325 | sub _iter_del { | ||
| 326 | |||
| 327 | my ($ref) = @_; | ||
| 328 | return if (! ref $ref); | ||
| 329 | if (ref($ref) ne 'ARRAY') { | ||
| 330 | delete $ref->{$_} for ( grep {$_ =~ /^__/} keys %{$ref} ); | ||
| 331 | _iter_del($ref->{$_}) for keys %{$ref}; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | _iter_del($_) for @{$ref}; | ||
| 335 | } | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | sub dump { | ||
| 340 | |||
| 341 | my ($self) = @_; | ||
| 342 | |||
| 343 | my $fh = $self->{__fh}; | ||
| 344 | dunlock $self; | ||
| 345 | _iter_del($self); | ||
| 346 | |||
| 347 | my $dump = ''; | ||
| 348 | |||
| 349 | { | ||
| 350 | local $Data::Dumper::Indent = 1; | ||
| 351 | local $Data::Dumper::Terse = 1; | ||
| 352 | local $Data::Dumper::Sortkeys = 1; | ||
| 353 | $dump = Dumper $self; | ||
| 354 | } | ||
| 355 | |||
| 356 | return $dump; | ||
| 357 | |||
| 358 | } | ||
| 359 | |||
| 360 | 1; | ||
| 361 | |||
| 362 | |||
| 363 | __END__ | ||